From 587ed7afe1d1152ca6cd3a86a3579a8d96844ed2 Mon Sep 17 00:00:00 2001 From: "Scott C. Livingston" Date: Fri, 1 Nov 2024 13:27:16 -0700 Subject: [PATCH] http: default behavior within rule --- src/bin/rrhttp.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/bin/rrhttp.rs b/src/bin/rrhttp.rs index f0e0eeb..be8975e 100644 --- a/src/bin/rrhttp.rs +++ b/src/bin/rrhttp.rs @@ -298,15 +298,21 @@ struct RequestRule { // Same interpretation pattern as `has_params` has_body: Option, + // block => if query or body key is not explicitly in schema, then reject. + // allow (default) => query or body keys not in the schema are ignored. + #[serde(default)] + default: ConfigMode, + #[serde(default)] schema: Option>, } -#[derive(Clone, Debug, Deserialize, PartialEq)] +#[derive(Clone, Debug, Default, Deserialize, PartialEq)] #[serde(rename_all = "kebab-case")] enum ConfigMode { - Block, + #[default] Allow, + Block, } #[derive(Clone, Debug, Deserialize)] @@ -600,6 +606,7 @@ mod tests { has_params: None, has_body: None, schema: None, + default: ConfigMode::Allow, }); let mut req = Request { @@ -685,6 +692,16 @@ rules: q.insert("Height".to_string(), Some("7.7".into())); } assert!(!config.is_valid(&req)); + + // Default allow unknown query parts + if let Some(q) = &mut req.query { + // First, fix Height to be valid + q.insert("Height".to_string(), Some("600".into())); + + // Then, add new one that is not explicitly in rule + q.insert("FileName".to_string(), Some("image1".into())); + } + assert!(config.is_valid(&req)); } #[test]