Skip to content

fix: read policy conditions and session tags in name order on all builds - #783

Open
LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/780-sort-policy-documents
Open

LeeroyHannigan wants to merge 3 commits into
mainfrom
fix/780-sort-policy-documents

Conversation

@LeeroyHannigan

Copy link
Copy Markdown
Collaborator

On a build with serde_json's preserve_order feature, 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

bson declares serde_json with preserve_order, and Cargo turns the feature on for every crate in a build that compiles the MongoDB backend crate. That is a --features mongodb binary, and also any build of the whole workspace, such as CI's cargo build --release and cargo test --workspace. There a parsed JSON map keeps its written order. A build of the extenddb package 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 in extenddb_auth::policy::document that returns the entries in name order:

  • parse_conditions, for identity policies and permissions boundaries (through PolicyDocument::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_tags reports the first violation in these pairs, and duplicate_tag_key_ci names the later of two keys that differ only in case, so {"team": "a", "Team": "b"} reported Team on a preserve_order build and team elsewhere.

#780 proposed sorting the whole document after serde_json::from_str in PolicyDocument. That would not reach the other two loops: CreateRole's trust policy arrives through axum's Json extractor, and AssumeRole's session tags through serde_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): a Condition with the operators BadA and BadB reports unknown operator: BadA.
  • conditions_are_read_in_name_order (policy document): StringLike on aws:b and aws:a with Bool on aws:c gives the keys aws: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: Team then team, and duplicate_tag_key_ci names team.

cargo test --workspace, which CI runs, builds these crates with preserve_order. Locally:

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

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 LJMart11 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  1. 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 as condition on <key> holds <value>, and it iterates the raw map in written order. With serde_json/preserve_order, StringEquals with aws:a and aws:b both holding x${ reports aws:a or aws:b depending on which is written first; without it, always aws:a. Reading ops and keys in raw_conditions through by_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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 demohan self-assigned this Nov 27, 2026

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB build: a policy document with two faults reports a different first error than on the other backends

3 participants