From 992f5503db92c85634700712ccbaa0d71b42fd98 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:19:08 +0000 Subject: [PATCH 01/21] refactor(server): move batch and transaction authorization targets into a submodule leading_keys.rs is 601 lines. Move AuthzTarget, batch_transact_targets, batch_transact_tables and batch_table_keys unchanged into leading_keys/batch.rs, and re-export the two functions that request authorization calls. --- crates/server/src/leading_keys.rs | 199 +--------------------- crates/server/src/leading_keys/batch.rs | 210 ++++++++++++++++++++++++ 2 files changed, 212 insertions(+), 197 deletions(-) create mode 100644 crates/server/src/leading_keys/batch.rs diff --git a/crates/server/src/leading_keys.rs b/crates/server/src/leading_keys.rs index 0f2577985..23fbc5ca2 100644 --- a/crates/server/src/leading_keys.rs +++ b/crates/server/src/leading_keys.rs @@ -69,19 +69,6 @@ use extenddb_core::expression::{ use extenddb_core::types::{KeySchemaElement, KeyType, TableKeyInfo}; use serde_json::Value; -/// A single `(action, table)` authorization to perform for a batch or -/// transaction operation, carrying the LeadingKeys the service would evaluate. -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub(crate) struct AuthzTarget { - /// The bare operation string the action maps to (no `dynamodb:` prefix). - pub action: String, - /// The table the action is authorized against. - pub table: String, - /// The partition key value(s) for this authorization, or `None` when the - /// key cannot be determined (the engine then rejects the request). - pub leading_keys: Option>, -} - /// Find the HASH (partition) key attribute name of a key schema. fn hash_attr(schema: &[extenddb_core::types::KeySchemaElement]) -> Option<&str> { schema @@ -412,190 +399,8 @@ fn values_map(input: &Value) -> HashMap { out } -/// The per-(action, table) or per-item authorizations for a batch or -/// transaction operation, each carrying the LeadingKeys the service evaluates. -/// -/// Returns `None` for non-batch/transact operations. Batch operations -/// authorize once per table with the set of all keys for that table; -/// transaction operations authorize once per item with that item's key, which -/// is how the service evaluates a `ForAnyValue` condition differently for the -/// two (a batch of one own and one other key passes `ForAnyValue`; a -/// transaction of the same two does not). Transaction items that give the same -/// target are returned once, in the order of their first occurrence: the -/// decision for a target depends only on its action, table and LeadingKeys, so -/// a hundred `ConditionCheck`s on one partition need one evaluation. -/// -/// `key_info_by_table` is the authorization snapshot; a `None` entry is a table -/// that does not exist, whose targets carry no LeadingKeys (the engine returns -/// `ResourceNotFoundException`). -pub(crate) fn batch_transact_targets( - operation: &str, - input: &Value, - key_info_by_table: &HashMap>, -) -> Option> { - let pk_attr_of = |table: &str| -> Option { - key_info_by_table - .get(table) - .and_then(Option::as_ref) - .and_then(|ki| hash_attr(&ki.key_schema).map(ToOwned::to_owned)) - }; - let mut seen = std::collections::HashSet::new(); - let mut push_unique = |out: &mut Vec, target: AuthzTarget| { - if seen.insert(target.clone()) { - out.push(target); - } - }; - - match operation { - "BatchGetItem" | "BatchWriteItem" => { - let items = input.get("RequestItems")?.as_object()?; - let mut out = Vec::new(); - for (table, body) in items { - let keys = batch_table_keys(operation, body, pk_attr_of(table).as_deref()); - out.push(AuthzTarget { - action: operation.to_owned(), - table: table.clone(), - leading_keys: keys, - }); - } - Some(out) - } - "TransactGetItems" => { - let items = input.get("TransactItems")?.as_array()?; - let mut out = Vec::new(); - for entry in items { - if let Some(get) = entry.get("Get") - && let Some(table) = get.get("TableName").and_then(Value::as_str) - { - let lk = pk_attr_of(table) - .and_then(|pk| { - get.get("Key") - .and_then(|k| pk_from_map(k, &pk, NumberForm::Decimal)) - }) - .map(|v| vec![v]); - push_unique( - &mut out, - AuthzTarget { - action: "GetItem".to_owned(), - table: table.to_owned(), - leading_keys: lk, - }, - ); - } - } - Some(out) - } - "TransactWriteItems" => { - let items = input.get("TransactItems")?.as_array()?; - let mut out = Vec::new(); - for entry in items { - for (sub, action, key_field) in [ - ("Put", "PutItem", "Item"), - ("Delete", "DeleteItem", "Key"), - ("Update", "UpdateItem", "Key"), - ("ConditionCheck", "ConditionCheckItem", "Key"), - ] { - if let Some(op) = entry.get(sub) - && let Some(table) = op.get("TableName").and_then(Value::as_str) - { - let lk = pk_attr_of(table) - .and_then(|pk| { - op.get(key_field) - .and_then(|m| pk_from_map(m, &pk, NumberForm::Decimal)) - }) - .map(|v| vec![v]); - push_unique( - &mut out, - AuthzTarget { - action: action.to_owned(), - table: table.to_owned(), - leading_keys: lk, - }, - ); - } - } - } - Some(out) - } - _ => None, - } -} - -/// The distinct table names a batch or transaction operation addresses, or -/// `None` for any other operation (and for a batch or transaction request whose -/// items container is absent). The `Some`/`None` result mirrors -/// `batch_transact_targets`, so the caller gates the batch and transaction -/// branch on this and then builds the per-target authorizations once. Walking -/// the body for table names here avoids building a throwaway set of full -/// targets only to discover which tables to fetch key info for. -pub(crate) fn batch_transact_tables( - operation: &str, - input: &Value, -) -> Option> { - let mut tables = std::collections::BTreeSet::new(); - match operation { - "BatchGetItem" | "BatchWriteItem" => { - for table in input.get("RequestItems")?.as_object()?.keys() { - tables.insert(table.clone()); - } - } - "TransactGetItems" => { - for entry in input.get("TransactItems")?.as_array()? { - if let Some(table) = entry - .get("Get") - .and_then(|g| g.get("TableName")) - .and_then(Value::as_str) - { - tables.insert(table.to_owned()); - } - } - } - "TransactWriteItems" => { - for entry in input.get("TransactItems")?.as_array()? { - for sub in ["Put", "Delete", "Update", "ConditionCheck"] { - if let Some(table) = entry - .get(sub) - .and_then(|o| o.get("TableName")) - .and_then(Value::as_str) - { - tables.insert(table.to_owned()); - } - } - } - } - _ => return None, - } - Some(tables) -} - -/// Collect the partition key values a single table receives in a batch request. -/// Returns `None` when the table's key schema is unknown (the engine then -/// rejects), or when no key yields a value. -fn batch_table_keys(operation: &str, body: &Value, pk_attr: Option<&str>) -> Option> { - let pk_attr = pk_attr?; - let mut out = Vec::new(); - if operation == "BatchGetItem" { - for key in body.get("Keys")?.as_array()? { - if let Some(v) = pk_from_map(key, pk_attr, NumberForm::Normalized) { - out.push(v); - } - } - } else { - // BatchWriteItem: a list of {PutRequest:{Item}} / {DeleteRequest:{Key}}. - for req in body.as_array()? { - let map = req - .get("PutRequest") - .and_then(|p| p.get("Item")) - .or_else(|| req.get("DeleteRequest").and_then(|d| d.get("Key"))); - if let Some(m) = map - && let Some(v) = pk_from_map(m, pk_attr, NumberForm::Decimal) - { - out.push(v); - } - } - } - if out.is_empty() { None } else { Some(out) } -} +mod batch; +pub(crate) use batch::{batch_transact_tables, batch_transact_targets}; #[cfg(test)] mod tests; diff --git a/crates/server/src/leading_keys/batch.rs b/crates/server/src/leading_keys/batch.rs new file mode 100644 index 000000000..d18ac7053 --- /dev/null +++ b/crates/server/src/leading_keys/batch.rs @@ -0,0 +1,210 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! The per-table and per-item authorizations of a batch or transaction +//! operation. + +use std::collections::HashMap; + +use extenddb_core::types::TableKeyInfo; +use serde_json::Value; + +use super::{NumberForm, hash_attr, pk_from_map}; + +/// A single `(action, table)` authorization to perform for a batch or +/// transaction operation, carrying the LeadingKeys the service would evaluate. +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub(crate) struct AuthzTarget { + /// The bare operation string the action maps to (no `dynamodb:` prefix). + pub action: String, + /// The table the action is authorized against. + pub table: String, + /// The partition key value(s) for this authorization, or `None` when the + /// key cannot be determined (the engine then rejects the request). + pub leading_keys: Option>, +} + +/// The per-(action, table) or per-item authorizations for a batch or +/// transaction operation, each carrying the LeadingKeys the service evaluates. +/// +/// Returns `None` for non-batch/transact operations. Batch operations +/// authorize once per table with the set of all keys for that table; +/// transaction operations authorize once per item with that item's key, which +/// is how the service evaluates a `ForAnyValue` condition differently for the +/// two (a batch of one own and one other key passes `ForAnyValue`; a +/// transaction of the same two does not). Transaction items that give the same +/// target are returned once, in the order of their first occurrence: the +/// decision for a target depends only on its action, table and LeadingKeys, so +/// a hundred `ConditionCheck`s on one partition need one evaluation. +/// +/// `key_info_by_table` is the authorization snapshot; a `None` entry is a table +/// that does not exist, whose targets carry no LeadingKeys (the engine returns +/// `ResourceNotFoundException`). +pub(crate) fn batch_transact_targets( + operation: &str, + input: &Value, + key_info_by_table: &HashMap>, +) -> Option> { + let pk_attr_of = |table: &str| -> Option { + key_info_by_table + .get(table) + .and_then(Option::as_ref) + .and_then(|ki| hash_attr(&ki.key_schema).map(ToOwned::to_owned)) + }; + let mut seen = std::collections::HashSet::new(); + let mut push_unique = |out: &mut Vec, target: AuthzTarget| { + if seen.insert(target.clone()) { + out.push(target); + } + }; + + match operation { + "BatchGetItem" | "BatchWriteItem" => { + let items = input.get("RequestItems")?.as_object()?; + let mut out = Vec::new(); + for (table, body) in items { + let keys = batch_table_keys(operation, body, pk_attr_of(table).as_deref()); + out.push(AuthzTarget { + action: operation.to_owned(), + table: table.clone(), + leading_keys: keys, + }); + } + Some(out) + } + "TransactGetItems" => { + let items = input.get("TransactItems")?.as_array()?; + let mut out = Vec::new(); + for entry in items { + if let Some(get) = entry.get("Get") + && let Some(table) = get.get("TableName").and_then(Value::as_str) + { + let lk = pk_attr_of(table) + .and_then(|pk| { + get.get("Key") + .and_then(|k| pk_from_map(k, &pk, NumberForm::Decimal)) + }) + .map(|v| vec![v]); + push_unique( + &mut out, + AuthzTarget { + action: "GetItem".to_owned(), + table: table.to_owned(), + leading_keys: lk, + }, + ); + } + } + Some(out) + } + "TransactWriteItems" => { + let items = input.get("TransactItems")?.as_array()?; + let mut out = Vec::new(); + for entry in items { + for (sub, action, key_field) in [ + ("Put", "PutItem", "Item"), + ("Delete", "DeleteItem", "Key"), + ("Update", "UpdateItem", "Key"), + ("ConditionCheck", "ConditionCheckItem", "Key"), + ] { + if let Some(op) = entry.get(sub) + && let Some(table) = op.get("TableName").and_then(Value::as_str) + { + let lk = pk_attr_of(table) + .and_then(|pk| { + op.get(key_field) + .and_then(|m| pk_from_map(m, &pk, NumberForm::Decimal)) + }) + .map(|v| vec![v]); + push_unique( + &mut out, + AuthzTarget { + action: action.to_owned(), + table: table.to_owned(), + leading_keys: lk, + }, + ); + } + } + } + Some(out) + } + _ => None, + } +} + +/// The distinct table names a batch or transaction operation addresses, or +/// `None` for any other operation (and for a batch or transaction request whose +/// items container is absent). The `Some`/`None` result mirrors +/// `batch_transact_targets`, so the caller gates the batch and transaction +/// branch on this and then builds the per-target authorizations once. Walking +/// the body for table names here avoids building a throwaway set of full +/// targets only to discover which tables to fetch key info for. +pub(crate) fn batch_transact_tables( + operation: &str, + input: &Value, +) -> Option> { + let mut tables = std::collections::BTreeSet::new(); + match operation { + "BatchGetItem" | "BatchWriteItem" => { + for table in input.get("RequestItems")?.as_object()?.keys() { + tables.insert(table.clone()); + } + } + "TransactGetItems" => { + for entry in input.get("TransactItems")?.as_array()? { + if let Some(table) = entry + .get("Get") + .and_then(|g| g.get("TableName")) + .and_then(Value::as_str) + { + tables.insert(table.to_owned()); + } + } + } + "TransactWriteItems" => { + for entry in input.get("TransactItems")?.as_array()? { + for sub in ["Put", "Delete", "Update", "ConditionCheck"] { + if let Some(table) = entry + .get(sub) + .and_then(|o| o.get("TableName")) + .and_then(Value::as_str) + { + tables.insert(table.to_owned()); + } + } + } + } + _ => return None, + } + Some(tables) +} + +/// Collect the partition key values a single table receives in a batch request. +/// Returns `None` when the table's key schema is unknown (the engine then +/// rejects), or when no key yields a value. +fn batch_table_keys(operation: &str, body: &Value, pk_attr: Option<&str>) -> Option> { + let pk_attr = pk_attr?; + let mut out = Vec::new(); + if operation == "BatchGetItem" { + for key in body.get("Keys")?.as_array()? { + if let Some(v) = pk_from_map(key, pk_attr, NumberForm::Normalized) { + out.push(v); + } + } + } else { + // BatchWriteItem: a list of {PutRequest:{Item}} / {DeleteRequest:{Key}}. + for req in body.as_array()? { + let map = req + .get("PutRequest") + .and_then(|p| p.get("Item")) + .or_else(|| req.get("DeleteRequest").and_then(|d| d.get("Key"))); + if let Some(m) = map + && let Some(v) = pk_from_map(m, pk_attr, NumberForm::Decimal) + { + out.push(v); + } + } + } + if out.is_empty() { None } else { Some(out) } +} From b302f902e11e9b713313188593b52fa1b03eb6aa Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 31 Oct 2026 22:37:45 +0000 Subject: [PATCH 02/21] fix(server): populate dynamodb:Attributes for every item request The dynamodb:Attributes condition key was read from ProjectionExpression alone, only on single-table requests, and a document path such as secret.a went into it as written. Writes, AttributesToGet reads and every batch and transaction request reached the policy evaluator without the key, so a Deny gated on ForAnyValue:StringEquals over an attribute never applied to them, and an Allow gated on ForAllValues:StringEquals granted them whatever attributes they wrote or read. The key now holds the top-level names a request names: the item or key attributes, AttributesToGet, the paths in the projection, update, condition and filter expressions with ExpressionAttributeNames resolved, the legacy Expected, AttributeUpdates, QueryFilter and ScanFilter maps, ExclusiveStartKey, and the key attributes of the table and of a queried index. A batch request carries the names per table and a transaction per item. A DeleteItem and a read with no projection hold only the names the request gives, not those of the stored item. --- crates/auth/src/policy/condition.rs | 17 +- crates/auth/src/policy/context.rs | 16 +- crates/server/src/leading_keys/batch.rs | 43 +- crates/server/src/leading_keys/tests.rs | 10 + crates/server/src/lib.rs | 1 + crates/server/src/request_attributes.rs | 381 +++++++++++++ crates/server/src/request_attributes/tests.rs | 532 ++++++++++++++++++ crates/server/src/request_helpers.rs | 94 +--- tests/test_tag_request_allow_conditions.py | 9 +- 9 files changed, 999 insertions(+), 104 deletions(-) create mode 100644 crates/server/src/request_attributes.rs create mode 100644 crates/server/src/request_attributes/tests.rs diff --git a/crates/auth/src/policy/condition.rs b/crates/auth/src/policy/condition.rs index 6164cce58..0f7886798 100644 --- a/crates/auth/src/policy/condition.rs +++ b/crates/auth/src/policy/condition.rs @@ -129,14 +129,15 @@ pub fn evaluate_condition(condition: &Condition, context: &impl ConditionContext // `dynamodb:LeadingKeys` and `dynamodb:Attributes`. For a // multivalued key the context decides whether that answer applies: // it is right only where ExtendDB leaves the key absent on the same - // requests the service does. ExtendDB leaves `dynamodb:Attributes` - // absent on writes, on `AttributesToGet` reads and on batch and - // transaction targets, and `dynamodb:LeadingKeys` absent on query - // shapes it cannot pin to a partition value, where the service - // populates them; matching there would grant an Allow the service - // refuses, so those keys keep the no-match rule. `aws:TagKeys` is - // populated on exactly the three operations the service reference - // lists it for, so its absent-key answer is IAM's. + // requests the service does. ExtendDB leaves `dynamodb:LeadingKeys` + // absent on query shapes it cannot pin to a partition value, where + // the service populates it, and `dynamodb:Attributes` absent only + // on a request that names no attribute, where the service's answer + // for a bare negated operator was not measured; matching there + // could grant an Allow the service refuses, so those keys keep the + // no-match rule. `aws:TagKeys` is populated on exactly the three + // operations the service reference lists it for, so its absent-key + // answer is IAM's. None => { is_negative_operator(other) && (!is_multivalued diff --git a/crates/auth/src/policy/context.rs b/crates/auth/src/policy/context.rs index be764624c..ed3372726 100755 --- a/crates/auth/src/policy/context.rs +++ b/crates/auth/src/policy/context.rs @@ -457,13 +457,15 @@ impl ConditionContext for RequestContext { // ExtendDB applies that only to `aws:TagKeys`, which it populates on // exactly the three operations the AWS service authorization reference // lists it for (TagResource, UntagResource, CreateTable with Tags), so - // its absence here is the service's absence. `dynamodb:Attributes` is - // left absent on writes, on reads that project with `AttributesToGet` - // and on batch and transaction targets, and `dynamodb:LeadingKeys` on - // query shapes the extractor cannot pin to a partition value, where the - // service populates both; for those two an absent key keeps the - // no-match rule, so an Allow gated on a bare negated operator over them - // grants nothing, as the service grants nothing for the present key. + // its absence here is the service's absence. `dynamodb:LeadingKeys` is + // left absent on query shapes the extractor cannot pin to a partition + // value, where the service populates it. `dynamodb:Attributes` is + // absent only on a request that names no attribute (a `Scan` that + // projects and filters nothing, or a request on a table that does not + // exist), where the service's answer for a bare negated operator was + // not measured. For those two an absent key keeps the no-match rule, + // so an Allow gated on a bare negated operator over them grants + // nothing. super::casefold::case_fold(key) == "aws:tagkeys" } } diff --git a/crates/server/src/leading_keys/batch.rs b/crates/server/src/leading_keys/batch.rs index d18ac7053..ca0eacec2 100644 --- a/crates/server/src/leading_keys/batch.rs +++ b/crates/server/src/leading_keys/batch.rs @@ -6,13 +6,16 @@ use std::collections::HashMap; +use extenddb_core::limits::LimitsConfig; use extenddb_core::types::TableKeyInfo; use serde_json::Value; use super::{NumberForm, hash_attr, pk_from_map}; +use crate::request_attributes::{batch_table_attributes, transact_item_attributes}; /// A single `(action, table)` authorization to perform for a batch or -/// transaction operation, carrying the LeadingKeys the service would evaluate. +/// transaction operation, carrying the LeadingKeys and Attributes the service +/// would evaluate. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub(crate) struct AuthzTarget { /// The bare operation string the action maps to (no `dynamodb:` prefix). @@ -22,10 +25,14 @@ pub(crate) struct AuthzTarget { /// The partition key value(s) for this authorization, or `None` when the /// key cannot be determined (the engine then rejects the request). pub leading_keys: Option>, + /// The `dynamodb:Attributes` names for this authorization, or `None` when + /// it names no attribute. See `crate::request_attributes`. + pub attributes: Option>, } /// The per-(action, table) or per-item authorizations for a batch or -/// transaction operation, each carrying the LeadingKeys the service evaluates. +/// transaction operation, each carrying the LeadingKeys and Attributes the +/// service evaluates. /// /// Returns `None` for non-batch/transact operations. Batch operations /// authorize once per table with the set of all keys for that table; @@ -34,22 +41,25 @@ pub(crate) struct AuthzTarget { /// two (a batch of one own and one other key passes `ForAnyValue`; a /// transaction of the same two does not). Transaction items that give the same /// target are returned once, in the order of their first occurrence: the -/// decision for a target depends only on its action, table and LeadingKeys, so -/// a hundred `ConditionCheck`s on one partition need one evaluation. +/// decision for a target depends only on its action, table, LeadingKeys and +/// Attributes, so a hundred identical `ConditionCheck`s on one partition need +/// one evaluation. /// /// `key_info_by_table` is the authorization snapshot; a `None` entry is a table -/// that does not exist, whose targets carry no LeadingKeys (the engine returns -/// `ResourceNotFoundException`). +/// that does not exist, whose targets carry no LeadingKeys and none of the +/// table's key attributes in Attributes (the engine returns +/// `ResourceNotFoundException`). `limits` are the engine's expression limits, +/// under which the expressions are read for Attributes. pub(crate) fn batch_transact_targets( operation: &str, input: &Value, key_info_by_table: &HashMap>, + limits: &LimitsConfig, ) -> Option> { + let key_info_of = + |table: &str| -> Option<&TableKeyInfo> { key_info_by_table.get(table)?.as_ref() }; let pk_attr_of = |table: &str| -> Option { - key_info_by_table - .get(table) - .and_then(Option::as_ref) - .and_then(|ki| hash_attr(&ki.key_schema).map(ToOwned::to_owned)) + key_info_of(table).and_then(|ki| hash_attr(&ki.key_schema).map(ToOwned::to_owned)) }; let mut seen = std::collections::HashSet::new(); let mut push_unique = |out: &mut Vec, target: AuthzTarget| { @@ -68,6 +78,7 @@ pub(crate) fn batch_transact_targets( action: operation.to_owned(), table: table.clone(), leading_keys: keys, + attributes: batch_table_attributes(operation, body, key_info_of(table), limits), }); } Some(out) @@ -91,6 +102,12 @@ pub(crate) fn batch_transact_targets( action: "GetItem".to_owned(), table: table.to_owned(), leading_keys: lk, + attributes: transact_item_attributes( + "Get", + get, + key_info_of(table), + limits, + ), }, ); } @@ -122,6 +139,12 @@ pub(crate) fn batch_transact_targets( action: action.to_owned(), table: table.to_owned(), leading_keys: lk, + attributes: transact_item_attributes( + sub, + op, + key_info_of(table), + limits, + ), }, ); } diff --git a/crates/server/src/leading_keys/tests.rs b/crates/server/src/leading_keys/tests.rs index c81e3baaa..51de039d5 100644 --- a/crates/server/src/leading_keys/tests.rs +++ b/crates/server/src/leading_keys/tests.rs @@ -14,6 +14,16 @@ use serde_json::json; const MAX_TOKENS: usize = 4096; +/// The batch and transaction targets under the default expression limits. +fn batch_transact_targets( + operation: &str, + input: &Value, + key_info: &HashMap>, +) -> Option> { + let limits = extenddb_core::limits::LimitsConfig::default(); + super::batch_transact_targets(operation, input, key_info, &limits) +} + fn ks(name: &str, kt: KeyType) -> KeySchemaElement { KeySchemaElement { attribute_name: name.to_owned(), diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index 3b1b30309..a71535ec8 100755 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -20,6 +20,7 @@ mod leading_keys; pub mod management; mod metrics_endpoint; pub mod rate_limit; +mod request_attributes; mod request_helpers; mod request_tags; mod response; diff --git a/crates/server/src/request_attributes.rs b/crates/server/src/request_attributes.rs new file mode 100644 index 000000000..3bc74b353 --- /dev/null +++ b/crates/server/src/request_attributes.rs @@ -0,0 +1,381 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! `dynamodb:Attributes` extraction for request authorization. +//! +//! IAM evaluates `dynamodb:Attributes` against the top-level attribute names a +//! request accesses. A top-level attribute is accessed when the request names +//! it, or a nested attribute inside it, in a request parameter: a projection of +//! `Name, Address.City` gives `Name` and `Address`. The names come from the +//! request alone. A `DeleteItem` and a read with no projection hold only the +//! names the request gives, not the names of the stored item they delete or +//! return (measured 2026-10-31), and `ReturnValues` and `Select` add no names. +//! +//! What the key holds for each operation: +//! +//! - `GetItem`: the table's key attributes, `AttributesToGet`, and the paths in +//! `ProjectionExpression`. +//! - `PutItem`: the table's key attributes, the item's attributes, `Expected`, +//! and the paths in `ConditionExpression`. +//! - `UpdateItem`: the `Key` attributes, the table's key attributes, +//! `AttributeUpdates`, `Expected`, and the paths in `UpdateExpression` (the +//! attribute each action names and the paths in its value) and in +//! `ConditionExpression`. +//! - `DeleteItem`: the `Key` attributes, the table's key attributes, `Expected`, +//! and the paths in `ConditionExpression`. +//! - `Query`: the key attributes of the table and of the queried index, +//! `AttributesToGet`, `QueryFilter`, `ExclusiveStartKey`, and the paths in +//! `FilterExpression` and `ProjectionExpression`. A key condition can name +//! only key attributes, which the key already holds. +//! - `Scan`: `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the paths +//! in `FilterExpression` and `ProjectionExpression`. A `Scan` names no key, so +//! a `Scan` that gives none of these leaves the key absent. +//! - `SearchVectors`: the paths in `ProjectionExpression`. +//! - `BatchGetItem`: per table, the attributes of every key, the table's key +//! attributes, `AttributesToGet`, and the paths in `ProjectionExpression`. +//! - `BatchWriteItem`: per table, the attributes of every `PutRequest` item and +//! every `DeleteRequest` key, and the table's key attributes. +//! - `TransactGetItems` and `TransactWriteItems`: per item, as for the +//! single-item operation the item maps to. A `ConditionCheck` holds the +//! table's key attributes and the paths in its `ConditionExpression`. +//! +//! The service's answers were measured on 2026-10-31 for the item attributes of +//! `PutItem`, the attribute a `SET` action names (by name and through +//! `ExpressionAttributeNames`), `DeleteItem`, `AttributesToGet` on `GetItem` and +//! `Scan`, a `ProjectionExpression` naming a document path, reads with no +//! projection, and the batch and transaction forms of those requests. The rest +//! follows the IAM documentation for the key: it holds the attributes a request +//! names, including those read in a condition or a filter, and a policy that +//! lists the allowed attributes must list the key attributes of the table and of +//! its indexes. +//! +//! An expression is read with the tokenizer and parser the engine uses, under +//! the same size, token and depth limits, so every expression the engine +//! accepts gives its names here. An expression the engine refuses gives none: +//! the engine then answers `ValidationException` without reading or writing an +//! item. A `#name` with no `ExpressionAttributeNames` entry is skipped for the +//! same reason. + +use std::collections::BTreeSet; + +use extenddb_core::expression::{ + Expr, ExpressionKind, PathElement, UpdateAction, parse_condition_with_depth_limit, + parse_projection, parse_update_from, tokenize_for, tokenize_with_limit, +}; +use extenddb_core::limits::LimitsConfig; +use extenddb_core::types::TableKeyInfo; +use serde_json::{Map, Value}; + +/// The `dynamodb:Attributes` value of a single-table operation: the sorted, +/// distinct top-level names the request accesses. `None` when it names none, +/// and for an operation that reads or writes no item. +pub(crate) fn single_table_attributes( + operation: &str, + input: &Value, + key_info: Option<&TableKeyInfo>, + limits: &LimitsConfig, +) -> Option> { + let mut names = Names::new(input, limits); + match operation { + "GetItem" => names.get_item(input, key_info), + "PutItem" => names.put_item(input, key_info), + "UpdateItem" => names.update_item(input, key_info), + "DeleteItem" => names.delete_item(input, key_info), + "Query" => { + names.key_schema(key_info); + names.index_key_schema(input, key_info); + names.map_keys(input.get("QueryFilter")); + names.range_read(input); + } + "Scan" => { + names.map_keys(input.get("ScanFilter")); + names.range_read(input); + } + "SearchVectors" => names.projection(input.get("ProjectionExpression")), + _ => return None, + } + names.finish() +} + +/// The `dynamodb:Attributes` value of one table's part of a `BatchGetItem` or +/// `BatchWriteItem`: `body` is that table's `RequestItems` entry. `None` for +/// any other operation, and when the part names no attribute. +pub(crate) fn batch_table_attributes( + operation: &str, + body: &Value, + key_info: Option<&TableKeyInfo>, + limits: &LimitsConfig, +) -> Option> { + let mut names = Names::new(body, limits); + match operation { + "BatchGetItem" => { + for key in body + .get("Keys") + .and_then(Value::as_array) + .into_iter() + .flatten() + { + names.map_keys(Some(key)); + } + names.get_item(body, key_info); + } + "BatchWriteItem" => { + for request in body.as_array().into_iter().flatten() { + names.map_keys(request.get("PutRequest").and_then(|p| p.get("Item"))); + names.map_keys(request.get("DeleteRequest").and_then(|d| d.get("Key"))); + } + names.key_schema(key_info); + } + _ => return None, + } + names.finish() +} + +/// The `dynamodb:Attributes` value of one transaction item: `sub` is the +/// item's member name (`Get`, `Put`, `Update`, `Delete` or `ConditionCheck`) +/// and `item` its body. `None` for any other member, and when the item names +/// no attribute. +pub(crate) fn transact_item_attributes( + sub: &str, + item: &Value, + key_info: Option<&TableKeyInfo>, + limits: &LimitsConfig, +) -> Option> { + let mut names = Names::new(item, limits); + match sub { + "Get" => { + names.key_schema(key_info); + names.projection(item.get("ProjectionExpression")); + } + "Put" => names.put_item(item, key_info), + "Update" => names.update_item(item, key_info), + "Delete" => names.delete_item(item, key_info), + "ConditionCheck" => { + names.key_schema(key_info); + names.condition(item.get("ConditionExpression")); + } + _ => return None, + } + names.finish() +} + +/// The top-level names one request (or one batch part or transaction item) +/// accesses, with the `ExpressionAttributeNames` its expressions resolve +/// against. +struct Names<'a> { + found: BTreeSet, + expression_names: Option<&'a Map>, + limits: &'a LimitsConfig, +} + +impl<'a> Names<'a> { + fn new(request: &'a Value, limits: &'a LimitsConfig) -> Self { + Self { + found: BTreeSet::new(), + expression_names: request + .get("ExpressionAttributeNames") + .and_then(Value::as_object), + limits, + } + } + + fn get_item(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { + self.key_schema(key_info); + self.list(request.get("AttributesToGet")); + self.projection(request.get("ProjectionExpression")); + } + + fn put_item(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { + self.key_schema(key_info); + self.map_keys(request.get("Item")); + self.map_keys(request.get("Expected")); + self.condition(request.get("ConditionExpression")); + } + + fn update_item(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { + self.map_keys(request.get("Key")); + self.key_schema(key_info); + self.map_keys(request.get("AttributeUpdates")); + self.map_keys(request.get("Expected")); + self.condition(request.get("ConditionExpression")); + self.update(request.get("UpdateExpression")); + } + + fn delete_item(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { + self.map_keys(request.get("Key")); + self.key_schema(key_info); + self.map_keys(request.get("Expected")); + self.condition(request.get("ConditionExpression")); + } + + /// The parameters `Query` and `Scan` share. + fn range_read(&mut self, request: &Value) { + self.list(request.get("AttributesToGet")); + self.map_keys(request.get("ExclusiveStartKey")); + self.condition(request.get("FilterExpression")); + self.projection(request.get("ProjectionExpression")); + } + + /// The table's key attributes. A table that does not exist adds none. + fn key_schema(&mut self, key_info: Option<&TableKeyInfo>) { + for element in key_info.into_iter().flat_map(|ki| &ki.key_schema) { + self.found.insert(element.attribute_name.clone()); + } + } + + /// The key attributes of the index a `Query` names. An index the table + /// does not have adds none: the engine refuses that request. + fn index_key_schema(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { + let (Some(key_info), Some(index)) = + (key_info, request.get("IndexName").and_then(Value::as_str)) + else { + return; + }; + let found = key_info + .global_secondary_indexes + .iter() + .chain(&key_info.local_secondary_indexes) + .find(|i| i.index_name == index); + for element in found.into_iter().flat_map(|i| &i.key_schema) { + self.found.insert(element.attribute_name.clone()); + } + } + + /// The keys of a map of top-level attribute names (`Item`, `Key`, + /// `Expected`, `AttributeUpdates`, `QueryFilter`, `ScanFilter`, + /// `ExclusiveStartKey`), as written. + fn map_keys(&mut self, value: Option<&Value>) { + if let Some(map) = value.and_then(Value::as_object) { + self.found.extend(map.keys().cloned()); + } + } + + /// The entries of `AttributesToGet`, as written: each is a top-level name, + /// so a `.` in one is part of the name, not a path separator. + fn list(&mut self, value: Option<&Value>) { + let entries = value.and_then(Value::as_array).into_iter().flatten(); + self.found + .extend(entries.filter_map(Value::as_str).map(ToOwned::to_owned)); + } + + /// The expression text in `value`, or `None` when there is none or it is + /// over the size limit the engine refuses it under. + fn text<'v>(&self, value: Option<&'v Value>) -> Option<&'v str> { + value + .and_then(Value::as_str) + .filter(|s| !s.is_empty() && s.len() <= self.limits.max_expression_length_bytes) + } + + /// The paths in a `ConditionExpression` or `FilterExpression`. + fn condition(&mut self, value: Option<&Value>) { + let Some(text) = self.text(value) else { + return; + }; + let parsed = + tokenize_with_limit(text, self.limits.max_expression_tokens).and_then(|tokens| { + parse_condition_with_depth_limit(&tokens, self.limits.max_expression_depth) + }); + if let Ok(expr) = parsed { + self.expr(&expr); + } + } + + /// The attribute each `UpdateExpression` action names and the paths in its + /// value. + fn update(&mut self, value: Option<&Value>) { + let Some(text) = self.text(value) else { + return; + }; + let parsed = tokenize_for( + text, + self.limits.max_expression_tokens, + ExpressionKind::Update, + ) + .and_then(|tokens| parse_update_from(&tokens, text)); + for action in parsed.iter().flatten() { + match action { + UpdateAction::Set { path, value } + | UpdateAction::Add { path, value } + | UpdateAction::Delete { path, value } => { + self.path(path); + self.expr(value); + } + UpdateAction::Remove { path } => self.path(path), + } + } + } + + /// The paths in a `ProjectionExpression`. + fn projection(&mut self, value: Option<&Value>) { + let Some(text) = self.text(value) else { + return; + }; + let parsed = tokenize_for( + text, + self.limits.max_expression_tokens, + ExpressionKind::Projection, + ) + .and_then(|tokens| parse_projection(&tokens)); + for path in parsed.iter().flatten() { + self.path(path); + } + } + + fn expr(&mut self, expr: &Expr) { + match expr { + Expr::Path(path) => self.path(path), + Expr::Placeholder(_) => {} + Expr::Compare { left, right, .. } | Expr::Arithmetic { left, right, .. } => { + self.expr(left); + self.expr(right); + } + Expr::And(left, right) | Expr::Or(left, right) => { + self.expr(left); + self.expr(right); + } + Expr::Not(inner) => self.expr(inner), + Expr::Function { args, .. } => args.iter().for_each(|arg| self.expr(arg)), + Expr::Between { operand, low, high } => { + self.expr(operand); + self.expr(low); + self.expr(high); + } + Expr::In { operand, list } => { + self.expr(operand); + list.iter().for_each(|item| self.expr(item)); + } + } + } + + /// The top-level name of a document path: its first element, with a + /// `#name` resolved through `ExpressionAttributeNames`. + fn path(&mut self, path: &[PathElement]) { + let Some(PathElement::Attribute(first)) = path.first() else { + return; + }; + let name = if first.starts_with('#') { + match self + .expression_names + .and_then(|names| names.get(first)) + .and_then(Value::as_str) + { + Some(name) => name, + None => return, + } + } else { + first.as_str() + }; + self.found.insert(name.to_owned()); + } + + fn finish(self) -> Option> { + if self.found.is_empty() { + None + } else { + Some(self.found.into_iter().collect()) + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/server/src/request_attributes/tests.rs b/crates/server/src/request_attributes/tests.rs new file mode 100644 index 000000000..4ac14a1cf --- /dev/null +++ b/crates/server/src/request_attributes/tests.rs @@ -0,0 +1,532 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Unit tests for `dynamodb:Attributes` extraction. +//! +//! The rows named "measured" mirror the service's answers of 2026-10-31; the +//! rest follow the IAM documentation for the key. + +use std::collections::HashMap; + +use super::*; +use extenddb_core::types::{ + IndexInfo, IndexType, KeySchemaElement, KeyType, Projection, ProjectionType, +}; +use serde_json::json; + +fn ks(name: &str, key_type: KeyType) -> KeySchemaElement { + KeySchemaElement { + attribute_name: name.to_owned(), + key_type, + } +} + +fn index(name: &str, index_type: IndexType, key_schema: Vec) -> IndexInfo { + IndexInfo { + index_name: name.to_owned(), + index_id: name.to_owned(), + index_type, + key_schema, + projection: Projection { + projection_type: ProjectionType::All, + non_key_attributes: None, + }, + } +} + +/// A table keyed on `pk` alone, as the measured table was. +fn hash_table() -> TableKeyInfo { + TableKeyInfo { + table_name: "t".to_owned(), + key_schema: vec![ks("pk", KeyType::Hash)], + base_key_schema: vec![ks("pk", KeyType::Hash)], + ..Default::default() + } +} + +/// A table keyed on `pk` and `sk`, with a GSI on `gpk`/`gsk` and an LSI on +/// `pk`/`lsk`. +fn indexed_table() -> TableKeyInfo { + TableKeyInfo { + table_name: "i".to_owned(), + key_schema: vec![ks("pk", KeyType::Hash), ks("sk", KeyType::Range)], + base_key_schema: vec![ks("pk", KeyType::Hash), ks("sk", KeyType::Range)], + global_secondary_indexes: vec![index( + "gsi1", + IndexType::Gsi, + vec![ks("gpk", KeyType::Hash), ks("gsk", KeyType::Range)], + )], + local_secondary_indexes: vec![index( + "lsi1", + IndexType::Lsi, + vec![ks("pk", KeyType::Hash), ks("lsk", KeyType::Range)], + )], + ..Default::default() + } +} + +fn single(operation: &str, input: &Value, key_info: Option<&TableKeyInfo>) -> Option> { + single_table_attributes(operation, input, key_info, &LimitsConfig::default()) +} + +fn names(list: &[&str]) -> Option> { + Some(list.iter().map(|s| (*s).to_owned()).collect()) +} + +// ---- writes --------------------------------------------------------------- + +#[test] +fn put_item_holds_the_key_schema_and_every_item_attribute() { + // Measured: PutItem {pk, ssn} matches a ForAnyValue Deny on ssn, and + // {pk, name} does not. + let input = json!({"TableName": "t", "Item": {"pk": {"S": "a"}, "ssn": {"S": "1"}}}); + assert_eq!( + single("PutItem", &input, Some(&hash_table())), + names(&["pk", "ssn"]) + ); + let input = json!({"TableName": "t", "Item": {"pk": {"S": "a"}}}); + assert_eq!( + single("PutItem", &input, Some(&hash_table())), + names(&["pk"]) + ); +} + +#[test] +fn put_item_adds_the_names_its_condition_reads() { + let input = json!({ + "TableName": "t", + "Item": {"pk": {"S": "a"}}, + "ConditionExpression": + "attribute_not_exists(#p) AND (size(secret.a) > :n OR ssn IN (:x, :y))", + "ExpressionAttributeNames": {"#p": "pk"}, + }); + assert_eq!( + single("PutItem", &input, Some(&hash_table())), + names(&["pk", "secret", "ssn"]) + ); + let legacy = json!({ + "TableName": "t", + "Item": {"pk": {"S": "a"}}, + "Expected": {"ssn": {"Exists": false}}, + }); + assert_eq!( + single("PutItem", &legacy, Some(&hash_table())), + names(&["pk", "ssn"]) + ); +} + +#[test] +fn update_item_holds_the_attribute_set_names() { + // Measured: `SET ssn = :v` matches a Deny on ssn; `SET #n = :v` with + // `#n` = `name` does not. + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "UpdateExpression": "SET ssn = :v", + }); + assert_eq!( + single("UpdateItem", &input, Some(&hash_table())), + names(&["pk", "ssn"]) + ); + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "UpdateExpression": "SET #n = :v", + "ExpressionAttributeNames": {"#n": "name"}, + }); + assert_eq!( + single("UpdateItem", &input, Some(&hash_table())), + names(&["name", "pk"]) + ); +} + +#[test] +fn update_item_holds_every_action_target_and_value_path() { + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "UpdateExpression": "SET a = b + :v, c.d = list_append(e, :l), \ + f = if_not_exists(g[0].h, :z) REMOVE i.j[2] ADD k :n DELETE #m :s", + "ConditionExpression": "attribute_exists(x.y)", + "ExpressionAttributeNames": {"#m": "mset"}, + }); + assert_eq!( + single("UpdateItem", &input, Some(&hash_table())), + names(&["a", "b", "c", "e", "f", "g", "i", "k", "mset", "pk", "x"]) + ); +} + +#[test] +fn update_item_holds_the_legacy_attribute_updates_and_expected() { + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "AttributeUpdates": {"ssn": {"Action": "DELETE"}}, + "Expected": {"name": {"Value": {"S": "n"}}}, + }); + assert_eq!( + single("UpdateItem", &input, Some(&hash_table())), + names(&["name", "pk", "ssn"]) + ); +} + +#[test] +fn delete_item_holds_the_key_and_condition_names_only() { + // Measured: DeleteItem of an item that holds ssn is allowed under a Deny + // on ssn, so the stored item's attributes are not in the key. + let input = json!({"TableName": "t", "Key": {"pk": {"S": "a"}}}); + assert_eq!( + single("DeleteItem", &input, Some(&hash_table())), + names(&["pk"]) + ); + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "ConditionExpression": "ssn = :v", + }); + assert_eq!( + single("DeleteItem", &input, Some(&hash_table())), + names(&["pk", "ssn"]) + ); +} + +#[test] +fn return_values_and_select_add_no_names() { + let input = json!({ + "TableName": "t", + "Item": {"pk": {"S": "a"}}, + "ReturnValues": "ALL_OLD", + }); + assert_eq!( + single("PutItem", &input, Some(&hash_table())), + names(&["pk"]) + ); + let input = json!({"TableName": "t", "Select": "ALL_ATTRIBUTES"}); + assert_eq!(single("Scan", &input, Some(&hash_table())), None); +} + +// ---- reads ---------------------------------------------------------------- + +#[test] +fn get_item_holds_attributes_to_get_as_written() { + // Measured: AttributesToGet=[ssn] matches a Deny on ssn. Each entry is a + // top-level name, so `a.b` is one name. + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "AttributesToGet": ["ssn", "a.b"], + }); + assert_eq!( + single("GetItem", &input, Some(&hash_table())), + names(&["a.b", "pk", "ssn"]) + ); +} + +#[test] +fn get_item_holds_the_top_level_name_of_each_projected_path() { + // Measured: `secret.a` and `#s.a` (`#s` = `secret`) both match a Deny on + // secret. + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "ProjectionExpression": "secret.a, #s.b, l[0].c, #n", + "ExpressionAttributeNames": {"#s": "other", "#n": "name"}, + }); + assert_eq!( + single("GetItem", &input, Some(&hash_table())), + names(&["l", "name", "other", "pk", "secret"]) + ); +} + +#[test] +fn get_item_with_no_projection_holds_only_the_key_schema() { + // Measured: GetItem with no projection is allowed under a Deny on ssn and + // returns the whole item. + let input = json!({"TableName": "i", "Key": {"pk": {"S": "a"}, "sk": {"S": "b"}}}); + assert_eq!( + single("GetItem", &input, Some(&indexed_table())), + names(&["pk", "sk"]) + ); +} + +#[test] +fn a_table_that_does_not_exist_adds_no_key_schema() { + let input = json!({"TableName": "gone", "Key": {"pk": {"S": "a"}}}); + assert_eq!(single("GetItem", &input, None), None); + let input = + json!({"TableName": "gone", "Key": {"pk": {"S": "a"}}, "ProjectionExpression": "ssn"}); + assert_eq!(single("GetItem", &input, None), names(&["ssn"])); +} + +#[test] +fn query_holds_the_table_and_index_keys_and_its_read_parameters() { + let input = json!({ + "TableName": "i", + "IndexName": "gsi1", + "KeyConditionExpression": "gpk = :g", + "FilterExpression": "ssn = :s", + "ProjectionExpression": "#n.first", + "ExpressionAttributeNames": {"#n": "name"}, + "ExclusiveStartKey": {"pk": {"S": "a"}, "sk": {"S": "b"}, "gpk": {"S": "g"}, "gsk": {"S": "h"}}, + }); + assert_eq!( + single("Query", &input, Some(&indexed_table())), + names(&["gpk", "gsk", "name", "pk", "sk", "ssn"]) + ); + let lsi = json!({"TableName": "i", "IndexName": "lsi1", "KeyConditionExpression": "pk = :p"}); + assert_eq!( + single("Query", &lsi, Some(&indexed_table())), + names(&["lsk", "pk", "sk"]) + ); +} + +#[test] +fn query_holds_the_legacy_filter_and_attributes_to_get() { + let input = json!({ + "TableName": "i", + "KeyConditions": {"pk": {"ComparisonOperator": "EQ", "AttributeValueList": [{"S": "a"}]}}, + "QueryFilter": {"ssn": {"ComparisonOperator": "NOT_NULL"}}, + "AttributesToGet": ["name"], + }); + assert_eq!( + single("Query", &input, Some(&indexed_table())), + names(&["name", "pk", "sk", "ssn"]) + ); +} + +#[test] +fn scan_with_no_named_attribute_leaves_the_key_absent() { + // Measured: Scan with Limit=2 and no projection is allowed under a Deny on + // ssn and returns whole items. + let input = json!({"TableName": "t", "Limit": 2}); + assert_eq!(single("Scan", &input, Some(&hash_table())), None); +} + +#[test] +fn scan_holds_its_filter_projection_and_start_key_names() { + // Measured: Scan with AttributesToGet=[ssn] matches a Deny on ssn. + let input = json!({"TableName": "t", "AttributesToGet": ["ssn"]}); + assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["ssn"])); + let input = json!({ + "TableName": "t", + "FilterExpression": "begins_with(#c, :p)", + "ProjectionExpression": "name", + "ExpressionAttributeNames": {"#c": "code"}, + "ExclusiveStartKey": {"pk": {"S": "a"}}, + "ScanFilter": {"age": {"ComparisonOperator": "NULL"}}, + }); + assert_eq!( + single("Scan", &input, Some(&hash_table())), + names(&["age", "code", "name", "pk"]) + ); +} + +#[test] +fn search_vectors_holds_its_projected_paths() { + let input = json!({ + "TableName": "t", + "IndexName": "vi", + "ProjectionExpression": "#v.x, name", + "ExpressionAttributeNames": {"#v": "vec"}, + }); + assert_eq!( + single("SearchVectors", &input, Some(&hash_table())), + names(&["name", "vec"]) + ); +} + +#[test] +fn an_operation_that_touches_no_item_has_no_attributes() { + let input = json!({"TableName": "t", "ProjectionExpression": "ssn"}); + assert_eq!(single("DescribeTable", &input, Some(&hash_table())), None); + assert_eq!(single("ListTables", &json!({}), None), None); +} + +// ---- expressions the engine refuses --------------------------------------- + +#[test] +fn an_unresolved_name_placeholder_is_skipped() { + let input = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "ProjectionExpression": "#missing, ssn", + }); + assert_eq!( + single("GetItem", &input, Some(&hash_table())), + names(&["pk", "ssn"]) + ); +} + +#[test] +fn an_expression_the_engine_refuses_gives_no_names() { + let input = json!({ + "TableName": "t", + "Item": {"pk": {"S": "a"}}, + "ConditionExpression": "ssn = = :v", + }); + assert_eq!( + single("PutItem", &input, Some(&hash_table())), + names(&["pk"]) + ); + + let tight = LimitsConfig { + max_expression_tokens: 3, + ..LimitsConfig::default() + }; + let input = json!({"TableName": "gone", "ProjectionExpression": "a, b, c"}); + assert_eq!( + single_table_attributes("GetItem", &input, None, &tight), + None + ); + + let small = LimitsConfig { + max_expression_length_bytes: 4, + ..LimitsConfig::default() + }; + let input = json!({"TableName": "gone", "ProjectionExpression": "abcde"}); + assert_eq!( + single_table_attributes("GetItem", &input, None, &small), + None + ); + let input = json!({"TableName": "gone", "ProjectionExpression": "abcd"}); + assert_eq!( + single_table_attributes("GetItem", &input, None, &small), + names(&["abcd"]) + ); +} + +// ---- batch and transaction requests --------------------------------------- + +#[test] +fn batch_get_item_holds_every_key_and_the_projection_per_table() { + // Measured: BatchGetItem with ProjectionExpression=ssn matches a Deny on + // ssn; with no projection it does not. + let body = json!({ + "Keys": [{"pk": {"S": "a"}}, {"pk": {"S": "b"}}], + "ProjectionExpression": "ssn", + }); + let limits = LimitsConfig::default(); + assert_eq!( + batch_table_attributes("BatchGetItem", &body, Some(&hash_table()), &limits), + names(&["pk", "ssn"]) + ); + let body = json!({"Keys": [{"pk": {"S": "a"}}], "AttributesToGet": ["name"]}); + assert_eq!( + batch_table_attributes("BatchGetItem", &body, Some(&hash_table()), &limits), + names(&["name", "pk"]) + ); + let body = json!({"Keys": [{"pk": {"S": "a"}}]}); + assert_eq!( + batch_table_attributes("BatchGetItem", &body, Some(&hash_table()), &limits), + names(&["pk"]) + ); +} + +#[test] +fn batch_write_item_holds_every_put_item_and_delete_key() { + // Measured: a PutRequest of {pk, ssn} matches a Deny on ssn. + let body = json!([ + {"PutRequest": {"Item": {"pk": {"S": "a"}, "ssn": {"S": "1"}}}}, + {"PutRequest": {"Item": {"pk": {"S": "b"}, "name": {"S": "n"}}}}, + {"DeleteRequest": {"Key": {"pk": {"S": "c"}}}}, + ]); + let limits = LimitsConfig::default(); + assert_eq!( + batch_table_attributes("BatchWriteItem", &body, Some(&hash_table()), &limits), + names(&["name", "pk", "ssn"]) + ); + assert_eq!( + batch_table_attributes("Scan", &body, Some(&hash_table()), &limits), + None + ); +} + +#[test] +fn transaction_items_hold_what_their_single_item_operation_holds() { + // Measured: a TransactGetItems Get with ProjectionExpression=ssn and a + // TransactWriteItems Put of {pk, ssn} match a Deny on ssn. + let ki = Some(hash_table()); + let limits = LimitsConfig::default(); + let item = |sub: &str, body: Value| transact_item_attributes(sub, &body, ki.as_ref(), &limits); + assert_eq!( + item( + "Get", + json!({"Key": {"pk": {"S": "a"}}, "ProjectionExpression": "ssn"}) + ), + names(&["pk", "ssn"]) + ); + assert_eq!( + item("Get", json!({"Key": {"pk": {"S": "a"}}})), + names(&["pk"]) + ); + assert_eq!( + item( + "Put", + json!({"Item": {"pk": {"S": "a"}, "ssn": {"S": "1"}}}) + ), + names(&["pk", "ssn"]) + ); + assert_eq!( + item( + "Update", + json!({"Key": {"pk": {"S": "a"}}, "UpdateExpression": "REMOVE ssn"}) + ), + names(&["pk", "ssn"]) + ); + assert_eq!( + item( + "Delete", + json!({"Key": {"pk": {"S": "a"}}, "ConditionExpression": "attribute_exists(ssn)"}) + ), + names(&["pk", "ssn"]) + ); + assert_eq!( + item( + "ConditionCheck", + json!({"Key": {"pk": {"S": "a"}}, "ConditionExpression": "#s = :v", + "ExpressionAttributeNames": {"#s": "ssn"}}) + ), + names(&["pk", "ssn"]) + ); + assert_eq!(item("Other", json!({"Key": {"pk": {"S": "a"}}})), None); +} + +#[test] +fn batch_and_transaction_targets_carry_the_attributes() { + let key_info: HashMap> = + HashMap::from([("t".to_owned(), Some(hash_table()))]); + let limits = LimitsConfig::default(); + + let batch = json!({"RequestItems": {"t": [ + {"PutRequest": {"Item": {"pk": {"S": "a"}, "ssn": {"S": "1"}}}}, + ]}}); + let targets = + crate::leading_keys::batch_transact_targets("BatchWriteItem", &batch, &key_info, &limits) + .expect("a batch request has targets"); + assert_eq!(targets.len(), 1); + assert_eq!(targets[0].attributes, names(&["pk", "ssn"])); + + // Two checks on one key with different conditions are two evaluations; + // a repeat of the first is not a third. + let check = |expr: &str| { + json!({"ConditionCheck": {"TableName": "t", "Key": {"pk": {"S": "a"}}, + "ConditionExpression": expr}}) + }; + let transact = json!({"TransactItems": [ + check("attribute_exists(ssn)"), + check("attribute_exists(name)"), + check("attribute_exists(ssn)"), + ]}); + let targets = crate::leading_keys::batch_transact_targets( + "TransactWriteItems", + &transact, + &key_info, + &limits, + ) + .expect("a transaction has targets"); + let attributes: Vec<_> = targets.iter().map(|t| t.attributes.clone()).collect(); + assert_eq!( + attributes, + vec![names(&["pk", "ssn"]), names(&["name", "pk"])] + ); +} diff --git a/crates/server/src/request_helpers.rs b/crates/server/src/request_helpers.rs index 9ca4d7dce..590739b46 100755 --- a/crates/server/src/request_helpers.rs +++ b/crates/server/src/request_helpers.rs @@ -265,10 +265,12 @@ pub(crate) async fn authorize_request( // the set of keys that table receives; transaction operations authorize // once per item with that item's key. Each target carries the // `dynamodb:LeadingKeys` value(s) the service evaluates, extracted per the - // table's key schema. The resource ARN per target is unchanged: each is - // still that table's ARN. (Measured against live DynamoDB 2026-10-20.) - // Targets with the same (action, table, LeadingKeys) are evaluated once, - // since the decision depends only on those three for a given identity. + // table's key schema, and the `dynamodb:Attributes` names of that table's + // part of the batch or of that item (see `request_attributes`). The + // resource ARN per target is unchanged: each is still that table's ARN. + // (Measured against live DynamoDB 2026-10-20 and 2026-10-31.) Targets with + // the same (action, table, LeadingKeys, Attributes) are evaluated once, + // since the decision depends only on those four for a given identity. if let Some(tables) = crate::leading_keys::batch_transact_tables(operation, input) { // A request outside the operation's count limits is refused before any // key info is read or any policy is evaluated, with the engine's own @@ -295,9 +297,13 @@ pub(crate) async fn authorize_request( let ki = key_info_for_authz(state.storage.table_key_info(account_id, &table).await)?; authorized_key_info.insert(table, ki); } - let targets = - crate::leading_keys::batch_transact_targets(operation, input, &authorized_key_info) - .unwrap_or_default(); + let targets = crate::leading_keys::batch_transact_targets( + operation, + input, + &authorized_key_info, + &state.limits, + ) + .unwrap_or_default(); if targets.is_empty() { // Malformed body (the count checks above have already refused an @@ -324,6 +330,7 @@ pub(crate) async fn authorize_request( authz_resource::table_resource_arn(&target.table, &state.region, account_id); let params = extenddb_auth::policy::context::RequestParams { leading_keys: target.leading_keys, + attributes: target.attributes, ..base_params(request_tags.clone(), request_tag_keys.clone()) }; authorization::check_authorization( @@ -358,7 +365,12 @@ pub(crate) async fn authorize_request( key_info.as_ref(), state.limits.max_expression_tokens, ), - attributes: extract_attributes(input), + attributes: crate::request_attributes::single_table_attributes( + operation, + input, + key_info.as_ref(), + &state.limits, + ), select: input .get("Select") .and_then(|v| v.as_str()) @@ -403,36 +415,10 @@ pub(crate) async fn authorize_request( Ok(authorized_key_info) } -/// Extract attribute names from the request for `dynamodb:Attributes`. -/// -/// Collects attribute names from `ProjectionExpression` (comma-separated list -/// of top-level names). Resolves `ExpressionAttributeNames` placeholders -/// (e.g. `#n` → `name`) when present. -/// Returns `None` when no projection is specified. -pub(crate) fn extract_attributes(input: &Value) -> Option> { - let proj = input.get("ProjectionExpression")?.as_str()?; - let ean = input - .get("ExpressionAttributeNames") - .and_then(|v| v.as_object()); - let names: Vec = proj - .split(',') - .map(str::trim) - .filter(|s| !s.is_empty()) - .map(|s| { - ean.and_then(|m| m.get(s)) - .and_then(|v| v.as_str()) - .unwrap_or(s) - .to_owned() - }) - .collect(); - if names.is_empty() { None } else { Some(names) } -} - #[cfg(test)] mod tests { use super::*; use extenddb_core::types::TableKeyInfo; - use serde_json::json; fn sample_key_info() -> TableKeyInfo { TableKeyInfo { @@ -510,46 +496,6 @@ mod tests { ); } - #[test] - fn extract_attributes_resolves_expression_attribute_names() { - let input = json!({ - "ProjectionExpression": "#n, #v", - "ExpressionAttributeNames": { - "#n": "name", - "#v": "value" - } - }); - let result = extract_attributes(&input); - assert_eq!(result, Some(vec!["name".to_owned(), "value".to_owned()])); - } - - #[test] - fn extract_attributes_mixed_placeholders_and_literals() { - let input = json!({ - "ProjectionExpression": "#n, age", - "ExpressionAttributeNames": { - "#n": "name" - } - }); - let result = extract_attributes(&input); - assert_eq!(result, Some(vec!["name".to_owned(), "age".to_owned()])); - } - - #[test] - fn extract_attributes_no_expression_attribute_names() { - let input = json!({ - "ProjectionExpression": "name, age" - }); - let result = extract_attributes(&input); - assert_eq!(result, Some(vec!["name".to_owned(), "age".to_owned()])); - } - - #[test] - fn extract_attributes_no_projection() { - let input = json!({"TableName": "test"}); - assert_eq!(extract_attributes(&input), None); - } - #[test] fn missing_target_no_auth_returns_missing_auth_token() { // S-7: No Authorization header + no X-Amz-Target → MissingAuthenticationToken diff --git a/tests/test_tag_request_allow_conditions.py b/tests/test_tag_request_allow_conditions.py index 313b16313..ceb364a56 100644 --- a/tests/test_tag_request_allow_conditions.py +++ b/tests/test_tag_request_allow_conditions.py @@ -7,11 +7,10 @@ `StringNotEqualsIgnoreCase`) on an absent multivalued key and never on a present one (measured with `iam simulate-custom-policy` on 2026-10-24). ExtendDB applies the absent-key answer to `aws:TagKeys` alone, which it populates on -exactly the operations the AWS service authorization reference lists it for. It -leaves `dynamodb:Attributes` absent on writes, on `AttributesToGet` reads and on -batch and transaction targets, where the service populates the key and answers -implicitDeny for a bare operator, so for that key an absent key does not match -and such an Allow grants nothing on those operations. +exactly the operations the AWS service authorization reference lists it for. +`dynamodb:Attributes` is present on every operation below, as on the service, so +a bare negated operator over it does not match and such an Allow grants nothing +on those operations. The same statement is checked as a user policy, a role policy, a permissions boundary and a session policy, and as the only statement of a trust policy. From 7827a5c5087875bc91e462142bd7a8191549f29f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 31 Oct 2026 23:44:14 +0000 Subject: [PATCH 03/21] test: cover dynamodb:Attributes on writes, legacy reads, batch and transaction requests One test per request shape measured on DynamoDB on 2026-10-31, under a user policy of the shape of the session policy each was measured with: a Deny gated on ForAnyValue:StringEquals over one attribute, and an Allow of PutItem gated on ForAllValues:StringEquals over pk. Each asserts the service's answer, the action the refusal names, and that a refused write stores nothing. --- tests/test_dynamodb_attributes_condition.py | 434 ++++++++++++++++++++ 1 file changed, 434 insertions(+) create mode 100644 tests/test_dynamodb_attributes_condition.py diff --git a/tests/test_dynamodb_attributes_condition.py b/tests/test_dynamodb_attributes_condition.py new file mode 100644 index 000000000..c1597a47b --- /dev/null +++ b/tests/test_dynamodb_attributes_condition.py @@ -0,0 +1,434 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""dynamodb:Attributes on writes, AttributesToGet reads, document paths, and +batch and transaction requests. + +Every request shape here was sent to live DynamoDB on 2026-10-31 under a +session policy of the same shape as the user policy the test attaches, twice, +with the same answer both times. The policies: + + * deny(X): Allow dynamodb:* on the table, and Deny dynamodb:* on the table + with ForAnyValue:StringEquals {"dynamodb:Attributes": [X]}. + * allow-put for-all(pk): the only statement allows dynamodb:PutItem on the + table with ForAllValues:StringEquals {"dynamodb:Attributes": ["pk"]}. + +Under deny(ssn) the service refused a PutItem, BatchWriteItem or +TransactWriteItems Put of an item holding ssn, an UpdateItem that sets ssn, +and a GetItem, Scan, BatchGetItem or TransactGetItems read that names ssn in +AttributesToGet or ProjectionExpression. Under deny(secret) it refused the +projections `secret.a` and `#s.a`, so a document path is matched by its +top-level name. It allowed a DeleteItem of an item holding ssn, an UpdateItem +that sets another attribute through a name placeholder, and reads with no +projection, which returned whole items. allow-put for-all(pk) admitted a +PutItem of {pk} and refused one of {pk, ssn}. + +Prerequisites: + - extenddb running with `auth.provider = "builtin"` on EXTENDDB_TEST_ENDPOINT + - Admin credentials in EXTENDDB_ADMIN_USER / EXTENDDB_ADMIN_PASSWORD env vars + +Run: + EXTENDDB_TEST_ENDPOINT=https://localhost:18443 \\ + EXTENDDB_ADMIN_USER=admin \\ + EXTENDDB_ADMIN_PASSWORD= \\ + pytest tests/test_dynamodb_attributes_condition.py -v + +REQ-TEST-001, REQ-AUTH-002 +""" + +from __future__ import annotations + +import os +import uuid +from dataclasses import dataclass +from typing import Any, Callable + +import boto3 +import pytest +from botocore.config import Config as BotoConfig +from botocore.exceptions import ClientError + +from conftest import wait_for_active +from management_helpers import ManagementClient + +# The measured item, and four items holding ssn for the DeleteItem shape. +ITEM_A = { + "pk": {"S": "a"}, + "ssn": {"S": "123-45-6789"}, + "name": {"S": "alice"}, + "secret": {"M": {"a": {"S": "1"}, "b": {"S": "2"}}}, +} +DELETABLE = ["d1", "d2", "d3", "d4"] +KEY_A = {"pk": {"S": "a"}} + +ALLOW_ALL = { + "Version": "2012-10-17", + "Statement": [{"Effect": "Allow", "Action": "dynamodb:*", "Resource": "*"}], +} + + +def _require_auth_env() -> tuple[str, str, str]: + endpoint = os.environ.get("EXTENDDB_TEST_ENDPOINT", "").strip() + admin_user = os.environ.get("EXTENDDB_ADMIN_USER", "").strip() + admin_pass = os.environ.get("EXTENDDB_ADMIN_PASSWORD", "").strip() + if not endpoint or not admin_user or not admin_pass: + pytest.fail( + "MISCONFIGURED: dynamodb:Attributes tests require EXTENDDB_TEST_ENDPOINT, " + "EXTENDDB_ADMIN_USER, and EXTENDDB_ADMIN_PASSWORD. " + "These must be set by devtools/run-tests before test execution." + ) + return endpoint, admin_user, admin_pass + + +def _make_client(endpoint_url: str, access_key: str, secret_key: str, + region: str) -> Any: + kwargs: dict = dict( + service_name="dynamodb", + endpoint_url=endpoint_url, + aws_access_key_id=access_key, + aws_secret_access_key=secret_key, + region_name=region, + config=BotoConfig(retries={"max_attempts": 0}), + ) + # Self-signed certs from ``extenddb init``: disable SSL verification. + if endpoint_url.startswith("https://"): + kwargs["verify"] = False + return boto3.client(**kwargs) + + +def _create_user_with_key(mgmt: ManagementClient, account_id: str, + user_name: str) -> tuple[str, str]: + resp = mgmt.create_user(account_id, user_name, "TestPass123!") + assert resp.status_code == 201, resp.text + resp = mgmt.create_access_key(account_id, user_name) + assert resp.status_code == 201, resp.text + creds = resp.json() + return creds["access_key_id"], creds["secret_access_key"] + + +def _deny(arn: str, attribute: str) -> dict: + """deny(X): everything on the table except a request naming `attribute`.""" + return { + "Version": "2012-10-17", + "Statement": [ + {"Effect": "Allow", "Action": "dynamodb:*", "Resource": arn}, + { + "Effect": "Deny", + "Action": "dynamodb:*", + "Resource": arn, + "Condition": { + "ForAnyValue:StringEquals": {"dynamodb:Attributes": [attribute]} + }, + }, + ], + } + + +def _allow_put_for_all(arn: str, attributes: list[str]) -> dict: + """allow-put for-all: PutItem only, and only of the listed attributes.""" + return { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": "dynamodb:PutItem", + "Resource": arn, + "Condition": { + "ForAllValues:StringEquals": {"dynamodb:Attributes": attributes} + }, + } + ], + } + + +@dataclass +class Env: + admin: Any + table: str + deny_ssn: Any + deny_secret: Any + allow_put_pk: Any + + +@pytest.fixture(scope="module") +def auth_env(): + return _require_auth_env() + + +@pytest.fixture(scope="module") +def mgmt(auth_env) -> ManagementClient: + endpoint, admin_user, admin_pass = auth_env + return ManagementClient(endpoint, admin_user, admin_pass) + + +@pytest.fixture(scope="module") +def region() -> str: + return os.environ.get("AWS_DEFAULT_REGION", "us-east-1") + + +@pytest.fixture(scope="module") +def account_id(mgmt) -> str: + acct_id = f"{uuid.uuid4().int % 10**12:012d}" + resp = mgmt.create_account(acct_id, f"attrs-test-{acct_id}") + assert resp.status_code == 201, resp.text + yield acct_id + mgmt.delete_account(acct_id) + + +@pytest.fixture(scope="module") +def env(auth_env, mgmt, account_id, region) -> Env: + endpoint = auth_env[0] + owner = f"owner-{uuid.uuid4().hex[:8]}" + ak, sk = _create_user_with_key(mgmt, account_id, owner) + resp = mgmt.put_user_policy(account_id, owner, "full", ALLOW_ALL) + assert resp.status_code == 204, resp.text + admin = _make_client(endpoint, ak, sk, region) + + table = f"attrs-{uuid.uuid4().hex[:8]}" + admin.create_table( + TableName=table, + AttributeDefinitions=[{"AttributeName": "pk", "AttributeType": "S"}], + KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}], + BillingMode="PAY_PER_REQUEST", + ) + wait_for_active(admin, table) + admin.put_item(TableName=table, Item=ITEM_A) + for pk in DELETABLE: + admin.put_item( + TableName=table, + Item={"pk": {"S": pk}, "name": {"S": "x"}, "ssn": {"S": "1"}}, + ) + arn = f"arn:aws:dynamodb:{region}:{account_id}:table/{table}" + + def client_with(policy: dict) -> Any: + user = f"u-{uuid.uuid4().hex[:8]}" + uak, usk = _create_user_with_key(mgmt, account_id, user) + resp = mgmt.put_user_policy(account_id, user, "p", policy) + assert resp.status_code == 204, resp.text + return _make_client(endpoint, uak, usk, region) + + yield Env( + admin=admin, + table=table, + deny_ssn=client_with(_deny(arn, "ssn")), + deny_secret=client_with(_deny(arn, "secret")), + allow_put_pk=client_with(_allow_put_for_all(arn, ["pk"])), + ) + try: + admin.delete_table(TableName=table) + except ClientError: + pass + + +def _refusal(call: Callable[[], Any], action: str) -> str: + """The message of the AccessDeniedException `call` raises for `action`.""" + with pytest.raises(ClientError) as exc: + call() + err = exc.value.response["Error"] + assert err["Code"] == "AccessDeniedException", exc.value.response + assert f"perform: dynamodb:{action} on resource:" in err["Message"], err + return err["Message"] + + +def _assert_denied(call: Callable[[], Any], action: str) -> None: + msg = _refusal(call, action) + assert "with an explicit deny in an identity-based policy" in msg, msg + + +def _new_key() -> dict: + return {"pk": {"S": f"k-{uuid.uuid4().hex[:8]}"}} + + +def _stored(env: Env, key: dict) -> dict | None: + return env.admin.get_item(TableName=env.table, Key=key, ConsistentRead=True).get("Item") + + +# -- writes ------------------------------------------------------------------ + + +def test_put_item_of_an_item_holding_a_denied_attribute_is_denied(env: Env): + key = _new_key() + _assert_denied( + lambda: env.deny_ssn.put_item(TableName=env.table, Item={**key, "ssn": {"S": "1"}}), + "PutItem", + ) + assert _stored(env, key) is None, "a refused write stores nothing" + + +def test_update_item_setting_a_denied_attribute_is_denied(env: Env): + key = _new_key() + _assert_denied( + lambda: env.deny_ssn.update_item( + TableName=env.table, + Key=key, + UpdateExpression="SET ssn = :v", + ExpressionAttributeValues={":v": {"S": "1"}}, + ), + "UpdateItem", + ) + assert _stored(env, key) is None, "a refused write stores nothing" + + +def test_put_item_without_the_denied_attribute_is_allowed(env: Env): + key = _new_key() + env.deny_ssn.put_item(TableName=env.table, Item={**key, "name": {"S": "n"}}) + assert _stored(env, key) == {**key, "name": {"S": "n"}} + + +def test_update_item_setting_another_attribute_through_a_placeholder_is_allowed(env: Env): + key = _new_key() + env.deny_ssn.update_item( + TableName=env.table, + Key=key, + UpdateExpression="SET #n = :v", + ExpressionAttributeNames={"#n": "name"}, + ExpressionAttributeValues={":v": {"S": "n"}}, + ) + assert _stored(env, key) == {**key, "name": {"S": "n"}} + + +def test_delete_item_of_an_item_holding_the_denied_attribute_is_allowed(env: Env): + key = {"pk": {"S": DELETABLE[0]}} + assert "ssn" in _stored(env, key) + env.deny_ssn.delete_item(TableName=env.table, Key=key) + assert _stored(env, key) is None + + +def test_for_all_values_allow_refuses_a_put_of_an_unlisted_attribute(env: Env): + key = _new_key() + msg = _refusal( + lambda: env.allow_put_pk.put_item( + TableName=env.table, Item={**key, "ssn": {"S": "1"}} + ), + "PutItem", + ) + assert "because no identity-based policy allows the dynamodb:PutItem action" in msg, msg + assert _stored(env, key) is None, "a refused write stores nothing" + + +def test_for_all_values_allow_admits_a_put_of_listed_attributes_only(env: Env): + key = _new_key() + env.allow_put_pk.put_item(TableName=env.table, Item=key) + assert _stored(env, key) == key + + +# -- reads ------------------------------------------------------------------- + + +def test_get_item_attributes_to_get_naming_a_denied_attribute_is_denied(env: Env): + _assert_denied( + lambda: env.deny_ssn.get_item( + TableName=env.table, Key=KEY_A, AttributesToGet=["ssn"] + ), + "GetItem", + ) + + +def test_scan_attributes_to_get_naming_a_denied_attribute_is_denied(env: Env): + _assert_denied( + lambda: env.deny_ssn.scan(TableName=env.table, AttributesToGet=["ssn"]), + "Scan", + ) + + +def test_get_item_projection_naming_a_denied_attribute_is_denied(env: Env): + _assert_denied( + lambda: env.deny_ssn.get_item( + TableName=env.table, Key=KEY_A, ProjectionExpression="ssn" + ), + "GetItem", + ) + + +def test_get_item_attributes_to_get_of_another_attribute_returns_only_it(env: Env): + resp = env.deny_ssn.get_item(TableName=env.table, Key=KEY_A, AttributesToGet=["name"]) + assert resp["Item"] == {"name": {"S": "alice"}} + + +def test_get_item_projection_of_a_nested_path_is_matched_by_its_top_level_name(env: Env): + _assert_denied( + lambda: env.deny_secret.get_item( + TableName=env.table, Key=KEY_A, ProjectionExpression="secret.a" + ), + "GetItem", + ) + + +def test_get_item_projection_of_a_placeholder_path_is_matched_by_its_top_level_name( + env: Env, +): + _assert_denied( + lambda: env.deny_secret.get_item( + TableName=env.table, + Key=KEY_A, + ProjectionExpression="#s.a", + ExpressionAttributeNames={"#s": "secret"}, + ), + "GetItem", + ) + + +def test_get_item_with_no_projection_returns_the_whole_item(env: Env): + resp = env.deny_ssn.get_item(TableName=env.table, Key=KEY_A) + assert resp["Item"] == ITEM_A + + +def test_scan_with_no_projection_returns_whole_items(env: Env): + resp = env.deny_ssn.scan(TableName=env.table, Limit=2) + assert resp["Count"] == 2 + for item in resp["Items"]: + assert item == _stored(env, {"pk": item["pk"]}) + + +# -- batch and transaction requests ------------------------------------------ + + +def test_batch_get_item_projection_naming_a_denied_attribute_is_denied(env: Env): + _assert_denied( + lambda: env.deny_ssn.batch_get_item( + RequestItems={env.table: {"Keys": [KEY_A], "ProjectionExpression": "ssn"}} + ), + "BatchGetItem", + ) + + +def test_transact_get_items_projection_naming_a_denied_attribute_is_denied(env: Env): + _assert_denied( + lambda: env.deny_ssn.transact_get_items( + TransactItems=[ + {"Get": {"TableName": env.table, "Key": KEY_A, "ProjectionExpression": "ssn"}} + ] + ), + "GetItem", + ) + + +def test_batch_write_item_put_of_an_item_holding_a_denied_attribute_is_denied(env: Env): + key = _new_key() + _assert_denied( + lambda: env.deny_ssn.batch_write_item( + RequestItems={env.table: [{"PutRequest": {"Item": {**key, "ssn": {"S": "1"}}}}]} + ), + "BatchWriteItem", + ) + assert _stored(env, key) is None, "a refused write stores nothing" + + +def test_transact_write_items_put_of_an_item_holding_a_denied_attribute_is_denied( + env: Env, +): + key = _new_key() + _assert_denied( + lambda: env.deny_ssn.transact_write_items( + TransactItems=[ + {"Put": {"TableName": env.table, "Item": {**key, "ssn": {"S": "1"}}}} + ] + ), + "PutItem", + ) + assert _stored(env, key) is None, "a refused write stores nothing" + + +def test_batch_get_item_with_no_projection_returns_the_whole_item(env: Env): + resp = env.deny_ssn.batch_get_item(RequestItems={env.table: {"Keys": [KEY_A]}}) + assert resp["Responses"][env.table] == [ITEM_A] From d95c2cd7093d2c9cea8ea3333d00f90c2429d61e Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:01:37 +0000 Subject: [PATCH 04/21] docs: describe what dynamodb:Attributes holds for each operation The security model manual lists the names the key holds for each item, batch and transaction operation, and which entries were measured on DynamoDB. The troubleshooting entry for an Attributes condition covers a Deny that does not block a DeleteItem or a read with no projection, and an allowlist that omits the key attributes. The upgrade manual records the change for v0.1.15 and the policy statements it affects. --- docs/manuals/07-upgrade-manual.md | 12 ++++++++++++ docs/manuals/10-security-model.md | 19 ++++++++++++++++++- docs/troubleshooting.md | 5 +++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index f4451a0f5..9d812d043 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -665,6 +665,18 @@ A policy whose Arn values are literal ARNs, applied to a context value that is a No catalog migration is required. Before upgrading a deployment that restricts tagging, check the policies of each account for the statements above. +### v0.1.15: `dynamodb:Attributes` holds the attributes every item request names + +**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition, filter or update expression reads, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey`, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). + +**Who is affected.** Any deployment with a policy statement that names `dynamodb:Attributes` in a condition: + +- A `Deny` gated on `ForAnyValue:StringEquals` over an attribute now applies to writes that name it, to `AttributesToGet` reads and projections of it or of a path inside it, to conditions and filters that read it, and to the batch and transaction forms of those requests. It did not apply to them before. +- An `Allow` gated on `ForAllValues:StringEquals` used to grant those requests whatever attributes they named, because the absent key satisfied it. It now grants only when every name the request holds is in the list. A list that omits the table's key attributes stops granting `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and the batch and transaction requests; add the key attributes of the table and of every index the statement covers. +- `Null` with value `true` on `dynamodb:Attributes` matched those requests before, and matched a `GetItem`, `Query`, `BatchGetItem` or `TransactGetItems` read with no projection. It no longer matches any of them, because each holds at least the table's key attributes; it still matches a `Scan` that names no attribute. A statement that used it to refuse reads with no projection no longer refuses them. + +A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. + --- ## License diff --git a/docs/manuals/10-security-model.md b/docs/manuals/10-security-model.md index bb7a1c75d..88d5958d3 100755 --- a/docs/manuals/10-security-model.md +++ b/docs/manuals/10-security-model.md @@ -110,7 +110,7 @@ Set operators and `IfExists` can be combined: `ForAllValues:StringEqualsIfExists | `aws:PrincipalTag/` | String | Tag on the authenticated principal (user or role) | | `aws:ResourceTag/` | String | Tag on the target DynamoDB table | | `dynamodb:LeadingKeys` | Multi-valued string | Partition key values being accessed | -| `dynamodb:Attributes` | Multi-valued string | Attribute names being read or written | +| `dynamodb:Attributes` | Multi-valued string | Top-level names of the attributes the request names (see below) | | `dynamodb:Select` | String | The `Select` parameter value | | `dynamodb:ReturnValues` | String | The `ReturnValues` parameter value | | `dynamodb:ReturnConsumedCapacity` | String | The `ReturnConsumedCapacity` parameter value | @@ -133,6 +133,23 @@ extenddb supports the same access control patterns as real AWS IAM: `dynamodb:LeadingKeys` is evaluated on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`. A `Query` on a global secondary index binds the key to the index's partition key value (its first HASH attribute). `Scan` and `SearchVectors` target no single partition and carry no `dynamodb:LeadingKeys` value, so a `ForAllValues` condition does not fence them; fence a full-table read with `ForAnyValue:StringEquals` or a `Null` guard on the statement. A numeric partition key is compared as the normalized number on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and `BatchGetItem`, and as Java's `BigDecimal` prints it (`10.0` stays `10.0`, `1E1` is `1E+1`) on `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, which is what DynamoDB does. +`dynamodb:Attributes` holds the top-level names of the attributes a request names, with `ExpressionAttributeNames` placeholders resolved. A document path contributes its first name: a projection of `address.city` or `#a.city` gives `address`, and `tags[0]` gives `tags`. An entry of `AttributesToGet` is one name as written, dot included. The key holds: + +| Operation | `dynamodb:Attributes` holds | +|-----------|-----------------------------| +| `GetItem` | The table's key attributes, `AttributesToGet`, and the attributes in `ProjectionExpression` | +| `PutItem` | The table's key attributes, the item's attributes, `Expected`, and the attributes in `ConditionExpression` | +| `UpdateItem` | The `Key` attributes, the table's key attributes, `AttributeUpdates`, `Expected`, the attributes in `ConditionExpression`, and the attributes in `UpdateExpression`: the target of every `SET`, `REMOVE`, `ADD` and `DELETE` action and the attributes its value reads | +| `DeleteItem` | The `Key` attributes, the table's key attributes, `Expected`, and the attributes in `ConditionExpression` | +| `Query` | The key attributes of the table and of the queried index, `AttributesToGet`, `QueryFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | +| `Scan` | `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | +| `SearchVectors` | The attributes in `ProjectionExpression` | +| `BatchGetItem` | Per table: the attributes of every key, the table's key attributes, `AttributesToGet`, and the attributes in `ProjectionExpression` | +| `BatchWriteItem` | Per table: the attributes of every `PutRequest` item and `DeleteRequest` key, and the table's key attributes | +| `TransactGetItems`, `TransactWriteItems` | Per item, what the single-item operation holds: `Get` as `GetItem`, `Put` as `PutItem`, `Update` as `UpdateItem`, `Delete` as `DeleteItem`; a `ConditionCheck` holds the table's key attributes and the attributes in its `ConditionExpression` | + +The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). A `Scan` that names no attribute is evaluated with the key absent. An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `Scan` and `SearchVectors` holds them. The entries for the item attributes of `PutItem`, the target of a `SET`, `DeleteItem`, `AttributesToGet` on `GetItem` and `Scan`, projected document paths, reads with no projection, and the batch and transaction forms of those requests were measured on DynamoDB on 2026-10-31. The other entries follow the IAM documentation for the key, which counts the attributes read in a condition or a filter and requires the key attributes in an allowlist. + ### Resource ARNs Resources are identified by ARN: `arn:aws:dynamodb:::table/`. Wildcard matching (`*`, `?`) in policy Resource fields follows AWS IAM conventions. `NotResource` is also supported. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 11a35c1b5..f49de30bc 100755 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -331,6 +331,11 @@ These errors occur when `auth.provider = "builtin"` is enabled. To allowlist attributes (allow only when *every* requested attribute is in the set), use `ForAllValues:` on an `Allow` statement. The same rule applies to `dynamodb:LeadingKeys`. +`dynamodb:Attributes` holds the top-level names of the attributes the request names: the attributes of a `PutItem` item and of the items and keys in a `BatchWriteItem` request, the targets and operands of an `UpdateExpression`, `AttributesToGet`, the first name of each projected document path (`secret` for `secret.a`), the attributes a `ConditionExpression` or `FilterExpression` reads, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). Batch requests are evaluated per table and transactions per item. The [security model manual](manuals/10-security-model.md) lists what the key holds for each operation. Two cases follow from this: + +- A `Deny` on `ForAnyValue:StringEquals` over `ssn` does not block a `DeleteItem` of an item that holds `ssn`, or a read with no projection, which returns `ssn` with the rest of the item: neither request names `ssn`. DynamoDB answers the same way (measured 2026-10-31). +- An `Allow` on `ForAllValues:StringEquals` that does not list the table's key attributes grants no `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, batch or transaction request, because each of them holds the key attributes. List the key attributes of the table and of every index the statement covers. + > Note: prior to v0.1.6, extenddb incorrectly treated a bare operator on these keys as an implicit AND across the requested values, so a single-attribute request appeared to be denied while a multi-attribute request leaked. That behavior diverged from AWS and gave a false sense of security. See the upgrade manual for migration guidance. ## Connection Issues From cd5c766e2ac23c4abd224141df6a19c0ed06529c Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:14:58 +0000 Subject: [PATCH 05/21] refactor(server): merge identical match arms in the Attributes extractor Rename the ExpressionAttributeNames field so it no longer repeats the struct name, merge the match arms that walk two operands, and replace for_each with loops. Behaviour is unchanged. --- crates/server/src/request_attributes.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/server/src/request_attributes.rs b/crates/server/src/request_attributes.rs index 3bc74b353..78080b2b4 100644 --- a/crates/server/src/request_attributes.rs +++ b/crates/server/src/request_attributes.rs @@ -164,7 +164,7 @@ pub(crate) fn transact_item_attributes( /// against. struct Names<'a> { found: BTreeSet, - expression_names: Option<&'a Map>, + placeholders: Option<&'a Map>, limits: &'a LimitsConfig, } @@ -172,7 +172,7 @@ impl<'a> Names<'a> { fn new(request: &'a Value, limits: &'a LimitsConfig) -> Self { Self { found: BTreeSet::new(), - expression_names: request + placeholders: request .get("ExpressionAttributeNames") .and_then(Value::as_object), limits, @@ -325,16 +325,19 @@ impl<'a> Names<'a> { match expr { Expr::Path(path) => self.path(path), Expr::Placeholder(_) => {} - Expr::Compare { left, right, .. } | Expr::Arithmetic { left, right, .. } => { - self.expr(left); - self.expr(right); - } - Expr::And(left, right) | Expr::Or(left, right) => { + Expr::Compare { left, right, .. } + | Expr::Arithmetic { left, right, .. } + | Expr::And(left, right) + | Expr::Or(left, right) => { self.expr(left); self.expr(right); } Expr::Not(inner) => self.expr(inner), - Expr::Function { args, .. } => args.iter().for_each(|arg| self.expr(arg)), + Expr::Function { args, .. } => { + for arg in args { + self.expr(arg); + } + } Expr::Between { operand, low, high } => { self.expr(operand); self.expr(low); @@ -342,7 +345,9 @@ impl<'a> Names<'a> { } Expr::In { operand, list } => { self.expr(operand); - list.iter().for_each(|item| self.expr(item)); + for item in list { + self.expr(item); + } } } } @@ -355,7 +360,7 @@ impl<'a> Names<'a> { }; let name = if first.starts_with('#') { match self - .expression_names + .placeholders .and_then(|names| names.get(first)) .and_then(Value::as_str) { From da1247044163e67a5ac483343447d10f43ecf522 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:48:20 +0000 Subject: [PATCH 06/21] fix(server): leave the paths a SET value reads out of dynamodb:Attributes DynamoDB authorizes an UpdateItem whose only mention of an attribute is in a SET value. Under a Deny gated on ForAnyValue:StringEquals over ssn, it authorized SET #n = ssn, SET #n = if_not_exists(ssn, :d), list_append(ssn, :l) and ssn + :one, and refused REMOVE ssn, ADD ssn :one and DELETE ssn :s (measured 2026-11-01). The key for an UpdateItem and for a transaction's Update item now holds the attribute each action names, and no longer the paths its value reads. --- crates/server/src/request_attributes.rs | 43 ++++++++++--------- crates/server/src/request_attributes/tests.rs | 17 +++++++- 2 files changed, 37 insertions(+), 23 deletions(-) diff --git a/crates/server/src/request_attributes.rs b/crates/server/src/request_attributes.rs index 78080b2b4..ea14a7d0c 100644 --- a/crates/server/src/request_attributes.rs +++ b/crates/server/src/request_attributes.rs @@ -18,9 +18,11 @@ //! - `PutItem`: the table's key attributes, the item's attributes, `Expected`, //! and the paths in `ConditionExpression`. //! - `UpdateItem`: the `Key` attributes, the table's key attributes, -//! `AttributeUpdates`, `Expected`, and the paths in `UpdateExpression` (the -//! attribute each action names and the paths in its value) and in -//! `ConditionExpression`. +//! `AttributeUpdates`, `Expected`, the paths in `ConditionExpression`, and +//! the attribute each `SET`, `REMOVE`, `ADD` and `DELETE` action in +//! `UpdateExpression` names. The paths a `SET` value reads are not in the +//! key: the service authorizes `SET #n = ssn`, which copies `ssn` into +//! `name`, under a Deny on `ssn` (measured 2026-11-01). //! - `DeleteItem`: the `Key` attributes, the table's key attributes, `Expected`, //! and the paths in `ConditionExpression`. //! - `Query`: the key attributes of the table and of the queried index, @@ -39,15 +41,17 @@ //! single-item operation the item maps to. A `ConditionCheck` holds the //! table's key attributes and the paths in its `ConditionExpression`. //! -//! The service's answers were measured on 2026-10-31 for the item attributes of -//! `PutItem`, the attribute a `SET` action names (by name and through -//! `ExpressionAttributeNames`), `DeleteItem`, `AttributesToGet` on `GetItem` and -//! `Scan`, a `ProjectionExpression` naming a document path, reads with no -//! projection, and the batch and transaction forms of those requests. The rest -//! follows the IAM documentation for the key: it holds the attributes a request -//! names, including those read in a condition or a filter, and a policy that -//! lists the allowed attributes must list the key attributes of the table and of -//! its indexes. +//! The service's answers were measured on 2026-10-31 and 2026-11-01 for the +//! item attributes of `PutItem`, every kind of `UpdateExpression` action and +//! `SET` value, `DeleteItem`, `ConditionExpression` on `PutItem` and +//! `DeleteItem`, `FilterExpression` on `Query` and `Scan`, `Expected`, +//! `AttributeUpdates`, `ScanFilter`, `AttributesToGet` on `GetItem` and `Scan`, +//! the table's key attributes on `GetItem`, a `ProjectionExpression` naming a +//! document path, reads with no projection, a `ConditionCheck`, and the batch +//! and transaction forms of `GetItem` and `PutItem`. The rest follows the same +//! rules: `ExclusiveStartKey`, `QueryFilter`, `AttributesToGet` on `Query` and +//! `BatchGetItem`, the queried index's key attributes, `DeleteRequest` keys, +//! and the `Update` and `Delete` items of a transaction. //! //! An expression is read with the tokenizer and parser the engine uses, under //! the same size, token and depth limits, so every expression the engine @@ -280,8 +284,8 @@ impl<'a> Names<'a> { } } - /// The attribute each `UpdateExpression` action names and the paths in its - /// value. + /// The attribute each `UpdateExpression` action names. The paths a value + /// reads are left out, as the service leaves them out. fn update(&mut self, value: Option<&Value>) { let Some(text) = self.text(value) else { return; @@ -294,13 +298,10 @@ impl<'a> Names<'a> { .and_then(|tokens| parse_update_from(&tokens, text)); for action in parsed.iter().flatten() { match action { - UpdateAction::Set { path, value } - | UpdateAction::Add { path, value } - | UpdateAction::Delete { path, value } => { - self.path(path); - self.expr(value); - } - UpdateAction::Remove { path } => self.path(path), + UpdateAction::Set { path, .. } + | UpdateAction::Remove { path } + | UpdateAction::Add { path, .. } + | UpdateAction::Delete { path, .. } => self.path(path), } } } diff --git a/crates/server/src/request_attributes/tests.rs b/crates/server/src/request_attributes/tests.rs index 4ac14a1cf..ebbdd8269 100644 --- a/crates/server/src/request_attributes/tests.rs +++ b/crates/server/src/request_attributes/tests.rs @@ -141,7 +141,10 @@ fn update_item_holds_the_attribute_set_names() { } #[test] -fn update_item_holds_every_action_target_and_value_path() { +fn update_item_holds_every_action_target_but_not_the_paths_a_value_reads() { + // Measured: REMOVE, ADD and DELETE of ssn match a Deny on ssn, and a SET + // value that reads ssn (`ssn`, `#s`, `if_not_exists`, `list_append`, + // `ssn + :one`) does not. let input = json!({ "TableName": "t", "Key": {"pk": {"S": "a"}}, @@ -152,7 +155,17 @@ fn update_item_holds_every_action_target_and_value_path() { }); assert_eq!( single("UpdateItem", &input, Some(&hash_table())), - names(&["a", "b", "c", "e", "f", "g", "i", "k", "mset", "pk", "x"]) + names(&["a", "c", "f", "i", "k", "mset", "pk", "x"]) + ); + let copy = json!({ + "TableName": "t", + "Key": {"pk": {"S": "a"}}, + "UpdateExpression": "SET #n = #s, o = if_not_exists(ssn, :d), p = :one + ssn", + "ExpressionAttributeNames": {"#n": "name", "#s": "ssn"}, + }); + assert_eq!( + single("UpdateItem", ©, Some(&hash_table())), + names(&["name", "o", "p", "pk"]) ); } From ee8541082c6742082695e0f30da3bbe8d4cb865b Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 00:57:43 +0000 Subject: [PATCH 07/21] test: cover dynamodb:Attributes for conditions, filters, update actions and key attributes One test per request shape measured on DynamoDB on 2026-11-01: a condition, filter, Expected, AttributeUpdates or ScanFilter that reads a denied attribute, a REMOVE, ADD or DELETE of it, and a ConditionCheck on it are refused; a SET value that reads it is authorized and copies it; a bare StringNotEquals grants no Scan; a Null condition does not refuse a GetItem with no projection; and an allowlist without the table's key attribute refuses a projected GetItem. The new module reuses the fixtures of test_dynamodb_attributes_condition.py. --- tests/test_dynamodb_attributes_condition.py | 82 +++++-- tests/test_dynamodb_attributes_expressions.py | 232 ++++++++++++++++++ 2 files changed, 290 insertions(+), 24 deletions(-) create mode 100644 tests/test_dynamodb_attributes_expressions.py diff --git a/tests/test_dynamodb_attributes_condition.py b/tests/test_dynamodb_attributes_condition.py index c1597a47b..34f57dcb6 100644 --- a/tests/test_dynamodb_attributes_condition.py +++ b/tests/test_dynamodb_attributes_condition.py @@ -1,27 +1,33 @@ # Copyright 2026 ExtendDB contributors # SPDX-License-Identifier: Apache-2.0 -"""dynamodb:Attributes on writes, AttributesToGet reads, document paths, and -batch and transaction requests. +"""dynamodb:Attributes on writes, reads, document paths, conditions, filters, +legacy parameters, and batch and transaction requests. -Every request shape here was sent to live DynamoDB on 2026-10-31 under a -session policy of the same shape as the user policy the test attaches, twice, -with the same answer both times. The policies: +Every request shape here was sent to live DynamoDB on 2026-10-31 or +2026-11-01 under a session policy of the same shape as the user policy the +test attaches, twice, with the same answer both times. The policies: * deny(X): Allow dynamodb:* on the table, and Deny dynamodb:* on the table with ForAnyValue:StringEquals {"dynamodb:Attributes": [X]}. - * allow-put for-all(pk): the only statement allows dynamodb:PutItem on the - table with ForAllValues:StringEquals {"dynamodb:Attributes": ["pk"]}. - -Under deny(ssn) the service refused a PutItem, BatchWriteItem or -TransactWriteItems Put of an item holding ssn, an UpdateItem that sets ssn, -and a GetItem, Scan, BatchGetItem or TransactGetItems read that names ssn in -AttributesToGet or ProjectionExpression. Under deny(secret) it refused the -projections `secret.a` and `#s.a`, so a document path is matched by its -top-level name. It allowed a DeleteItem of an item holding ssn, an UpdateItem -that sets another attribute through a name placeholder, and reads with no -projection, which returned whole items. allow-put for-all(pk) admitted a -PutItem of {pk} and refused one of {pk, ssn}. + * allow-only(action, condition): the only statement allows the action on + the table under the condition: ForAllValues:StringEquals over a list of + names, or a bare StringNotEquals {"dynamodb:Attributes": "ssn"}. + * deny-get-when-absent: Allow dynamodb:* on the table, and Deny + dynamodb:GetItem with Null {"dynamodb:Attributes": "true"}. + +Under deny(ssn) the service refused every request that names ssn as an item +attribute, the target of a SET, REMOVE, ADD or DELETE action, an attribute a +condition, filter, Expected, AttributeUpdates or ScanFilter reads, or an entry +of AttributesToGet or ProjectionExpression, in the single, batch and +transaction forms measured. A document path is matched by its top-level name. +It authorized an UpdateItem whose only mention of ssn is in a SET value, which +copies ssn into another attribute. It allowed a DeleteItem of an item holding +ssn and reads with no projection, which returned whole items: the key holds +the names the request gives, and on GetItem the table's key attribute too, so +an Allow for all of ["name"] refused a GetItem of name and deny-get-when-absent +did not refuse a GetItem with no projection. A bare StringNotEquals granted no +Scan, whether the Scan named an attribute or not. Prerequisites: - extenddb running with `auth.provider = "builtin"` on EXTENDDB_TEST_ENDPOINT @@ -124,23 +130,41 @@ def _deny(arn: str, attribute: str) -> dict: } -def _allow_put_for_all(arn: str, attributes: list[str]) -> dict: - """allow-put for-all: PutItem only, and only of the listed attributes.""" +def _allow_only(arn: str, action: str, condition: dict) -> dict: + """A policy whose only statement allows `action` on the table under `condition`.""" return { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", - "Action": "dynamodb:PutItem", + "Action": f"dynamodb:{action}", "Resource": arn, - "Condition": { - "ForAllValues:StringEquals": {"dynamodb:Attributes": attributes} - }, + "Condition": condition, } ], } +def _for_all(attributes: list[str]) -> dict: + return {"ForAllValues:StringEquals": {"dynamodb:Attributes": attributes}} + + +def _deny_get_when_absent(arn: str) -> dict: + """Everything on the table, except a GetItem whose key is absent.""" + return { + "Version": "2012-10-17", + "Statement": [ + {"Effect": "Allow", "Action": "dynamodb:*", "Resource": arn}, + { + "Effect": "Deny", + "Action": "dynamodb:GetItem", + "Resource": arn, + "Condition": {"Null": {"dynamodb:Attributes": "true"}}, + }, + ], + } + + @dataclass class Env: admin: Any @@ -148,6 +172,10 @@ class Env: deny_ssn: Any deny_secret: Any allow_put_pk: Any + allow_scan_unless_ssn: Any + deny_get_when_absent: Any + allow_get_name: Any + allow_get_pk_name: Any @pytest.fixture(scope="module") @@ -212,7 +240,13 @@ def client_with(policy: dict) -> Any: table=table, deny_ssn=client_with(_deny(arn, "ssn")), deny_secret=client_with(_deny(arn, "secret")), - allow_put_pk=client_with(_allow_put_for_all(arn, ["pk"])), + allow_put_pk=client_with(_allow_only(arn, "PutItem", _for_all(["pk"]))), + allow_scan_unless_ssn=client_with( + _allow_only(arn, "Scan", {"StringNotEquals": {"dynamodb:Attributes": "ssn"}}) + ), + deny_get_when_absent=client_with(_deny_get_when_absent(arn)), + allow_get_name=client_with(_allow_only(arn, "GetItem", _for_all(["name"]))), + allow_get_pk_name=client_with(_allow_only(arn, "GetItem", _for_all(["pk", "name"]))), ) try: admin.delete_table(TableName=table) diff --git a/tests/test_dynamodb_attributes_expressions.py b/tests/test_dynamodb_attributes_expressions.py new file mode 100644 index 000000000..2018ca77a --- /dev/null +++ b/tests/test_dynamodb_attributes_expressions.py @@ -0,0 +1,232 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""dynamodb:Attributes for conditions, filters, legacy parameters, update +actions, key attributes, and bare and Null conditions. + +Every request shape here was sent to live DynamoDB on 2026-11-01 under a +session policy of the same shape as the user policy the test attaches, twice, +with the same answer both times. The policies and the fixtures are those of +test_dynamodb_attributes_condition.py; its docstring describes them. + +Prerequisites and run command: as for test_dynamodb_attributes_condition.py. + +REQ-TEST-001, REQ-AUTH-002 +""" + +from __future__ import annotations + +import re +import uuid +from typing import Any, Callable + +import pytest +from botocore.exceptions import ClientError + +# The fixtures are imported so this module gets its own account and table. +from test_dynamodb_attributes_condition import ( # noqa: F401 + ITEM_A, + KEY_A, + Env, + _assert_denied, + _new_key, + _refusal, + _stored, + account_id, + auth_env, + env, + mgmt, + region, +) + +MISSING_ATTRIBUTE = "The provided expression refers to an attribute that does not exist in the item" + + +def _shapes_naming_ssn(table: str, key: dict) -> dict[str, tuple[str, Callable[[Any], Any]]]: + """Requests that name ssn only in a condition, a filter, a legacy parameter + or an update action target, each with the action its refusal names.""" + one = {":one": {"N": "1"}} + return { + "put_condition": ("PutItem", lambda c: c.put_item( + TableName=table, Item=key, ConditionExpression="attribute_not_exists(ssn)")), + "put_expected": ("PutItem", lambda c: c.put_item( + TableName=table, Item=key, Expected={"ssn": {"Exists": False}})), + "delete_condition": ("DeleteItem", lambda c: c.delete_item( + TableName=table, Key=key, ConditionExpression="attribute_not_exists(ssn)")), + "update_remove": ("UpdateItem", lambda c: c.update_item( + TableName=table, Key=key, UpdateExpression="REMOVE ssn")), + "update_add": ("UpdateItem", lambda c: c.update_item( + TableName=table, Key=key, UpdateExpression="ADD ssn :one", + ExpressionAttributeValues=one)), + "update_delete": ("UpdateItem", lambda c: c.update_item( + TableName=table, Key=key, UpdateExpression="DELETE ssn :s", + ExpressionAttributeValues={":s": {"SS": ["x"]}})), + "update_attribute_updates": ("UpdateItem", lambda c: c.update_item( + TableName=table, Key=key, + AttributeUpdates={"ssn": {"Value": {"S": "1"}, "Action": "PUT"}})), + "query_filter": ("Query", lambda c: c.query( + TableName=table, KeyConditionExpression="pk = :p", FilterExpression="ssn = :v", + ExpressionAttributeValues={":p": KEY_A["pk"], ":v": {"S": "1"}})), + "scan_filter": ("Scan", lambda c: c.scan( + TableName=table, FilterExpression="ssn = :v", + ExpressionAttributeValues={":v": {"S": "1"}})), + "scan_legacy_filter": ("Scan", lambda c: c.scan( + TableName=table, ScanFilter={"ssn": {"ComparisonOperator": "NOT_NULL"}})), + "condition_check": ("ConditionCheckItem", lambda c: c.transact_write_items( + TransactItems=[{"ConditionCheck": { + "TableName": table, "Key": KEY_A, + "ConditionExpression": "attribute_exists(ssn)"}}])), + } + + +@pytest.mark.parametrize("shape", sorted(_shapes_naming_ssn("t", {}))) +def test_a_request_naming_a_denied_attribute_outside_its_item_is_denied(env: Env, shape: str): + key = _new_key() + seeded = {**key, "ssn": {"S": "1"}} + env.admin.put_item(TableName=env.table, Item=seeded) + action, call = _shapes_naming_ssn(env.table, key)[shape] + _assert_denied(lambda: call(env.deny_ssn), action) + assert _stored(env, key) == seeded, "a refused request changes nothing" + + +# -- SET values ------------------------------------------------------------ + +# Each SET value reads ssn, on an item that does not hold it, with the answer +# the service gave: authorized, then refused by evaluation (None means the +# update succeeds). +SET_VALUES_READING_SSN = { + "SET #n = ssn": MISSING_ATTRIBUTE, + "SET #n = #s": MISSING_ATTRIBUTE, + "SET #n = if_not_exists(ssn, :d)": None, + "SET #n = if_not_exists(#n, ssn)": MISSING_ATTRIBUTE, + "SET #n = list_append(ssn, :l)": MISSING_ATTRIBUTE, + "SET #n = list_append(:l, ssn)": MISSING_ATTRIBUTE, + "SET #n = ssn + :one": MISSING_ATTRIBUTE, + "SET #n = :one + ssn": MISSING_ATTRIBUTE, +} +_VALUES = {":d": {"S": "d"}, ":l": {"L": [{"S": "x"}]}, ":one": {"N": "1"}} + + +@pytest.mark.parametrize("expression", list(SET_VALUES_READING_SSN)) +def test_a_set_value_reading_a_denied_attribute_is_authorized(env: Env, expression: str): + key = _new_key() + env.admin.put_item(TableName=env.table, Item=key) + names = {"#n": "name"} + if "#s" in expression: + names["#s"] = "ssn" + kwargs: dict = {"ExpressionAttributeNames": names} + values = {v: _VALUES[v] for v in sorted(set(re.findall(r":\w+", expression)))} + if values: + kwargs["ExpressionAttributeValues"] = values + expected = SET_VALUES_READING_SSN[expression] + + def update() -> Any: + return env.deny_ssn.update_item( + TableName=env.table, Key=key, UpdateExpression=expression, **kwargs + ) + + if expected is None: + update() + assert _stored(env, key) == {**key, "name": {"S": "d"}} + return + with pytest.raises(ClientError) as exc: + update() + err = exc.value.response["Error"] + assert err["Code"] == "ValidationException", exc.value.response + assert expected in err["Message"], err + + +def test_a_set_value_copies_a_denied_attribute_into_another_one(env: Env): + key = _new_key() + env.admin.put_item(TableName=env.table, Item={**key, "ssn": {"S": "999-00-1111"}}) + resp = env.deny_ssn.update_item( + TableName=env.table, + Key=key, + UpdateExpression="SET #n = ssn", + ExpressionAttributeNames={"#n": "name"}, + ReturnValues="UPDATED_NEW", + ) + assert resp["Attributes"] == {"name": {"S": "999-00-1111"}} + read = env.deny_ssn.get_item( + TableName=env.table, Key=key, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ) + assert read["Item"] == {"name": {"S": "999-00-1111"}} + + +# -- reads the key does not stop ------------------------------------------- + + +def test_query_with_no_filter_or_projection_returns_the_whole_item(env: Env): + resp = env.deny_ssn.query( + TableName=env.table, + KeyConditionExpression="pk = :p", + ExpressionAttributeValues={":p": KEY_A["pk"]}, + ) + assert resp["Items"] == [ITEM_A] + + +def test_scan_from_an_exclusive_start_key_is_allowed(env: Env): + resp = env.deny_ssn.scan(TableName=env.table, ExclusiveStartKey=KEY_A) + assert "Items" in resp + + +# -- bare and Null conditions ---------------------------------------------- + + +def test_a_bare_negated_operator_grants_no_scan_naming_no_attribute(env: Env): + msg = _refusal(lambda: env.allow_scan_unless_ssn.scan(TableName=env.table), "Scan") + assert "because no identity-based policy allows the dynamodb:Scan action" in msg, msg + + +def test_a_bare_negated_operator_grants_no_scan_naming_another_attribute(env: Env): + msg = _refusal( + lambda: env.allow_scan_unless_ssn.scan( + TableName=env.table, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ), + "Scan", + ) + assert "because no identity-based policy allows the dynamodb:Scan action" in msg, msg + + +def test_a_null_deny_does_not_refuse_a_get_item_with_no_projection(env: Env): + resp = env.deny_get_when_absent.get_item(TableName=env.table, Key=KEY_A) + assert resp["Item"] == ITEM_A + + +def test_a_null_deny_does_not_refuse_a_projected_get_item(env: Env): + resp = env.deny_get_when_absent.get_item( + TableName=env.table, Key=KEY_A, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ) + assert resp["Item"] == {"name": {"S": "alice"}} + + +# -- the table's key attribute is in the key on GetItem ---------------------- + + +def test_for_all_values_allow_without_the_key_attribute_refuses_a_projected_get(env: Env): + msg = _refusal( + lambda: env.allow_get_name.get_item( + TableName=env.table, Key=KEY_A, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ), + "GetItem", + ) + assert "because no identity-based policy allows the dynamodb:GetItem action" in msg, msg + + +def test_for_all_values_allow_with_the_key_attribute_admits_a_projected_get(env: Env): + resp = env.allow_get_pk_name.get_item( + TableName=env.table, Key=KEY_A, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ) + assert resp["Item"] == {"name": {"S": "alice"}} + + +def test_for_all_values_allow_with_the_key_attribute_admits_a_get_with_no_projection( + env: Env, +): + resp = env.allow_get_pk_name.get_item(TableName=env.table, Key=KEY_A) + assert resp["Item"] == ITEM_A From c5b49ee16824ed7d51c1a7b4c0fe6078f7f57834 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 01:29:46 +0000 Subject: [PATCH 08/21] docs: describe the update action rule for dynamodb:Attributes The security model manual, the troubleshooting guide and the upgrade manual now say that the key holds the target of each UpdateExpression action and not the attributes a SET value reads, and that, as in DynamoDB, a policy on dynamodb:Attributes does not stop an UpdateItem from copying a denied attribute's value into another attribute through a SET value. The security model manual lists the entries measured on DynamoDB on 2026-10-31 and 2026-11-01, and records that a bare StringNotEquals on the key matches neither a present nor an absent key. --- docs/manuals/07-upgrade-manual.md | 4 ++-- docs/manuals/10-security-model.md | 6 ++++-- docs/troubleshooting.md | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index 9d812d043..c0d9c3d69 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -667,7 +667,7 @@ No catalog migration is required. Before upgrading a deployment that restricts t ### v0.1.15: `dynamodb:Attributes` holds the attributes every item request names -**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition, filter or update expression reads, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey`, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). +**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition or filter expression reads, the target of each update expression action, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey`, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). It does not hold the attributes a `SET` value reads. **Who is affected.** Any deployment with a policy statement that names `dynamodb:Attributes` in a condition: @@ -675,7 +675,7 @@ No catalog migration is required. Before upgrading a deployment that restricts t - An `Allow` gated on `ForAllValues:StringEquals` used to grant those requests whatever attributes they named, because the absent key satisfied it. It now grants only when every name the request holds is in the list. A list that omits the table's key attributes stops granting `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and the batch and transaction requests; add the key attributes of the table and of every index the statement covers. - `Null` with value `true` on `dynamodb:Attributes` matched those requests before, and matched a `GetItem`, `Query`, `BatchGetItem` or `TransactGetItems` read with no projection. It no longer matches any of them, because each holds at least the table's key attributes; it still matches a `Scan` that names no attribute. A statement that used it to refuse reads with no projection no longer refuses them. -A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. +A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). Nor does it stop an `UpdateItem` from copying the attribute's value into another attribute through a `SET` value such as `SET #n = ssn`, as on DynamoDB (measured 2026-11-01). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. --- diff --git a/docs/manuals/10-security-model.md b/docs/manuals/10-security-model.md index 88d5958d3..d93f5ae6b 100755 --- a/docs/manuals/10-security-model.md +++ b/docs/manuals/10-security-model.md @@ -139,7 +139,7 @@ extenddb supports the same access control patterns as real AWS IAM: |-----------|-----------------------------| | `GetItem` | The table's key attributes, `AttributesToGet`, and the attributes in `ProjectionExpression` | | `PutItem` | The table's key attributes, the item's attributes, `Expected`, and the attributes in `ConditionExpression` | -| `UpdateItem` | The `Key` attributes, the table's key attributes, `AttributeUpdates`, `Expected`, the attributes in `ConditionExpression`, and the attributes in `UpdateExpression`: the target of every `SET`, `REMOVE`, `ADD` and `DELETE` action and the attributes its value reads | +| `UpdateItem` | The `Key` attributes, the table's key attributes, `AttributeUpdates`, `Expected`, the attributes in `ConditionExpression`, and the target of every `SET`, `REMOVE`, `ADD` and `DELETE` action in `UpdateExpression`; not the attributes a `SET` value reads | | `DeleteItem` | The `Key` attributes, the table's key attributes, `Expected`, and the attributes in `ConditionExpression` | | `Query` | The key attributes of the table and of the queried index, `AttributesToGet`, `QueryFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | | `Scan` | `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | @@ -148,7 +148,9 @@ extenddb supports the same access control patterns as real AWS IAM: | `BatchWriteItem` | Per table: the attributes of every `PutRequest` item and `DeleteRequest` key, and the table's key attributes | | `TransactGetItems`, `TransactWriteItems` | Per item, what the single-item operation holds: `Get` as `GetItem`, `Put` as `PutItem`, `Update` as `UpdateItem`, `Delete` as `DeleteItem`; a `ConditionCheck` holds the table's key attributes and the attributes in its `ConditionExpression` | -The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). A `Scan` that names no attribute is evaluated with the key absent. An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `Scan` and `SearchVectors` holds them. The entries for the item attributes of `PutItem`, the target of a `SET`, `DeleteItem`, `AttributesToGet` on `GetItem` and `Scan`, projected document paths, reads with no projection, and the batch and transaction forms of those requests were measured on DynamoDB on 2026-10-31. The other entries follow the IAM documentation for the key, which counts the attributes read in a condition or a filter and requires the key attributes in an allowlist. +The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). As in DynamoDB, a policy on `dynamodb:Attributes` does not stop an `UpdateItem` from copying a denied attribute's value into another attribute through a `SET` value: under a `Deny` on `ssn`, `SET #n = ssn` with `#n` for `name` is allowed, and a projection of `name` then returns the value (measured on DynamoDB 2026-11-01). A `Scan` that names no attribute is evaluated with the key absent. A bare `StringNotEquals` on the key does not match whether the key is present or absent, so an `Allow` gated on it grants nothing (measured on DynamoDB 2026-11-01 on `Scan`). An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `Scan` and `SearchVectors` holds them. + +These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item attributes of `PutItem`; the target of a `SET`, `REMOVE`, `ADD` or `DELETE` action and the attributes a `SET` value reads; `ConditionExpression` on `PutItem`, `DeleteItem` and a `ConditionCheck`; `FilterExpression` on `Query` and `Scan`; `Expected`, `AttributeUpdates` and `ScanFilter`; `AttributesToGet` on `GetItem` and `Scan`; the table's key attribute on `GetItem`; projected document paths; reads with no projection; `DeleteItem`; and the `BatchGetItem`, `TransactGetItems`, `BatchWriteItem` and `TransactWriteItems` forms of a projection and a put. The other entries (`ExclusiveStartKey`, `QueryFilter`, `AttributesToGet` on `Query` and `BatchGetItem`, the queried index's key attributes, `DeleteRequest` keys, and the `Update` and `Delete` items of a transaction) follow the same rules. ### Resource ARNs diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index f49de30bc..059f442ab 100755 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -331,10 +331,11 @@ These errors occur when `auth.provider = "builtin"` is enabled. To allowlist attributes (allow only when *every* requested attribute is in the set), use `ForAllValues:` on an `Allow` statement. The same rule applies to `dynamodb:LeadingKeys`. -`dynamodb:Attributes` holds the top-level names of the attributes the request names: the attributes of a `PutItem` item and of the items and keys in a `BatchWriteItem` request, the targets and operands of an `UpdateExpression`, `AttributesToGet`, the first name of each projected document path (`secret` for `secret.a`), the attributes a `ConditionExpression` or `FilterExpression` reads, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). Batch requests are evaluated per table and transactions per item. The [security model manual](manuals/10-security-model.md) lists what the key holds for each operation. Two cases follow from this: +`dynamodb:Attributes` holds the top-level names of the attributes the request names: the attributes of a `PutItem` item and of the items and keys in a `BatchWriteItem` request, the target of each `UpdateExpression` action, `AttributesToGet`, the first name of each projected document path (`secret` for `secret.a`), the attributes a `ConditionExpression` or `FilterExpression` reads, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). The attributes a `SET` value reads are not in it. Batch requests are evaluated per table and transactions per item. The [security model manual](manuals/10-security-model.md) lists what the key holds for each operation. It follows that: - A `Deny` on `ForAnyValue:StringEquals` over `ssn` does not block a `DeleteItem` of an item that holds `ssn`, or a read with no projection, which returns `ssn` with the rest of the item: neither request names `ssn`. DynamoDB answers the same way (measured 2026-10-31). -- An `Allow` on `ForAllValues:StringEquals` that does not list the table's key attributes grants no `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, batch or transaction request, because each of them holds the key attributes. List the key attributes of the table and of every index the statement covers. +- The same `Deny` does not block an `UpdateItem` that reads `ssn` only in a `SET` value. `SET #n = ssn` copies the value of `ssn` into `name`, and a projection of `name` then returns it. DynamoDB answers the same way (measured 2026-11-01). +- An `Allow` on `ForAllValues:StringEquals` that does not list the table's key attributes grants no `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, batch or transaction request, because each of them holds the key attributes (measured on DynamoDB 2026-11-01 for `GetItem`). List the key attributes of the table and of every index the statement covers. > Note: prior to v0.1.6, extenddb incorrectly treated a bare operator on these keys as an implicit AND across the requested values, so a single-attribute request appeared to be denied while a multi-attribute request leaked. That behavior diverged from AWS and gave a false sense of security. See the upgrade manual for migration guidance. From 5ab9c262cc428753737e1668c36dacb8b7de71a4 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:21:19 +0000 Subject: [PATCH 09/21] fix(server): hold the table's key attributes in dynamodb:Attributes on Scan A Scan now puts the key attributes of the table, and with IndexName the key attributes of that index, in dynamodb:Attributes, as GetItem and Query do. Before this change a Scan that named no attribute left the key absent. DynamoDB, measured on 2026-11-01: a Deny over pk refuses a Scan with no projection. An Allow of Scan alone with ForAllValues:StringEquals over ["name"] refuses a plain, a projected and a Select COUNT Scan; the same Allow over ["pk", "name"] admits the projected Scan, and over ["pk"] it admits a plain Scan. A Deny of Scan gated on Null {"dynamodb:Attributes": "true"} refuses neither a plain nor a projected Scan, and an Allow of Scan alone on the same Null grants neither a plain nor a Select COUNT Scan. With the key absent, ExtendDB used to give the opposite answer to each plain and Select COUNT Scan here except the one under ["pk"]; it also granted the projected Scan under ["name"], whose key held name alone. The index key attributes follow the IAM guide's rule that a policy on dynamodb:Attributes lists the key attributes of the table and of every index it covers. They were not measured. --- crates/server/src/request_attributes.rs | 66 +++++++++++------- crates/server/src/request_attributes/tests.rs | 68 ++++++++++++++++--- 2 files changed, 103 insertions(+), 31 deletions(-) diff --git a/crates/server/src/request_attributes.rs b/crates/server/src/request_attributes.rs index ea14a7d0c..a7d9bfaef 100644 --- a/crates/server/src/request_attributes.rs +++ b/crates/server/src/request_attributes.rs @@ -9,7 +9,9 @@ //! `Name, Address.City` gives `Name` and `Address`. The names come from the //! request alone. A `DeleteItem` and a read with no projection hold only the //! names the request gives, not the names of the stored item they delete or -//! return (measured 2026-10-31), and `ReturnValues` and `Select` add no names. +//! return (measured 2026-10-31). `ReturnValues`, +//! `ReturnValuesOnConditionCheckFailure` and `Select` add no names +//! (`ReturnValuesOnConditionCheckFailure` measured 2026-11-01). //! //! What the key holds for each operation: //! @@ -29,36 +31,52 @@ //! `AttributesToGet`, `QueryFilter`, `ExclusiveStartKey`, and the paths in //! `FilterExpression` and `ProjectionExpression`. A key condition can name //! only key attributes, which the key already holds. -//! - `Scan`: `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the paths -//! in `FilterExpression` and `ProjectionExpression`. A `Scan` names no key, so -//! a `Scan` that gives none of these leaves the key absent. -//! - `SearchVectors`: the paths in `ProjectionExpression`. +//! - `Scan`: the key attributes of the table and of the scanned index, +//! `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the paths in +//! `FilterExpression` and `ProjectionExpression`. A `Scan` that names no +//! attribute, a `Select COUNT` `Scan` included, holds the table's key +//! attributes (measured 2026-11-01). +//! - `SearchVectors`: the paths in `ProjectionExpression`. A `SearchVectors` +//! with no `ProjectionExpression` leaves the key absent. //! - `BatchGetItem`: per table, the attributes of every key, the table's key //! attributes, `AttributesToGet`, and the paths in `ProjectionExpression`. //! - `BatchWriteItem`: per table, the attributes of every `PutRequest` item and -//! every `DeleteRequest` key, and the table's key attributes. +//! every `DeleteRequest` key, and the table's key attributes. Each table's +//! part is evaluated once, with the union of its items' names, as on the +//! service (measured 2026-11-01). //! - `TransactGetItems` and `TransactWriteItems`: per item, as for the //! single-item operation the item maps to. A `ConditionCheck` holds the //! table's key attributes and the paths in its `ConditionExpression`. //! //! The service's answers were measured on 2026-10-31 and 2026-11-01 for the -//! item attributes of `PutItem`, every kind of `UpdateExpression` action and -//! `SET` value, `DeleteItem`, `ConditionExpression` on `PutItem` and -//! `DeleteItem`, `FilterExpression` on `Query` and `Scan`, `Expected`, -//! `AttributeUpdates`, `ScanFilter`, `AttributesToGet` on `GetItem` and `Scan`, -//! the table's key attributes on `GetItem`, a `ProjectionExpression` naming a -//! document path, reads with no projection, a `ConditionCheck`, and the batch -//! and transaction forms of `GetItem` and `PutItem`. The rest follows the same -//! rules: `ExclusiveStartKey`, `QueryFilter`, `AttributesToGet` on `Query` and -//! `BatchGetItem`, the queried index's key attributes, `DeleteRequest` keys, -//! and the `Update` and `Delete` items of a transaction. +//! item attributes of `PutItem`; every kind of `UpdateExpression` action and +//! `SET` value; `DeleteItem`; `ConditionExpression` on `PutItem`, `DeleteItem`, +//! a `ConditionCheck` and a transaction `Delete`; `FilterExpression` on `Query` +//! and `Scan`; `Expected` on `PutItem`; `AttributeUpdates`; `ScanFilter` and +//! `QueryFilter`; `AttributesToGet` on `GetItem`, `Query`, `Scan` and +//! `BatchGetItem`; the table's key attributes on `GetItem`, `Query`, `Scan`, +//! `UpdateItem`, `DeleteItem` and a `DeleteRequest`; a `ProjectionExpression` +//! naming a document path; reads with no projection; a transaction `Update`; +//! and the batch and transaction forms of `GetItem` and `PutItem`. The rest +//! follows the rules measured for the same parameter on other operations: +//! `ProjectionExpression` on `Query` and `Scan`, `Expected` and +//! `ConditionExpression` on `UpdateItem`, and `Expected` on `DeleteItem`. The +//! key attributes of the queried or scanned index follow the IAM guide's rule +//! that a policy on the key lists the primary key and index key attributes of +//! the table and of every secondary index the policy covers; they were not +//! measured. Whether `ExclusiveStartKey` adds names of its own cannot be told +//! on the service: a valid start key holds only key attributes, which the key +//! already holds. //! //! An expression is read with the tokenizer and parser the engine uses, under -//! the same size, token and depth limits, so every expression the engine -//! accepts gives its names here. An expression the engine refuses gives none: -//! the engine then answers `ValidationException` without reading or writing an -//! item. A `#name` with no `ExpressionAttributeNames` entry is skipped for the -//! same reason. +//! the same size, token and depth limits, so on every operation other than +//! `SearchVectors` an expression the engine accepts gives all its names here. +//! `SearchVectors` matches a `#name` against `ExpressionAttributeNames` keys +//! with their leading `#` removed, so it accepts keys this module does not +//! resolve, and its `SearchConditionExpression` is not read here (both #729). +//! An expression the engine refuses gives none: the engine then answers +//! `ValidationException` without reading or writing an item. A `#name` with no +//! `ExpressionAttributeNames` entry is skipped for the same reason. use std::collections::BTreeSet; @@ -92,6 +110,8 @@ pub(crate) fn single_table_attributes( names.range_read(input); } "Scan" => { + names.key_schema(key_info); + names.index_key_schema(input, key_info); names.map_keys(input.get("ScanFilter")); names.range_read(input); } @@ -227,8 +247,8 @@ impl<'a> Names<'a> { } } - /// The key attributes of the index a `Query` names. An index the table - /// does not have adds none: the engine refuses that request. + /// The key attributes of the index a `Query` or `Scan` names. An index the + /// table does not have adds none: the engine refuses that request. fn index_key_schema(&mut self, request: &Value, key_info: Option<&TableKeyInfo>) { let (Some(key_info), Some(index)) = (key_info, request.get("IndexName").and_then(Value::as_str)) diff --git a/crates/server/src/request_attributes/tests.rs b/crates/server/src/request_attributes/tests.rs index ebbdd8269..722b2670d 100644 --- a/crates/server/src/request_attributes/tests.rs +++ b/crates/server/src/request_attributes/tests.rs @@ -3,8 +3,8 @@ //! Unit tests for `dynamodb:Attributes` extraction. //! -//! The rows named "measured" mirror the service's answers of 2026-10-31; the -//! rest follow the IAM documentation for the key. +//! The rows named "measured" mirror the service's answers of 2026-10-31 and +//! 2026-11-01; the rest follow the IAM documentation for the key. use std::collections::HashMap; @@ -215,7 +215,7 @@ fn return_values_and_select_add_no_names() { names(&["pk"]) ); let input = json!({"TableName": "t", "Select": "ALL_ATTRIBUTES"}); - assert_eq!(single("Scan", &input, Some(&hash_table())), None); + assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["pk"])); } // ---- reads ---------------------------------------------------------------- @@ -308,18 +308,70 @@ fn query_holds_the_legacy_filter_and_attributes_to_get() { } #[test] -fn scan_with_no_named_attribute_leaves_the_key_absent() { - // Measured: Scan with Limit=2 and no projection is allowed under a Deny on - // ssn and returns whole items. +fn scan_with_no_named_attribute_holds_the_table_key_attributes() { + // Measured: a Deny on pk refuses a Scan with no projection, and a Deny + // gated on Null true refuses none, so the key holds pk. A Scan with + // Limit=2 and no projection is allowed under a Deny on ssn and returns + // whole items. + let input = json!({"TableName": "t"}); + assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["pk"])); let input = json!({"TableName": "t", "Limit": 2}); - assert_eq!(single("Scan", &input, Some(&hash_table())), None); + assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["pk"])); +} + +#[test] +fn a_select_count_scan_holds_the_table_key_attributes() { + // Measured: an Allow of Scan gated on ForAllValues ["name"], or on Null + // true, grants no Select COUNT Scan. + let input = json!({"TableName": "t", "Select": "COUNT"}); + assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["pk"])); +} + +#[test] +fn scan_holds_the_sort_key_of_the_table() { + let input = json!({"TableName": "i", "Limit": 1}); + assert_eq!( + single("Scan", &input, Some(&indexed_table())), + names(&["pk", "sk"]) + ); +} + +#[test] +fn scan_on_an_index_holds_the_index_key_attributes() { + // Follows the IAM guide's rule for the key attributes of an index; not + // measured. + let gsi = json!({"TableName": "i", "IndexName": "gsi1"}); + assert_eq!( + single("Scan", &gsi, Some(&indexed_table())), + names(&["gpk", "gsk", "pk", "sk"]) + ); + let lsi = json!({"TableName": "i", "IndexName": "lsi1"}); + assert_eq!( + single("Scan", &lsi, Some(&indexed_table())), + names(&["lsk", "pk", "sk"]) + ); + let unknown = json!({"TableName": "i", "IndexName": "nope"}); + assert_eq!( + single("Scan", &unknown, Some(&indexed_table())), + names(&["pk", "sk"]) + ); +} + +#[test] +fn a_scan_of_a_table_that_does_not_exist_names_only_what_it_gives() { + assert_eq!(single("Scan", &json!({"TableName": "gone"}), None), None); + let input = json!({"TableName": "gone", "ExclusiveStartKey": {"pk": {"S": "a"}}}); + assert_eq!(single("Scan", &input, None), names(&["pk"])); } #[test] fn scan_holds_its_filter_projection_and_start_key_names() { // Measured: Scan with AttributesToGet=[ssn] matches a Deny on ssn. let input = json!({"TableName": "t", "AttributesToGet": ["ssn"]}); - assert_eq!(single("Scan", &input, Some(&hash_table())), names(&["ssn"])); + assert_eq!( + single("Scan", &input, Some(&hash_table())), + names(&["pk", "ssn"]) + ); let input = json!({ "TableName": "t", "FilterExpression": "begins_with(#c, :p)", From 0f14e8a574318952ed2f955fc848d1eb2136e142 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:21:19 +0000 Subject: [PATCH 10/21] test: cover dynamodb:Attributes rows DynamoDB answered on 2026-11-01 Wire tests for rows DynamoDB answered on 2026-11-01 that had none: a Deny over pk, Allows over ForAllValues lists, and Null in a Deny and an Allow, on Scan; the table's key attribute on Query, UpdateItem, DeleteItem and a BatchWriteItem DeleteRequest; QueryFilter, AttributesToGet on Query and BatchGetItem, and the Update and Delete items of TransactWriteItems under a Deny on ssn; and ReturnValues ALL_OLD and ReturnValuesOnConditionCheckFailure ALL_OLD, which return the stored item with the denied attribute in it. An Allow of BatchWriteItem alone with ForAnyValue:StringEquals over ["name"] admits a batch that puts {pk, name} and {pk, ssn} on one table and writes both items, admits {pk, name} alone, and refuses {pk, ssn} alone. The service evaluates each table's part of the batch once, with the names of all its items, and ExtendDB does the same. Two rows were not measured, and their docstrings say so: a PutItem with ReturnValues ALL_OLD follows the UpdateItem row, and a Query on a global secondary index follows the IAM guide's rule that a policy on the key lists the key attributes of the table and of every index it covers. --- tests/test_dynamodb_attributes_expressions.py | 167 +++++++- ...test_dynamodb_attributes_key_attributes.py | 363 ++++++++++++++++++ 2 files changed, 525 insertions(+), 5 deletions(-) create mode 100644 tests/test_dynamodb_attributes_key_attributes.py diff --git a/tests/test_dynamodb_attributes_expressions.py b/tests/test_dynamodb_attributes_expressions.py index 2018ca77a..47c2a0927 100644 --- a/tests/test_dynamodb_attributes_expressions.py +++ b/tests/test_dynamodb_attributes_expressions.py @@ -2,12 +2,14 @@ # SPDX-License-Identifier: Apache-2.0 """dynamodb:Attributes for conditions, filters, legacy parameters, update -actions, key attributes, and bare and Null conditions. +actions, transaction items, key attributes, returned items, and bare and Null +conditions. -Every request shape here was sent to live DynamoDB on 2026-11-01 under a -session policy of the same shape as the user policy the test attaches, twice, -with the same answer both times. The policies and the fixtures are those of -test_dynamodb_attributes_condition.py; its docstring describes them. +Unless its docstring says otherwise, every request shape here was sent to live +DynamoDB on 2026-11-01 under a session policy of the same shape as the user +policy the test attaches, twice, with the same answer both times. The policies +and the fixtures are those of test_dynamodb_attributes_condition.py; its +docstring describes them. Prerequisites and run command: as for test_dynamodb_attributes_condition.py. @@ -230,3 +232,158 @@ def test_for_all_values_allow_with_the_key_attribute_admits_a_get_with_no_projec ): resp = env.allow_get_pk_name.get_item(TableName=env.table, Key=KEY_A) assert resp["Item"] == ITEM_A + + +# -- legacy reads and transaction items -------------------------------------- + +KEY_CONDITIONS_A = {"pk": {"ComparisonOperator": "EQ", "AttributeValueList": [KEY_A["pk"]]}} + + +def test_query_legacy_filter_naming_a_denied_attribute_is_denied(env: Env): + """A Query whose QueryFilter reads ssn is refused under deny(ssn).""" + _assert_denied( + lambda: env.deny_ssn.query( + TableName=env.table, KeyConditions=KEY_CONDITIONS_A, + QueryFilter={"ssn": {"ComparisonOperator": "NOT_NULL"}}, + ), + "Query", + ) + + +def test_query_attributes_to_get_naming_a_denied_attribute_is_denied(env: Env): + """A Query with AttributesToGet ["ssn"] is refused under deny(ssn).""" + _assert_denied( + lambda: env.deny_ssn.query( + TableName=env.table, KeyConditions=KEY_CONDITIONS_A, AttributesToGet=["ssn"] + ), + "Query", + ) + + +def test_query_attributes_to_get_of_another_attribute_returns_only_it(env: Env): + """A Query with AttributesToGet ["name"] returns name only.""" + resp = env.deny_ssn.query( + TableName=env.table, KeyConditions=KEY_CONDITIONS_A, AttributesToGet=["name"] + ) + assert resp["Items"] == [{"name": {"S": "alice"}}] + + +def test_batch_get_item_attributes_to_get_naming_a_denied_attribute_is_denied(env: Env): + """A BatchGetItem with AttributesToGet ["ssn"] is refused under + deny(ssn).""" + _assert_denied( + lambda: env.deny_ssn.batch_get_item( + RequestItems={env.table: {"Keys": [KEY_A], "AttributesToGet": ["ssn"]}} + ), + "BatchGetItem", + ) + + +def test_a_transaction_update_setting_a_denied_attribute_is_denied(env: Env): + """A TransactWriteItems Update that sets ssn is refused under + deny(ssn), and the refusal names dynamodb:UpdateItem.""" + key = _new_key() + env.admin.put_item(TableName=env.table, Item=key) + _assert_denied( + lambda: env.deny_ssn.transact_write_items(TransactItems=[{"Update": { + "TableName": env.table, "Key": key, "UpdateExpression": "SET ssn = :v", + "ExpressionAttributeValues": {":v": {"S": "1"}}, + }}]), + "UpdateItem", + ) + assert _stored(env, key) == key, "a refused write changes nothing" + + +def test_a_transaction_delete_whose_condition_reads_a_denied_attribute_is_denied(env: Env): + """A TransactWriteItems Delete whose condition reads ssn is refused + under deny(ssn), and the refusal names dynamodb:DeleteItem.""" + key = _new_key() + seeded = {**key, "ssn": {"S": "1"}} + env.admin.put_item(TableName=env.table, Item=seeded) + _assert_denied( + lambda: env.deny_ssn.transact_write_items(TransactItems=[{"Delete": { + "TableName": env.table, "Key": key, + "ConditionExpression": "attribute_not_exists(ssn)", + }}]), + "DeleteItem", + ) + assert _stored(env, key) == seeded, "a refused write changes nothing" + + +def test_a_transaction_delete_with_no_condition_is_allowed(env: Env): + """A TransactWriteItems Delete with no condition of an item holding + ssn is allowed under deny(ssn).""" + key = _new_key() + env.admin.put_item(TableName=env.table, Item={**key, "ssn": {"S": "1"}}) + env.deny_ssn.transact_write_items( + TransactItems=[{"Delete": {"TableName": env.table, "Key": key}}] + ) + assert _stored(env, key) is None + + +# -- returned items the key does not stop ------------------------------------ + + +def _seed_with_ssn(env: Env) -> tuple[dict, dict]: + key = _new_key() + seeded = {**key, "ssn": {"S": "1"}, "name": {"S": "old"}} + env.admin.put_item(TableName=env.table, Item=seeded) + return key, seeded + + +def test_return_values_all_old_on_an_update_returns_the_denied_attribute(env: Env): + """Under deny(ssn), an UpdateItem that sets name with ReturnValues + ALL_OLD on an item holding ssn is allowed and returns the old item, ssn + included.""" + key, seeded = _seed_with_ssn(env) + resp = env.deny_ssn.update_item( + TableName=env.table, Key=key, UpdateExpression="SET #n = :v", + ExpressionAttributeNames={"#n": "name"}, + ExpressionAttributeValues={":v": {"S": "new"}}, + ReturnValues="ALL_OLD", + ) + assert resp["Attributes"] == seeded + + +def test_return_values_all_old_on_a_put_returns_the_denied_attribute(env: Env): + """Follows the UpdateItem test above, not measured on PutItem: under + deny(ssn), a PutItem of pk and name with ReturnValues ALL_OLD on an item + holding ssn is allowed and returns the old item, ssn included.""" + key, seeded = _seed_with_ssn(env) + resp = env.deny_ssn.put_item( + TableName=env.table, Item={**key, "name": {"S": "new"}}, ReturnValues="ALL_OLD" + ) + assert resp["Attributes"] == seeded + + +def test_a_failed_put_condition_returns_the_whole_stored_item(env: Env): + """Under deny(ssn), a PutItem of pk whose condition fails, with + ReturnValuesOnConditionCheckFailure ALL_OLD, answers + ConditionalCheckFailedException carrying the stored item, ssn included.""" + key, seeded = _seed_with_ssn(env) + with pytest.raises(ClientError) as exc: + env.deny_ssn.put_item( + TableName=env.table, Item=key, ConditionExpression="attribute_not_exists(pk)", + ReturnValuesOnConditionCheckFailure="ALL_OLD", + ) + assert exc.value.response["Error"]["Code"] == "ConditionalCheckFailedException" + assert exc.value.response.get("Item") == seeded, exc.value.response + assert _stored(env, key) == seeded + + +def test_a_failed_condition_check_returns_the_whole_stored_item(env: Env): + """Under deny(ssn), a TransactWriteItems ConditionCheck that fails, + with ReturnValuesOnConditionCheckFailure ALL_OLD, answers + TransactionCanceledException whose cancellation reason carries the stored + item, ssn included.""" + key, seeded = _seed_with_ssn(env) + with pytest.raises(ClientError) as exc: + env.deny_ssn.transact_write_items(TransactItems=[{"ConditionCheck": { + "TableName": env.table, "Key": key, + "ConditionExpression": "attribute_not_exists(pk)", + "ReturnValuesOnConditionCheckFailure": "ALL_OLD", + }}]) + assert exc.value.response["Error"]["Code"] == "TransactionCanceledException" + reasons = exc.value.response.get("CancellationReasons") + assert reasons and reasons[0]["Code"] == "ConditionalCheckFailed", exc.value.response + assert reasons[0].get("Item") == seeded, reasons diff --git a/tests/test_dynamodb_attributes_key_attributes.py b/tests/test_dynamodb_attributes_key_attributes.py new file mode 100644 index 000000000..292479340 --- /dev/null +++ b/tests/test_dynamodb_attributes_key_attributes.py @@ -0,0 +1,363 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""dynamodb:Attributes and the key attributes of the table and of a queried +index: Allows over ForAllValues lists on Query, UpdateItem, DeleteItem, +BatchWriteItem and Scan, a Deny over the key attribute on Scan, Null in a +Deny and an Allow on Scan, and the evaluation of a BatchWriteItem once per +table. + +Unless its docstring says otherwise, every request shape here was sent to live +DynamoDB on 2026-11-01 under a session policy of the same shape as the user +policy the test attaches, twice, with the same answer both times. The fixtures +are those of test_dynamodb_attributes_condition.py; its docstring describes the +policies. + +Prerequisites and run command: as for test_dynamodb_attributes_condition.py. + +REQ-TEST-001, REQ-AUTH-002 +""" + +from __future__ import annotations + +import json +import uuid +from typing import Any, Callable + +import pytest +from botocore.exceptions import ClientError + +from conftest import wait_for_active + +# The fixtures are imported so this module gets its own account and table. +from test_dynamodb_attributes_condition import ( # noqa: F401 + KEY_A, + Env, + _allow_only, + _assert_denied, + _create_user_with_key, + _deny, + _for_all, + _make_client, + _new_key, + _refusal, + _stored, + account_id, + auth_env, + env, + mgmt, + region, +) + +NULL_KEY = {"Null": {"dynamodb:Attributes": "true"}} + + +def _arn(region: str, account_id: str, table: str) -> str: + return f"arn:aws:dynamodb:{region}:{account_id}:table/{table}" + + +def _deny_when_absent(arn: str, action: str) -> dict: + """Everything on the table, except an `action` request whose key is absent.""" + return { + "Version": "2012-10-17", + "Statement": [ + {"Effect": "Allow", "Action": "dynamodb:*", "Resource": arn}, + { + "Effect": "Deny", + "Action": f"dynamodb:{action}", + "Resource": arn, + "Condition": NULL_KEY, + }, + ], + } + + +@pytest.fixture(scope="module") +def table_arn(env: Env, account_id: str, region: str) -> str: + return _arn(region, account_id, env.table) + + +@pytest.fixture(scope="module") +def client_for(auth_env, mgmt, account_id, region) -> Callable[[dict], Any]: + """A client for a new user whose only policy is the one given, made once + per policy.""" + endpoint = auth_env[0] + made: dict[str, Any] = {} + + def make(policy: dict) -> Any: + key = json.dumps(policy, sort_keys=True) + if key not in made: + user = f"u-{uuid.uuid4().hex[:8]}" + ak, sk = _create_user_with_key(mgmt, account_id, user) + resp = mgmt.put_user_policy(account_id, user, "p", policy) + assert resp.status_code == 204, resp.text + made[key] = _make_client(endpoint, ak, sk, region) + return made[key] + + return make + + +def _assert_not_allowed(call: Callable[[], Any], action: str) -> None: + msg = _refusal(call, action) + assert f"because no identity-based policy allows the dynamodb:{action} action" in msg, msg + + +# -- the table's key attributes on other operations -------------------------- + + +def _naming_name_and_the_key(table: str) -> dict[str, Callable[[Any], Any]]: + """Per action, a request that names name, the table's key attribute, or + both.""" + return { + "Query": lambda c: c.query( + TableName=table, KeyConditionExpression="pk = :p", ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}, + ExpressionAttributeValues={":p": KEY_A["pk"]}), + "UpdateItem": lambda c: c.update_item( + TableName=table, Key=_new_key(), UpdateExpression="SET #n = :v", + ExpressionAttributeNames={"#n": "name"}, + ExpressionAttributeValues={":v": {"S": "n"}}), + "DeleteItem": lambda c: c.delete_item(TableName=table, Key=_new_key()), + "BatchWriteItem": lambda c: c.batch_write_item( + RequestItems={table: [{"DeleteRequest": {"Key": _new_key()}}]}), + } + + +KEY_ATTRIBUTE_ACTIONS = ["Query", "UpdateItem", "DeleteItem", "BatchWriteItem"] + + +@pytest.mark.parametrize("action", KEY_ATTRIBUTE_ACTIONS) +def test_for_all_values_allow_without_the_key_attribute_refuses( + env: Env, client_for, table_arn: str, action: str +): + """An Allow of the action alone with ForAllValues:StringEquals + over ["name"] refuses a Query projecting name, an UpdateItem setting name, + a DeleteItem and a BatchWriteItem DeleteRequest, because the key holds pk.""" + client = client_for(_allow_only(table_arn, action, _for_all(["name"]))) + _assert_not_allowed(lambda: _naming_name_and_the_key(env.table)[action](client), action) + + +@pytest.mark.parametrize("action", KEY_ATTRIBUTE_ACTIONS) +def test_for_all_values_allow_with_the_key_attribute_admits( + env: Env, client_for, table_arn: str, action: str +): + """The same Allow over ["pk", "name"] admits each request.""" + client = client_for(_allow_only(table_arn, action, _for_all(["pk", "name"]))) + _naming_name_and_the_key(env.table)[action](client) + + +# -- Scan holds the table's key attributes ----------------------------------- + + +def _scans(table: str) -> dict[str, Callable[[Any], Any]]: + return { + "plain": lambda c: c.scan(TableName=table, Limit=2), + "projected": lambda c: c.scan( + TableName=table, Limit=2, ProjectionExpression="#n", + ExpressionAttributeNames={"#n": "name"}), + "count": lambda c: c.scan(TableName=table, Select="COUNT"), + } + + +def _assert_whole_items(env: Env, resp: dict) -> None: + assert resp["Count"] == 2 + for item in resp["Items"]: + assert item == _stored(env, {"pk": item["pk"]}) + + +def test_a_deny_on_the_key_attribute_refuses_a_scan_with_no_projection( + env: Env, client_for, table_arn: str +): + """A Deny over pk refuses a Scan that names no attribute.""" + client = client_for(_deny(table_arn, "pk")) + _assert_denied(lambda: client.scan(TableName=env.table, Limit=1), "Scan") + + +def test_a_deny_on_the_key_attribute_refuses_a_scan_from_an_exclusive_start_key( + env: Env, client_for, table_arn: str +): + """A Deny over pk refuses a Scan whose only mention of pk is + ExclusiveStartKey. The Scan holds pk without the start key too, so whether + ExclusiveStartKey adds names of its own cannot be told on the service.""" + client = client_for(_deny(table_arn, "pk")) + _assert_denied(lambda: client.scan(TableName=env.table, ExclusiveStartKey=KEY_A), "Scan") + + +@pytest.mark.parametrize("shape", ["plain", "projected", "count"]) +def test_for_all_values_allow_without_the_key_attribute_refuses_a_scan( + env: Env, client_for, table_arn: str, shape: str +): + """An Allow of Scan alone with ForAllValues:StringEquals over + ["name"] refuses a Scan with no projection, a Scan projecting name and a + Select COUNT Scan.""" + client = client_for(_allow_only(table_arn, "Scan", _for_all(["name"]))) + _assert_not_allowed(lambda: _scans(env.table)[shape](client), "Scan") + + +def test_for_all_values_allow_with_the_key_attribute_admits_a_projected_scan( + env: Env, client_for, table_arn: str +): + """The same Allow over ["pk", "name"] admits a Scan projecting + name.""" + client = client_for(_allow_only(table_arn, "Scan", _for_all(["pk", "name"]))) + assert "Items" in _scans(env.table)["projected"](client) + + +def test_for_all_values_allow_of_the_key_attribute_admits_a_scan_of_whole_items( + env: Env, client_for, table_arn: str +): + """An Allow over ["pk"] admits a Scan with no projection, which + returns whole items.""" + client = client_for(_allow_only(table_arn, "Scan", _for_all(["pk"]))) + _assert_whole_items(env, _scans(env.table)["plain"](client)) + + +def test_a_null_deny_does_not_refuse_a_scan_with_no_projection( + env: Env, client_for, table_arn: str +): + """A Deny of Scan gated on Null true does not refuse a Scan that + names no attribute, because the key is present; the Scan returns whole + items.""" + client = client_for(_deny_when_absent(table_arn, "Scan")) + _assert_whole_items(env, _scans(env.table)["plain"](client)) + + +def test_a_null_deny_does_not_refuse_a_projected_scan(env: Env, client_for, table_arn: str): + """The same Deny does not refuse a Scan projecting name.""" + client = client_for(_deny_when_absent(table_arn, "Scan")) + assert "Items" in _scans(env.table)["projected"](client) + + +@pytest.mark.parametrize("shape", ["plain", "count"]) +def test_a_null_allow_grants_no_scan(env: Env, client_for, table_arn: str, shape: str): + """An Allow of Scan alone gated on Null true grants neither a Scan + with no projection nor a Select COUNT Scan.""" + client = client_for(_allow_only(table_arn, "Scan", NULL_KEY)) + _assert_not_allowed(lambda: _scans(env.table)[shape](client), "Scan") + + +# -- the queried index's key attributes -------------------------------------- + + +@pytest.fixture(scope="module") +def indexed(env: Env, account_id: str, region: str): + """A table keyed on pk with a global secondary index by-gk keyed on gk, + and the table's ARN.""" + table = f"attrs-ix-{uuid.uuid4().hex[:8]}" + env.admin.create_table( + TableName=table, + AttributeDefinitions=[ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "gk", "AttributeType": "S"}, + ], + KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"}], + GlobalSecondaryIndexes=[{ + "IndexName": "by-gk", + "KeySchema": [{"AttributeName": "gk", "KeyType": "HASH"}], + "Projection": {"ProjectionType": "ALL"}, + }], + BillingMode="PAY_PER_REQUEST", + ) + wait_for_active(env.admin, table) + env.admin.put_item(TableName=table, Item={"pk": {"S": "a"}, "gk": {"S": "g"}}) + yield table, _arn(region, account_id, table) + try: + env.admin.delete_table(TableName=table) + except ClientError: + pass + + +def _allow_query_on_indexes(arn: str, attributes: list[str]) -> dict: + return { + "Version": "2012-10-17", + "Statement": [{ + "Effect": "Allow", + "Action": "dynamodb:Query", + "Resource": [arn, f"{arn}/index/*"], + "Condition": _for_all(attributes), + }], + } + + +def _query_by_gk(client: Any, table: str) -> Any: + return client.query( + TableName=table, IndexName="by-gk", KeyConditionExpression="gk = :g", + ExpressionAttributeValues={":g": {"S": "g"}}, + ) + + +def test_for_all_values_allow_without_the_index_key_refuses_a_query_on_the_index( + indexed, client_for +): + """Follows the IAM guide's rule, not measured: a policy on + dynamodb:Attributes lists the key attributes of the table and of every + index it covers. An Allow of Query with ForAllValues:StringEquals over + ["pk"] refuses a Query on the index keyed on gk.""" + table, arn = indexed + client = client_for(_allow_query_on_indexes(arn, ["pk"])) + _assert_not_allowed(lambda: _query_by_gk(client, table), "Query") + + +def test_for_all_values_allow_with_the_index_key_admits_a_query_on_the_index( + indexed, client_for +): + """Follows the IAM guide's rule, not measured: the same Allow over + ["pk", "gk"] admits the Query on the index.""" + table, arn = indexed + client = client_for(_allow_query_on_indexes(arn, ["pk", "gk"])) + assert "Items" in _query_by_gk(client, table) + + +# -- a BatchWriteItem is evaluated once per table ---------------------------- + + +def _allow_batch_write_naming_name(table_arn: str) -> dict: + return _allow_only( + table_arn, + "BatchWriteItem", + {"ForAnyValue:StringEquals": {"dynamodb:Attributes": ["name"]}}, + ) + + +def test_a_batch_write_is_evaluated_once_per_table_with_the_names_of_all_its_items( + env: Env, client_for, table_arn: str +): + """An Allow of BatchWriteItem alone with ForAnyValue:StringEquals + over ["name"] admits a batch that puts {pk, name} and {pk, ssn} on one + table, and both items are written.""" + client = client_for(_allow_batch_write_naming_name(table_arn)) + named, other = _new_key(), _new_key() + resp = client.batch_write_item(RequestItems={env.table: [ + {"PutRequest": {"Item": {**named, "name": {"S": "n"}}}}, + {"PutRequest": {"Item": {**other, "ssn": {"S": "1"}}}}, + ]}) + assert resp["UnprocessedItems"] == {} + assert _stored(env, named) == {**named, "name": {"S": "n"}} + assert _stored(env, other) == {**other, "ssn": {"S": "1"}} + + +def test_a_batch_write_of_an_item_naming_a_listed_attribute_is_allowed( + env: Env, client_for, table_arn: str +): + """The same Allow admits a batch that puts {pk, name} alone.""" + client = client_for(_allow_batch_write_naming_name(table_arn)) + key = _new_key() + client.batch_write_item( + RequestItems={env.table: [{"PutRequest": {"Item": {**key, "name": {"S": "n"}}}}]} + ) + assert _stored(env, key) == {**key, "name": {"S": "n"}} + + +def test_a_batch_write_of_an_item_naming_no_listed_attribute_is_refused( + env: Env, client_for, table_arn: str +): + """The same Allow refuses a batch that puts {pk, ssn} alone.""" + client = client_for(_allow_batch_write_naming_name(table_arn)) + key = _new_key() + _assert_not_allowed( + lambda: client.batch_write_item( + RequestItems={env.table: [{"PutRequest": {"Item": {**key, "ssn": {"S": "1"}}}}]} + ), + "BatchWriteItem", + ) + assert _stored(env, key) is None, "a refused write stores nothing" From 2a821f94e8f727e9277e540b903e8a74ec734a4f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:21:19 +0000 Subject: [PATCH 11/21] docs: describe dynamodb:Attributes on Scan and the upgrade's policy shapes The security model, the upgrade entry, the troubleshooting guide and the policy evaluator's comments now say that a Scan holds the table's key attributes. On a table that exists the key is absent only on a SearchVectors with no ProjectionExpression. On a table that does not exist it is also absent on a GetItem or transaction Get that names no attribute outside its Key, a Query that names none outside its key condition and a Scan that names none, because the extractor reads no Key on a GetItem or a Get and no key condition on a Query. The rows DynamoDB answered on 2026-11-01 are labelled measured; the index key attributes and the names of ExclusiveStartKey are labelled unmeasured. The upgrade entry opens its list with the rule the shapes follow: every item request other than SearchVectors now carries the key, holding at least the table's key attributes, so a ForAllValues condition no longer holds vacuously, a ForAnyValue condition can match, Null true stops matching and Null false starts matching. Its bullets cover a Deny gated on a ForAnyValue string operator; a Deny gated on a negated one, which now needs the key attributes in its list; an Allow gated on ForAllValues; an Allow gated on ForAnyValue over any listed attribute, with the per-table BatchWriteItem union as its example; the negated Allow shapes; and Null with either value. The security model says that a Deny on the key does not stop ReturnValues ALL_OLD or ReturnValuesOnConditionCheckFailure ALL_OLD from returning the whole stored item, as on DynamoDB, and that the IAM guide's guard against ReturnValues returning attributes is a condition on dynamodb:ReturnValues. The extractor's module docs say that ReturnValues and ReturnValuesOnConditionCheckFailure were measured to add no names and that Select was not measured. The unit test module docs say which unmeasured rows follow the IAM guide's rule and which follow the rule measured for the same parameter on another operation. --- crates/auth/src/policy/condition.rs | 20 ++++++++++++------- .../src/policy/condition/tests/multivalued.rs | 18 +++++++++++------ crates/auth/src/policy/context.rs | 15 ++++++++------ crates/server/src/request_attributes.rs | 6 +++--- crates/server/src/request_attributes/tests.rs | 7 +++++-- docs/manuals/07-upgrade-manual.md | 16 +++++++++------ docs/manuals/10-security-model.md | 12 ++++++----- docs/troubleshooting.md | 4 ++-- 8 files changed, 61 insertions(+), 37 deletions(-) diff --git a/crates/auth/src/policy/condition.rs b/crates/auth/src/policy/condition.rs index 0f7886798..c00ae39a6 100644 --- a/crates/auth/src/policy/condition.rs +++ b/crates/auth/src/policy/condition.rs @@ -131,13 +131,19 @@ pub fn evaluate_condition(condition: &Condition, context: &impl ConditionContext // it is right only where ExtendDB leaves the key absent on the same // requests the service does. ExtendDB leaves `dynamodb:LeadingKeys` // absent on query shapes it cannot pin to a partition value, where - // the service populates it, and `dynamodb:Attributes` absent only - // on a request that names no attribute, where the service's answer - // for a bare negated operator was not measured; matching there - // could grant an Allow the service refuses, so those keys keep the - // no-match rule. `aws:TagKeys` is populated on exactly the three - // operations the service reference lists it for, so its absent-key - // answer is IAM's. + // the service populates it. It leaves `dynamodb:Attributes` absent + // only on a `SearchVectors` with no `ProjectionExpression` (#729) + // and, on a table that does not exist, on a `GetItem` or + // transaction `Get` that names no attribute outside its `Key`, a + // `Query` that names none outside its key condition and a `Scan` + // that names none. On the service the key holds the table's key + // attributes on every item request measured, a `Scan` that names + // no attribute included, and an Allow gated on a bare + // `StringNotEquals` over it grants no `Scan` (measured 2026-11-01). + // Matching on those absent keys could grant an Allow the service + // refuses, so they keep the no-match rule. `aws:TagKeys` is + // populated on exactly the three operations the service reference + // lists it for, so its absent-key answer is IAM's. None => { is_negative_operator(other) && (!is_multivalued diff --git a/crates/auth/src/policy/condition/tests/multivalued.rs b/crates/auth/src/policy/condition/tests/multivalued.rs index aad148ae8..2eedae166 100644 --- a/crates/auth/src/policy/condition/tests/multivalued.rs +++ b/crates/auth/src/policy/condition/tests/multivalued.rs @@ -459,12 +459,18 @@ fn bare_negated_op_absent_tagkeys_matches() { #[test] fn bare_negated_op_absent_dynamodb_keys_do_not_match() { // IAM matches a bare negated operator on absent dynamodb:Attributes and - // dynamodb:LeadingKeys too, but ExtendDB leaves both absent on requests - // where the service populates them (Attributes on writes, AttributesToGet - // reads and batch and transaction targets; LeadingKeys on query shapes the - // extractor cannot pin). Matching there would grant an Allow the service - // refuses for the present key, so for these two keys an absent key keeps - // the no-match rule (BR-7085). + // dynamodb:LeadingKeys too. ExtendDB leaves dynamodb:LeadingKeys absent on + // query shapes it cannot pin to a partition value, where the service + // populates it. It leaves dynamodb:Attributes absent only on a + // SearchVectors with no ProjectionExpression (#729) and, on a table that + // does not exist, on a GetItem or transaction Get that names no attribute + // outside its Key, a Query that names none outside its key condition and a + // Scan that names none. On the service the key holds the table's key + // attributes on every item request measured, a Scan that names no + // attribute included, and an Allow gated on a bare StringNotEquals over it + // grants no Scan (measured 2026-11-01). Matching on those absent keys could + // grant an Allow the service refuses, so for these two keys an absent key + // keeps the no-match rule (BR-7085). let ctx = TestContext::new(); for key in ["dynamodb:Attributes", "dynamodb:LeadingKeys"] { for op in [ diff --git a/crates/auth/src/policy/context.rs b/crates/auth/src/policy/context.rs index ed3372726..16220ec67 100755 --- a/crates/auth/src/policy/context.rs +++ b/crates/auth/src/policy/context.rs @@ -460,12 +460,15 @@ impl ConditionContext for RequestContext { // its absence here is the service's absence. `dynamodb:LeadingKeys` is // left absent on query shapes the extractor cannot pin to a partition // value, where the service populates it. `dynamodb:Attributes` is - // absent only on a request that names no attribute (a `Scan` that - // projects and filters nothing, or a request on a table that does not - // exist), where the service's answer for a bare negated operator was - // not measured. For those two an absent key keeps the no-match rule, - // so an Allow gated on a bare negated operator over them grants - // nothing. + // absent only on a `SearchVectors` with no `ProjectionExpression` + // (#729) and, on a table that does not exist, on a `GetItem` or + // transaction `Get` that names no attribute outside its `Key`, a + // `Query` that names none outside its key condition and a `Scan` that + // names none. On the service the key is present on a `Scan` that + // names no attribute, and an Allow gated on a bare `StringNotEquals` + // over it grants no `Scan` (measured 2026-11-01). + // For those two keys an absent key keeps the no-match rule, so an + // Allow gated on a bare negated operator over them grants nothing. super::casefold::case_fold(key) == "aws:tagkeys" } } diff --git a/crates/server/src/request_attributes.rs b/crates/server/src/request_attributes.rs index a7d9bfaef..8c7a854f7 100644 --- a/crates/server/src/request_attributes.rs +++ b/crates/server/src/request_attributes.rs @@ -9,9 +9,9 @@ //! `Name, Address.City` gives `Name` and `Address`. The names come from the //! request alone. A `DeleteItem` and a read with no projection hold only the //! names the request gives, not the names of the stored item they delete or -//! return (measured 2026-10-31). `ReturnValues`, -//! `ReturnValuesOnConditionCheckFailure` and `Select` add no names -//! (`ReturnValuesOnConditionCheckFailure` measured 2026-11-01). +//! return (measured 2026-10-31). `ReturnValues` and +//! `ReturnValuesOnConditionCheckFailure` add no names (measured 2026-11-01), +//! and `Select` adds none (not measured). //! //! What the key holds for each operation: //! diff --git a/crates/server/src/request_attributes/tests.rs b/crates/server/src/request_attributes/tests.rs index 722b2670d..b44d97f25 100644 --- a/crates/server/src/request_attributes/tests.rs +++ b/crates/server/src/request_attributes/tests.rs @@ -3,8 +3,11 @@ //! Unit tests for `dynamodb:Attributes` extraction. //! -//! The rows named "measured" mirror the service's answers of 2026-10-31 and -//! 2026-11-01; the rest follow the IAM documentation for the key. +//! Rows marked "Measured" mirror the service's answers of 2026-10-31 and +//! 2026-11-01; the parent module's docs list what was measured. Index key +//! attributes and a table's sort key follow the IAM guide's rule, not measured +//! (the measured table was keyed on `pk` alone). Other unmeasured parameters +//! follow the rule measured for the same parameter on another operation. use std::collections::HashMap; diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index c0d9c3d69..c54e40be2 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -643,7 +643,7 @@ To get the intended behaviour, write the condition on `aws:ResourceTag/`. - A `Deny` with a wildcarded `ArnNotEquals` now exempts the callers the wildcard matches; before, it applied to every caller. - A bare `*` value now matches every ARN. An `Allow` gated on `ArnEquals: "*"` grants, and a `Deny` gated on it applies, whenever the key holds an ARN. A `Deny` gated on `ArnNotEquals: "*"` now applies only when the key is absent, and an `Allow` gated on it grants only then. AWS IAM Access Analyzer reports a bare `*` Arn value as an error, and the IAM policy simulator refuses to evaluate one against an ARN, but ExtendDB accepts it at write. Replace any of these shapes before upgrading if you wrote it by hand. - An Arn operator over a key whose value is not an ARN, such as `aws:ResourceTag/`, is now false, so a `Deny` written that way no longer applies and an `Allow` gated on one no longer grants. -- `dynamodb:LeadingKeys`, `dynamodb:Attributes` and `aws:TagKeys` hold values the caller or the admin chooses: the partition key values in a request (every key in a batch), the attribute names in a projection, and the session tag keys in an AssumeRole call. Each value can be ARN-shaped or not. A `ForAnyValue:` or `ForAllValues:` Arn condition over one of these keys is now false while any value in the set is not an ARN and the policy lists at least one value. A `Deny` gated on `ForAnyValue:ArnNotLike` with `arn:aws:dynamodb:us-east-1:123456789012:table/Z*` on `dynamodb:LeadingKeys` no longer applies to a request whose partition key values include one that is not an ARN, and `ForAnyValue:ArnLike` with `j*` on the same key no longer holds for the partition key value `junk`. +- `dynamodb:LeadingKeys`, `dynamodb:Attributes` and `aws:TagKeys` hold values the caller or the admin chooses: the partition key values in a request (every key in a batch), the attribute names in a request, and the session tag keys in an AssumeRole call. Each value can be ARN-shaped or not. A `ForAnyValue:` or `ForAllValues:` Arn condition over one of these keys is now false while any value in the set is not an ARN and the policy lists at least one value. A `Deny` gated on `ForAnyValue:ArnNotLike` with `arn:aws:dynamodb:us-east-1:123456789012:table/Z*` on `dynamodb:LeadingKeys` no longer applies to a request whose partition key values include one that is not an ARN, and `ForAnyValue:ArnLike` with `j*` on the same key no longer holds for the partition key value `junk`. A policy whose Arn values are literal ARNs, applied to a context value that is an ARN, evaluates as before. @@ -667,13 +667,17 @@ No catalog migration is required. Before upgrading a deployment that restricts t ### v0.1.15: `dynamodb:Attributes` holds the attributes every item request names -**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition or filter expression reads, the target of each update expression action, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey`, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). It does not hold the attributes a `SET` value reads. +**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition or filter expression reads, the target of each update expression action, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey` (whether it adds names of its own cannot be told on the service), and on every item operation other than `SearchVectors` the key attributes of the table and of the index a `Query` or `Scan` names (the index key attributes follow the IAM guide's rule, not measured). It does not hold the attributes a `SET` value reads. -**Who is affected.** Any deployment with a policy statement that names `dynamodb:Attributes` in a condition: +**Who is affected.** Any deployment with a policy statement that names `dynamodb:Attributes` in a condition. On a table that exists, every item request other than `SearchVectors` now carries the key, holding at least the table's key attributes, so a `ForAllValues` condition no longer holds vacuously on an absent key, a `ForAnyValue` condition can match, `Null` with value `true` stops matching and `Null` with value `false` starts matching. A `SearchVectors` still holds only the names in its `ProjectionExpression` and leaves the key absent when it has none. Statements of these shapes now decide differently: -- A `Deny` gated on `ForAnyValue:StringEquals` over an attribute now applies to writes that name it, to `AttributesToGet` reads and projections of it or of a path inside it, to conditions and filters that read it, and to the batch and transaction forms of those requests. It did not apply to them before. -- An `Allow` gated on `ForAllValues:StringEquals` used to grant those requests whatever attributes they named, because the absent key satisfied it. It now grants only when every name the request holds is in the list. A list that omits the table's key attributes stops granting `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and the batch and transaction requests; add the key attributes of the table and of every index the statement covers. -- `Null` with value `true` on `dynamodb:Attributes` matched those requests before, and matched a `GetItem`, `Query`, `BatchGetItem` or `TransactGetItems` read with no projection. It no longer matches any of them, because each holds at least the table's key attributes; it still matches a `Scan` that names no attribute. A statement that used it to refuse reads with no projection no longer refuses them. +- A `Deny` gated on a `ForAnyValue:String*` operator that matches an attribute's name (`StringEquals`, `StringLike`, `StringEqualsIgnoreCase`) now applies to writes that name the attribute, to update actions that target it, to `AttributesToGet` reads, to projections of a path inside it, to conditions and filters that read it, and to batch and transaction requests. It did not apply to them before. +- A `Deny` gated on a negated `ForAnyValue:String*` operator (`StringNotEquals`, `StringNotLike`, `StringNotEqualsIgnoreCase`) now applies to every item request whose names fall outside its list, the key attributes included, so the list must hold the key attributes of the table and of every index the statement covers (the index key attributes follow the IAM guide's rule, not measured). A list that omits them now refuses every `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction request on the table. +- An `Allow` gated on `ForAllValues:StringEquals` used to grant every request that left the key absent, whatever attributes it named, because the absent key satisfied it. It now grants only when every name the request holds is in the list. A list that omits the table's key attributes stops granting `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests (measured on DynamoDB 2026-11-01 for `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `BatchWriteItem` `DeleteRequest`); add the key attributes of the table and of every index the statement covers (the index key attributes follow the IAM guide's rule, not measured). +- An `Allow` gated on a `ForAnyValue:String*` operator now grants every item request its `Action` covers that holds a name the list matches. Before, it granted only a single-table request whose `ProjectionExpression` named one. A `BatchWriteItem` is evaluated once per table, with the union of that table's item names, so an `Allow` of `BatchWriteItem` over `["name"]` grants a batch that puts `{pk, name}` and `{pk, ssn}` on one table and writes both items, and refuses a batch that puts `{pk, ssn}` alone, as on DynamoDB (measured 2026-11-01). A list that holds a key attribute grants every item request other than `SearchVectors` that the `Action` covers, because each holds the table's key attributes; DynamoDB puts them in the key on `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `BatchWriteItem` `DeleteRequest` (measured 2026-11-01). An `Allow` of `GetItem` over `["pk"]`, on a table keyed on `pk`, grants every `GetItem` on the table, one with no projection included, which returns the whole item. +- An `Allow` of `PutItem` gated on `ForAnyValue:StringNotEquals` over `["ssn"]` granted no `PutItem` before, and now grants one of `{pk, ssn}`, because `pk` is outside the list. An `Allow` of `PutItem` gated on `ForAllValues:StringNotEquals` over `["ssn"]` granted every `PutItem` before, because the absent key satisfied it, and now refuses one of `{pk, ssn}` and grants one of `{pk, name}`. +- `Null` with value `true` matched every request that left the key absent before. A statement gated on it no longer matches any item request except a `SearchVectors` with no `ProjectionExpression` and, on a table that does not exist, a `GetItem` or transaction `Get` that names no attribute outside its `Key`, a `Query` that names none outside its key condition and a `Scan` that names none. A `Deny` gated on it stops refusing the other item requests and an `Allow` gated on it stops granting them, as on DynamoDB (measured 2026-11-01 on `GetItem` and `Scan`). +- `Null` with value `false` matched only a single-table request with a `ProjectionExpression` before, and now matches every item request that carries the key. An `Allow` gated on it, written to require that a read names its attributes, now grants each of these requests that its `Action` covers, a read with no projection and its whole item included, and a `Deny` gated on it now refuses them. A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). Nor does it stop an `UpdateItem` from copying the attribute's value into another attribute through a `SET` value such as `SET #n = ssn`, as on DynamoDB (measured 2026-11-01). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. diff --git a/docs/manuals/10-security-model.md b/docs/manuals/10-security-model.md index d93f5ae6b..d959f04b8 100755 --- a/docs/manuals/10-security-model.md +++ b/docs/manuals/10-security-model.md @@ -142,15 +142,17 @@ extenddb supports the same access control patterns as real AWS IAM: | `UpdateItem` | The `Key` attributes, the table's key attributes, `AttributeUpdates`, `Expected`, the attributes in `ConditionExpression`, and the target of every `SET`, `REMOVE`, `ADD` and `DELETE` action in `UpdateExpression`; not the attributes a `SET` value reads | | `DeleteItem` | The `Key` attributes, the table's key attributes, `Expected`, and the attributes in `ConditionExpression` | | `Query` | The key attributes of the table and of the queried index, `AttributesToGet`, `QueryFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | -| `Scan` | `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | -| `SearchVectors` | The attributes in `ProjectionExpression` | +| `Scan` | The key attributes of the table and of the scanned index, `AttributesToGet`, `ScanFilter`, `ExclusiveStartKey`, and the attributes in `FilterExpression` and `ProjectionExpression` | +| `SearchVectors` | The attributes in `ProjectionExpression`; the key is absent when there is none | | `BatchGetItem` | Per table: the attributes of every key, the table's key attributes, `AttributesToGet`, and the attributes in `ProjectionExpression` | -| `BatchWriteItem` | Per table: the attributes of every `PutRequest` item and `DeleteRequest` key, and the table's key attributes | +| `BatchWriteItem` | Per table: the attributes of every `PutRequest` item and `DeleteRequest` key, and the table's key attributes, evaluated once with the union of the table's items | | `TransactGetItems`, `TransactWriteItems` | Per item, what the single-item operation holds: `Get` as `GetItem`, `Put` as `PutItem`, `Update` as `UpdateItem`, `Delete` as `DeleteItem`; a `ConditionCheck` holds the table's key attributes and the attributes in its `ConditionExpression` | -The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). As in DynamoDB, a policy on `dynamodb:Attributes` does not stop an `UpdateItem` from copying a denied attribute's value into another attribute through a `SET` value: under a `Deny` on `ssn`, `SET #n = ssn` with `#n` for `name` is allowed, and a projection of `name` then returns the value (measured on DynamoDB 2026-11-01). A `Scan` that names no attribute is evaluated with the key absent. A bare `StringNotEquals` on the key does not match whether the key is present or absent, so an `Allow` gated on it grants nothing (measured on DynamoDB 2026-11-01 on `Scan`). An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `Scan` and `SearchVectors` holds them. +The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). As in DynamoDB, a policy on `dynamodb:Attributes` does not stop an `UpdateItem` from copying a denied attribute's value into another attribute through a `SET` value: under a `Deny` on `ssn`, `SET #n = ssn` with `#n` for `name` is allowed, and a projection of `name` then returns the value (measured on DynamoDB 2026-11-01). A `Scan` holds the table's key attributes when it names no attribute, a `Select COUNT` `Scan` included, so a `Null` condition on the key does not match it: a `Deny` of `Scan` gated on `Null` with value `true` refuses neither a `Scan` with no projection nor a projected one, and an `Allow` of `Scan` gated on it grants neither a `Scan` with no projection nor a `Select COUNT` `Scan` (measured on DynamoDB 2026-11-01). A bare `StringNotEquals` on the key does not match whether the key is present or absent, so an `Allow` gated on it grants nothing; on DynamoDB it granted no `Scan`, whether the `Scan` named an attribute or not (measured 2026-11-01). An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `SearchVectors` holds them. The key is absent on a `SearchVectors` with no `ProjectionExpression` and, on a table that does not exist, on a `GetItem` or transaction `Get` that names no attribute outside its `Key`, a `Query` that names none outside its key condition and a `Scan` that names none. -These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item attributes of `PutItem`; the target of a `SET`, `REMOVE`, `ADD` or `DELETE` action and the attributes a `SET` value reads; `ConditionExpression` on `PutItem`, `DeleteItem` and a `ConditionCheck`; `FilterExpression` on `Query` and `Scan`; `Expected`, `AttributeUpdates` and `ScanFilter`; `AttributesToGet` on `GetItem` and `Scan`; the table's key attribute on `GetItem`; projected document paths; reads with no projection; `DeleteItem`; and the `BatchGetItem`, `TransactGetItems`, `BatchWriteItem` and `TransactWriteItems` forms of a projection and a put. The other entries (`ExclusiveStartKey`, `QueryFilter`, `AttributesToGet` on `Query` and `BatchGetItem`, the queried index's key attributes, `DeleteRequest` keys, and the `Update` and `Delete` items of a transaction) follow the same rules. +A `Deny` on `dynamodb:Attributes` does not stop `ReturnValues` `ALL_OLD` or `ReturnValuesOnConditionCheckFailure` `ALL_OLD` from returning the whole stored item, the denied attribute included. Under a `Deny` on `ssn`, an `UpdateItem` of `name` with `ReturnValues` `ALL_OLD` returns the old item with `ssn` in it, and a `PutItem` whose condition fails and a `TransactWriteItems` `ConditionCheck` that fails, each with `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, return the stored item with `ssn` in the error. DynamoDB does the same (measured 2026-11-01). ExtendDB returns the whole stored item in each of these cases, and on a `PutItem` with `ReturnValues` `ALL_OLD`, whose answer on DynamoDB was not measured. The IAM guide's guard against `ReturnValues` returning attributes is a condition on `dynamodb:ReturnValues`. ExtendDB sets that key only when the request carries `ReturnValues`; DynamoDB sets it to `NONE` on a `PutItem` that carries none (measured 2026-11-01, tracked in #717). + +These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item attributes of `PutItem`; the target of a `SET`, `REMOVE`, `ADD` or `DELETE` action and the attributes a `SET` value reads; `ConditionExpression` on `PutItem`, `DeleteItem`, a `ConditionCheck` and a transaction `Delete`; `FilterExpression` on `Query` and `Scan`; `Expected` on `PutItem`, `AttributeUpdates`, `ScanFilter` and `QueryFilter`; `AttributesToGet` on `GetItem`, `Query`, `Scan` and `BatchGetItem`; the table's key attributes on `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `DeleteRequest`; projected document paths; reads with no projection; `DeleteItem`; a transaction `Update`; the `BatchGetItem`, `TransactGetItems`, `BatchWriteItem` and `TransactWriteItems` forms of a projection and a put; and the evaluation of a `BatchWriteItem` once per table. `ProjectionExpression` on `Query` and `Scan`, `Expected` and `ConditionExpression` on `UpdateItem`, and `Expected` on `DeleteItem` follow the rule measured for the same parameter on the other operations. The key attributes of the queried or scanned index follow the IAM guide's rule that a policy on `dynamodb:Attributes` lists the primary key and index key attributes of the table and of every secondary index the policy covers; they were not measured. Whether `ExclusiveStartKey` adds names of its own cannot be told on DynamoDB, because a valid start key holds only key attributes, which the key already holds. ### Resource ARNs diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 059f442ab..a11ae8e2b 100755 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -331,11 +331,11 @@ These errors occur when `auth.provider = "builtin"` is enabled. To allowlist attributes (allow only when *every* requested attribute is in the set), use `ForAllValues:` on an `Allow` statement. The same rule applies to `dynamodb:LeadingKeys`. -`dynamodb:Attributes` holds the top-level names of the attributes the request names: the attributes of a `PutItem` item and of the items and keys in a `BatchWriteItem` request, the target of each `UpdateExpression` action, `AttributesToGet`, the first name of each projected document path (`secret` for `secret.a`), the attributes a `ConditionExpression` or `FilterExpression` reads, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, and on every item operation other than `Scan` and `SearchVectors` the key attributes of the table (and of a queried index). The attributes a `SET` value reads are not in it. Batch requests are evaluated per table and transactions per item. The [security model manual](manuals/10-security-model.md) lists what the key holds for each operation. It follows that: +`dynamodb:Attributes` holds the top-level names of the attributes the request names: the attributes of a `PutItem` item and of the items and keys in a `BatchWriteItem` request, the target of each `UpdateExpression` action, `AttributesToGet`, the first name of each projected document path (`secret` for `secret.a`), the attributes a `ConditionExpression` or `FilterExpression` reads, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, and on every item operation other than `SearchVectors` the key attributes of the table (and of the index a `Query` or `Scan` names). The attributes a `SET` value reads are not in it. Batch requests are evaluated per table and transactions per item. The [security model manual](manuals/10-security-model.md) lists what the key holds for each operation. It follows that: - A `Deny` on `ForAnyValue:StringEquals` over `ssn` does not block a `DeleteItem` of an item that holds `ssn`, or a read with no projection, which returns `ssn` with the rest of the item: neither request names `ssn`. DynamoDB answers the same way (measured 2026-10-31). - The same `Deny` does not block an `UpdateItem` that reads `ssn` only in a `SET` value. `SET #n = ssn` copies the value of `ssn` into `name`, and a projection of `name` then returns it. DynamoDB answers the same way (measured 2026-11-01). -- An `Allow` on `ForAllValues:StringEquals` that does not list the table's key attributes grants no `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, batch or transaction request, because each of them holds the key attributes (measured on DynamoDB 2026-11-01 for `GetItem`). List the key attributes of the table and of every index the statement covers. +- An `Allow` on `ForAllValues:StringEquals` that does not list the table's key attributes grants no `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch or transaction request, because each of them holds the key attributes (measured on DynamoDB 2026-11-01 for `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `BatchWriteItem` `DeleteRequest`). List the key attributes of the table and of every index the statement covers; the index key attributes follow the IAM guide's rule and were not measured. > Note: prior to v0.1.6, extenddb incorrectly treated a bare operator on these keys as an implicit AND across the requested values, so a single-attribute request appeared to be denied while a multi-attribute request leaked. That behavior diverged from AWS and gave a false sense of security. See the upgrade manual for migration guidance. From 59fc1939cc395a17d858bf6787d84957ccfc53d4 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:09:55 +0000 Subject: [PATCH 12/21] fix(server): default dynamodb:Select, ReturnValues and ReturnConsumedCapacity ExtendDB set dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity only from the request's own parameters, and only on requests with a single authorization target. When a request left the parameter out, the key was absent, and the batch and transaction targets never carried the keys. DynamoDB evaluates the value the operation uses instead (measured 2026-11-01): - dynamodb:Select is SPECIFIC_ATTRIBUTES on a GetItem, Query or Scan with a projection and ALL_ATTRIBUTES on one without, and the same on each table of a BatchGetItem and on each Get of a TransactGetItems. A Query or Scan with a Select holds it as given. - dynamodb:ReturnValues is NONE on a PutItem, UpdateItem or DeleteItem without ReturnValues, and on the Put, Update and Delete items of a TransactWriteItems. A BatchWriteItem leaves the key absent. - dynamodb:ReturnConsumedCapacity is NONE on a GetItem, a BatchWriteItem and the Get of a TransactGetItems without ReturnConsumedCapacity. With the key absent, a Deny on Select ALL_ATTRIBUTES did not refuse a read with no projection, which returns the whole item, and an Allow gated on ReturnValues NONE refused writes that DynamoDB grants. The parameter_keys module holds these values for a single-table request, for each table of a batch and for each transaction item, and the batch and transaction targets carry them, so two Gets of one partition that differ only in their projection are evaluated apart. A Query or Scan of an index with no projection and no Select holds ALL_PROJECTED_ATTRIBUTES, the default the DynamoDB API reference gives for a read of an index; that was not measured. dynamodb:ReturnConsumedCapacity defaults to NONE on the other item operations as on the measured ones. GetItem, BatchGetItem and TransactGetItems take no Select parameter, so a Select member in them no longer decides the key: the engine ignores the member and returns the whole item when nothing is projected. --- crates/server/src/leading_keys/batch.rs | 22 +- crates/server/src/lib.rs | 1 + crates/server/src/parameter_keys.rs | 160 +++++++ crates/server/src/parameter_keys/tests.rs | 491 ++++++++++++++++++++++ crates/server/src/request_helpers.rs | 37 +- 5 files changed, 686 insertions(+), 25 deletions(-) create mode 100644 crates/server/src/parameter_keys.rs create mode 100644 crates/server/src/parameter_keys/tests.rs diff --git a/crates/server/src/leading_keys/batch.rs b/crates/server/src/leading_keys/batch.rs index ca0eacec2..d0fc97403 100644 --- a/crates/server/src/leading_keys/batch.rs +++ b/crates/server/src/leading_keys/batch.rs @@ -11,11 +11,12 @@ use extenddb_core::types::TableKeyInfo; use serde_json::Value; use super::{NumberForm, hash_attr, pk_from_map}; +use crate::parameter_keys::{self, ParameterKeys}; use crate::request_attributes::{batch_table_attributes, transact_item_attributes}; /// A single `(action, table)` authorization to perform for a batch or -/// transaction operation, carrying the LeadingKeys and Attributes the service -/// would evaluate. +/// transaction operation, carrying the LeadingKeys, Attributes, Select, +/// ReturnValues and ReturnConsumedCapacity values the service would evaluate. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub(crate) struct AuthzTarget { /// The bare operation string the action maps to (no `dynamodb:` prefix). @@ -28,11 +29,14 @@ pub(crate) struct AuthzTarget { /// The `dynamodb:Attributes` names for this authorization, or `None` when /// it names no attribute. See `crate::request_attributes`. pub attributes: Option>, + /// The `dynamodb:Select`, `dynamodb:ReturnValues` and + /// `dynamodb:ReturnConsumedCapacity` values. See `crate::parameter_keys`. + pub parameters: ParameterKeys, } /// The per-(action, table) or per-item authorizations for a batch or -/// transaction operation, each carrying the LeadingKeys and Attributes the -/// service evaluates. +/// transaction operation, each carrying the condition key values the service +/// evaluates. /// /// Returns `None` for non-batch/transact operations. Batch operations /// authorize once per table with the set of all keys for that table; @@ -41,9 +45,10 @@ pub(crate) struct AuthzTarget { /// two (a batch of one own and one other key passes `ForAnyValue`; a /// transaction of the same two does not). Transaction items that give the same /// target are returned once, in the order of their first occurrence: the -/// decision for a target depends only on its action, table, LeadingKeys and -/// Attributes, so a hundred identical `ConditionCheck`s on one partition need -/// one evaluation. +/// decision for a target depends only on its action, table and condition key +/// values, so a hundred identical `ConditionCheck`s on one partition need one +/// evaluation. Two `Get`s of one partition that differ only in whether they +/// project give two targets, since their `dynamodb:Select` values differ. /// /// `key_info_by_table` is the authorization snapshot; a `None` entry is a table /// that does not exist, whose targets carry no LeadingKeys and none of the @@ -79,6 +84,7 @@ pub(crate) fn batch_transact_targets( table: table.clone(), leading_keys: keys, attributes: batch_table_attributes(operation, body, key_info_of(table), limits), + parameters: parameter_keys::batch_table(operation, body, input), }); } Some(out) @@ -108,6 +114,7 @@ pub(crate) fn batch_transact_targets( key_info_of(table), limits, ), + parameters: parameter_keys::transact_item("Get", get, input), }, ); } @@ -145,6 +152,7 @@ pub(crate) fn batch_transact_targets( key_info_of(table), limits, ), + parameters: parameter_keys::transact_item(sub, op, input), }, ); } diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index a71535ec8..bfec0b28a 100755 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -19,6 +19,7 @@ pub mod key_info_cache; mod leading_keys; pub mod management; mod metrics_endpoint; +mod parameter_keys; pub mod rate_limit; mod request_attributes; mod request_helpers; diff --git a/crates/server/src/parameter_keys.rs b/crates/server/src/parameter_keys.rs new file mode 100644 index 000000000..512604d3f --- /dev/null +++ b/crates/server/src/parameter_keys.rs @@ -0,0 +1,160 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! `dynamodb:Select`, `dynamodb:ReturnValues` and +//! `dynamodb:ReturnConsumedCapacity` for request authorization. +//! +//! Each key holds the value of the request parameter it is named after. When a +//! request that takes the parameter leaves it out, the service evaluates the +//! value the operation then uses, so the key is present whether or not the +//! caller spells that value out (measured 2026-11-01): +//! +//! - `dynamodb:Select` on `GetItem`, `Query`, `Scan`, each table of a +//! `BatchGetItem` and each `Get` of a `TransactGetItems`: +//! `SPECIFIC_ATTRIBUTES` for a read with a projection (`ProjectionExpression` +//! or `AttributesToGet`), `ALL_ATTRIBUTES` for a read of a table without one, +//! and the `Select` of a `Query` or `Scan` that has one. `GetItem`, +//! `BatchGetItem` and `TransactGetItems` take no `Select` parameter, so a +//! `Select` member in one of them is not read: the engine ignores it and +//! returns what the projection asks for. +//! - `dynamodb:ReturnValues` on `PutItem`, `UpdateItem` and `DeleteItem`: the +//! request's `ReturnValues`, or `NONE`. The `Put`, `Update` and `Delete` items +//! of a `TransactWriteItems` take no `ReturnValues` and hold `NONE`. A +//! `BatchWriteItem` and a `ConditionCheck` leave the key absent. +//! - `dynamodb:ReturnConsumedCapacity` on the item operations: the request's +//! `ReturnConsumedCapacity`, or `NONE`. A batch or transaction request carries +//! one value, and each of its tables or items is evaluated with it. +//! +//! Measured on 2026-11-01: `dynamodb:Select` on `GetItem` and `Query` with and +//! without a projection, on `Scan` without one and with `Select` `COUNT`, on +//! `Query` with `Select` `COUNT`, and on `BatchGetItem` and a `TransactGetItems` +//! `Get` with and without a projection; `dynamodb:ReturnValues` on `PutItem`, +//! `UpdateItem` and `DeleteItem` with and without the parameter, on a +//! `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a +//! `TransactWriteItems`; `dynamodb:ReturnConsumedCapacity` on `GetItem` with and +//! without the parameter, on a `BatchWriteItem` and on a `TransactGetItems` +//! `Get`. `dynamodb:ReturnConsumedCapacity` on the other item operations follows +//! those. A read that projects with `AttributesToGet` is treated as one with a +//! `ProjectionExpression`, which the engine reads it as; that was not measured. +//! A `Query` or `Scan` of an index with no projection and no `Select` holds +//! `ALL_PROJECTED_ATTRIBUTES`, the default the DynamoDB API reference gives for +//! a read of an index; that was not measured either. +//! +//! A key the operation does not take holds the request's parameter as given, +//! as before: a `Select` member in a `PutItem` reaches the policy as written. +//! The batch and transaction targets carry only the keys their operation takes. + +use serde_json::Value; + +/// `dynamodb:Select` for a read with a projection and no `Select`. +const SPECIFIC_ATTRIBUTES: &str = "SPECIFIC_ATTRIBUTES"; +/// `dynamodb:Select` for a read of a table with neither. +const ALL_ATTRIBUTES: &str = "ALL_ATTRIBUTES"; +/// `dynamodb:Select` for a `Query` or `Scan` of an index with neither (the API +/// reference's default, not measured). +const ALL_PROJECTED_ATTRIBUTES: &str = "ALL_PROJECTED_ATTRIBUTES"; +/// `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` for a request +/// that takes the parameter and leaves it out. +const NONE: &str = "NONE"; + +/// The `dynamodb:Select`, `dynamodb:ReturnValues` and +/// `dynamodb:ReturnConsumedCapacity` values of one authorization. `None` +/// leaves a key absent. +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] +pub(crate) struct ParameterKeys { + pub select: Option, + pub return_values: Option, + pub return_consumed_capacity: Option, +} + +/// The keys of a request authorized once, against one resource. +pub(crate) fn single_table(operation: &str, input: &Value) -> ParameterKeys { + let mut keys = ParameterKeys { + select: given(input, "Select"), + return_values: given(input, "ReturnValues"), + return_consumed_capacity: given(input, "ReturnConsumedCapacity"), + }; + match operation { + "GetItem" => keys.select = Some(projection_select(input).to_owned()), + "Query" | "Scan" => { + keys.select = Some( + keys.select + .unwrap_or_else(|| range_read_select(input).to_owned()), + ); + } + "PutItem" | "UpdateItem" | "DeleteItem" => { + keys.return_values = Some(keys.return_values.unwrap_or_else(|| NONE.to_owned())); + } + _ => return keys, + } + keys.return_consumed_capacity = Some(consumed_capacity(input)); + keys +} + +/// The keys of one table's part of a `BatchGetItem` or `BatchWriteItem`: +/// `body` is that table's `RequestItems` entry and `request` the whole +/// request, which carries `ReturnConsumedCapacity`. +pub(crate) fn batch_table(operation: &str, body: &Value, request: &Value) -> ParameterKeys { + ParameterKeys { + select: (operation == "BatchGetItem").then(|| projection_select(body).to_owned()), + return_values: None, + return_consumed_capacity: Some(consumed_capacity(request)), + } +} + +/// The keys of one transaction item: `sub` is the item's member name (`Get`, +/// `Put`, `Update`, `Delete` or `ConditionCheck`), `item` its body and +/// `request` the whole request, which carries `ReturnConsumedCapacity`. +pub(crate) fn transact_item(sub: &str, item: &Value, request: &Value) -> ParameterKeys { + ParameterKeys { + select: (sub == "Get").then(|| projection_select(item).to_owned()), + return_values: matches!(sub, "Put" | "Update" | "Delete").then(|| NONE.to_owned()), + return_consumed_capacity: Some(consumed_capacity(request)), + } +} + +/// The request's own value of `parameter`, when it is a string. A `null` is +/// absent, as the engine reads it. +fn given(request: &Value, parameter: &str) -> Option { + request + .get(parameter) + .and_then(Value::as_str) + .map(ToOwned::to_owned) +} + +/// Whether a read projects. The engine projects whenever the request carries +/// `ProjectionExpression` or `AttributesToGet`, and refuses an empty one, so a +/// read without either returns every attribute. A `null` member is absent. +fn has_projection(read: &Value) -> bool { + read.get("ProjectionExpression") + .is_some_and(Value::is_string) + || read.get("AttributesToGet").is_some_and(Value::is_array) +} + +/// `dynamodb:Select` of a read that takes no `Select` parameter. +fn projection_select(read: &Value) -> &'static str { + if has_projection(read) { + SPECIFIC_ATTRIBUTES + } else { + ALL_ATTRIBUTES + } +} + +/// `dynamodb:Select` of a `Query` or `Scan` with no `Select`. +fn range_read_select(read: &Value) -> &'static str { + if has_projection(read) { + SPECIFIC_ATTRIBUTES + } else if read.get("IndexName").is_some_and(Value::is_string) { + ALL_PROJECTED_ATTRIBUTES + } else { + ALL_ATTRIBUTES + } +} + +/// `dynamodb:ReturnConsumedCapacity` of a request that takes the parameter. +fn consumed_capacity(request: &Value) -> String { + given(request, "ReturnConsumedCapacity").unwrap_or_else(|| NONE.to_owned()) +} + +#[cfg(test)] +mod tests; diff --git a/crates/server/src/parameter_keys/tests.rs b/crates/server/src/parameter_keys/tests.rs new file mode 100644 index 000000000..74a6baa68 --- /dev/null +++ b/crates/server/src/parameter_keys/tests.rs @@ -0,0 +1,491 @@ +// Copyright 2026 ExtendDB contributors +// SPDX-License-Identifier: Apache-2.0 + +//! Unit tests for the `dynamodb:Select`, `dynamodb:ReturnValues` and +//! `dynamodb:ReturnConsumedCapacity` values. +//! +//! The defaults mirror DynamoDB's answers of 2026-11-01; the module +//! documentation lists which were measured and which follow them. + +use std::collections::HashMap; + +use extenddb_core::limits::LimitsConfig; +use extenddb_core::types::{KeySchemaElement, KeyType, TableKeyInfo}; +use serde_json::{Value, json}; + +use super::*; + +fn keys(select: Option<&str>, return_values: Option<&str>, rcc: Option<&str>) -> ParameterKeys { + ParameterKeys { + select: select.map(ToOwned::to_owned), + return_values: return_values.map(ToOwned::to_owned), + return_consumed_capacity: rcc.map(ToOwned::to_owned), + } +} + +/// `base` with the members of `extra` added. +fn with(base: &Value, extra: &Value) -> Value { + let mut out = base.clone(); + for (k, v) in extra.as_object().expect("extra is an object") { + out[k] = v.clone(); + } + out +} + +fn read() -> Value { + json!({"TableName": "t", "Key": {"pk": {"S": "a"}}}) +} + +fn projected() -> Value { + json!({"ProjectionExpression": "#n", "ExpressionAttributeNames": {"#n": "name"}}) +} + +fn select_of(operation: &str, input: &Value) -> Option { + single_table(operation, input).select +} + +// -- dynamodb:Select ---------------------------------------------------------- + +#[test] +fn get_item_selects_from_its_projection() { + assert_eq!( + select_of("GetItem", &read()).as_deref(), + Some("ALL_ATTRIBUTES") + ); + assert_eq!( + select_of("GetItem", &with(&read(), &projected())).as_deref(), + Some("SPECIFIC_ATTRIBUTES") + ); + assert_eq!( + select_of( + "GetItem", + &with(&read(), &json!({"AttributesToGet": ["name"]})) + ) + .as_deref(), + Some("SPECIFIC_ATTRIBUTES") + ); +} + +#[test] +fn get_item_does_not_read_a_select_member() { + // GetItem takes no Select: the engine ignores the member and returns the + // whole item, so the member must not decide the key. + let unprojected = with(&read(), &json!({"Select": "SPECIFIC_ATTRIBUTES"})); + assert_eq!( + select_of("GetItem", &unprojected).as_deref(), + Some("ALL_ATTRIBUTES") + ); + let projected = with(&with(&read(), &projected()), &json!({"Select": "COUNT"})); + assert_eq!( + select_of("GetItem", &projected).as_deref(), + Some("SPECIFIC_ATTRIBUTES") + ); +} + +#[test] +fn a_null_projection_is_no_projection() { + // serde reads a null member as absent, so the engine returns every attribute. + let nulls = with( + &read(), + &json!({"ProjectionExpression": null, "AttributesToGet": null, "Select": null}), + ); + for operation in ["GetItem", "Query", "Scan"] { + assert_eq!( + select_of(operation, &nulls).as_deref(), + Some("ALL_ATTRIBUTES"), + "{operation}" + ); + } + let body = json!({"Keys": [{"pk": {"S": "a"}}], "ProjectionExpression": null}); + assert_eq!( + batch_table("BatchGetItem", &body, &json!({})) + .select + .as_deref(), + Some("ALL_ATTRIBUTES") + ); + let get = json!({"TableName": "t", "Key": {"pk": {"S": "a"}}, "ProjectionExpression": null}); + assert_eq!( + transact_item("Get", &get, &json!({})).select.as_deref(), + Some("ALL_ATTRIBUTES") + ); +} + +#[test] +fn query_and_scan_default_select_from_the_projection_and_the_index() { + let index = json!({"IndexName": "gsi"}); + for operation in ["Query", "Scan"] { + let cases = [ + (read(), "ALL_ATTRIBUTES"), + (with(&read(), &projected()), "SPECIFIC_ATTRIBUTES"), + ( + with(&read(), &json!({"AttributesToGet": ["name"]})), + "SPECIFIC_ATTRIBUTES", + ), + (with(&read(), &index), "ALL_PROJECTED_ATTRIBUTES"), + ( + with(&with(&read(), &index), &projected()), + "SPECIFIC_ATTRIBUTES", + ), + (with(&read(), &json!({"IndexName": null})), "ALL_ATTRIBUTES"), + ]; + for (input, expected) in cases { + assert_eq!( + select_of(operation, &input).as_deref(), + Some(expected), + "{operation} {input}" + ); + } + } +} + +#[test] +fn query_and_scan_keep_a_given_select() { + let index_and_projection = with(&with(&read(), &json!({"IndexName": "gsi"})), &projected()); + for operation in ["Query", "Scan"] { + for given in [ + "COUNT", + "ALL_ATTRIBUTES", + "ALL_PROJECTED_ATTRIBUTES", + "SPECIFIC_ATTRIBUTES", + ] { + for base in [read(), index_and_projection.clone()] { + let input = with(&base, &json!({"Select": given})); + assert_eq!( + select_of(operation, &input).as_deref(), + Some(given), + "{operation} {input}" + ); + } + } + } +} + +// -- dynamodb:ReturnValues ---------------------------------------------------- + +#[test] +fn single_item_writes_default_return_values_to_none() { + for operation in ["PutItem", "UpdateItem", "DeleteItem"] { + for input in [read(), with(&read(), &json!({"ReturnValues": null}))] { + assert_eq!( + single_table(operation, &input).return_values.as_deref(), + Some("NONE"), + "{operation} {input}" + ); + } + } +} + +#[test] +fn single_item_writes_keep_given_return_values() { + for operation in ["PutItem", "UpdateItem", "DeleteItem"] { + for given in ["ALL_OLD", "ALL_NEW", "UPDATED_OLD", "UPDATED_NEW", "NONE"] { + let input = with(&read(), &json!({"ReturnValues": given})); + assert_eq!( + single_table(operation, &input).return_values.as_deref(), + Some(given), + "{operation} {given}" + ); + } + } +} + +// -- dynamodb:ReturnConsumedCapacity ----------------------------------------- + +#[test] +fn item_operations_default_return_consumed_capacity_to_none() { + for operation in [ + "GetItem", + "PutItem", + "UpdateItem", + "DeleteItem", + "Query", + "Scan", + ] { + assert_eq!( + single_table(operation, &read()) + .return_consumed_capacity + .as_deref(), + Some("NONE"), + "{operation}" + ); + for given in ["TOTAL", "INDEXES", "NONE"] { + let input = with(&read(), &json!({"ReturnConsumedCapacity": given})); + assert_eq!( + single_table(operation, &input) + .return_consumed_capacity + .as_deref(), + Some(given), + "{operation} {given}" + ); + } + } +} + +// -- keys an operation does not take ------------------------------------------- + +#[test] +fn a_key_the_operation_does_not_take_holds_the_member_as_given() { + let all = json!({"Select": "COUNT", "ReturnValues": "ALL_OLD"}); + for operation in ["PutItem", "UpdateItem", "DeleteItem"] { + assert_eq!(select_of(operation, &read()), None, "{operation}"); + assert_eq!( + select_of(operation, &with(&read(), &all)).as_deref(), + Some("COUNT") + ); + } + for operation in ["GetItem", "Query", "Scan"] { + assert_eq!(single_table(operation, &read()).return_values, None); + assert_eq!( + single_table(operation, &with(&read(), &all)) + .return_values + .as_deref(), + Some("ALL_OLD"), + "{operation}" + ); + } +} + +#[test] +fn other_operations_get_no_default() { + let given = json!({ + "TableName": "t", + "Select": "COUNT", + "ReturnValues": "ALL_OLD", + "ReturnConsumedCapacity": "TOTAL", + }); + for operation in [ + "DescribeTable", + "CreateTable", + "ListTables", + "SearchVectors", + ] { + assert_eq!( + single_table(operation, &json!({"TableName": "t"})), + ParameterKeys::default(), + "{operation}" + ); + assert_eq!( + single_table(operation, &given), + keys(Some("COUNT"), Some("ALL_OLD"), Some("TOTAL")), + "{operation}" + ); + } +} + +// -- batch and transaction parts ------------------------------------------------- + +#[test] +fn batch_get_item_tables_select_from_their_own_projection() { + let keys_only = json!({"Keys": [{"pk": {"S": "a"}}]}); + let request = json!({"RequestItems": {"t": keys_only}}); + assert_eq!( + batch_table("BatchGetItem", &keys_only, &request), + keys(Some("ALL_ATTRIBUTES"), None, Some("NONE")) + ); + for projection in [projected(), json!({"AttributesToGet": ["name"]})] { + assert_eq!( + batch_table("BatchGetItem", &with(&keys_only, &projection), &request).select, + Some("SPECIFIC_ATTRIBUTES".to_owned()), + "{projection}" + ); + } + let with_select = with(&keys_only, &json!({"Select": "SPECIFIC_ATTRIBUTES"})); + assert_eq!( + batch_table("BatchGetItem", &with_select, &request) + .select + .as_deref(), + Some("ALL_ATTRIBUTES"), + "a table part takes no Select" + ); + let total = with(&request, &json!({"ReturnConsumedCapacity": "TOTAL"})); + assert_eq!( + batch_table("BatchGetItem", &keys_only, &total) + .return_consumed_capacity + .as_deref(), + Some("TOTAL") + ); +} + +#[test] +fn batch_write_item_tables_carry_only_return_consumed_capacity() { + let body = json!([{"PutRequest": {"Item": {"pk": {"S": "a"}}}}]); + let request = json!({"RequestItems": {"t": body}}); + assert_eq!( + batch_table("BatchWriteItem", &body, &request), + keys(None, None, Some("NONE")) + ); + let total = with(&request, &json!({"ReturnConsumedCapacity": "TOTAL"})); + assert_eq!( + batch_table("BatchWriteItem", &body, &total), + keys(None, None, Some("TOTAL")) + ); +} + +#[test] +fn transaction_gets_select_from_their_own_projection() { + let get = json!({"TableName": "t", "Key": {"pk": {"S": "a"}}}); + assert_eq!( + transact_item("Get", &get, &json!({})), + keys(Some("ALL_ATTRIBUTES"), None, Some("NONE")) + ); + assert_eq!( + transact_item("Get", &with(&get, &projected()), &json!({})) + .select + .as_deref(), + Some("SPECIFIC_ATTRIBUTES") + ); + let total = json!({"ReturnConsumedCapacity": "TOTAL"}); + assert_eq!( + transact_item("Get", &get, &total) + .return_consumed_capacity + .as_deref(), + Some("TOTAL") + ); +} + +#[test] +fn transaction_writes_hold_return_values_none() { + let item = json!({"TableName": "t", "Key": {"pk": {"S": "a"}}, "ReturnValues": "ALL_OLD"}); + for sub in ["Put", "Update", "Delete"] { + // A transaction item takes no ReturnValues; the engine refuses the member. + assert_eq!( + transact_item(sub, &item, &json!({})), + keys(None, Some("NONE"), Some("NONE")), + "{sub}" + ); + } + assert_eq!( + transact_item("ConditionCheck", &item, &json!({})), + keys(None, None, Some("NONE")) + ); + let total = json!({"ReturnConsumedCapacity": "INDEXES"}); + for sub in ["Put", "Update", "Delete", "ConditionCheck"] { + assert_eq!( + transact_item(sub, &item, &total) + .return_consumed_capacity + .as_deref(), + Some("INDEXES"), + "{sub}" + ); + } +} + +// -- the batch and transaction targets ------------------------------------------ + +fn table(hash: &str, range: Option<&str>) -> TableKeyInfo { + let mut key_schema = vec![KeySchemaElement { + attribute_name: hash.to_owned(), + key_type: KeyType::Hash, + }]; + if let Some(range) = range { + key_schema.push(KeySchemaElement { + attribute_name: range.to_owned(), + key_type: KeyType::Range, + }); + } + TableKeyInfo { + table_name: "t".to_owned(), + account_id: "123456789012".to_owned(), + base_key_schema: key_schema.clone(), + key_schema, + ..Default::default() + } +} + +fn targets(operation: &str, input: &Value, ki: TableKeyInfo) -> Vec<(String, ParameterKeys)> { + let snapshot = HashMap::from([("t".to_owned(), Some(ki))]); + crate::leading_keys::batch_transact_targets( + operation, + input, + &snapshot, + &LimitsConfig::default(), + ) + .expect("a batch or transaction operation") + .into_iter() + .map(|t| (t.action, t.parameters)) + .collect() +} + +#[test] +fn batch_and_transaction_targets_carry_the_keys() { + let key = json!({"pk": {"S": "a"}}); + let bg = json!({ + "RequestItems": {"t": {"Keys": [key], "ProjectionExpression": "pk"}}, + "ReturnConsumedCapacity": "TOTAL", + }); + assert_eq!( + targets("BatchGetItem", &bg, table("pk", None)), + [( + "BatchGetItem".to_owned(), + keys(Some("SPECIFIC_ATTRIBUTES"), None, Some("TOTAL")) + )] + ); + let bw = json!({"RequestItems": {"t": [{"DeleteRequest": {"Key": key}}]}}); + assert_eq!( + targets("BatchWriteItem", &bw, table("pk", None)), + [("BatchWriteItem".to_owned(), keys(None, None, Some("NONE")))] + ); + let tg = json!({"TransactItems": [{"Get": {"TableName": "t", "Key": key}}]}); + assert_eq!( + targets("TransactGetItems", &tg, table("pk", None)), + [( + "GetItem".to_owned(), + keys(Some("ALL_ATTRIBUTES"), None, Some("NONE")) + )] + ); + let tw = json!({"TransactItems": [ + {"Put": {"TableName": "t", "Item": {"pk": {"S": "p"}}}}, + {"Update": {"TableName": "t", "Key": {"pk": {"S": "u"}}, "UpdateExpression": "SET n = :v", + "ExpressionAttributeValues": {":v": {"S": "x"}}}}, + {"Delete": {"TableName": "t", "Key": {"pk": {"S": "d"}}}}, + {"ConditionCheck": {"TableName": "t", "Key": {"pk": {"S": "c"}}, + "ConditionExpression": "attribute_exists(pk)"}}, + ]}); + assert_eq!( + targets("TransactWriteItems", &tw, table("pk", None)), + [ + ("PutItem".to_owned(), keys(None, Some("NONE"), Some("NONE"))), + ( + "UpdateItem".to_owned(), + keys(None, Some("NONE"), Some("NONE")) + ), + ( + "DeleteItem".to_owned(), + keys(None, Some("NONE"), Some("NONE")) + ), + ( + "ConditionCheckItem".to_owned(), + keys(None, None, Some("NONE")) + ), + ] + ); +} + +#[test] +fn transaction_gets_that_differ_only_in_select_are_evaluated_apart() { + // Two Gets of one partition on a table keyed on (pk, sk): the first + // projects pk, the second projects nothing. Both hold LeadingKeys ["a"] + // and Attributes ["pk", "sk"], so only dynamodb:Select tells them apart, + // and a Deny on ALL_ATTRIBUTES must see the second. + let tg = json!({"TransactItems": [ + {"Get": {"TableName": "t", "Key": {"pk": {"S": "a"}, "sk": {"S": "1"}}, + "ProjectionExpression": "pk"}}, + {"Get": {"TableName": "t", "Key": {"pk": {"S": "a"}, "sk": {"S": "2"}}}}, + ]}); + let snapshot = HashMap::from([("t".to_owned(), Some(table("pk", Some("sk"))))]); + let all = crate::leading_keys::batch_transact_targets( + "TransactGetItems", + &tg, + &snapshot, + &LimitsConfig::default(), + ) + .expect("a transaction"); + assert_eq!(all.len(), 2, "{all:?}"); + assert_eq!(all[0].leading_keys, all[1].leading_keys); + assert_eq!(all[0].attributes, all[1].attributes); + assert_eq!( + all[0].parameters.select.as_deref(), + Some("SPECIFIC_ATTRIBUTES") + ); + assert_eq!(all[1].parameters.select.as_deref(), Some("ALL_ATTRIBUTES")); +} diff --git a/crates/server/src/request_helpers.rs b/crates/server/src/request_helpers.rs index 590739b46..ec5ab6e3e 100755 --- a/crates/server/src/request_helpers.rs +++ b/crates/server/src/request_helpers.rs @@ -265,12 +265,15 @@ pub(crate) async fn authorize_request( // the set of keys that table receives; transaction operations authorize // once per item with that item's key. Each target carries the // `dynamodb:LeadingKeys` value(s) the service evaluates, extracted per the - // table's key schema, and the `dynamodb:Attributes` names of that table's - // part of the batch or of that item (see `request_attributes`). The + // table's key schema, the `dynamodb:Attributes` names of that table's + // part of the batch or of that item (see `request_attributes`), and its + // `dynamodb:Select`, `dynamodb:ReturnValues` and + // `dynamodb:ReturnConsumedCapacity` values (see `parameter_keys`). The // resource ARN per target is unchanged: each is still that table's ARN. - // (Measured against live DynamoDB 2026-10-20 and 2026-10-31.) Targets with - // the same (action, table, LeadingKeys, Attributes) are evaluated once, - // since the decision depends only on those four for a given identity. + // (Measured against live DynamoDB 2026-10-20, 2026-10-31 and 2026-11-01.) + // Targets with the same action, table and condition key values are + // evaluated once, since the decision depends only on those for a given + // identity. if let Some(tables) = crate::leading_keys::batch_transact_tables(operation, input) { // A request outside the operation's count limits is refused before any // key info is read or any policy is evaluated, with the engine's own @@ -331,6 +334,9 @@ pub(crate) async fn authorize_request( let params = extenddb_auth::policy::context::RequestParams { leading_keys: target.leading_keys, attributes: target.attributes, + select: target.parameters.select, + return_values: target.parameters.return_values, + return_consumed_capacity: target.parameters.return_consumed_capacity, ..base_params(request_tags.clone(), request_tag_keys.clone()) }; authorization::check_authorization( @@ -357,7 +363,11 @@ pub(crate) async fn authorize_request( if targets.len() == 1 { // Single-resource operations carry the request params the policy engine - // reads for item-level condition keys. + // reads for item-level condition keys. `dynamodb:Select`, + // `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold + // the value the operation uses when the request leaves the parameter + // out (see `parameter_keys`). + let parameters = crate::parameter_keys::single_table(operation, input); let params = extenddb_auth::policy::context::RequestParams { leading_keys: crate::leading_keys::single_table_leading_keys( input, @@ -371,18 +381,9 @@ pub(crate) async fn authorize_request( key_info.as_ref(), &state.limits, ), - select: input - .get("Select") - .and_then(|v| v.as_str()) - .map(ToOwned::to_owned), - return_values: input - .get("ReturnValues") - .and_then(|v| v.as_str()) - .map(ToOwned::to_owned), - return_consumed_capacity: input - .get("ReturnConsumedCapacity") - .and_then(|v| v.as_str()) - .map(ToOwned::to_owned), + select: parameters.select, + return_values: parameters.return_values, + return_consumed_capacity: parameters.return_consumed_capacity, ..base_params(request_tags, request_tag_keys) }; let (action, resource_arn) = &targets[0]; From 903b433a3481788260b6201eea7926d0e9d8d33d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:27:22 +0000 Subject: [PATCH 13/21] test: cover the Select, ReturnValues and ReturnConsumedCapacity defaults Wire tests for the rows DynamoDB answered on 2026-11-01 for dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity on requests that leave the parameter out: - A Deny of GetItem or Query with StringNotEqualsIfExists over SPECIFIC_ATTRIBUTES lets a read with a projection through and refuses one without. - A Deny of GetItem, Query and Scan with StringEquals over ALL_ATTRIBUTES refuses a read with no projection and lets a projected GetItem and a Select COUNT Scan or Query through. The same holds for each table of a BatchGetItem and for the Get of a TransactGetItems, and an Allow of BatchGetItem gated on SPECIFIC_ATTRIBUTES grants a projected one. - An Allow gated on ReturnValues NONE grants a PutItem, UpdateItem or DeleteItem without ReturnValues and the Put, Update and Delete items of a TransactWriteItems, refuses the single-item writes that ask for ALL_OLD or ALL_NEW, and refuses a BatchWriteItem. - An Allow gated on ReturnConsumedCapacity NONE grants a GetItem, a BatchWriteItem and a TransactGetItems without ReturnConsumedCapacity, and refuses a GetItem that asks for TOTAL. Each refused write is checked to leave the table unchanged. Two rows were not measured, and their docstrings say so. A Query of a global secondary index with no projection and no Select holds ALL_PROJECTED_ATTRIBUTES, as the API reference gives for a read of an index, so a Deny on ALL_PROJECTED_ATTRIBUTES refuses it and a Deny on ALL_ATTRIBUTES does not. Two Gets of one partition in a TransactGetItems that differ only in their projection are evaluated apart, following the measured TransactGetItems rows. --- tests/test_dynamodb_condition_key_defaults.py | 487 ++++++++++++++++++ 1 file changed, 487 insertions(+) create mode 100644 tests/test_dynamodb_condition_key_defaults.py diff --git a/tests/test_dynamodb_condition_key_defaults.py b/tests/test_dynamodb_condition_key_defaults.py new file mode 100644 index 000000000..7e3c81821 --- /dev/null +++ b/tests/test_dynamodb_condition_key_defaults.py @@ -0,0 +1,487 @@ +# Copyright 2026 ExtendDB contributors +# SPDX-License-Identifier: Apache-2.0 + +"""dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity +when a request leaves the parameter out. + +DynamoDB evaluates the value the operation uses when the request carries no +parameter, so the key is present whether or not the caller spells it out: + + * dynamodb:Select is SPECIFIC_ATTRIBUTES on a GetItem, Query or Scan with a + projection and ALL_ATTRIBUTES on one without, and the same on each table + of a BatchGetItem and on the Get of a TransactGetItems. A Select the + request gives is used as given. + * dynamodb:ReturnValues is NONE on a PutItem, UpdateItem or DeleteItem + without ReturnValues and on the Put, Update and Delete items of a + TransactWriteItems. A BatchWriteItem leaves the key absent. + * dynamodb:ReturnConsumedCapacity is NONE on a GetItem, a BatchWriteItem and + the Get of a TransactGetItems without ReturnConsumedCapacity. + +Unless its docstring says otherwise, every request shape here was sent to live +DynamoDB on 2026-11-01 under a session policy of the same shape as the user +policy the test attaches, twice, with the same answer both times. The fixtures +are those of test_dynamodb_attributes_condition.py, plus a table keyed on +(pk, sk) with a global secondary index for the two rows that were not +measured. + +Prerequisites and run command: as for test_dynamodb_attributes_condition.py. + +REQ-TEST-001, REQ-AUTH-002 +""" + +from __future__ import annotations + +import json +import uuid +from typing import Any, Callable + +import pytest +from botocore.exceptions import ClientError + +from conftest import wait_for_active + +# The fixtures are imported so this module gets its own account and table. +from test_dynamodb_attributes_condition import ( # noqa: F401 + ITEM_A, + KEY_A, + Env, + _allow_only, + _assert_denied, + _create_user_with_key, + _make_client, + _new_key, + _refusal, + _stored, + account_id, + auth_env, + env, + mgmt, + region, +) + +RV_NONE = {"StringEquals": {"dynamodb:ReturnValues": "NONE"}} +RCC_NONE = {"StringEquals": {"dynamodb:ReturnConsumedCapacity": "NONE"}} +NAME = {"ProjectionExpression": "#n", "ExpressionAttributeNames": {"#n": "name"}} + + +def _deny_select(resources: list[str], actions: list[str], operator: str, + value: str) -> dict: + """Everything on the resources, except `actions` when `operator` holds + over dynamodb:Select and `value`.""" + return { + "Version": "2012-10-17", + "Statement": [ + {"Effect": "Allow", "Action": "dynamodb:*", "Resource": resources}, + { + "Effect": "Deny", + "Action": [f"dynamodb:{a}" for a in actions], + "Resource": resources, + "Condition": {operator: {"dynamodb:Select": value}}, + }, + ], + } + + +@pytest.fixture(scope="module") +def table_arn(env: Env, account_id: str, region: str) -> str: + return f"arn:aws:dynamodb:{region}:{account_id}:table/{env.table}" + + +@pytest.fixture(scope="module") +def client_for(auth_env, mgmt, account_id, region) -> Callable[[dict], Any]: + """A client for a new user whose only policy is the one given, made once + per policy.""" + endpoint = auth_env[0] + made: dict[str, Any] = {} + + def make(policy: dict) -> Any: + key = json.dumps(policy, sort_keys=True) + if key not in made: + user = f"u-{uuid.uuid4().hex[:8]}" + ak, sk = _create_user_with_key(mgmt, account_id, user) + resp = mgmt.put_user_policy(account_id, user, "p", policy) + assert resp.status_code == 204, resp.text + made[key] = _make_client(endpoint, ak, sk, region) + return made[key] + + return make + + +@pytest.fixture(scope="module") +def deny_all_attributes(client_for, table_arn: str) -> Callable[[list[str]], Any]: + """A client denied `actions` with StringEquals {"dynamodb:Select": + "ALL_ATTRIBUTES"} and allowed everything else on the table.""" + return lambda actions: client_for( + _deny_select([table_arn], actions, "StringEquals", "ALL_ATTRIBUTES")) + + +def _assert_not_allowed(call: Callable[[], Any], action: str) -> None: + msg = _refusal(call, action) + assert f"because no identity-based policy allows the dynamodb:{action} action" in msg, msg + + +def _named_item(env: Env) -> dict: + """A new item holding name, stored by the owner.""" + item = {**_new_key(), "name": {"S": "n"}} + env.admin.put_item(TableName=env.table, Item=item) + return item + + +# -- dynamodb:Select ---------------------------------------------------------- + + +@pytest.mark.parametrize("action", ["GetItem", "Query"]) +def test_projected_read_passes_a_deny_unless_specific_attributes( + env: Env, client_for, table_arn: str, action: str +): + """A Deny of the action with StringNotEqualsIfExists {"dynamodb:Select": + "SPECIFIC_ATTRIBUTES"} lets a read with a projection and no Select + through: its key holds SPECIFIC_ATTRIBUTES.""" + client = client_for(_deny_select( + [table_arn], [action], "StringNotEqualsIfExists", "SPECIFIC_ATTRIBUTES")) + if action == "GetItem": + resp = client.get_item(TableName=env.table, Key=KEY_A, **NAME) + assert resp["Item"] == {"name": ITEM_A["name"]} + else: + resp = client.query( + TableName=env.table, KeyConditionExpression="pk = :p", + ExpressionAttributeValues={":p": KEY_A["pk"]}, **NAME) + assert resp["Items"] == [{"name": ITEM_A["name"]}] + + +@pytest.mark.parametrize("action", ["GetItem", "Query"]) +def test_unprojected_read_is_denied_unless_specific_attributes( + env: Env, client_for, table_arn: str, action: str +): + """The same Deny refuses a read with no projection and no Select: its key + holds ALL_ATTRIBUTES.""" + client = client_for(_deny_select( + [table_arn], [action], "StringNotEqualsIfExists", "SPECIFIC_ATTRIBUTES")) + if action == "GetItem": + call = lambda: client.get_item(TableName=env.table, Key=KEY_A) + else: + call = lambda: client.query( + TableName=env.table, KeyConditionExpression="pk = :p", + ExpressionAttributeValues={":p": KEY_A["pk"]}) + _assert_denied(call, action) + + +@pytest.mark.parametrize("action", ["GetItem", "Query", "Scan"]) +def test_unprojected_read_is_denied_on_all_attributes( + env: Env, deny_all_attributes, action: str +): + """A Deny of GetItem, Query and Scan with StringEquals {"dynamodb:Select": + "ALL_ATTRIBUTES"} refuses a read with no projection and no Select.""" + client = deny_all_attributes(["GetItem", "Query", "Scan"]) + calls = { + "GetItem": lambda: client.get_item(TableName=env.table, Key=KEY_A), + "Query": lambda: client.query( + TableName=env.table, KeyConditionExpression="pk = :p", + ExpressionAttributeValues={":p": KEY_A["pk"]}), + "Scan": lambda: client.scan(TableName=env.table), + } + _assert_denied(calls[action], action) + + +def test_projected_get_item_passes_a_deny_on_all_attributes(env: Env, deny_all_attributes): + client = deny_all_attributes(["GetItem", "Query", "Scan"]) + resp = client.get_item(TableName=env.table, Key=KEY_A, **NAME) + assert resp["Item"] == {"name": ITEM_A["name"]} + + +def test_scan_with_select_count_passes_a_deny_on_all_attributes(env: Env, deny_all_attributes): + """A Select the request gives is used as given.""" + client = deny_all_attributes(["GetItem", "Query", "Scan"]) + resp = client.scan(TableName=env.table, Select="COUNT") + assert resp["Count"] >= 1 + assert "Items" not in resp + + +def test_query_with_select_count_passes_a_deny_on_all_attributes(env: Env, deny_all_attributes): + client = deny_all_attributes(["Query"]) + resp = client.query( + TableName=env.table, KeyConditionExpression="pk = :p", + ExpressionAttributeValues={":p": KEY_A["pk"]}, Select="COUNT") + assert resp["Count"] == 1 + + +def test_unprojected_batch_get_item_is_denied_on_all_attributes(env: Env, deny_all_attributes): + client = deny_all_attributes(["BatchGetItem"]) + _assert_denied( + lambda: client.batch_get_item(RequestItems={env.table: {"Keys": [KEY_A]}}), + "BatchGetItem", + ) + + +def test_projected_batch_get_item_passes_a_deny_on_all_attributes(env: Env, deny_all_attributes): + client = deny_all_attributes(["BatchGetItem"]) + resp = client.batch_get_item(RequestItems={env.table: {"Keys": [KEY_A], **NAME}}) + assert resp["Responses"][env.table] == [{"name": ITEM_A["name"]}] + + +def test_projected_batch_get_item_is_granted_specific_attributes( + env: Env, client_for, table_arn: str +): + """An Allow of BatchGetItem alone with StringEquals {"dynamodb:Select": + "SPECIFIC_ATTRIBUTES"} grants a BatchGetItem with a projection.""" + client = client_for(_allow_only( + table_arn, "BatchGetItem", + {"StringEquals": {"dynamodb:Select": "SPECIFIC_ATTRIBUTES"}})) + resp = client.batch_get_item(RequestItems={env.table: {"Keys": [KEY_A], **NAME}}) + assert resp["Responses"][env.table] == [{"name": ITEM_A["name"]}] + + +def test_unprojected_transaction_get_is_denied_on_all_attributes(env: Env, deny_all_attributes): + """The Get of a TransactGetItems is authorized as GetItem, with the key + set as on a GetItem.""" + client = deny_all_attributes(["GetItem"]) + _assert_denied( + lambda: client.transact_get_items( + TransactItems=[{"Get": {"TableName": env.table, "Key": KEY_A}}]), + "GetItem", + ) + + +def test_projected_transaction_get_passes_a_deny_on_all_attributes( + env: Env, deny_all_attributes +): + client = deny_all_attributes(["GetItem"]) + resp = client.transact_get_items( + TransactItems=[{"Get": {"TableName": env.table, "Key": KEY_A, **NAME}}]) + assert resp["Responses"] == [{"Item": {"name": ITEM_A["name"]}}] + + +# -- dynamodb:ReturnValues ---------------------------------------------------- + + +def test_put_item_without_return_values_is_granted_return_values_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "PutItem", RV_NONE)) + item = {**_new_key(), "name": {"S": "n"}} + client.put_item(TableName=env.table, Item=item) + assert _stored(env, {"pk": item["pk"]}) == item + + +def test_put_item_with_return_values_all_old_is_not_granted( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "PutItem", RV_NONE)) + item = {**_new_key(), "name": {"S": "n"}} + _assert_not_allowed( + lambda: client.put_item(TableName=env.table, Item=item, ReturnValues="ALL_OLD"), + "PutItem", + ) + assert _stored(env, {"pk": item["pk"]}) is None, "a refused write stores nothing" + + +def test_update_item_without_return_values_is_granted_return_values_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "UpdateItem", RV_NONE)) + item = _named_item(env) + key = {"pk": item["pk"]} + client.update_item( + TableName=env.table, Key=key, UpdateExpression="SET #n = :v", + ExpressionAttributeNames={"#n": "name"}, ExpressionAttributeValues={":v": {"S": "u"}}) + assert _stored(env, key) == {**key, "name": {"S": "u"}} + + +def test_update_item_with_return_values_all_new_is_not_granted( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "UpdateItem", RV_NONE)) + item = _named_item(env) + key = {"pk": item["pk"]} + _assert_not_allowed( + lambda: client.update_item( + TableName=env.table, Key=key, UpdateExpression="SET #n = :v", + ExpressionAttributeNames={"#n": "name"}, + ExpressionAttributeValues={":v": {"S": "u"}}, ReturnValues="ALL_NEW"), + "UpdateItem", + ) + assert _stored(env, key) == item, "a refused write changes nothing" + + +def test_delete_item_without_return_values_is_granted_return_values_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "DeleteItem", RV_NONE)) + item = _named_item(env) + key = {"pk": item["pk"]} + client.delete_item(TableName=env.table, Key=key) + assert _stored(env, key) is None + + +def test_delete_item_with_return_values_all_old_is_not_granted( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "DeleteItem", RV_NONE)) + item = _named_item(env) + key = {"pk": item["pk"]} + _assert_not_allowed( + lambda: client.delete_item(TableName=env.table, Key=key, ReturnValues="ALL_OLD"), + "DeleteItem", + ) + assert _stored(env, key) == item, "a refused write changes nothing" + + +def test_batch_write_item_is_not_granted_return_values_none( + env: Env, client_for, table_arn: str +): + """A BatchWriteItem leaves dynamodb:ReturnValues absent, so an Allow gated + on StringEquals NONE does not grant it.""" + client = client_for(_allow_only(table_arn, "BatchWriteItem", RV_NONE)) + item = {**_new_key(), "name": {"S": "n"}} + _assert_not_allowed( + lambda: client.batch_write_item( + RequestItems={env.table: [{"PutRequest": {"Item": item}}]}), + "BatchWriteItem", + ) + assert _stored(env, {"pk": item["pk"]}) is None, "a refused write stores nothing" + + +@pytest.mark.parametrize("action", ["PutItem", "UpdateItem", "DeleteItem"]) +def test_transaction_write_is_granted_return_values_none( + env: Env, client_for, table_arn: str, action: str +): + """Each Put, Update and Delete item of a TransactWriteItems is authorized + as PutItem, UpdateItem and DeleteItem with dynamodb:ReturnValues NONE. + The Delete names a key that holds no item, as on the service.""" + client = client_for(_allow_only(table_arn, action, RV_NONE)) + key = _new_key() + item = { + "PutItem": {"Put": {"TableName": env.table, "Item": {**key, "name": {"S": "t"}}}}, + "UpdateItem": {"Update": { + "TableName": env.table, "Key": key, "UpdateExpression": "SET #n = :v", + "ExpressionAttributeNames": {"#n": "name"}, + "ExpressionAttributeValues": {":v": {"S": "t"}}}}, + "DeleteItem": {"Delete": {"TableName": env.table, "Key": key}}, + }[action] + client.transact_write_items(TransactItems=[item]) + expected = None if action == "DeleteItem" else {**key, "name": {"S": "t"}} + assert _stored(env, key) == expected + + +# -- dynamodb:ReturnConsumedCapacity ----------------------------------------- + + +def test_get_item_without_return_consumed_capacity_is_granted_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "GetItem", RCC_NONE)) + resp = client.get_item(TableName=env.table, Key=KEY_A) + assert resp["Item"] == ITEM_A + + +def test_get_item_with_return_consumed_capacity_total_is_not_granted( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "GetItem", RCC_NONE)) + _assert_not_allowed( + lambda: client.get_item(TableName=env.table, Key=KEY_A, ReturnConsumedCapacity="TOTAL"), + "GetItem", + ) + + +def test_batch_write_item_without_return_consumed_capacity_is_granted_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "BatchWriteItem", RCC_NONE)) + item = {**_new_key(), "name": {"S": "n"}} + resp = client.batch_write_item(RequestItems={env.table: [{"PutRequest": {"Item": item}}]}) + assert resp["UnprocessedItems"] == {} + assert _stored(env, {"pk": item["pk"]}) == item + + +def test_transaction_get_without_return_consumed_capacity_is_granted_none( + env: Env, client_for, table_arn: str +): + client = client_for(_allow_only(table_arn, "GetItem", RCC_NONE)) + resp = client.transact_get_items( + TransactItems=[{"Get": {"TableName": env.table, "Key": KEY_A}}]) + assert resp["Responses"] == [{"Item": ITEM_A}] + + +# -- rows that were not measured --------------------------------------------- + + +@pytest.fixture(scope="module") +def indexed(env: Env, account_id: str, region: str) -> tuple[str, list[str]]: + """A table keyed on (pk, sk), with a global secondary index on gpk that + projects every attribute, holding items (a, 1) and (a, 2). Yields the + table name and the resources a policy on it and its indexes lists.""" + table = f"attrs-ix-{uuid.uuid4().hex[:8]}" + env.admin.create_table( + TableName=table, + AttributeDefinitions=[ + {"AttributeName": "pk", "AttributeType": "S"}, + {"AttributeName": "sk", "AttributeType": "S"}, + {"AttributeName": "gpk", "AttributeType": "S"}, + ], + KeySchema=[ + {"AttributeName": "pk", "KeyType": "HASH"}, + {"AttributeName": "sk", "KeyType": "RANGE"}, + ], + GlobalSecondaryIndexes=[{ + "IndexName": "gsi", + "KeySchema": [{"AttributeName": "gpk", "KeyType": "HASH"}], + "Projection": {"ProjectionType": "ALL"}, + }], + BillingMode="PAY_PER_REQUEST", + ) + wait_for_active(env.admin, table) + for sk in ["1", "2"]: + env.admin.put_item(TableName=table, Item={ + "pk": {"S": "a"}, "sk": {"S": sk}, "gpk": {"S": "g"}, "ssn": {"S": "1"}}) + arn = f"arn:aws:dynamodb:{region}:{account_id}:table/{table}" + yield table, [arn, f"{arn}/index/*"] + try: + env.admin.delete_table(TableName=table) + except ClientError: + pass + + +def _index_query(client: Any, table: str, **extra: Any) -> Any: + return client.query( + TableName=table, IndexName="gsi", KeyConditionExpression="gpk = :g", + ExpressionAttributeValues={":g": {"S": "g"}}, **extra) + + +def test_unprojected_index_query_holds_all_projected_attributes(indexed, client_for): + """Not measured: follows the API reference, which gives ALL_PROJECTED_ATTRIBUTES + as the default for a Query or Scan of an index with no projection and no + Select. A Deny on ALL_PROJECTED_ATTRIBUTES refuses such a Query, and a + Deny on ALL_ATTRIBUTES does not, though the index projects every + attribute; with Select ALL_ATTRIBUTES given, the Deny on ALL_ATTRIBUTES + refuses it.""" + table, resources = indexed + deny_projected = client_for(_deny_select( + resources, ["Query"], "StringEquals", "ALL_PROJECTED_ATTRIBUTES")) + _assert_denied(lambda: _index_query(deny_projected, table), "Query") + + deny_all = client_for(_deny_select(resources, ["Query"], "StringEquals", "ALL_ATTRIBUTES")) + _index_query(deny_all, table) + _assert_denied(lambda: _index_query(deny_all, table, Select="ALL_ATTRIBUTES"), "Query") + + +def test_transaction_gets_of_one_partition_are_evaluated_apart(indexed, client_for): + """Not measured: follows the TransactGetItems rows. Two Gets of partition a + whose only difference for authorization is that the first projects pk: + both hold the same dynamodb:LeadingKeys and dynamodb:Attributes, and a Deny + on ALL_ATTRIBUTES refuses the transaction because the second Get holds + ALL_ATTRIBUTES.""" + table, resources = indexed + client = client_for(_deny_select(resources, ["GetItem"], "StringEquals", "ALL_ATTRIBUTES")) + + def get(sk: str, **extra: Any) -> dict: + return {"Get": {"TableName": table, "Key": {"pk": {"S": "a"}, "sk": {"S": sk}}, **extra}} + + _assert_denied( + lambda: client.transact_get_items( + TransactItems=[get("1", ProjectionExpression="pk"), get("2")]), + "GetItem", + ) + resp = client.transact_get_items(TransactItems=[ + get("1", ProjectionExpression="pk"), get("2", ProjectionExpression="pk")]) + assert resp["Responses"] == [{"Item": {"pk": {"S": "a"}}}, {"Item": {"pk": {"S": "a"}}}] From f48250fda6bae02ec3a85746b404bbe17e86228d Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 17:43:36 +0000 Subject: [PATCH 14/21] docs: describe the Select, ReturnValues and ReturnConsumedCapacity defaults The security model's condition key table and a new section say which value dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity hold when a request leaves the parameter out, on single-table requests and on each table of a batch and each transaction item, and which rows DynamoDB answered on 2026-11-01. The section says that a Query or Scan of an index with no projection holds ALL_PROJECTED_ATTRIBUTES, following the API reference, so a statement meant to refuse whole-item reads of a table and its indexes lists both ALL_ATTRIBUTES and ALL_PROJECTED_ATTRIBUTES. The paragraph on ReturnValues ALL_OLD no longer says that ExtendDB sets dynamodb:ReturnValues only when the request carries ReturnValues. The upgrade manual's new entry lists the policy shapes that now decide differently: a Deny on Select ALL_ATTRIBUTES now refuses reads with no projection; a Deny gated on StringNotEqualsIfExists over Select SPECIFIC_ATTRIBUTES stops refusing projected reads; an Allow gated on Select SPECIFIC_ATTRIBUTES, ReturnValues NONE or ReturnConsumedCapacity NONE now grants the requests that omit the parameter; a Deny gated on StringNotEqualsIfExists over ReturnValues or ReturnConsumedCapacity NONE stops refusing them; and Null on the three keys no longer matches them. --- docs/manuals/07-upgrade-manual.md | 16 ++++++++++++++++ docs/manuals/10-security-model.md | 23 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index c54e40be2..23f00077f 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -681,6 +681,22 @@ No catalog migration is required. Before upgrading a deployment that restricts t A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). Nor does it stop an `UpdateItem` from copying the attribute's value into another attribute through a `SET` value such as `SET #n = ssn`, as on DynamoDB (measured 2026-11-01). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. +### v0.1.15: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter + +**What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same (measured 2026-11-01 on the requests the security model manual lists; the index value follows the DynamoDB API reference and was not measured). A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. + +**Who is affected.** Any deployment with a policy statement that names one of these keys in a condition. Statements of these shapes now decide differently: + +- A `Deny` gated on `StringEquals` with `dynamodb:Select` `ALL_ATTRIBUTES` now refuses a `GetItem`, `Query` or `Scan` with no projection and no `Select`, a `BatchGetItem` table with no projection and a `TransactGetItems` `Get` with no projection. Before, it refused none of them, and each returned the whole item. It does not refuse a `Query` or `Scan` of an index with no projection, which holds `ALL_PROJECTED_ATTRIBUTES`; list that value as well to refuse those reads. +- A `Deny` gated on `StringNotEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` refused every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covers, and every `Query` or `Scan` without `Select`, because the absent key satisfied it. It now stops refusing reads that project, and still refuses reads without a projection. +- An `Allow` gated on `StringEquals` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` now grants a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems` that projects without `Select`. Before, it granted only a `Query` or `Scan` that set `Select` to `SPECIFIC_ATTRIBUTES`, and a `GetItem` that carried a `Select` member of that value, whatever it projected. +- An `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` now grants a `PutItem`, `UpdateItem` or `DeleteItem` without `ReturnValues`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`. Before, it refused them. It still refuses a `BatchWriteItem`, which leaves the key absent, as on DynamoDB (measured 2026-11-01). +- An `Allow` gated on `StringEquals` with `dynamodb:ReturnConsumedCapacity` `NONE` now grants the `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction requests its `Action` covers that carry no `ReturnConsumedCapacity`. Before, it refused them. +- A `Deny` gated on `StringNotEqualsIfExists` with `dynamodb:ReturnValues` or `dynamodb:ReturnConsumedCapacity` `NONE` refused every request without the parameter, because the absent key satisfied it. It now stops refusing the requests that take the parameter and omit it. +- `Null` with value `true` on one of these keys matched every request without the parameter. It now matches none of the requests that take the parameter, so a `Deny` gated on it stops refusing them and an `Allow` gated on it stops granting them. `Null` with value `false` now matches them. + +No catalog migration is required. Before upgrading, check the policies of each account for the statements above. + --- ## License diff --git a/docs/manuals/10-security-model.md b/docs/manuals/10-security-model.md index d959f04b8..70110c417 100755 --- a/docs/manuals/10-security-model.md +++ b/docs/manuals/10-security-model.md @@ -111,9 +111,9 @@ Set operators and `IfExists` can be combined: `ForAllValues:StringEqualsIfExists | `aws:ResourceTag/` | String | Tag on the target DynamoDB table | | `dynamodb:LeadingKeys` | Multi-valued string | Partition key values being accessed | | `dynamodb:Attributes` | Multi-valued string | Top-level names of the attributes the request names (see below) | -| `dynamodb:Select` | String | The `Select` parameter value | -| `dynamodb:ReturnValues` | String | The `ReturnValues` parameter value | -| `dynamodb:ReturnConsumedCapacity` | String | The `ReturnConsumedCapacity` parameter value | +| `dynamodb:Select` | String | The `Select` parameter value, or the value the read uses without one (see below) | +| `dynamodb:ReturnValues` | String | The `ReturnValues` parameter value, or `NONE` on a write without one (see below) | +| `dynamodb:ReturnConsumedCapacity` | String | The `ReturnConsumedCapacity` parameter value, or `NONE` on an item request without one (see below) | | `dynamodb:FullTableScan` | Boolean | `true` for Scan operations | | `dynamodb:EnclosingOperation` | String | The enclosing operation for batch/transact sub-operations | @@ -150,10 +150,25 @@ extenddb supports the same access control patterns as real AWS IAM: The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). As in DynamoDB, a policy on `dynamodb:Attributes` does not stop an `UpdateItem` from copying a denied attribute's value into another attribute through a `SET` value: under a `Deny` on `ssn`, `SET #n = ssn` with `#n` for `name` is allowed, and a projection of `name` then returns the value (measured on DynamoDB 2026-11-01). A `Scan` holds the table's key attributes when it names no attribute, a `Select COUNT` `Scan` included, so a `Null` condition on the key does not match it: a `Deny` of `Scan` gated on `Null` with value `true` refuses neither a `Scan` with no projection nor a projected one, and an `Allow` of `Scan` gated on it grants neither a `Scan` with no projection nor a `Select COUNT` `Scan` (measured on DynamoDB 2026-11-01). A bare `StringNotEquals` on the key does not match whether the key is present or absent, so an `Allow` gated on it grants nothing; on DynamoDB it granted no `Scan`, whether the `Scan` named an attribute or not (measured 2026-11-01). An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `SearchVectors` holds them. The key is absent on a `SearchVectors` with no `ProjectionExpression` and, on a table that does not exist, on a `GetItem` or transaction `Get` that names no attribute outside its `Key`, a `Query` that names none outside its key condition and a `Scan` that names none. -A `Deny` on `dynamodb:Attributes` does not stop `ReturnValues` `ALL_OLD` or `ReturnValuesOnConditionCheckFailure` `ALL_OLD` from returning the whole stored item, the denied attribute included. Under a `Deny` on `ssn`, an `UpdateItem` of `name` with `ReturnValues` `ALL_OLD` returns the old item with `ssn` in it, and a `PutItem` whose condition fails and a `TransactWriteItems` `ConditionCheck` that fails, each with `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, return the stored item with `ssn` in the error. DynamoDB does the same (measured 2026-11-01). ExtendDB returns the whole stored item in each of these cases, and on a `PutItem` with `ReturnValues` `ALL_OLD`, whose answer on DynamoDB was not measured. The IAM guide's guard against `ReturnValues` returning attributes is a condition on `dynamodb:ReturnValues`. ExtendDB sets that key only when the request carries `ReturnValues`; DynamoDB sets it to `NONE` on a `PutItem` that carries none (measured 2026-11-01, tracked in #717). +A `Deny` on `dynamodb:Attributes` does not stop `ReturnValues` `ALL_OLD` or `ReturnValuesOnConditionCheckFailure` `ALL_OLD` from returning the whole stored item, the denied attribute included. Under a `Deny` on `ssn`, an `UpdateItem` of `name` with `ReturnValues` `ALL_OLD` returns the old item with `ssn` in it, and a `PutItem` whose condition fails and a `TransactWriteItems` `ConditionCheck` that fails, each with `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, return the stored item with `ssn` in the error. DynamoDB does the same (measured 2026-11-01). ExtendDB returns the whole stored item in each of these cases, and on a `PutItem` with `ReturnValues` `ALL_OLD`, whose answer on DynamoDB was not measured. The IAM guide's guard against `ReturnValues` returning attributes is a condition on `dynamodb:ReturnValues`, which holds `NONE` on a write that carries no `ReturnValues` (see below). These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item attributes of `PutItem`; the target of a `SET`, `REMOVE`, `ADD` or `DELETE` action and the attributes a `SET` value reads; `ConditionExpression` on `PutItem`, `DeleteItem`, a `ConditionCheck` and a transaction `Delete`; `FilterExpression` on `Query` and `Scan`; `Expected` on `PutItem`, `AttributeUpdates`, `ScanFilter` and `QueryFilter`; `AttributesToGet` on `GetItem`, `Query`, `Scan` and `BatchGetItem`; the table's key attributes on `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `DeleteRequest`; projected document paths; reads with no projection; `DeleteItem`; a transaction `Update`; the `BatchGetItem`, `TransactGetItems`, `BatchWriteItem` and `TransactWriteItems` forms of a projection and a put; and the evaluation of a `BatchWriteItem` once per table. `ProjectionExpression` on `Query` and `Scan`, `Expected` and `ConditionExpression` on `UpdateItem`, and `Expected` on `DeleteItem` follow the rule measured for the same parameter on the other operations. The key attributes of the queried or scanned index follow the IAM guide's rule that a policy on `dynamodb:Attributes` lists the primary key and index key attributes of the table and of every secondary index the policy covers; they were not measured. Whether `ExclusiveStartKey` adds names of its own cannot be told on DynamoDB, because a valid start key holds only key attributes, which the key already holds. +`dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity`. When a request leaves the parameter out, the key holds the value the operation then uses, as on DynamoDB, so a condition decides the same way whether or not the caller writes that value out: + +| Key | Requests | Value when the request has no such parameter | +|-----|----------|----------------------------------------------| +| `dynamodb:Select` | `GetItem`, `Query` and `Scan`, each table of a `BatchGetItem`, and each `Get` of a `TransactGetItems` | `SPECIFIC_ATTRIBUTES` with a `ProjectionExpression` or `AttributesToGet`, and `ALL_ATTRIBUTES` without one | +| `dynamodb:Select` | A `Query` or `Scan` with `IndexName` and no projection | `ALL_PROJECTED_ATTRIBUTES` | +| `dynamodb:ReturnValues` | `PutItem`, `UpdateItem` and `DeleteItem`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems` | `NONE` | +| `dynamodb:ReturnConsumedCapacity` | `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and `Scan`, and each table or item of a `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` or `TransactWriteItems` | `NONE` | + +A `Query` or `Scan` that carries `Select` holds it as given. `GetItem`, `BatchGetItem` and `TransactGetItems` take no `Select` parameter, so their value comes from the projection alone, and a `Select` member in one of them is not read. The `Put`, `Update` and `Delete` items of a transaction take no `ReturnValues` parameter and always hold `NONE`. A `BatchWriteItem` and a `ConditionCheck` leave `dynamodb:ReturnValues` absent. On a single-table request, a key the operation does not take, such as `dynamodb:ReturnValues` on a `GetItem`, holds the request's member of that name when it has one and is absent otherwise. A batch or transaction request carries one `ReturnConsumedCapacity`, and each of its tables or items is evaluated with it. + +So a `Deny` on `dynamodb:Select` `ALL_ATTRIBUTES` refuses a read with no projection, which returns the whole item, and an `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` grants a write that carries no `ReturnValues`. A `Query` or `Scan` of an index with no projection holds `ALL_PROJECTED_ATTRIBUTES`, even when the index projects every attribute, so a statement meant to refuse whole-item reads of a table and its indexes lists both `ALL_ATTRIBUTES` and `ALL_PROJECTED_ATTRIBUTES`. + +These values were measured on DynamoDB on 2026-11-01: `dynamodb:Select` on `GetItem` and `Query` with and without a projection, on `Scan` without one and with `Select` `COUNT`, on `Query` with `Select` `COUNT`, and on a `BatchGetItem` and a `TransactGetItems` `Get` with and without a projection; `dynamodb:ReturnValues` on `PutItem`, `UpdateItem` and `DeleteItem` with and without the parameter, on a `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` on `GetItem` with and without the parameter, on a `BatchWriteItem` and on a `TransactGetItems` `Get`. `dynamodb:ReturnConsumedCapacity` on the other requests follows those, and a projection written as `AttributesToGet` follows the rule measured for `ProjectionExpression`. `ALL_PROJECTED_ATTRIBUTES` on a read of an index follows the DynamoDB API reference, which gives it as the default when a `Query` or `Scan` of an index has neither `Select` nor `ProjectionExpression`; it was not measured. + ### Resource ARNs Resources are identified by ARN: `arn:aws:dynamodb:::table/`. Wildcard matching (`*`, `?`) in policy Resource fields follows AWS IAM conventions. `NotResource` is also supported. From 8b95c6ac52df9c7dcec6caaae5c14730fac57b49 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sun, 1 Nov 2026 20:08:36 +0000 Subject: [PATCH 15/21] docs(server): refine condition-key default notes after review Documentation and comment edits for the dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity defaults. No behaviour changes: single_table, batch_table and transact_item are untouched and no service measurement was added. Security model and upgrade manual: - Label every stated-but-unmeasured row as not measured: a Scan with a projection, a TransactGetItems Get with a projection, a ConditionCheck leaving dynamodb:ReturnValues absent, AttributesToGet read as a projection, and dynamodb:ReturnConsumedCapacity on the operations beyond those measured. - Correct the condition key table so the dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity rows name the requests that hold each default rather than every write or item request. - Scope the StringNotEquals and Null breaking-change bullets to the requests that now hold each key, and note that GetItem, BatchGetItem and TransactGetItems hold dynamodb:Select though they take no Select parameter, while BatchWriteItem, ConditionCheck and SearchVectors still leave their key absent. - Add the breaking-change shapes the entry had missed: an Allow gated on StringEqualsIfExists over dynamodb:Select SPECIFIC_ATTRIBUTES, which matches the Developer Guide's Example 3; an Allow gated on StringEquals over dynamodb:Select ALL_PROJECTED_ATTRIBUTES, which matches Example 5; and the bare negated operator forms alongside the IfExists forms. State the general rule that a condition the absent key satisfied is now evaluated against the default value. - Note that dynamodb:ReturnValues reflects the ReturnValues parameter alone and not ReturnValuesOnConditionCheckFailure, so an Allow gated on NONE grants a write whose failed condition can still return the stored item. parameter_keys.rs: - The module doc no longer describes a key the operation does not take holding a stray request member. - The given doc states that a null Select or projection member reads as absent, while the engine refuses a null ReturnValues or ReturnConsumedCapacity. - parameter_keys/tests.rs comments the null ReturnValues case as the default path. --- crates/server/src/parameter_keys.rs | 37 ++++++++++++++--------- crates/server/src/parameter_keys/tests.rs | 4 +++ docs/manuals/07-upgrade-manual.md | 14 ++++++--- docs/manuals/10-security-model.md | 10 +++--- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/crates/server/src/parameter_keys.rs b/crates/server/src/parameter_keys.rs index 512604d3f..b3d7333b7 100644 --- a/crates/server/src/parameter_keys.rs +++ b/crates/server/src/parameter_keys.rs @@ -7,7 +7,8 @@ //! Each key holds the value of the request parameter it is named after. When a //! request that takes the parameter leaves it out, the service evaluates the //! value the operation then uses, so the key is present whether or not the -//! caller spells that value out (measured 2026-11-01): +//! caller spells that value out. The list after the bullets says which values +//! were measured on 2026-11-01 and which follow them: //! //! - `dynamodb:Select` on `GetItem`, `Query`, `Scan`, each table of a //! `BatchGetItem` and each `Get` of a `TransactGetItems`: @@ -27,21 +28,26 @@ //! //! Measured on 2026-11-01: `dynamodb:Select` on `GetItem` and `Query` with and //! without a projection, on `Scan` without one and with `Select` `COUNT`, on -//! `Query` with `Select` `COUNT`, and on `BatchGetItem` and a `TransactGetItems` -//! `Get` with and without a projection; `dynamodb:ReturnValues` on `PutItem`, -//! `UpdateItem` and `DeleteItem` with and without the parameter, on a -//! `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a +//! `Query` with `Select` `COUNT`, on a `BatchGetItem` with and without a +//! projection, and on a `TransactGetItems` `Get` without one; `dynamodb:ReturnValues` +//! on `PutItem`, `UpdateItem` and `DeleteItem` with and without the parameter, +//! on a `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a //! `TransactWriteItems`; `dynamodb:ReturnConsumedCapacity` on `GetItem` with and //! without the parameter, on a `BatchWriteItem` and on a `TransactGetItems` //! `Get`. `dynamodb:ReturnConsumedCapacity` on the other item operations follows -//! those. A read that projects with `AttributesToGet` is treated as one with a -//! `ProjectionExpression`, which the engine reads it as; that was not measured. -//! A `Query` or `Scan` of an index with no projection and no `Select` holds -//! `ALL_PROJECTED_ATTRIBUTES`, the default the DynamoDB API reference gives for -//! a read of an index; that was not measured either. +//! those and was not measured. A read that projects with `AttributesToGet` is +//! treated as one with a `ProjectionExpression`, which the engine reads it as; +//! that was not measured. `SPECIFIC_ATTRIBUTES` on a `Scan` with a projection +//! follows the `Query` and `GetItem` rows and was not measured, and a +//! `TransactGetItems` `Get` with a projection was measured only against a `Deny` +//! on `ALL_ATTRIBUTES`, which it passed, so its `SPECIFIC_ATTRIBUTES` value +//! follows the `BatchGetItem` and `GetItem` rows and was not measured directly. +//! A `ConditionCheck` leaving `dynamodb:ReturnValues` absent follows the +//! transaction write items and was not measured. A `Query` or `Scan` of an index +//! with no projection and no `Select` holds `ALL_PROJECTED_ATTRIBUTES`, the +//! default the DynamoDB API reference gives for a read of an index; that was not +//! measured either. //! -//! A key the operation does not take holds the request's parameter as given, -//! as before: a `Select` member in a `PutItem` reaches the policy as written. //! The batch and transaction targets carry only the keys their operation takes. use serde_json::Value; @@ -113,8 +119,11 @@ pub(crate) fn transact_item(sub: &str, item: &Value, request: &Value) -> Paramet } } -/// The request's own value of `parameter`, when it is a string. A `null` is -/// absent, as the engine reads it. +/// The request's own value of `parameter`, when it is a string; `None` for a +/// missing member or a `null`. The engine reads a `null` `Select` or projection +/// member as absent, and refuses a `null` `ReturnValues` or +/// `ReturnConsumedCapacity` with a `SerializationException`, so such a request +/// is rejected before this value decides anything. fn given(request: &Value, parameter: &str) -> Option { request .get(parameter) diff --git a/crates/server/src/parameter_keys/tests.rs b/crates/server/src/parameter_keys/tests.rs index 74a6baa68..f3f859083 100644 --- a/crates/server/src/parameter_keys/tests.rs +++ b/crates/server/src/parameter_keys/tests.rs @@ -164,6 +164,10 @@ fn query_and_scan_keep_a_given_select() { #[test] fn single_item_writes_default_return_values_to_none() { + // The null case exercises the default path only: `given` treats a null + // member as absent, so the key holds NONE. A real request carrying + // `"ReturnValues": null` is refused by the engine with a + // SerializationException before authorization, so it never reaches here. for operation in ["PutItem", "UpdateItem", "DeleteItem"] { for input in [read(), with(&read(), &json!({"ReturnValues": null}))] { assert_eq!( diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index 23f00077f..de946b065 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -683,17 +683,21 @@ A `DeleteItem` and a read with no projection hold only the names the request giv ### v0.1.15: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter -**What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same (measured 2026-11-01 on the requests the security model manual lists; the index value follows the DynamoDB API reference and was not measured). A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. +**What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. **Who is affected.** Any deployment with a policy statement that names one of these keys in a condition. Statements of these shapes now decide differently: - A `Deny` gated on `StringEquals` with `dynamodb:Select` `ALL_ATTRIBUTES` now refuses a `GetItem`, `Query` or `Scan` with no projection and no `Select`, a `BatchGetItem` table with no projection and a `TransactGetItems` `Get` with no projection. Before, it refused none of them, and each returned the whole item. It does not refuse a `Query` or `Scan` of an index with no projection, which holds `ALL_PROJECTED_ATTRIBUTES`; list that value as well to refuse those reads. -- A `Deny` gated on `StringNotEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` refused every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covers, and every `Query` or `Scan` without `Select`, because the absent key satisfied it. It now stops refusing reads that project, and still refuses reads without a projection. +- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` refused every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covers, and every `Query` or `Scan` without `Select`, because the absent key satisfied it. It now stops refusing reads that project, and still refuses reads without a projection. - An `Allow` gated on `StringEquals` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` now grants a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems` that projects without `Select`. Before, it granted only a `Query` or `Scan` that set `Select` to `SPECIFIC_ATTRIBUTES`, and a `GetItem` that carried a `Select` member of that value, whatever it projected. -- An `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` now grants a `PutItem`, `UpdateItem` or `DeleteItem` without `ReturnValues`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`. Before, it refused them. It still refuses a `BatchWriteItem`, which leaves the key absent, as on DynamoDB (measured 2026-11-01). +- An `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` now grants a `PutItem`, `UpdateItem` or `DeleteItem` without `ReturnValues`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`. Before, it refused them. It still refuses a `BatchWriteItem`, which leaves the key absent, as on DynamoDB (measured 2026-11-01). `dynamodb:ReturnValues` reflects the `ReturnValues` parameter alone and not `ReturnValuesOnConditionCheckFailure`, so such an `Allow` also grants a write that sets `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, and that write returns the stored item in the error when its condition fails. - An `Allow` gated on `StringEquals` with `dynamodb:ReturnConsumedCapacity` `NONE` now grants the `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction requests its `Action` covers that carry no `ReturnConsumedCapacity`. Before, it refused them. -- A `Deny` gated on `StringNotEqualsIfExists` with `dynamodb:ReturnValues` or `dynamodb:ReturnConsumedCapacity` `NONE` refused every request without the parameter, because the absent key satisfied it. It now stops refusing the requests that take the parameter and omit it. -- `Null` with value `true` on one of these keys matched every request without the parameter. It now matches none of the requests that take the parameter, so a `Deny` gated on it stops refusing them and an `Allow` gated on it stops granting them. `Null` with value `false` now matches them. +- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:ReturnValues` `NONE` refused every request its `Action` covered, because the absent key satisfied it. It now refuses only the requests that leave the key absent: it stops refusing a `PutItem`, `UpdateItem` or `DeleteItem` that omits `ReturnValues` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`, and still refuses a `BatchWriteItem` and a `ConditionCheck`. The same shape over `dynamodb:ReturnConsumedCapacity` `NONE` stops refusing a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` and each table or item of a batch or transaction that omits the parameter, and still refuses a `SearchVectors` that omits it, which leaves the key absent. +- `Null` with value `true` on one of these keys matched every request without the parameter. It now matches only the requests that still leave the key absent: for `dynamodb:Select` it no longer matches a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems`; for `dynamodb:ReturnValues` it no longer matches a `PutItem`, `UpdateItem`, `DeleteItem` or transaction write item, and still matches a `BatchWriteItem` and a `ConditionCheck`; for `dynamodb:ReturnConsumedCapacity` it no longer matches a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` or a table or item of a batch or transaction, and still matches a `SearchVectors` that omits the parameter. A `Deny` gated on it stops refusing the requests that now hold the key, an `Allow` gated on it stops granting them, and `Null` with value `false` now matches them. +- An `Allow` gated on `StringEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` granted every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covered, and every `Query` or `Scan` without `Select`, because the absent key satisfied the `IfExists`. This is the Developer Guide's Example 3, which grants `UpdateItem`, `GetItem`, `Query`, `BatchGetItem` and `Scan` with a `ForAllValues:StringEquals` attribute list and a `StringEqualsIfExists` on `Select`. It now grants only reads that project, and refuses a read with no projection, which returns the whole item and so would return attributes outside the list. Check any policy copied from Example 3. +- An `Allow` gated on `StringEquals` with `dynamodb:Select` `ALL_PROJECTED_ATTRIBUTES`, the Developer Guide's Example 5 second policy, now grants a `Query` or `Scan` of an index with no projection and no `Select`, which before it refused. This depends on the index default, which follows the API reference and was not measured. + +An `IfExists` or negated condition that the absent key satisfied is now evaluated against the default value, and a positive condition on a default value (`ALL_ATTRIBUTES`, `ALL_PROJECTED_ATTRIBUTES` or `NONE`) now matches the requests that hold it. No catalog migration is required. Before upgrading, check the policies of each account for the statements above. diff --git a/docs/manuals/10-security-model.md b/docs/manuals/10-security-model.md index 70110c417..632d32e03 100755 --- a/docs/manuals/10-security-model.md +++ b/docs/manuals/10-security-model.md @@ -112,8 +112,8 @@ Set operators and `IfExists` can be combined: `ForAllValues:StringEqualsIfExists | `dynamodb:LeadingKeys` | Multi-valued string | Partition key values being accessed | | `dynamodb:Attributes` | Multi-valued string | Top-level names of the attributes the request names (see below) | | `dynamodb:Select` | String | The `Select` parameter value, or the value the read uses without one (see below) | -| `dynamodb:ReturnValues` | String | The `ReturnValues` parameter value, or `NONE` on a write without one (see below) | -| `dynamodb:ReturnConsumedCapacity` | String | The `ReturnConsumedCapacity` parameter value, or `NONE` on an item request without one (see below) | +| `dynamodb:ReturnValues` | String | The `ReturnValues` parameter value, or `NONE` on a `PutItem`, `UpdateItem`, `DeleteItem` or transaction write item without one (see below) | +| `dynamodb:ReturnConsumedCapacity` | String | The `ReturnConsumedCapacity` parameter value, or `NONE` on the item, batch and transaction requests the section below lists | | `dynamodb:FullTableScan` | Boolean | `true` for Scan operations | | `dynamodb:EnclosingOperation` | String | The enclosing operation for batch/transact sub-operations | @@ -150,7 +150,7 @@ extenddb supports the same access control patterns as real AWS IAM: The names come from the request alone. A `DeleteItem` holds the key attributes of the item it deletes, not the item's other attributes, and a read with no projection holds the key attributes while it returns every attribute of the item, so a `Deny` on `ForAnyValue:StringEquals` over an attribute blocks neither (measured on DynamoDB 2026-10-31). As in DynamoDB, a policy on `dynamodb:Attributes` does not stop an `UpdateItem` from copying a denied attribute's value into another attribute through a `SET` value: under a `Deny` on `ssn`, `SET #n = ssn` with `#n` for `name` is allowed, and a projection of `name` then returns the value (measured on DynamoDB 2026-11-01). A `Scan` holds the table's key attributes when it names no attribute, a `Select COUNT` `Scan` included, so a `Null` condition on the key does not match it: a `Deny` of `Scan` gated on `Null` with value `true` refuses neither a `Scan` with no projection nor a projected one, and an `Allow` of `Scan` gated on it grants neither a `Scan` with no projection nor a `Select COUNT` `Scan` (measured on DynamoDB 2026-11-01). A bare `StringNotEquals` on the key does not match whether the key is present or absent, so an `Allow` gated on it grants nothing; on DynamoDB it granted no `Scan`, whether the `Scan` named an attribute or not (measured 2026-11-01). An allowlist written as an `Allow` on `ForAllValues:StringEquals` must list the key attributes of the table and of every index the statement covers, because every item request other than `SearchVectors` holds them. The key is absent on a `SearchVectors` with no `ProjectionExpression` and, on a table that does not exist, on a `GetItem` or transaction `Get` that names no attribute outside its `Key`, a `Query` that names none outside its key condition and a `Scan` that names none. -A `Deny` on `dynamodb:Attributes` does not stop `ReturnValues` `ALL_OLD` or `ReturnValuesOnConditionCheckFailure` `ALL_OLD` from returning the whole stored item, the denied attribute included. Under a `Deny` on `ssn`, an `UpdateItem` of `name` with `ReturnValues` `ALL_OLD` returns the old item with `ssn` in it, and a `PutItem` whose condition fails and a `TransactWriteItems` `ConditionCheck` that fails, each with `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, return the stored item with `ssn` in the error. DynamoDB does the same (measured 2026-11-01). ExtendDB returns the whole stored item in each of these cases, and on a `PutItem` with `ReturnValues` `ALL_OLD`, whose answer on DynamoDB was not measured. The IAM guide's guard against `ReturnValues` returning attributes is a condition on `dynamodb:ReturnValues`, which holds `NONE` on a write that carries no `ReturnValues` (see below). +A `Deny` on `dynamodb:Attributes` does not stop `ReturnValues` `ALL_OLD` or `ReturnValuesOnConditionCheckFailure` `ALL_OLD` from returning the whole stored item, the denied attribute included. Under a `Deny` on `ssn`, an `UpdateItem` of `name` with `ReturnValues` `ALL_OLD` returns the old item with `ssn` in it, and a `PutItem` whose condition fails and a `TransactWriteItems` `ConditionCheck` that fails, each with `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, return the stored item with `ssn` in the error. DynamoDB does the same (measured 2026-11-01). ExtendDB returns the whole stored item in each of these cases, and on a `PutItem` with `ReturnValues` `ALL_OLD`, whose answer on DynamoDB was not measured. The IAM guide's guard against `ReturnValues` returning attributes is a condition on `dynamodb:ReturnValues`, which holds `NONE` on a write that carries no `ReturnValues` (see below). That key reflects the `ReturnValues` parameter alone, not `ReturnValuesOnConditionCheckFailure`, so an `Allow` gated on `dynamodb:ReturnValues` `NONE` grants a write that carries no `ReturnValues`, and such a write whose condition fails still returns the stored item in the error when it sets `ReturnValuesOnConditionCheckFailure` `ALL_OLD`. These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item attributes of `PutItem`; the target of a `SET`, `REMOVE`, `ADD` or `DELETE` action and the attributes a `SET` value reads; `ConditionExpression` on `PutItem`, `DeleteItem`, a `ConditionCheck` and a transaction `Delete`; `FilterExpression` on `Query` and `Scan`; `Expected` on `PutItem`, `AttributeUpdates`, `ScanFilter` and `QueryFilter`; `AttributesToGet` on `GetItem`, `Query`, `Scan` and `BatchGetItem`; the table's key attributes on `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `DeleteRequest`; projected document paths; reads with no projection; `DeleteItem`; a transaction `Update`; the `BatchGetItem`, `TransactGetItems`, `BatchWriteItem` and `TransactWriteItems` forms of a projection and a put; and the evaluation of a `BatchWriteItem` once per table. `ProjectionExpression` on `Query` and `Scan`, `Expected` and `ConditionExpression` on `UpdateItem`, and `Expected` on `DeleteItem` follow the rule measured for the same parameter on the other operations. The key attributes of the queried or scanned index follow the IAM guide's rule that a policy on `dynamodb:Attributes` lists the primary key and index key attributes of the table and of every secondary index the policy covers; they were not measured. Whether `ExclusiveStartKey` adds names of its own cannot be told on DynamoDB, because a valid start key holds only key attributes, which the key already holds. @@ -163,11 +163,11 @@ These entries were measured on DynamoDB on 2026-10-31 and 2026-11-01: the item a | `dynamodb:ReturnValues` | `PutItem`, `UpdateItem` and `DeleteItem`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems` | `NONE` | | `dynamodb:ReturnConsumedCapacity` | `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` and `Scan`, and each table or item of a `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` or `TransactWriteItems` | `NONE` | -A `Query` or `Scan` that carries `Select` holds it as given. `GetItem`, `BatchGetItem` and `TransactGetItems` take no `Select` parameter, so their value comes from the projection alone, and a `Select` member in one of them is not read. The `Put`, `Update` and `Delete` items of a transaction take no `ReturnValues` parameter and always hold `NONE`. A `BatchWriteItem` and a `ConditionCheck` leave `dynamodb:ReturnValues` absent. On a single-table request, a key the operation does not take, such as `dynamodb:ReturnValues` on a `GetItem`, holds the request's member of that name when it has one and is absent otherwise. A batch or transaction request carries one `ReturnConsumedCapacity`, and each of its tables or items is evaluated with it. +A `Query` or `Scan` that carries `Select` holds it as given. `GetItem`, `BatchGetItem` and `TransactGetItems` take no `Select` parameter, so their value comes from the projection alone, and a `Select` member in one of them is not read. The `Put`, `Update` and `Delete` items of a transaction take no `ReturnValues` parameter and always hold `NONE`. A `BatchWriteItem` leaves `dynamodb:ReturnValues` absent; a `ConditionCheck` is treated the same, which was not measured. A batch or transaction request carries one `ReturnConsumedCapacity`, and each of its tables or items is evaluated with it. So a `Deny` on `dynamodb:Select` `ALL_ATTRIBUTES` refuses a read with no projection, which returns the whole item, and an `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` grants a write that carries no `ReturnValues`. A `Query` or `Scan` of an index with no projection holds `ALL_PROJECTED_ATTRIBUTES`, even when the index projects every attribute, so a statement meant to refuse whole-item reads of a table and its indexes lists both `ALL_ATTRIBUTES` and `ALL_PROJECTED_ATTRIBUTES`. -These values were measured on DynamoDB on 2026-11-01: `dynamodb:Select` on `GetItem` and `Query` with and without a projection, on `Scan` without one and with `Select` `COUNT`, on `Query` with `Select` `COUNT`, and on a `BatchGetItem` and a `TransactGetItems` `Get` with and without a projection; `dynamodb:ReturnValues` on `PutItem`, `UpdateItem` and `DeleteItem` with and without the parameter, on a `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` on `GetItem` with and without the parameter, on a `BatchWriteItem` and on a `TransactGetItems` `Get`. `dynamodb:ReturnConsumedCapacity` on the other requests follows those, and a projection written as `AttributesToGet` follows the rule measured for `ProjectionExpression`. `ALL_PROJECTED_ATTRIBUTES` on a read of an index follows the DynamoDB API reference, which gives it as the default when a `Query` or `Scan` of an index has neither `Select` nor `ProjectionExpression`; it was not measured. +These values were measured on DynamoDB on 2026-11-01: `dynamodb:Select` on `GetItem` and `Query` with and without a projection, on `Scan` without one and with `Select` `COUNT`, on `Query` with `Select` `COUNT`, on a `BatchGetItem` with and without a projection, and on a `TransactGetItems` `Get` without one; `dynamodb:ReturnValues` on `PutItem`, `UpdateItem` and `DeleteItem` with and without the parameter, on a `BatchWriteItem`, and on the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` on `GetItem` with and without the parameter, on a `BatchWriteItem` and on a `TransactGetItems` `Get`. `dynamodb:ReturnConsumedCapacity` on the other requests follows those, and a projection written as `AttributesToGet` follows the rule measured for `ProjectionExpression`; neither was measured directly. `SPECIFIC_ATTRIBUTES` on a `Scan` with a projection follows the `Query` and `GetItem` rows and was not measured, and a `TransactGetItems` `Get` with a projection was measured only against a `Deny` on `ALL_ATTRIBUTES`, which it passed, so its `SPECIFIC_ATTRIBUTES` value follows the `BatchGetItem` and `GetItem` rows. A `ConditionCheck` leaving `dynamodb:ReturnValues` absent was not measured. `ALL_PROJECTED_ATTRIBUTES` on a read of an index follows the DynamoDB API reference, which gives it as the default when a `Query` or `Scan` of an index has neither `Select` nor `ProjectionExpression`; it was not measured. ### Resource ARNs From 5c213abf13f65b508b66352400cb52f02f025d1f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Mon, 2 Nov 2026 09:54:41 +0000 Subject: [PATCH 16/21] fix(server): set condition keys only on operations that take them single_table seeded dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity from the request body for every operation and then adjusted them per operation, so an operation that does not take a parameter still carried a member of that name from the body. A policy that grants a broad action gated only on such a key could then be satisfied by adding that member to a request the operation does not model: a Select on a write or a control-plane call, a ReturnValues on a read. Build the keys per operation, the rule batch_table and transact_item already follow: Select on GetItem, Query and Scan; ReturnValues on PutItem, UpdateItem and DeleteItem; ReturnConsumedCapacity on the operations that report it; none on a control-plane operation. SearchVectors keeps its ReturnConsumedCapacity, read as given. The measured per-operation defaults do not change; two unit tests that pinned the old copying behaviour now assert the key is absent. --- crates/server/src/parameter_keys.rs | 56 +++++++++++++++-------- crates/server/src/parameter_keys/tests.rs | 47 ++++++++++++------- 2 files changed, 66 insertions(+), 37 deletions(-) diff --git a/crates/server/src/parameter_keys.rs b/crates/server/src/parameter_keys.rs index b3d7333b7..5c7692590 100644 --- a/crates/server/src/parameter_keys.rs +++ b/crates/server/src/parameter_keys.rs @@ -48,7 +48,8 @@ //! default the DynamoDB API reference gives for a read of an index; that was not //! measured either. //! -//! The batch and transaction targets carry only the keys their operation takes. +//! On a single table and in a batch or transaction, an operation carries only +//! the keys it takes; a member an operation does not take is not read. use serde_json::Value; @@ -73,28 +74,43 @@ pub(crate) struct ParameterKeys { pub return_consumed_capacity: Option, } -/// The keys of a request authorized once, against one resource. +/// The keys of a request authorized once, against one resource. Each key is +/// set only on the operations whose request carries that parameter, the rule +/// `batch_table` and `transact_item` follow: a member an operation does not +/// take (a `Select` or `ReturnValues` on a write, any of the three on a +/// control-plane request) is not read, so a condition on that key cannot be +/// satisfied by a member added to a request the operation does not model. pub(crate) fn single_table(operation: &str, input: &Value) -> ParameterKeys { - let mut keys = ParameterKeys { - select: given(input, "Select"), - return_values: given(input, "ReturnValues"), - return_consumed_capacity: given(input, "ReturnConsumedCapacity"), - }; match operation { - "GetItem" => keys.select = Some(projection_select(input).to_owned()), - "Query" | "Scan" => { - keys.select = Some( - keys.select - .unwrap_or_else(|| range_read_select(input).to_owned()), - ); - } - "PutItem" | "UpdateItem" | "DeleteItem" => { - keys.return_values = Some(keys.return_values.unwrap_or_else(|| NONE.to_owned())); - } - _ => return keys, + "GetItem" => ParameterKeys { + select: Some(projection_select(input).to_owned()), + return_values: None, + return_consumed_capacity: Some(consumed_capacity(input)), + }, + "Query" | "Scan" => ParameterKeys { + select: Some( + given(input, "Select").unwrap_or_else(|| range_read_select(input).to_owned()), + ), + return_values: None, + return_consumed_capacity: Some(consumed_capacity(input)), + }, + "PutItem" | "UpdateItem" | "DeleteItem" => ParameterKeys { + select: None, + return_values: Some(given(input, "ReturnValues").unwrap_or_else(|| NONE.to_owned())), + return_consumed_capacity: Some(consumed_capacity(input)), + }, + // SearchVectors takes ReturnConsumedCapacity but no Select or + // ReturnValues. Its value is not measured, so it is read as given and + // left absent when the request omits it, as before. + "SearchVectors" => ParameterKeys { + select: None, + return_values: None, + return_consumed_capacity: given(input, "ReturnConsumedCapacity"), + }, + // Control-plane and every other single-resource operation take none of + // the three. + _ => ParameterKeys::default(), } - keys.return_consumed_capacity = Some(consumed_capacity(input)); - keys } /// The keys of one table's part of a `BatchGetItem` or `BatchWriteItem`: diff --git a/crates/server/src/parameter_keys/tests.rs b/crates/server/src/parameter_keys/tests.rs index f3f859083..e794f6ea6 100644 --- a/crates/server/src/parameter_keys/tests.rs +++ b/crates/server/src/parameter_keys/tests.rs @@ -228,41 +228,44 @@ fn item_operations_default_return_consumed_capacity_to_none() { // -- keys an operation does not take ------------------------------------------- #[test] -fn a_key_the_operation_does_not_take_holds_the_member_as_given() { +fn a_key_the_operation_does_not_take_is_absent() { + // A member an operation does not take must not set its key. Otherwise a + // policy that grants a broad action gated only on, say, dynamodb:Select + // could be satisfied by a Select member added to a write or a DeleteTable. let all = json!({"Select": "COUNT", "ReturnValues": "ALL_OLD"}); for operation in ["PutItem", "UpdateItem", "DeleteItem"] { assert_eq!(select_of(operation, &read()), None, "{operation}"); assert_eq!( - select_of(operation, &with(&read(), &all)).as_deref(), - Some("COUNT") + select_of(operation, &with(&read(), &all)), + None, + "{operation} does not read a Select member" ); } for operation in ["GetItem", "Query", "Scan"] { - assert_eq!(single_table(operation, &read()).return_values, None); assert_eq!( - single_table(operation, &with(&read(), &all)) - .return_values - .as_deref(), - Some("ALL_OLD"), + single_table(operation, &read()).return_values, + None, "{operation}" ); + assert_eq!( + single_table(operation, &with(&read(), &all)).return_values, + None, + "{operation} does not read a ReturnValues member" + ); } } #[test] -fn other_operations_get_no_default() { +fn other_operations_read_only_the_keys_they_take() { let given = json!({ "TableName": "t", "Select": "COUNT", "ReturnValues": "ALL_OLD", "ReturnConsumedCapacity": "TOTAL", }); - for operation in [ - "DescribeTable", - "CreateTable", - "ListTables", - "SearchVectors", - ] { + // Control-plane operations take none of the three keys, so no member of + // those names is read, with or without it in the body. + for operation in ["DescribeTable", "CreateTable", "ListTables", "DeleteTable"] { assert_eq!( single_table(operation, &json!({"TableName": "t"})), ParameterKeys::default(), @@ -270,10 +273,20 @@ fn other_operations_get_no_default() { ); assert_eq!( single_table(operation, &given), - keys(Some("COUNT"), Some("ALL_OLD"), Some("TOTAL")), - "{operation}" + ParameterKeys::default(), + "{operation} reads no member" ); } + // SearchVectors takes ReturnConsumedCapacity, but not Select or ReturnValues. + assert_eq!( + single_table("SearchVectors", &json!({"TableName": "t"})), + ParameterKeys::default() + ); + assert_eq!( + single_table("SearchVectors", &given), + keys(None, None, Some("TOTAL")), + "SearchVectors reads only ReturnConsumedCapacity" + ); } // -- batch and transaction parts ------------------------------------------------- From e8a0cc5bf2e7cdcdbf97ef67d4b727745b7de75e Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:22:11 +0000 Subject: [PATCH 17/21] fix(server): import json! in the request_helpers tests Main's tests in crates/server/src/request_helpers.rs call json! in seven places and import it in the tests module. The first #732 commits on this branch removed every json! call from the tests and the import with them. The merge kept that removal and main's calls, so the tests module had no json! in scope. This commit restores the import as main has it. --- crates/server/src/request_helpers.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/server/src/request_helpers.rs b/crates/server/src/request_helpers.rs index 20f30ea32..9c03aeaf2 100755 --- a/crates/server/src/request_helpers.rs +++ b/crates/server/src/request_helpers.rs @@ -538,6 +538,7 @@ pub(crate) async fn authorize_request( mod tests { use super::*; use extenddb_core::types::TableKeyInfo; + use serde_json::json; fn sample_key_info() -> TableKeyInfo { TableKeyInfo { From c67b241ed24485881e6046da123befb15714e4c0 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:24:32 +0000 Subject: [PATCH 18/21] docs: remove the second copy of the dynamodb:Attributes upgrade entry Main holds the final #732 text of the upgrade manual entry on dynamodb:Attributes, ahead of the console metrics and StreamArn entries. This branch carried the first #732 text of the same entry after the StreamArn entry. The merge kept both copies. They sit at different lines, so they did not conflict. This commit removes the copy from this branch, and the entry for the dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity values now follows the StreamArn entry. --- docs/manuals/07-upgrade-manual.md | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index d216d12b9..6c724f3fe 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -1137,22 +1137,6 @@ No catalog migration is required. DescribeStream and GetShardIterator now refuse a client-built `StreamArn` with the `arn:aws-cn:` or `arn:aws-us-gov:` partition with `ValidationException` "Invalid StreamArn", also on a deployment whose `server.region` is a China or GovCloud region. The server issues every stream ARN as `arn:aws:`, so a `StreamArn` taken from DescribeTable or ListStreams is not affected. No catalog migration is required. -### v0.1.15: `dynamodb:Attributes` holds the attributes every item request names - -**What changed.** `dynamodb:Attributes` now holds the top-level names of the attributes a request names on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, `SearchVectors`, `BatchGetItem`, `BatchWriteItem`, `TransactGetItems` and `TransactWriteItems`, as the security model manual lists them per operation. Before this release the key was read from `ProjectionExpression` alone, on single-table requests only, and a projected document path was compared as the whole path: the key was absent on `PutItem`, `UpdateItem` and `DeleteItem`, on reads that use `AttributesToGet`, and on every batch and transaction request, and a projection of `secret.a` held `secret.a` instead of `secret`. The key now also holds the attributes a condition or filter expression reads, the target of each update expression action, the legacy `Expected`, `AttributeUpdates`, `QueryFilter` and `ScanFilter` names, `ExclusiveStartKey` (whether it adds names of its own cannot be told on the service), and on every item operation other than `SearchVectors` the key attributes of the table and of the index a `Query` or `Scan` names (the index key attributes follow the IAM guide's rule, not measured). It does not hold the attributes a `SET` value reads. - -**Who is affected.** Any deployment with a policy statement that names `dynamodb:Attributes` in a condition. On a table that exists, every item request other than `SearchVectors` now carries the key, holding at least the table's key attributes, so a `ForAllValues` condition no longer holds vacuously on an absent key, a `ForAnyValue` condition can match, `Null` with value `true` stops matching and `Null` with value `false` starts matching. A `SearchVectors` still holds only the names in its `ProjectionExpression` and leaves the key absent when it has none. Statements of these shapes now decide differently: - -- A `Deny` gated on a `ForAnyValue:String*` operator that matches an attribute's name (`StringEquals`, `StringLike`, `StringEqualsIgnoreCase`) now applies to writes that name the attribute, to update actions that target it, to `AttributesToGet` reads, to projections of a path inside it, to conditions and filters that read it, and to batch and transaction requests. It did not apply to them before. -- A `Deny` gated on a negated `ForAnyValue:String*` operator (`StringNotEquals`, `StringNotLike`, `StringNotEqualsIgnoreCase`) now applies to every item request whose names fall outside its list, the key attributes included, so the list must hold the key attributes of the table and of every index the statement covers (the index key attributes follow the IAM guide's rule, not measured). A list that omits them now refuses every `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction request on the table. -- An `Allow` gated on `ForAllValues:StringEquals` used to grant every request that left the key absent, whatever attributes it named, because the absent key satisfied it. It now grants only when every name the request holds is in the list. A list that omits the table's key attributes stops granting `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests (measured on DynamoDB 2026-11-01 for `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `BatchWriteItem` `DeleteRequest`); add the key attributes of the table and of every index the statement covers (the index key attributes follow the IAM guide's rule, not measured). -- An `Allow` gated on a `ForAnyValue:String*` operator now grants every item request its `Action` covers that holds a name the list matches. Before, it granted only a single-table request whose `ProjectionExpression` named one. A `BatchWriteItem` is evaluated once per table, with the union of that table's item names, so an `Allow` of `BatchWriteItem` over `["name"]` grants a batch that puts `{pk, name}` and `{pk, ssn}` on one table and writes both items, and refuses a batch that puts `{pk, ssn}` alone, as on DynamoDB (measured 2026-11-01). A list that holds a key attribute grants every item request other than `SearchVectors` that the `Action` covers, because each holds the table's key attributes; DynamoDB puts them in the key on `GetItem`, `Query`, `Scan`, `UpdateItem`, `DeleteItem` and a `BatchWriteItem` `DeleteRequest` (measured 2026-11-01). An `Allow` of `GetItem` over `["pk"]`, on a table keyed on `pk`, grants every `GetItem` on the table, one with no projection included, which returns the whole item. -- An `Allow` of `PutItem` gated on `ForAnyValue:StringNotEquals` over `["ssn"]` granted no `PutItem` before, and now grants one of `{pk, ssn}`, because `pk` is outside the list. An `Allow` of `PutItem` gated on `ForAllValues:StringNotEquals` over `["ssn"]` granted every `PutItem` before, because the absent key satisfied it, and now refuses one of `{pk, ssn}` and grants one of `{pk, name}`. -- `Null` with value `true` matched every request that left the key absent before. A statement gated on it no longer matches any item request except a `SearchVectors` with no `ProjectionExpression` and, on a table that does not exist, a `GetItem` or transaction `Get` that names no attribute outside its `Key`, a `Query` that names none outside its key condition and a `Scan` that names none. A `Deny` gated on it stops refusing the other item requests and an `Allow` gated on it stops granting them, as on DynamoDB (measured 2026-11-01 on `GetItem` and `Scan`). -- `Null` with value `false` matched only a single-table request with a `ProjectionExpression` before, and now matches every item request that carries the key. An `Allow` gated on it, written to require that a read names its attributes, now grants each of these requests that its `Action` covers, a read with no projection and its whole item included, and a `Deny` gated on it now refuses them. - -A `DeleteItem` and a read with no projection hold only the names the request gives, not those of the stored item, so a `Deny` on an attribute blocks neither deleting an item that holds it nor reading the item with no projection, as on DynamoDB (measured 2026-10-31). Nor does it stop an `UpdateItem` from copying the attribute's value into another attribute through a `SET` value such as `SET #n = ssn`, as on DynamoDB (measured 2026-11-01). No catalog migration is required. Before upgrading, check the policies of each account for the statements above. - ### v0.1.15: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter **What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. From 68756de182ae2e57e961e80600661a401644bf2f Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Wed, 18 Nov 2026 13:06:15 +0000 Subject: [PATCH 19/21] docs(upgrade): give the condition key default entry release v1.0.0 Release 0.1.15 shipped without this branch, and main (#1107) now carries version 1.0.0. The upgrade manual entry of this branch on the dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity defaults said v0.1.15, so its heading now says v1.0.0. --- docs/manuals/07-upgrade-manual.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index 9edb2c360..3ea473462 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -1217,7 +1217,7 @@ No catalog migration is required. DescribeStream and GetShardIterator now refuse a client-built `StreamArn` with the `arn:aws-cn:` or `arn:aws-us-gov:` partition with `ValidationException` "Invalid StreamArn", also on a deployment whose `server.region` is a China or GovCloud region. The server issues every stream ARN as `arn:aws:`, so a `StreamArn` taken from DescribeTable or ListStreams is not affected. No catalog migration is required. -### v0.1.15: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter +### v1.0.0: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter **What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. From fe9923adb659e305d4ca14e03320bd385c038328 Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:18:49 +0000 Subject: [PATCH 20/21] docs(upgrade): move the condition key default entry to v1.1.0 Release 1.0.0 shipped without this branch. The upgrade manual entry of this branch on the dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity defaults said v1.0.0, so its heading now says v1.1.0. --- docs/manuals/07-upgrade-manual.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index 3ea473462..8ce15ac1f 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -1217,7 +1217,7 @@ No catalog migration is required. DescribeStream and GetShardIterator now refuse a client-built `StreamArn` with the `arn:aws-cn:` or `arn:aws-us-gov:` partition with `ValidationException` "Invalid StreamArn", also on a deployment whose `server.region` is a China or GovCloud region. The server issues every stream ARN as `arn:aws:`, so a `StreamArn` taken from DescribeTable or ListStreams is not affected. No catalog migration is required. -### v1.0.0: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter +### v1.1.0: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter **What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. From 551d76cc9b0f48503cbf0a745a388fc2c962524c Mon Sep 17 00:00:00 2001 From: Lee Hannigan <29015211+LeeroyHannigan@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:46:45 +0000 Subject: [PATCH 21/21] docs(upgrade): list the condition key default entry with the v1.1.0 entries Behavior Changes by Release lists the newest release first. The entry of this branch on the dynamodb:Select, dynamodb:ReturnValues and dynamodb:ReturnConsumedCapacity defaults sat after the v0.1.15 entries, at the end of the section. It now follows the v1.1.0 entry of #398 at the top of the section. The entry keeps its text. --- docs/manuals/07-upgrade-manual.md | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/manuals/07-upgrade-manual.md b/docs/manuals/07-upgrade-manual.md index dc4ef5674..d894593b4 100755 --- a/docs/manuals/07-upgrade-manual.md +++ b/docs/manuals/07-upgrade-manual.md @@ -399,6 +399,26 @@ deployment, even when no catalog migration is required. No catalog migration or configuration change is required. +### v1.1.0: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter + +**What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. + +**Who is affected.** Any deployment with a policy statement that names one of these keys in a condition. Statements of these shapes now decide differently: + +- A `Deny` gated on `StringEquals` with `dynamodb:Select` `ALL_ATTRIBUTES` now refuses a `GetItem`, `Query` or `Scan` with no projection and no `Select`, a `BatchGetItem` table with no projection and a `TransactGetItems` `Get` with no projection. Before, it refused none of them, and each returned the whole item. It does not refuse a `Query` or `Scan` of an index with no projection, which holds `ALL_PROJECTED_ATTRIBUTES`; list that value as well to refuse those reads. +- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` refused every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covers, and every `Query` or `Scan` without `Select`, because the absent key satisfied it. It now stops refusing reads that project, and still refuses reads without a projection. +- An `Allow` gated on `StringEquals` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` now grants a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems` that projects without `Select`. Before, it granted only a `Query` or `Scan` that set `Select` to `SPECIFIC_ATTRIBUTES`, and a `GetItem` that carried a `Select` member of that value, whatever it projected. +- An `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` now grants a `PutItem`, `UpdateItem` or `DeleteItem` without `ReturnValues`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`. Before, it refused them. It still refuses a `BatchWriteItem`, which leaves the key absent, as on DynamoDB (measured 2026-11-01). `dynamodb:ReturnValues` reflects the `ReturnValues` parameter alone and not `ReturnValuesOnConditionCheckFailure`, so such an `Allow` also grants a write that sets `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, and that write returns the stored item in the error when its condition fails. +- An `Allow` gated on `StringEquals` with `dynamodb:ReturnConsumedCapacity` `NONE` now grants the `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction requests its `Action` covers that carry no `ReturnConsumedCapacity`. Before, it refused them. +- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:ReturnValues` `NONE` refused every request its `Action` covered, because the absent key satisfied it. It now refuses only the requests that leave the key absent: it stops refusing a `PutItem`, `UpdateItem` or `DeleteItem` that omits `ReturnValues` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`, and still refuses a `BatchWriteItem` and a `ConditionCheck`. The same shape over `dynamodb:ReturnConsumedCapacity` `NONE` stops refusing a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` and each table or item of a batch or transaction that omits the parameter, and still refuses a `SearchVectors` that omits it, which leaves the key absent. +- `Null` with value `true` on one of these keys matched every request without the parameter. It now matches only the requests that still leave the key absent: for `dynamodb:Select` it no longer matches a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems`; for `dynamodb:ReturnValues` it no longer matches a `PutItem`, `UpdateItem`, `DeleteItem` or transaction write item, and still matches a `BatchWriteItem` and a `ConditionCheck`; for `dynamodb:ReturnConsumedCapacity` it no longer matches a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` or a table or item of a batch or transaction, and still matches a `SearchVectors` that omits the parameter. A `Deny` gated on it stops refusing the requests that now hold the key, an `Allow` gated on it stops granting them, and `Null` with value `false` now matches them. +- An `Allow` gated on `StringEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` granted every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covered, and every `Query` or `Scan` without `Select`, because the absent key satisfied the `IfExists`. This is the Developer Guide's Example 3, which grants `UpdateItem`, `GetItem`, `Query`, `BatchGetItem` and `Scan` with a `ForAllValues:StringEquals` attribute list and a `StringEqualsIfExists` on `Select`. It now grants only reads that project, and refuses a read with no projection, which returns the whole item and so would return attributes outside the list. Check any policy copied from Example 3. +- An `Allow` gated on `StringEquals` with `dynamodb:Select` `ALL_PROJECTED_ATTRIBUTES`, the Developer Guide's Example 5 second policy, now grants a `Query` or `Scan` of an index with no projection and no `Select`, which before it refused. This depends on the index default, which follows the API reference and was not measured. + +An `IfExists` or negated condition that the absent key satisfied is now evaluated against the default value, and a positive condition on a default value (`ALL_ATTRIBUTES`, `ALL_PROJECTED_ATTRIBUTES` or `NONE`) now matches the requests that hold it. + +No catalog migration is required. Before upgrading, check the policies of each account for the statements above. + ### v1.0.0: Cassandra reads and writes at `LOCAL_QUORUM` **What changed.** The Cassandra backend runs its reads and writes at `LOCAL_QUORUM`. 0.1.15 ran many of them at `ONE`, the driver's default, in each area: items (the reads of GetItem, Query and Scan among them), transactions, TTL, streams, the catalog, IAM and `init`. A read with `ConsistentRead` `false` also runs at `LOCAL_QUORUM`. A lightweight transaction runs Paxos at `LOCAL_SERIAL` and commits at `LOCAL_QUORUM`. 0.1.15 ran many of its lightweight transactions at those levels already, and others, among them the admin user creation and most IAM writes, at the driver's defaults, `SERIAL` and `ONE`. Batches ran at `LOCAL_QUORUM` before and still do. No setting changes these levels (#945). @@ -1228,26 +1248,6 @@ No catalog migration is required. DescribeStream and GetShardIterator now refuse a client-built `StreamArn` with the `arn:aws-cn:` or `arn:aws-us-gov:` partition with `ValidationException` "Invalid StreamArn", also on a deployment whose `server.region` is a China or GovCloud region. The server issues every stream ARN as `arn:aws:`, so a `StreamArn` taken from DescribeTable or ListStreams is not affected. No catalog migration is required. -### v1.1.0: `dynamodb:Select`, `dynamodb:ReturnValues` and `dynamodb:ReturnConsumedCapacity` hold a value when the request omits the parameter - -**What changed.** Before this release the three keys held the request's `Select`, `ReturnValues` and `ReturnConsumedCapacity` parameters only, on requests with a single authorization target. A request that left the parameter out left the key absent, and the tables of a batch and the items of a transaction never carried the keys. Each key now holds the value the operation uses when the request omits the parameter, on single-table requests, on each table of a batch and on each transaction item, as the security model manual lists: `dynamodb:Select` is `SPECIFIC_ATTRIBUTES` on a read with a projection, `ALL_ATTRIBUTES` on a read of a table without one, and `ALL_PROJECTED_ATTRIBUTES` on a `Query` or `Scan` of an index without one; `dynamodb:ReturnValues` is `NONE` on `PutItem`, `UpdateItem`, `DeleteItem` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`; and `dynamodb:ReturnConsumedCapacity` is `NONE` on `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan` and the batch and transaction requests. DynamoDB does the same on the rows the security model manual marks measured on 2026-11-01; the others listed there (the index default, a projected `Scan`, a projected `TransactGetItems` `Get`, a `ConditionCheck`, `AttributesToGet` as a projection, and `dynamodb:ReturnConsumedCapacity` on the operations beyond those measured) follow the measured rows and the API reference and were not measured. A `Select` member in a `GetItem`, which takes no `Select` parameter, no longer sets `dynamodb:Select`: the key comes from the projection, as it does on a `BatchGetItem` table and a `TransactGetItems` `Get`. - -**Who is affected.** Any deployment with a policy statement that names one of these keys in a condition. Statements of these shapes now decide differently: - -- A `Deny` gated on `StringEquals` with `dynamodb:Select` `ALL_ATTRIBUTES` now refuses a `GetItem`, `Query` or `Scan` with no projection and no `Select`, a `BatchGetItem` table with no projection and a `TransactGetItems` `Get` with no projection. Before, it refused none of them, and each returned the whole item. It does not refuse a `Query` or `Scan` of an index with no projection, which holds `ALL_PROJECTED_ATTRIBUTES`; list that value as well to refuse those reads. -- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` refused every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covers, and every `Query` or `Scan` without `Select`, because the absent key satisfied it. It now stops refusing reads that project, and still refuses reads without a projection. -- An `Allow` gated on `StringEquals` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` now grants a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems` that projects without `Select`. Before, it granted only a `Query` or `Scan` that set `Select` to `SPECIFIC_ATTRIBUTES`, and a `GetItem` that carried a `Select` member of that value, whatever it projected. -- An `Allow` gated on `StringEquals` with `dynamodb:ReturnValues` `NONE` now grants a `PutItem`, `UpdateItem` or `DeleteItem` without `ReturnValues`, and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`. Before, it refused them. It still refuses a `BatchWriteItem`, which leaves the key absent, as on DynamoDB (measured 2026-11-01). `dynamodb:ReturnValues` reflects the `ReturnValues` parameter alone and not `ReturnValuesOnConditionCheckFailure`, so such an `Allow` also grants a write that sets `ReturnValuesOnConditionCheckFailure` `ALL_OLD`, and that write returns the stored item in the error when its condition fails. -- An `Allow` gated on `StringEquals` with `dynamodb:ReturnConsumedCapacity` `NONE` now grants the `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query`, `Scan`, batch and transaction requests its `Action` covers that carry no `ReturnConsumedCapacity`. Before, it refused them. -- A `Deny` gated on `StringNotEquals` or `StringNotEqualsIfExists` with `dynamodb:ReturnValues` `NONE` refused every request its `Action` covered, because the absent key satisfied it. It now refuses only the requests that leave the key absent: it stops refusing a `PutItem`, `UpdateItem` or `DeleteItem` that omits `ReturnValues` and the `Put`, `Update` and `Delete` items of a `TransactWriteItems`, and still refuses a `BatchWriteItem` and a `ConditionCheck`. The same shape over `dynamodb:ReturnConsumedCapacity` `NONE` stops refusing a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` and each table or item of a batch or transaction that omits the parameter, and still refuses a `SearchVectors` that omits it, which leaves the key absent. -- `Null` with value `true` on one of these keys matched every request without the parameter. It now matches only the requests that still leave the key absent: for `dynamodb:Select` it no longer matches a `GetItem`, `Query`, `Scan`, `BatchGetItem` or `TransactGetItems`; for `dynamodb:ReturnValues` it no longer matches a `PutItem`, `UpdateItem`, `DeleteItem` or transaction write item, and still matches a `BatchWriteItem` and a `ConditionCheck`; for `dynamodb:ReturnConsumedCapacity` it no longer matches a `GetItem`, `PutItem`, `UpdateItem`, `DeleteItem`, `Query` or `Scan` or a table or item of a batch or transaction, and still matches a `SearchVectors` that omits the parameter. A `Deny` gated on it stops refusing the requests that now hold the key, an `Allow` gated on it stops granting them, and `Null` with value `false` now matches them. -- An `Allow` gated on `StringEqualsIfExists` with `dynamodb:Select` `SPECIFIC_ATTRIBUTES` granted every `GetItem`, `BatchGetItem` and `TransactGetItems` its `Action` covered, and every `Query` or `Scan` without `Select`, because the absent key satisfied the `IfExists`. This is the Developer Guide's Example 3, which grants `UpdateItem`, `GetItem`, `Query`, `BatchGetItem` and `Scan` with a `ForAllValues:StringEquals` attribute list and a `StringEqualsIfExists` on `Select`. It now grants only reads that project, and refuses a read with no projection, which returns the whole item and so would return attributes outside the list. Check any policy copied from Example 3. -- An `Allow` gated on `StringEquals` with `dynamodb:Select` `ALL_PROJECTED_ATTRIBUTES`, the Developer Guide's Example 5 second policy, now grants a `Query` or `Scan` of an index with no projection and no `Select`, which before it refused. This depends on the index default, which follows the API reference and was not measured. - -An `IfExists` or negated condition that the absent key satisfied is now evaluated against the default value, and a positive condition on a default value (`ALL_ATTRIBUTES`, `ALL_PROJECTED_ATTRIBUTES` or `NONE`) now matches the requests that hold it. - -No catalog migration is required. Before upgrading, check the policies of each account for the statements above. - --- ### v0.1.15: numbers and table names are checked as DynamoDB checks them, and expression depth is limited