From 1f54819498e9f419c690f1da7a6dcfa4f0b41576 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:39:21 +0000 Subject: [PATCH 1/2] fix: read policy conditions and session tags in name order on all builds Any build that compiles the MongoDB backend crate, which includes a build of the whole workspace, depends on bson, which turns on serde_json's preserve_order feature for every crate in that build. There a parsed JSON map keeps the order it was written in; a build of the extenddb package without the MongoDB backend sorts it by name. Three loops over such maps stop at the first fault, so on a preserve_order build a request with two faults got an error chosen by written order: - parse_conditions, for identity policies and permissions boundaries, stopped at the first unknown operator or malformed value, and its conditions came out in written order. - validate_trust_condition, the trust policy check of CreateRole and the console, returned the first invalid operator. - session_tag_pairs, for object-form AssumeRole session tags, gave the pairs in which validate_iam_tags reports the first violation and duplicate_tag_key_ci names the later of two keys that differ only in case. All three now read the map through a by_name helper in extenddb_auth::policy::document, which returns the entries in name order. A sort of the whole document after serde_json::from_str in PolicyDocument would not reach the other two: CreateRole's trust policy arrives through axum's Json extractor, and AssumeRole's session tags through serde_json::from_slice. Unit tests check, each in both written orders: a policy Condition with two unknown operators reports the one whose name sorts first; conditions come out in name order of operator, then key; a trust policy Condition with two unknown operators reports the first in name order; and object-form session tags come out in name order, so duplicate_tag_key_ci names the same key. cargo test --workspace, which CI runs, builds these crates with preserve_order. With --features serde_json/preserve_order, extenddb-auth and extenddb-server pass; with the loops reading the maps directly, the four tests fail. Fixes #780 --- crates/auth/src/policy/document.rs | 56 ++++++++++++++++++++- crates/server/src/management/assume_role.rs | 49 ++++++++++++++++-- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/crates/auth/src/policy/document.rs b/crates/auth/src/policy/document.rs index dedcd5e2a..c227995a3 100755 --- a/crates/auth/src/policy/document.rs +++ b/crates/auth/src/policy/document.rs @@ -390,12 +390,12 @@ pub fn parse_conditions(value: &Value) -> Result, PolicyParseErro }; let mut conditions = Vec::new(); - for (op_name, keys_obj) in obj { + for (op_name, keys_obj) in by_name(obj) { let operator = parse_operator(op_name)?; let keys = keys_obj .as_object() .ok_or_else(|| PolicyParseError(format!("condition {op_name} must be an object")))?; - for (key, vals) in keys { + for (key, vals) in by_name(keys) { let values = parse_string_or_array(vals)?; conditions.push(Condition { operator: operator.clone(), @@ -407,6 +407,19 @@ pub fn parse_conditions(value: &Value) -> Result, PolicyParseErro Ok(conditions) } +/// The entries of `map` in name order. Any build that compiles the MongoDB +/// backend crate, which includes a build of the whole workspace, gets +/// serde_json's `preserve_order` feature through `bson`, so there a map keeps +/// the order it was written in; a build without that crate sorts it by name. +/// A parse or a check that stops at the first fault reports the same fault on +/// every build when it reads the entries in name order. +#[must_use] +pub fn by_name(map: &serde_json::Map) -> Vec<(&String, &Value)> { + let mut entries: Vec<_> = map.iter().collect(); + entries.sort_unstable_by(|a, b| a.0.cmp(b.0)); + entries +} + /// Parse a condition operator name, handling `ForAllValues:`, `ForAnyValue:` prefixes /// and `IfExists` suffix. fn parse_operator(name: &str) -> Result { @@ -512,6 +525,45 @@ mod tests { )); } + /// The fault reported for a Condition with two unknown operators is the + /// one whose name sorts first, in either order of the document (#780). + /// `cargo test --workspace` builds this crate with `preserve_order`, + /// which keeps the written order, so there the test tells the two apart. + #[test] + fn two_unknown_operators_report_the_first_in_name_order() { + for (first, second) in [("BadA", "BadB"), ("BadB", "BadA")] { + let json = format!( + r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow", + "Action":"dynamodb:GetItem","Resource":"*","Condition":{{ + "{first}":{{"aws:k":"v"}},"{second}":{{"aws:k":"v"}}}}}}]}}"# + ); + let err = PolicyDocument::from_json(&json).unwrap_err(); + assert_eq!(err.0, "unknown operator: BadA", "{first} before {second}"); + } + } + + /// Conditions come out in name order of operator, then key, in either + /// order of the document. + #[test] + fn conditions_are_read_in_name_order() { + for condition in [ + r#"{"StringLike":{"aws:b":"1","aws:a":"2"},"Bool":{"aws:c":"true"}}"#, + r#"{"Bool":{"aws:c":"true"},"StringLike":{"aws:a":"2","aws:b":"1"}}"#, + ] { + let json = format!( + r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow", + "Action":"dynamodb:GetItem","Resource":"*","Condition":{condition}}}]}}"# + ); + let doc = PolicyDocument::from_json(&json).unwrap(); + let keys: Vec<&str> = doc.statements[0] + .conditions + .iter() + .map(|c| c.key.as_str()) + .collect(); + assert_eq!(keys, ["aws:c", "aws:a", "aws:b"], "{condition}"); + } + } + #[test] fn parse_deny_with_not_action() { let json = r#"{ diff --git a/crates/server/src/management/assume_role.rs b/crates/server/src/management/assume_role.rs index 0be4b5295..edeeda75d 100755 --- a/crates/server/src/management/assume_role.rs +++ b/crates/server/src/management/assume_role.rs @@ -22,7 +22,7 @@ use super::crypto::{ }; use extenddb_auth::policy::condition::{ConditionMatch, evaluate_statement_conditions}; use extenddb_auth::policy::context::AssumeRoleContext; -use extenddb_auth::policy::document::parse_conditions; +use extenddb_auth::policy::document::{by_name, parse_conditions}; #[derive(Deserialize)] pub struct AssumeRoleRequest { @@ -624,7 +624,9 @@ fn session_tag_pairs(session_tags: &Value) -> Vec<(String, String)> { let mut pairs = Vec::new(); match session_tags { Value::Object(obj) => { - for (k, v) in obj { + // In name order, so the first bad key and the later of two keys + // that collide by case are the same on every build. + for (k, v) in by_name(obj) { if let Some(s) = scalar_to_string(v) { pairs.push((k.clone(), s)); } @@ -816,7 +818,9 @@ fn validate_trust_condition(cond: &Value) -> Result<(), String> { let obj = cond .as_object() .ok_or_else(|| "Condition must be an object".to_owned())?; - for (op_name, keys) in obj { + // In name order, so a block with two faults reports the same one on every + // build. + for (op_name, keys) in by_name(obj) { if !is_known_iam_condition_operator(op_name) { return Err(format!("invalid Condition operator: {op_name}")); } @@ -1664,6 +1668,45 @@ mod tests { } } + /// A trust policy `Condition` with two unknown operators reports the one + /// whose name sorts first, in either order of the document (#780). + /// `cargo test --workspace` builds `serde_json` with `preserve_order`, + /// which keeps the written order, so there the test tells the two apart. + #[test] + fn validate_reports_the_first_bad_operator_in_name_order() { + for (first, second) in [("BadA", "BadB"), ("BadB", "BadA")] { + let doc: Value = serde_json::from_str(&format!( + r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow", + "Principal":{{"AWS":"{CALLER}"}},"Action":"sts:AssumeRole", + "Condition":{{"{first}":{{"aws:k":"v"}},"{second}":{{"aws:k":"v"}}}}}}]}}"# + )) + .unwrap(); + assert_eq!( + validate_trust_policy(&doc).unwrap_err(), + "invalid Condition operator: BadA", + "{first} before {second}" + ); + } + } + + /// Object-form session tags are checked in name order: of two keys that + /// collide by case, the later in name order is the one reported, in either + /// order of the request. + #[test] + fn session_tag_pairs_are_in_name_order() { + for tags in [r#"{"team":"a","Team":"b"}"#, r#"{"Team":"b","team":"a"}"#] { + let tags: Value = serde_json::from_str(tags).unwrap(); + let pairs = session_tag_pairs(&tags); + let keys: Vec<&str> = pairs.iter().map(|(k, _)| k.as_str()).collect(); + assert_eq!(keys, ["Team", "team"], "{tags}"); + assert_eq!( + super::super::duplicate_tag_key_ci(&pairs).as_deref(), + Some("team"), + "{tags}" + ); + } + } + #[test] fn validate_accepts_alphanumeric_or_empty_sid() { for sid in ["Abc123", "", "aaaaaaaaaa"] { From 10c965e2a7942d128bd484ed2d7583505f40c220 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:38:07 +0000 Subject: [PATCH 2/2] fix(server): read the trust policy operator names in name order before the variable check Main added validate_trust_condition_operators in trust_variable_order.rs, a copy of the operator name check of validate_trust_condition. A trust policy write runs it first when a condition value holds a ${...} reference that IAM refuses. It read the Condition block in map order. On a build with the preserve_order feature of serde_json, a block with two unknown operators then reported the first written operator. This commit reads the block through by_name, as validate_trust_condition does on this branch. The trust policy test now also runs each document with the value a${, which takes the variable check path. --- crates/server/src/management/assume_role.rs | 31 +++++++++++-------- .../assume_role/trust_variable_order.rs | 5 ++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/crates/server/src/management/assume_role.rs b/crates/server/src/management/assume_role.rs index 8ede8e9bc..48fc7b76b 100755 --- a/crates/server/src/management/assume_role.rs +++ b/crates/server/src/management/assume_role.rs @@ -1799,23 +1799,28 @@ mod tests { } /// A trust policy `Condition` with two unknown operators reports the one - /// whose name sorts first, in either order of the document (#780). + /// whose name sorts first, in either order of the document (#780). The + /// same holds when the values also hold a `${...}` reference IAM refuses: + /// that check runs the operator check of `trust_variable_order` first. /// `cargo test --workspace` builds `serde_json` with `preserve_order`, /// which keeps the written order, so there the test tells the two apart. #[test] fn validate_reports_the_first_bad_operator_in_name_order() { - for (first, second) in [("BadA", "BadB"), ("BadB", "BadA")] { - let doc: Value = serde_json::from_str(&format!( - r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow", - "Principal":{{"AWS":"{CALLER}"}},"Action":"sts:AssumeRole", - "Condition":{{"{first}":{{"aws:k":"v"}},"{second}":{{"aws:k":"v"}}}}}}]}}"# - )) - .unwrap(); - assert_eq!( - validate_trust_policy(&doc).unwrap_err(), - "invalid Condition operator: BadA", - "{first} before {second}" - ); + for value in ["v", "a${"] { + for (first, second) in [("BadA", "BadB"), ("BadB", "BadA")] { + let doc: Value = serde_json::from_str(&format!( + r#"{{"Version":"2012-10-17","Statement":[{{"Effect":"Allow", + "Principal":{{"AWS":"{CALLER}"}},"Action":"sts:AssumeRole", + "Condition":{{"{first}":{{"aws:k":"{value}"}}, + "{second}":{{"aws:k":"{value}"}}}}}}]}}"# + )) + .unwrap(); + assert_eq!( + validate_trust_policy(&doc).unwrap_err(), + "invalid Condition operator: BadA", + "{value}: {first} before {second}" + ); + } } } diff --git a/crates/server/src/management/assume_role/trust_variable_order.rs b/crates/server/src/management/assume_role/trust_variable_order.rs index 61e584a69..bc342114a 100644 --- a/crates/server/src/management/assume_role/trust_variable_order.rs +++ b/crates/server/src/management/assume_role/trust_variable_order.rs @@ -16,6 +16,7 @@ use serde_json::Value; use super::{is_known_iam_condition_operator, non_null, validate_trust_effect}; +use extenddb_auth::policy::document::by_name; use extenddb_auth::policy::variable_syntax::{trust_arn_value_error, trust_variable_error}; /// Refuse a statement `Sid` outside IAM's charset. Measured on live IAM @@ -65,7 +66,9 @@ fn validate_trust_condition_operators(cond: &Value) -> Result<(), String> { let obj = cond .as_object() .ok_or_else(|| "Condition must be an object".to_owned())?; - for (op_name, keys) in obj { + // In name order, as `validate_trust_condition` reads it, so a block with + // two faults reports the same one on every build. + for (op_name, keys) in by_name(obj) { if !is_known_iam_condition_operator(op_name) { return Err(format!("invalid Condition operator: {op_name}")); }