Skip to content

fix(core): bound the nesting depth of update expressions - #1000

Merged
eliseong merged 2 commits into
mainfrom
fix/update-expression-depth
Nov 16, 2026
Merged

eliseong merged 2 commits into
mainfrom
fix/update-expression-depth

Conversation

@navareamol

Copy link
Copy Markdown
Collaborator

What this changes

The UpdateExpression parser had no nesting limit. A SET value nested in parentheses or calls recursed once per level, and a ConditionExpression or FilterExpression with nested size(size(...)) did the same. A debug build of the server aborted with a stack overflow at the default size and token limits:

  • 681 nested parentheses (4096 bytes) in a SET value
  • 1364 nested size calls in a condition

A release build was not measured.

The parser now takes max_expression_depth, and the engine, the server's dynamodb:Attributes reader and the fuzz targets pass it. Each parenthesis and each if_not_exists or list_append call in a SET value counts one level. A nested size call in a condition counts the same way. Past the limit the request fails with ValidationException: 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

UpdateItem / TransactWriteItems / DeleteItem / Scan / PartiQL UPDATE
   -> parse expression with max_expression_depth
   -> too deep?  ValidationException, nothing is written
   -> otherwise  evaluate as before
Same limit is used when the server reads attributes for a permission check,
so a deep expression cannot crash the server before access is decided.

Use cases handled

Case Result
Normal expressions Unchanged
Nesting exactly at the limit Accepted
One level past the limit ValidationException, item unchanged
Very deep nesting (50,000 levels, raised size and token limits) Refused, server stays up
Caller denied everything, deep expression AccessDeniedException, server stays up

Behavior change for PartiQL UPDATE

This is a behavior change. Please read it before approving.

PartiQL UPDATE is written as a classic UpdateExpression before it is parsed. A nested if_not_exists step becomes two classic levels: the call and a parenthesized sum. So a statement such as

UPDATE t SET a = if_not_exists(a, a + if_not_exists(a, a + ... 1))

  • with 75 steps still runs (measured)
  • with 76 or more steps used to run, and is now refused with 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

  • Not measured on live DynamoDB. What the real service does with deeply nested update expressions, and what its message is, is unknown. The tests check the error type.
  • The stack overflow was measured on a debug build only.
  • Not run: Cassandra, MongoDB, and a release-build server.
  • Wire tests at raised limits ran on SQLite only. PostgreSQL ran at default limits.

Testing

  • Unit tests in core (1146 pass), engine (870) and server (506): each form is nested to the limit and one past it, and 50,000 levels deep on a thread with a 1 MiB stack under a 10 second deadline.
  • Wire tests in 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.
  • Negative control: with the update depth check disabled, the groups, calls and PartiQL boundary tests fail, and the far-past test aborts with a stack overflow.
  • cargo fmt --check (workspace and fuzz crate), clippy 1.97 with -D warnings, build on 1.88: clean.
  • Hostile review: depth probes at default and raised limits on SQLite and PostgreSQL across update, condition and filter paths. No findings that block.

Amol Navare added 2 commits November 14, 2026 11:42
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.
@navareamol navareamol added this to the 1.0 milestone Nov 16, 2026
@navareamol navareamol added v1 Needed for the 1.0 release release-blocker Must ship for its milestone's release labels Nov 16, 2026
@navareamol navareamol self-assigned this Nov 16, 2026
@eliseong eliseong assigned eliseong and unassigned navareamol Nov 16, 2026
@eliseong

Copy link
Copy Markdown
Collaborator

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.

@eliseong
eliseong added this pull request to the merge queue Nov 16, 2026

@LeeroyHannigan LeeroyHannigan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 warnings and 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

  1. The limit refuses PartiQL UPDATE statements the service runs. A nested SET fresh = if_not_exists(fresh, 1 + if_not_exists(...)) is translated to classic text at two levels per step and checked against max_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 (fresh becomes 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 on update_parser.rs.
  2. A test gap lets sibling calls add up. Removing the depth decrement after a size call (parser.rs), or the leave() after an if_not_exists or list_append call (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 151 size(a)>:v terms, or an update of 151 if_not_exists actions, which are shallow. Please add both as accepted cases. Inline comment on update_parser.rs.

Minor

  • Code size: the depth check and its message appear three times in parser.rs (NOT, parenthesis, and the new size branch), update_parser.rs adds a second Nesting mechanism for the same check, and DEFAULT_MAX_DEPTH in update_parser.rs:31 repeats default_max_expression_depth() in limits/mod.rs. One guard for both parsers, taking the default from LimitsConfig::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 nested size calls 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 deepest size form 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 nested list_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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@LeeroyHannigan
LeeroyHannigan dismissed their stale review November 16, 2026 22:32

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.

Merged via the queue into main with commit 381c8c7 Nov 16, 2026
77 of 79 checks passed
@eliseong
eliseong deleted the fix/update-expression-depth branch November 16, 2026 22:32
LeeroyHannigan added a commit that referenced this pull request Nov 16, 2026
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.
LeeroyHannigan added a commit that referenced this pull request Nov 16, 2026
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.
LeeroyHannigan added a commit that referenced this pull request Nov 16, 2026
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.
robinnsc added a commit that referenced this pull request Nov 18, 2026
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>
robinnsc added a commit that referenced this pull request Nov 18, 2026
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>
LeeroyHannigan added a commit that referenced this pull request Nov 18, 2026
docs/manuals/07-upgrade-manual.md keeps the v0.1.15 entry that main adds for the request validation of #619, #620, #830 and #1000. The ClientRequestToken entry of this branch follows it.
LeeroyHannigan added a commit that referenced this pull request Nov 20, 2026
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.
amrith pushed a commit to amrith/extenddb that referenced this pull request Nov 20, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-blocker Must ship for its milestone's release security v1 Needed for the 1.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants