Skip to content
Open
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
56 changes: 54 additions & 2 deletions crates/auth/src/policy/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,12 @@ pub fn parse_conditions(value: &Value) -> Result<Vec<Condition>, 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(),
Expand All @@ -449,6 +449,19 @@ pub fn parse_conditions(value: &Value) -> Result<Vec<Condition>, 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<String, Value>) -> 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<ConditionOperator, PolicyParseError> {
Expand Down Expand Up @@ -554,6 +567,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#"{
Expand Down
54 changes: 51 additions & 3 deletions crates/server/src/management/assume_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use extenddb_auth::policy::arn_value::{
use extenddb_auth::policy::condition::{ConditionMatch, evaluate_statement_conditions};
use extenddb_auth::policy::context::AssumeRoleContext;
use extenddb_auth::policy::document::{
Condition, PolicyDocument, PolicyParseError, parse_conditions,
Condition, PolicyDocument, PolicyParseError, by_name, parse_conditions,
};

#[derive(Deserialize)]
Expand Down Expand Up @@ -735,7 +735,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));
}
Expand Down Expand Up @@ -935,7 +937,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}"));
}
Expand Down Expand Up @@ -1794,6 +1798,50 @@ mod tests {
}
}

/// A trust policy `Condition` with two unknown operators reports the one
/// 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 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}"
);
}
}
}

/// 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"] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

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.

// 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}"));
}
Expand Down
Loading