Repository navigation
fix: read policy conditions and session tags in name order on all builds - #783
LeeroyHannigan wants to merge 3 commits into
Conversation
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/server/src/management/assume_role.rs keeps the Condition, PolicyDocument and PolicyParseError imports from main and the by_name import from this branch.
…e 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.
LJMart11
left a comment
There was a problem hiding this comment.
Reviewed at 10c965e. The auth and server lib tests passed on the head (517 and 506) under 1.91.1. Review looked at the remaining places that read policy maps in written order.
1 finding is left inline.
Required — outside the diff
crates/auth/src/policy/variable_syntax.rs:155—raw_conditions(variable_syntax.rs:116-123) feeds the loop at line 155, which returns the first${fault ascondition on <key> holds <value>, and it iterates the raw map in written order. Withserde_json/preserve_order,StringEqualswithaws:aandaws:bboth holdingx${reportsaws:aoraws:bdepending on which is written first; without it, alwaysaws:a. Readingopsandkeysinraw_conditionsthroughby_name, plus a two-order test, would close it.
| .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 |
There was a problem hiding this comment.
The body says "Three loops" now read through by_name; the diff changes four, including validate_trust_condition_operators here. Also, the body gives 276 and 252 passed for extenddb-auth and extenddb-server; at this head their lib binaries run 517 and 506 on 1.91.1. Updating the body would close it.
demohan
left a comment
There was a problem hiding this comment.
The five loops this branch changes read in name order at 10c965e, and no allow or deny depends on the written order on any build; two more loops on the same writes still name the first fault in written order. On a SQLite build with serde_json/preserve_order, as a MongoDB or workspace build gets it, two unknown operators in an identity or trust policy, two bad CIDRs, two bad Arn values, case-duplicate session tags and two invalid tag keys now give one answer in both written orders, where the merge-base build split on each; without preserve_order, this head and main answer alike on all 20 cases on SQLite and PostgreSQL. Two operators with one false, case-duplicate condition keys, and a Deny beside a ${aws:TagKeys} value decide the same in both orders on every build, as IAM's policy simulator does, and AssumeRole with 41 session tags sorted, reversed or shuffled gives the same trust and aws:PrincipalTag decisions. Reverting each changed loop fails its test on a preserve_order build. The branch merges onto main 222d80a, builds, and passes the auth and server tests there with preserve_order on.
raw_conditions in crates/auth/src/policy/variable_syntax.rs:113-125 reads both maps in written order, and it feeds validate_identity_before_parse, the first check on every identity policy and permissions boundary write, and trust_variable_error and trust_arn_value_error on trust policy writes. On the preserve_order build, PutUserPolicy with {"StringEquals":{"aws:a":"a${","aws:b":"b${"}} names aws:a and the reversed order names aws:b, and CreateRole splits the same way; because this check runs before parse_conditions, a document whose faults are both bad ${ references never reaches the loop this branch changed. The Arn value loop in validate_trust_policy (crates/server/src/management/assume_role.rs:870) splits too: ArnEquals "bad-a" beside ArnLike "bad-b" reports whichever is written first. With by_name on those three loops every probe row matches the SQLite build. The a${ trust test cannot catch either, since both of its conditions use the same key and value; tests with two distinct keys and values in both orders would.
I will approve once those loops read by name, with both-orders tests.
On a build with
serde_json'spreserve_orderfeature, a policy document, a trust policy or a set of session tags with two faults got a different error than on other builds, chosen by the order the request wrote them in.Which builds
bsondeclaresserde_jsonwithpreserve_order, and Cargo turns the feature on for every crate in a build that compiles the MongoDB backend crate. That is a--features mongodbbinary, and also any build of the whole workspace, such as CI'scargo build --releaseandcargo test --workspace. There a parsed JSON map keeps its written order. A build of theextenddbpackage without the MongoDB backend (-p extenddb, as the Dockerfiles build it) sorts maps by name, so nothing changes there.Change
Three loops over a parsed map stop at the first fault. Each now reads the map through
by_name, a new helper inextenddb_auth::policy::documentthat returns the entries in name order:parse_conditions, for identity policies and permissions boundaries (throughPolicyDocument::from_json). It reported the first unknown operator or malformed value, and its conditions came out in written order, so a later check that names the first bad condition, such as the CIDR check, differed too.validate_trust_condition, the trust policy check of CreateRole and the console, which reported the first invalid operator.session_tag_pairs, for object-form AssumeRole session tags.validate_iam_tagsreports the first violation in these pairs, andduplicate_tag_key_cinames the later of two keys that differ only in case, so{"team": "a", "Team": "b"}reportedTeamon apreserve_orderbuild andteamelsewhere.#780 proposed sorting the whole document after
serde_json::from_strinPolicyDocument. That would not reach the other two loops: CreateRole's trust policy arrives through axum'sJsonextractor, and AssumeRole's session tags throughserde_json::from_slice. The loops are where the order is read.Tests
Each test runs both written orders:
two_unknown_operators_report_the_first_in_name_order(policy document): aConditionwith the operatorsBadAandBadBreportsunknown operator: BadA.conditions_are_read_in_name_order(policy document):StringLikeonaws:bandaws:awithBoolonaws:cgives the keysaws:c,aws:a,aws:b.validate_reports_the_first_bad_operator_in_name_order(trust policy):invalid Condition operator: BadA.session_tag_pairs_are_in_name_order:Teamthenteam, andduplicate_tag_key_cinamesteam.cargo test --workspace, which CI runs, builds these crates withpreserve_order. Locally:cargo +1.88.0 test --locked -p extenddb-auth -p extenddb-server: 276 and 252 passed (plus 3 doc tests).--features serde_json/preserve_order: the same counts passed. With the loops reading the maps directly, all four new tests fail there.cargo +1.97.0 fmt --all -- --checkandcargo +1.97.0 clippy -p extenddb-auth -p extenddb-server --all-targets -- -D warnings: clean.git merge-treeagainst the open PRs that edit these files (fix: limit failed logins per source address and bound bcrypt verification #718, fix(auth): match Arn condition values as IAM does and reject at write the values IAM rejects #692, fix(auth): follow IAM's rules for policy variables in condition values #541, fix(auth): expand policy variables in Resource and NotResource #535): no conflict. fix(server): honor explicit Deny in role trust policies #402 already conflicts with main inassume_role.rs, with the same 12 blocks with or without this change.IAM's and STS's answers to a request with two such faults were not measured; this change makes the builds agree with each other.
Fixes #780