Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cors): set url with chrome-extension:// as regex origin #499

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion aw-server/src/endpoints/cors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins};

use crate::config::AWConfig;

const CHROME_EXTENSION_PREFIX: &str = "chrome-extension://";

pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
let root_url = format!("http://127.0.0.1:{}", config.port);
let root_url_localhost = format!("http://localhost:{}", config.port);
let mut allowed_exact_origins = vec![root_url, root_url_localhost];
allowed_exact_origins.extend(config.cors.clone());
// url with chrome-extension:// is parsed by url crate as Opaque, so it
// should be used as regex origin
allowed_exact_origins.extend(
config
.cors
.clone()
.into_iter()
.filter(|c| !c.starts_with(CHROME_EXTENSION_PREFIX)),
);

if config.testing {
allowed_exact_origins.push("http://127.0.0.1:27180".to_string());
Expand All @@ -22,6 +32,13 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
if config.testing {
allowed_regex_origins.push("chrome-extension://.*".to_string());
}
allowed_regex_origins.extend(
config
.cors
.clone()
.into_iter()
.filter(|c| c.starts_with(CHROME_EXTENSION_PREFIX)),
);

let allowed_origins = AllowedOrigins::some(&allowed_exact_origins, &allowed_regex_origins);
let allowed_methods = vec![Method::Get, Method::Post, Method::Delete]
Expand All @@ -39,5 +56,6 @@ pub fn cors(config: &AWConfig) -> rocket_cors::Cors {
..Default::default()
}
.to_cors()
.inspect_err(|e| log::error!("failed to setup cors: {e}"))
.expect("Failed to set up CORS")
}