Repository navigation
fix(core): bound the nesting depth of update expressions - #1000
Conversation
The tests of the UpdateExpression parser move unchanged from update_parser.rs into update_parser_tests.rs, the layout the evaluator and the update evaluator use, so the parser file has room for the depth limit that follows. No code or test changes.
An UpdateExpression nests a level for each parenthesized value and each if_not_exists or list_append call. A ConditionExpression or FilterExpression nests a level for each size call read as an operand. The update parser took no depth limit: parse_update_expr in the engine and the dynamodb:Attributes reader in the server both called parse_update_from without max_expression_depth, so a SET value nested in parentheses or calls recursed once per level. The condition parser counted parentheses and NOT but not size(size(...)). A debug build of the server aborted with a stack overflow at the default size and token limits, on a SET value of 681 nested parentheses (4096 bytes) and on a condition of 1364 nested size calls. A release build was not measured. parse_update_from now takes the depth limit, and the engine, the server and the fuzz targets pass max_expression_depth. Parentheses and function calls in a SET value each count one level, and past the limit the parser refuses the expression with "Invalid UpdateExpression: expression nesting depth exceeded", the wording the condition parser uses. The condition parser counts a nested size call the same way. What the service does with these expressions has not been measured. This changes which PartiQL UPDATE statements run. PartiQL UPDATE is written as a classic UpdateExpression before it is parsed, and a nested if_not_exists step becomes two classic levels, the call and a parenthesized sum. A statement such as UPDATE t SET a = if_not_exists(a, a + if_not_exists(a, a + ... 1)) with 76 or more steps passes the PartiQL parser and ran before this change. It is now refused with a ValidationException; 75 steps still run. Nested parenthesized arithmetic counts one level per step and was accepted up to 99 steps in the same check, and the PartiQL parser refuses nested calls of 100 levels or more. Unit tests in core, engine and server nest each form to the limit and one past it, and 50,000 levels deep with raised size and token limits on a thread with a 1 MiB stack under a 10 second deadline. A wire test sends the same forms through UpdateItem, TransactWriteItems, DeleteItem and Scan, expects a ValidationException, and reads the item back unchanged. Wire tests also run a PartiQL UPDATE at 75 steps and refuse it at 76.
|
LGTM. This closes the crash from unbounded expression nesting. I confirmed that update, condition, and filter expressions nested past the limit are refused with a ValidationException, the item is left unchanged, and the server keeps serving, both at the default limits and with the size and token limits raised to 50000 levels. The base branch overflowed the stack and aborted on the same input. The part that matters most is the reader that runs before the permission check. On the base branch a caller who is denied could still crash the server with a deeply nested expression. On this branch that same caller gets an AccessDenied and the server stays up. I also checked nested map and list values, PartiQL, and key condition expressions, and none of them recurse without a bound. Two pre-existing items are out of scope for this change and already tracked. A deep ProjectionExpression document path is bounded by #597, which refuses a path of more than 32 elements. The difference between the filter operator count and the nesting depth limit is tracked in #501. |
LeeroyHannigan
left a comment
There was a problem hiding this comment.
I built this branch, ran the depth tests and the wire suite on SQLite and PostgreSQL, built main's release binary to compare, ran the mutation controls, and measured the classic and PartiQL nesting cases against DynamoDB in us-east-1.
What I ran
- The core, engine and server depth tests pass, the 50,000-level tests run on a 1 MiB stack without overflowing, and the 10 wire tests pass on SQLite (default and raised limits) and PostgreSQL. fmt, clippy 1.97 with
-D warningsand the 1.88 build are clean; CI is 64 of 64, and the only earlier red was the #993 flake, which is unrelated. - The limit closes a real problem: with raised limits, a caller with no policy crashes main's release server with a deep expression (
stack overflow, aborting), and this branch answers AccessDeniedException and stays up. The limit is worth having.
Before merge
- The limit refuses PartiQL
UPDATEstatements the service runs. A nestedSET fresh = if_not_exists(fresh, 1 + if_not_exists(...))is translated to classic text at two levels per step and checked againstmax_expression_depth(150), so it is refused from 76 steps. I measured DynamoDB in us-east-1: it runs this statement at 75, 76, 99 and 150 steps (freshbecomes 76, 77, 100, 151). The PartiQL parser on this branch admits it to 99 steps, and the translated-text check now refuses it from 76. So the branch narrows the accepted range on a form the service runs well past it, and widens issue #885. The PartiQL translation already raises the length and token limits for translated text; it should raise the depth the same way, so the translation admits what the PartiQL parser and the service allow. Inline comment onupdate_parser.rs. - A test gap lets sibling calls add up. Removing the depth decrement after a
sizecall (parser.rs), or theleave()after anif_not_existsorlist_appendcall (update_parser.rs), passes the whole core, engine and server suite. Nothing checks that calls side by side do not accumulate depth, so a regression there would refuse a condition of 151size(a)>:vterms, or an update of 151if_not_existsactions, which are shallow. Please add both as accepted cases. Inline comment onupdate_parser.rs.
Minor
- Code size: the depth check and its message appear three times in
parser.rs(NOT, parenthesis, and the newsizebranch),update_parser.rsadds a secondNestingmechanism for the same check, andDEFAULT_MAX_DEPTHinupdate_parser.rs:31repeatsdefault_max_expression_depth()inlimits/mod.rs. One guard for both parsers, taking the default fromLimitsConfig::default(), would remove the copies (code-size rule 2). - The wire item is created once for the module and never reset, so when an update refusal regresses the accepted update changes the item and the three ConditionExpression tests fail on the read-back rather than on their own check. Reset the item per case and make the eight refusal cases one parametrized table (code-size rule 4).
- Body: "the second commit only moves the parser tests" has the order backwards; the move is the first commit (
cadc43b1b) and the fix is the second (0a725806a). The "1364 nestedsizecalls at the default limits" figure is 8,190 bytes, over the 4,096-byte default, so both main and this branch refuse it for size before parsing; the deepestsizeform the default holds is 681. - The "caller denied everything, server stays up" row has no wire test. The property holds on a probe, and the server unit test pins it, but a wire case with a no-policy principal would cover it end to end.
Checked and measured
- The classic nested forms the limit also caps are not a parity change. I measured DynamoDB refusing
SET qty=(qty+(...:v))at every depth with a syntax error (token: "+", near: "qty+("), and nestedlist_append(list_append(...))with "The function is not allowed to be used this way", 150 through 301 levels alike. The service does not run these forms at any depth, so capping them is not a regression against it. ExtendDB accepting parenthesized SET arithmetic at all is a pre-existing extension beyond the service, out of scope here. - The move commit is lossless (21 test functions before and after, same names). The engine, server and fuzz callers all pass the depth through. The change shares no file with #998 or #999. It overlaps #558 (a 300-operator cap on the same expressions) and #399/#507 (which move the same update-parser tests another way); whichever of those merges first, this one rebases onto it.
| /// Enter one level, refusing the expression past `max_depth`. | ||
| fn enter(&mut self) -> Result<(), DynamoDbError> { | ||
| self.depth += 1; | ||
| if self.depth > self.max_depth { |
There was a problem hiding this comment.
This bound reaches PartiQL through the engine: a PartiQL UPDATE is translated to classic text, and a nested SET fresh = if_not_exists(fresh, 1 + if_not_exists(...)) costs two levels per step, so it is refused from 76 steps. I measured DynamoDB in us-east-1 running that statement at 75, 76, 99 and 150 steps (fresh becomes 76, 77, 100, 151), and the PartiQL parser on this branch admits it to 99 steps, so the translated-text check now refuses a range the service runs and the parser accepts. This widens #885. The translation already raises the length and token limits for translated text (translation_limits); please raise the depth there the same way, so a translated expression may nest as deep as the PartiQL parser allows, and the classic UpdateExpression path keeps 150.
| parse_set_function_call(tokens, pos) | ||
| nesting.enter()?; | ||
| let call = parse_set_function_call(tokens, pos, nesting)?; | ||
| nesting.leave(); |
There was a problem hiding this comment.
Removing this leave() after a call, or the matching *depth -= 1; after a size call in parser.rs, passes the whole core, engine and server suite. Nothing checks that calls side by side do not accumulate depth. With either change an update of 151 if_not_exists actions (SET a0=if_not_exists(a0,:v), a1=...), or a condition of 151 size(a)>:v terms joined by AND, would be refused, and both are shallow. Please add an accepted case for each so the per-call reset is pinned.
I posted this after the PR was already approved, so it missed the review window. The findings are tracked in #1057, assigned to the author, as a follow-up. Dismissing so it does not block the approved change.
crates/engine/src/expression_helpers.rs keeps the new tests of both sides at the end of its test module. This branch adds the request-map test for parse_key_condition_expr. Main (#1000) adds the depth-limit tests and their raised() and on_small_stack() helpers.
The merge of main at 381c8c7 gives parse_update_sections a depth limit. parse_update_expr_checked in crates/engine/src/expression_helpers.rs now passes max_expression_depth. UpdateItem, TransactWriteItems and the PartiQL item depth check parse through that function, so they keep the limit from main (#1000). One depth test from main in the same file calls two functions that this branch changed. It now calls parse_condition_expr_with, and it passes include_length to parse_optional_filter. The semantic tests pass the default depth limit to parse_update_sections.
The merge of main at 381c8c7 gives parse_update_sections a depth limit. parse_update_expr_checked in crates/engine/src/expression_helpers.rs now passes max_expression_depth. UpdateItem, TransactWriteItems and the PartiQL item depth check parse through that function, so they keep the limit from main (#1000). One depth test from main in the same file calls two functions that this branch changed. It now calls parse_condition_expr_with, and it passes include_length to parse_optional_filter. The semantic tests pass the default depth limit to parse_update_sections.
Moves the workspace version from 0.1.15 to 1.0.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 14 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (version and the five platform pins), the four licence-notice files (regenerated with cargo-about 0.9.0), and the example `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. RELEASED_THROUGH stays 12: migration 12 is still the last one, so 1.0.0 carries the catalog of 0.1.15 and freezes nothing new. Upgrade manual: - Behavior Changes by Release gets a v1.0.0 entry for #945: the Cassandra backend reads and writes at LOCAL_QUORUM where 0.1.15 ran many of them at ONE, so a request fails with HTTP 500 while fewer than a quorum of its keyspace's replicas are up. It says what to do before upgrading: a replication factor of 3, as cassandra-setup.md describes. - Current Status, the General Upgrade Procedure and the Rollback Procedure cover 0.1.15 to 1.0.0, with that Cassandra step. Current Status says which of its paragraphs are for 0.1.14 and earlier and that the backup store notes apply to every deployment. - A v0.1.15 entry for the request validation of #619, #620, #830 and #1000, which had none. It says the expression depth limit is ExtendDB's own. - The rollback to 0.1.14 or earlier states what a binary of those releases does on a migrated catalog, as measured on PostgreSQL with 0.1.14 and 0.1.10: exit status 1, "invalid catalog version '12'". docs/versioning.md: the 0.1.x releases get no more fixes. The Docker Hub pages name 0.1.15 as the first keyless-signed release. docker/README-mongodb.md ran extenddb/extenddb-mongodb:0.1.8; it now runs X.Y.Z, as the other image READMEs do. doc_version_literals now reads docker/ as well, so a stale image tag there fails it. It reports each image tag once, and skips another project's image tag and a link to a release. The npm launcher test now checks that the launcher refuses the release line before an X.0.0 launcher, which it skipped at patch 0. Signed-off-by: Scott Robinson <robinnsc@amazon.com>
Moves the workspace version from 0.1.15 to 1.0.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 14 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (version and the five platform pins), the four licence-notice files (regenerated with cargo-about 0.9.0), and the example `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. RELEASED_THROUGH stays 12: migration 12 is still the last one, so 1.0.0 carries the catalog of 0.1.15 and freezes nothing new. Upgrade manual: - Behavior Changes by Release gets a v1.0.0 entry for #945: the Cassandra backend reads and writes at LOCAL_QUORUM where 0.1.15 ran many of them at ONE, so a request fails with HTTP 500 while fewer than a quorum of its keyspace's replicas are up. It says what to do before upgrading: a replication factor of 3, as cassandra-setup.md describes. - A v1.0.0 entry for #1003. Without key_file the stored format and the catalog key stay as they are, and 0.1.15 and 1.0.0 each read the access keys the other wrote. serve and the backup commands refuse a catalog key that is not 32 bytes in base64, where 0.1.15 started and failed every signed request with HTTP 500; the entry says how to check the key before upgrading. The key moves into a locked key store, with a warning when the memlock limit is too low. key_file and the encryption-key commands are opt-in and link to the admin guide. - The rollback section of #1003 now gives the PostgreSQL refusal of 0.1.15 on a switched catalog: the encrypted connection string row stops it with exit status 1 before the missing key does. - Current Status, the General Upgrade Procedure and the Rollback Procedure cover 0.1.15 to 1.0.0, with the Cassandra step, the key check, and the rollback after encryption-key switch. Current Status says which of its paragraphs are for 0.1.14 and earlier and that the backup store notes apply to every deployment. - A v0.1.15 entry for the request validation of #619, #620, #830 and #1000, which had none. It says the expression depth limit is ExtendDB's own. - The rollback to 0.1.14 or earlier states what a binary of those releases does on a migrated catalog, as measured on PostgreSQL with 0.1.14 and 0.1.10: exit status 1, "invalid catalog version '12'". docs/versioning.md: the 0.1.x releases get no more fixes. The Docker Hub pages name 0.1.15 as the first keyless-signed release. docker/README-mongodb.md ran extenddb/extenddb-mongodb:0.1.8; it now runs X.Y.Z, as the other image READMEs do. doc_version_literals now reads docker/ as well, so a stale image tag there fails it. It reports each image tag once, and skips another project's image tag and a link to a release. The npm launcher test now checks that the launcher refuses the release line before an X.0.0 launcher, which it skipped at patch 0. Signed-off-by: Scott Robinson <robinnsc@amazon.com>
Four files conflicted. - crates/core/src/expression/mod.rs: main adds the test_timeout module next to the syntax_error module of this branch, and the merge keeps both modules. - crates/core/src/expression/parser.rs: main passes the nesting depth to parse_operand and parse_function_call, and this branch passes the source text to them. Both functions now take both values. A size call in an operand still enters one nesting level. - crates/core/src/expression/update_parser.rs: main limits the nesting depth of the update parser (#1000) and moves its tests to update_parser_tests.rs. The parse functions now take the source text and the nesting state. parse_update calls parse_update_from with the default depth of 150, so the two entry points give the same errors. The test changes of this branch move to update_parser_tests.rs, and the upd_msg helper passes the default depth. - crates/engine/src/expression_helpers.rs: each side adds tests at the end of the test module, and the merge keeps the tests of both sides. The unit tests of extenddb-core and extenddb-engine pass on the merge.
Moves the workspace version from 0.1.15 to 1.0.0 on every surface that carries it: the root Cargo.toml version and its 13 exact pins, the 14 workspace entries in Cargo.lock (no third-party entry changes), packaging/npm/package.json (version and the five platform pins), the four licence-notice files (regenerated with cargo-about 0.9.0), and the example `extenddb version` and `serve` output in docs/getting-started.md and docs/manuals/04-quickstart-setup-guide.md. RELEASED_THROUGH stays 12: migration 12 is still the last one, so 1.0.0 carries the catalog of 0.1.15 and freezes nothing new. Upgrade manual: - Behavior Changes by Release gets a v1.0.0 entry for ExtendDB#945: the Cassandra backend reads and writes at LOCAL_QUORUM where 0.1.15 ran many of them at ONE, so a request fails with HTTP 500 while fewer than a quorum of its keyspace's replicas are up. It says what to do before upgrading: a replication factor of 3, as cassandra-setup.md describes. - A v1.0.0 entry for ExtendDB#1003. Without key_file the stored format and the catalog key stay as they are, and 0.1.15 and 1.0.0 each read the access keys the other wrote. serve and the backup commands refuse a catalog key that is not 32 bytes in base64, where 0.1.15 started and failed every signed request with HTTP 500; the entry says how to check the key before upgrading. The key moves into a locked key store, with a warning when the memlock limit is too low. key_file and the encryption-key commands are opt-in and link to the admin guide. - The rollback section of ExtendDB#1003 now gives the PostgreSQL refusal of 0.1.15 on a switched catalog: the encrypted connection string row stops it with exit status 1 before the missing key does. - Current Status, the General Upgrade Procedure and the Rollback Procedure cover 0.1.15 to 1.0.0, with the Cassandra step, the key check, and the rollback after encryption-key switch. Current Status says which of its paragraphs are for 0.1.14 and earlier and that the backup store notes apply to every deployment. - A v0.1.15 entry for the request validation of ExtendDB#619, ExtendDB#620, ExtendDB#830 and ExtendDB#1000, which had none. It says the expression depth limit is ExtendDB's own. - The rollback to 0.1.14 or earlier states what a binary of those releases does on a migrated catalog, as measured on PostgreSQL with 0.1.14 and 0.1.10: exit status 1, "invalid catalog version '12'". docs/versioning.md: the 0.1.x releases get no more fixes. The Docker Hub pages name 0.1.15 as the first keyless-signed release. docker/README-mongodb.md ran extenddb/extenddb-mongodb:0.1.8; it now runs X.Y.Z, as the other image READMEs do. doc_version_literals now reads docker/ as well, so a stale image tag there fails it. It reports each image tag once, and skips another project's image tag and a link to a release. The npm launcher test now checks that the launcher refuses the release line before an X.0.0 launcher, which it skipped at patch 0. Signed-off-by: Scott Robinson <robinnsc@amazon.com>
What this changes
The
UpdateExpressionparser had no nesting limit. ASETvalue nested in parentheses or calls recursed once per level, and aConditionExpressionorFilterExpressionwith nestedsize(size(...))did the same. A debug build of the server aborted with a stack overflow at the default size and token limits:SETvaluesizecalls in a conditionA release build was not measured.
The parser now takes
max_expression_depth, and the engine, the server'sdynamodb:Attributesreader and the fuzz targets pass it. Each parenthesis and eachif_not_existsorlist_appendcall in aSETvalue counts one level. A nestedsizecall in a condition counts the same way. Past the limit the request fails withValidationException: Invalid UpdateExpression: expression nesting depth exceeded.The second commit only moves the parser tests into their own file, with no code change, to make room for the limit.
Workflow
Use cases handled
ValidationException, item unchangedAccessDeniedException, server stays upBehavior change for PartiQL UPDATE
This is a behavior change. Please read it before approving.
PartiQL UPDATE is written as a classic
UpdateExpressionbefore it is parsed. A nestedif_not_existsstep becomes two classic levels: the call and a parenthesized sum. So a statement such asUPDATE t SET a = if_not_exists(a, a + if_not_exists(a, a + ... 1))ValidationException(measured at 76, 80 and 99)Nested parenthesized arithmetic counts one level per step and was accepted up to 99 steps in the same check. The PartiQL parser itself refuses nested calls at 100 levels or more. That last claim comes from an earlier review report and was not re-measured for this PR.
Wire tests pin the boundary: 75 steps run, 76 are refused, and the item is unchanged after the refusal.
Failure cases and limits
Testing
tests/test_expression_depth_limit.py(10 tests): the forms above through UpdateItem, TransactWriteItems, DeleteItem and Scan, plus the PartiQL boundary. All pass on SQLite at default and raised limits, and on PostgreSQL at default limits. Each test reads the item back to show it is unchanged and the server still answers.cargo fmt --check(workspace and fuzz crate), clippy 1.97 with-D warnings, build on 1.88: clean.