fix(dynamodb): tokenize UpdateExpression whitespace-insensitively (#1862)

* fix(dynamodb): tokenize UpdateExpression whitespace-insensitively

Clause dispatch keyed on the literal "SET " prefix (keyword plus a single space) in both the request pre-validation and the parser, so a newline or tab after a clause keyword was rejected with 'Syntax error; token: "SET"'. Real AWS tokenizes UpdateExpression whitespace-insensitively; collapse whitespace runs before matching keywords.

Fixes #1640

* test(dynamodb): make createAndSeed tolerate a pre-existing table

CreateTable was asserted to return 200, so re-running the whitespace
tests against a persisted store failed on the second run when the table
already existed. Accept 200 or 400 (ResourceInUseException) for the
create; PutItem still overwrites, keeping the seed deterministic.

* test(dynamodb): cover ADD/DELETE, multiline RHS and 3-clause chains

Extends the UpdateExpression whitespace regression suite along the axes most
likely to hide a follow-on regression:

- ADD and DELETE with a newline after the keyword. Both reach their own clause
  parsers via a separate startsWith dispatch, independent of the SET path.
- A multiline function and arithmetic right-hand side: list_append(#l,\n:more)
  and if_not_exists(#c, :zero)\n+ :inc, so the depth-aware comma scan and the
  arithmetic operator scan are exercised on a wrapped expression.
- A chain of three clauses separated by newlines, so the clause-boundary scan
  runs repeatedly rather than once.

All three newlines sit after the clause keyword. A newline only before a
keyword already parsed on main, because indexOfKeyword's boundary check accepts
any whitespace, while the keyword itself is matched with a literal trailing
space. Verified red on main (2x 'Syntax error; token: "SET"', 1x '"ADD"') and
green with the fix.

* refactor(dynamodb): reserved-word check on the normalized expression

DynamoDbReservedWords.check ran on the raw UpdateExpression while clause
dispatch ran on the whitespace-normalized one. Its function-call lookahead in
extractBareIdentifiers skips only literal spaces, so a raw "if_not_exists\n(...)"
was read there as a bare identifier rather than a function name. Harmless today
since those names are not reserved, but running the check on the normalized
string keeps both entry points consistent.
This commit is contained in:
Ravi Arnan Irianto authored and GitHub committed 2026-07-28 21:56:28 -05:00
1 parent 2f435fd1c9
commit 9b6695c321
3 files changed
+187 -4

No files matched your search

@@ -510,8 +510,10 @@ public class DynamoDbJsonHandler {
// Validate EAN/EAV usage across UpdateExpression + ConditionExpression
if (updateExpression != null) {
// Pre-validate syntax: expression must start with a valid clause keyword
String ue = updateExpression.trim();
// Pre-validate syntax: expression must start with a valid clause keyword.
// AWS tokenizes UpdateExpression whitespace-insensitively, so collapse runs
// of whitespace (newlines, tabs, multiple spaces) before matching keywords.
String ue = updateExpression.trim().replaceAll("\\s+", " ");
if (!ue.isEmpty()) {
String upper = ue.toUpperCase();
if (!upper.startsWith("SET ") && !upper.startsWith("REMOVE ") && !upper.startsWith("ADD ") && !upper.startsWith("DELETE ")) {
@@ -1439,8 +1439,14 @@ public class DynamoDbService {
throw new AwsException("ValidationException",
"Invalid UpdateExpression: The expression can not be empty;", 400);
}
DynamoDbReservedWords.check(expression, "UpdateExpression");
String remaining = expression.trim();
// AWS tokenizes UpdateExpression whitespace-insensitively, so collapse runs of
// whitespace (newlines, tabs, multiple spaces) so clause-keyword dispatch and the
// comma-separated action parsing below work regardless of formatting. Normalize
// before the reserved-word check too: its function-call lookahead skips only
// literal spaces, so on a raw "if_not_exists\n(...)" it would read the function
// name as a bare identifier instead.
String remaining = expression.trim().replaceAll("\\s+", " ");
DynamoDbReservedWords.check(remaining, "UpdateExpression");
while (!remaining.isEmpty()) {
String upper = remaining.toUpperCase();