Repository navigation
fix(core): refuse an update whose result nests beyond 32 levels #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1fbc671
8fd65a0
564e314
c05cbc6
e1f2c92
38ef819
81768cc
f29f8e1
f7e3da1
e47a2ef
b14e5c8
e91da2e
2b1d169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // Copyright 2026 ExtendDB contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Nesting depth of the attributes an update expression writes. | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| use crate::error::DynamoDbError; | ||
| use crate::types::AttributeValue; | ||
| use crate::validation::nests_beyond_item_limit; | ||
|
|
||
| use super::ast::UpdateAction; | ||
| use super::resolver::{ExpressionMaps, resolve_element_name}; | ||
|
|
||
| /// Refuse an updated item whose SET or ADD targets now nest beyond the limit. | ||
| /// | ||
| /// The limit is the item limit of `crate::validation`: 31 M or L wrappers | ||
| /// around a scalar is 32 levels, the service's limit. | ||
| /// | ||
| /// Only the top-level attributes a SET or an ADD writes are checked, each | ||
| /// once however many actions write into it. ADD is checked because it | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After #596, ADD no longer creates missing maps on main. Merged with 222d80a, the request in |
||
| /// creates the missing maps on its path. REMOVE and DELETE cannot deepen a | ||
| /// value, and an attribute the update does not touch is left alone, so an | ||
| /// item stored deeper than the limit before this check existed stays | ||
| /// updatable through its other attributes. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// `ValidationException` `Nesting Levels have exceeded supported limits`, the | ||
| /// service's text for UpdateItem and for a TransactWriteItems Update | ||
| /// cancellation reason (measured 2026-10-25). | ||
| pub(super) fn validate_written_nesting_depth( | ||
| actions: &[UpdateAction], | ||
| item: &BTreeMap<String, AttributeValue>, | ||
| maps: &ExpressionMaps, | ||
| ) -> Result<(), DynamoDbError> { | ||
| let mut written = Vec::new(); | ||
| for action in actions { | ||
| let (UpdateAction::Set { path, .. } | UpdateAction::Add { path, .. }) = action else { | ||
| continue; | ||
| }; | ||
| let Some(first) = path.first() else { | ||
| continue; | ||
| }; | ||
| let name = resolve_element_name(first, maps)?; | ||
| if !written.contains(&name) { | ||
| written.push(name); | ||
| } | ||
| } | ||
| for name in &written { | ||
| if item.get(name.as_ref()).is_some_and(nests_beyond_item_limit) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add an entry under "Behavior Changes by Release" in |
||
| return Err(DynamoDbError::ValidationException( | ||
| "Nesting Levels have exceeded supported limits".to_owned(), | ||
| )); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::collections::{BTreeMap, HashMap}; | ||
|
|
||
| use super::*; | ||
| use crate::expression::{parse_update, tokenize}; | ||
|
|
||
| fn nmap(wrappers: usize) -> AttributeValue { | ||
| let mut v = AttributeValue::S("leaf".to_owned()); | ||
| for _ in 0..wrappers { | ||
| v = AttributeValue::M(BTreeMap::from([("k".to_owned(), v)])); | ||
| } | ||
| v | ||
| } | ||
|
|
||
| fn check(expr: &str, item: &BTreeMap<String, AttributeValue>) -> Result<(), DynamoDbError> { | ||
| let actions = parse_update(&tokenize(expr).unwrap()).unwrap(); | ||
| // The names map is keyed without the leading `#`. | ||
| let maps = ExpressionMaps::new( | ||
| HashMap::from([("m".to_owned(), "m".to_owned())]), | ||
| HashMap::new(), | ||
| ); | ||
| validate_written_nesting_depth(&actions, item, &maps) | ||
| } | ||
|
|
||
| fn is_nesting_error(result: Result<(), DynamoDbError>) -> bool { | ||
| matches!( | ||
| result, | ||
| Err(DynamoDbError::ValidationException(m)) | ||
| if m == "Nesting Levels have exceeded supported limits" | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn thirty_one_wrappers_pass_and_thirty_two_are_refused() { | ||
| let at = BTreeMap::from([("m".to_owned(), nmap(31))]); | ||
| assert!(check("SET #m = :v", &at).is_ok()); | ||
| let over = BTreeMap::from([("m".to_owned(), nmap(32))]); | ||
| assert!(is_nesting_error(check("SET #m.k = :v", &over))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_list_counts_as_a_wrapper_and_every_branch_is_checked() { | ||
| let mut deep = AttributeValue::S("leaf".to_owned()); | ||
| for _ in 0..32 { | ||
| deep = AttributeValue::L(vec![AttributeValue::N("1".to_owned()), deep]); | ||
| } | ||
| let item = BTreeMap::from([("m".to_owned(), deep)]); | ||
| assert!(is_nesting_error(check("SET #m[0] = :v", &item))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn attributes_the_update_does_not_set_are_not_checked() { | ||
| let item = BTreeMap::from([ | ||
| ("m".to_owned(), nmap(40)), | ||
| ("x".to_owned(), AttributeValue::S("y".to_owned())), | ||
| ]); | ||
| assert!(check("SET x = :v", &item).is_ok()); | ||
| assert!(check("REMOVE #m.k", &item).is_ok()); | ||
| assert!(is_nesting_error(check("SET #m.k = :v", &item))); | ||
| assert!(check("DELETE #m.k :v", &item).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_add_target_is_checked() { | ||
| let over = BTreeMap::from([("m".to_owned(), nmap(32))]); | ||
| assert!(is_nesting_error(check("ADD #m.k :v", &over))); | ||
| let at = BTreeMap::from([("m".to_owned(), nmap(31))]); | ||
| assert!(check("ADD #m.k :v", &at).is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn two_actions_into_one_attribute_are_checked_as_one() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional. With the de-duplication removed this test still passes, so it does not hold the "each once" in the doc comment. |
||
| let over = BTreeMap::from([("m".to_owned(), nmap(32))]); | ||
| assert!(is_nesting_error(check("SET #m.k = :v, #m.j = :v", &over))); | ||
| let at = BTreeMap::from([("m".to_owned(), nmap(31))]); | ||
| assert!(check("SET #m.k = :v, #m.j = :v ADD #m.i :v", &at).is_ok()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the depth check refuses, this sizes the statement with the path-tree sizer, which for a 31-element path counts about 200 bytes more than the service does before its depth check. With the deep SET beside a string
:b, in both orders, the service answers the nesting message for every:bup to 409,583 bytes and the size message from 409,584 (409,582 with the namebig); this head answers nesting at 409,350 and size from 409,400, on SQLite and PostgreSQL. At 409,500 bytes the service answered nesting on every send in three runs, twice with the exact shape oftest_a_statement_over_the_size_limit_is_refused_ahead_of_the_depth(tests/test_update_result_nesting_depth.py:246), so that test andupdate_evaluator_tests.rs:746assert an answer the service does not give. Main gives the same size message, so this is not a regression; only the text differs.