Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ pub struct ServerArgs {
// #[cfg(feature = "ui")]
#[arg(long, default_value_t = false)]
open: bool,

/// Enable cors verification
///
/// When enabled, the POST /new path require a seemless
/// Origin and Host header.
#[arg(long = "no-cors", action = clap::ArgAction::SetFalse)]
cors: bool,
}

impl ServerArgs {
Expand Down Expand Up @@ -310,6 +317,7 @@ fn build_state(ctx: Context, args: ServerArgs) -> Result<Arc<AppState>> {
aisle_path,
pantry_path,
url_prefix,
cors: args.cors,
checked_log_lock: Arc::new(tokio::sync::Mutex::new(())),
shopping_list_events,
#[cfg(feature = "sync")]
Expand Down Expand Up @@ -358,6 +366,7 @@ pub struct AppState {
pub aisle_path: Option<Utf8PathBuf>,
pub pantry_path: Option<Utf8PathBuf>,
pub url_prefix: String,
pub cors: bool,
/// Serializes access to `.shopping-checked` within this process.
/// File-level `flock` doesn't prevent two tasks in the *same* process
/// from racing on the file (the kernel treats them as one lock owner),
Expand Down
2 changes: 1 addition & 1 deletion src/server/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ async fn create_recipe(
Form(form): Form<NewRecipeForm>,
) -> impl IntoResponse {
// CSRF protection: verify request came from same origin
if !validate_same_origin(&headers, &host) {
if state.cors && !validate_same_origin(&headers, &host) {
tracing::warn!("CSRF validation failed for create_recipe request");
return (StatusCode::FORBIDDEN, "Invalid request origin").into_response();
}
Expand Down
Loading