Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

fix(dynamodb): resolve UpdateExpression aliases before the key-attribute check (#1464)

ppettitau086a78643c05
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 653846ec..446db62e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Fixed
+- **DynamoDB — an `UpdateExpression` alias is resolved before the key-attribute check** — `set #pk = :pk` with `#pk` mapped to a non-key attribute was refused with `Cannot update attribute pk. This attribute is part of the key`, because the check matched the key name as raw text inside the `#pk` alias and the `:pk` value reference. It now reads the alias-resolved attribute roots the update evaluator already reports, so only a path that resolves to the partition or sort key is rejected. Reported by @ppettitau.
- **RDS — `DescribeDBInstances` honors SDK `Filters`** — AWS Query clients serialize filters as `Filters.Filter.N` with `Values.Value.N`, but MiniStack only parsed its internal `Filters.member.N` form, so filters such as `db-cluster-id` were ignored and unrelated instances were returned. Both wire forms are now parsed with the existing AND-across-filters and OR-across-values semantics.
### Added
- **EC2 — instances can have a real box behind them (`RegisterImage`)** — `RunInstances` returned a `running` record and booted nothing. `RegisterImage` now takes a container reference in `ImageLocation` and returns an `ami-` id; launching that id boots the image as a container whose IP becomes the instance's address, so `ssm:SendCommand` can report a real exit code. Such an AMI is `instance-store` backed: no EBS root volume, `StopInstances` / `StartInstances` answer `UnsupportedOperation`, and a container that dies reports `terminated`. Snapshot-backed registration is accepted as AWS accepts it and fails at `RunInstances` with `InvalidAMIID.Unavailable`, as does a reference that cannot be pulled. Registering is the only opt-in — with nothing registered EC2 never reaches for Docker. Reported by @iot-rocket.
diff --git a/ministack/services/dynamodb.py b/ministack/services/dynamodb.py
index f4072c32..76d2cebe 100644
--- a/ministack/services/dynamodb.py
+++ b/ministack/services/dynamodb.py
@@ -1779,6 +1779,15 @@ def _delete_item(data):
return json_response(result)
+def _key_attribute_update_error(table, updated_attrs):
+ """AWS error for an update whose targets include a hash or range key."""
+ for key_name in (table.get("pk_name"), table.get("sk_name")):
+ if key_name and key_name in updated_attrs:
+ return error_response_json("ValidationException",
+ f"One or more parameter values were invalid: Cannot update attribute {key_name}. This attribute is part of the key", 400)
+ return None
+
+
def _update_item(data):
name = data.get("TableName")
err = _validate_data_plane_table_name(name)
@@ -1853,30 +1862,22 @@ def _update_item(data):
return error_response_json("ValidationException",
f'Invalid UpdateExpression: Syntax error; token: "{_tokens_pre[0]}", near: "{_near}"', 400)
- # AWS pre-rejects an UpdateExpression that targets a key attribute, even
- # when the item doesn't exist yet — the rejection is parse-time, not
- # diff-time. Cheap scan for `SET <keyAttr>[ =]` against the table's
- # declared key names (alias-resolved via ExpressionAttributeNames).
- if update_expr.strip():
- _key_names = [n for n in (table.get("pk_name"), table.get("sk_name")) if n]
- _resolved_ean = data.get("ExpressionAttributeNames") or {}
- for _kn in _key_names:
- # Direct mention of the key name as a SET / ADD / REMOVE / DELETE target.
- if re.search(rf"(?i)\b(SET|ADD|REMOVE|DELETE)\b[^,]*?\b{re.escape(_kn)}\b", update_expr):
- return error_response_json("ValidationException",
- f"One or more parameter values were invalid: Cannot update attribute {_kn}. This attribute is part of the key", 400)
- # Alias that resolves to a key name.
- for _alias, _resolved in _resolved_ean.items():
- if _resolved == _kn and re.search(rf"(?i)\b(SET|ADD|REMOVE|DELETE)\b[^,]*?{re.escape(_alias)}\b", update_expr):
- return error_response_json("ValidationException",
- f"One or more parameter values were invalid: Cannot update attribute {_kn}. This attribute is part of the key", 400)
-
updated_attrs = set()
if update_expr:
+ # AWS rejects an UpdateExpression that targets a key attribute, even
+ # when the item doesn't exist yet or the value is unchanged — the
+ # rejection is parse-time, and precedes the runtime operand-type
+ # errors the evaluator raises. `updated_attrs` is owned here so the
+ # targets resolved before a raise are still available; they are already
+ # alias-resolved, so the expression is never scanned a second time.
try:
- item, updated_attrs = _apply_update_expression(item, update_expr, eav, ean)
+ item, updated_attrs = _apply_update_expression(item, update_expr, eav, ean, updated_attrs)
except ValueError as exc:
- return error_response_json("ValidationException", str(exc), 400)
+ return (_key_attribute_update_error(table, updated_attrs)
+ or error_response_json("ValidationException", str(exc), 400))
+ key_err = _key_attribute_update_error(table, updated_attrs)
+ if key_err:
+ return key_err
elif attribute_updates:
try:
item = _apply_attribute_updates(item, attribute_updates)
@@ -5270,7 +5271,7 @@ def _evaluate_condition(expr, item, attr_values, attr_names,
# Update expression
# ---------------------------------------------------------------------------
-def _apply_update_expression(item, expr, attr_values, attr_names):
+def _apply_update_expression(item, expr, attr_values, attr_names, updated_attrs=None):
item = copy.deepcopy(item)
tokens = _tokenize(expr)
err = _check_redundant_parens(tokens)
@@ -5296,7 +5297,7 @@ def _apply_update_expression(item, expr, attr_values, attr_names):
if current_clause is not None:
clauses[current_clause] = current_tokens
- updated_attrs = set()
+ updated_attrs = set() if updated_attrs is None else updated_attrs
if 'SET' in clauses:
_apply_set(item, clauses['SET'], attr_values, attr_names, updated_attrs)
@@ -5454,6 +5455,7 @@ def _apply_add(item, tokens, attr_values, attr_names, updated_attrs):
add_val = attr_values.get(part[val_idx][1])
if not path or add_val is None:
continue
+ updated_attrs.add(path[0])
# ADD only accepts Number and set operands (parse-time in AWS), and the
# existing attribute must carry the same type (runtime in AWS).
@@ -5466,8 +5468,6 @@ def _apply_add(item, tokens, attr_values, attr_names, updated_attrs):
if existing is not None and _operand_type(existing) != op_type:
raise ValueError("An operand in the update expression has an incorrect data type")
- updated_attrs.add(path[0])
-
if "N" in add_val:
inc = Decimal(add_val["N"])
cur = Decimal(existing["N"]) if existing and "N" in existing else Decimal(0)
@@ -5496,6 +5496,7 @@ def _apply_delete(item, tokens, attr_values, attr_names, updated_attrs):
del_val = attr_values.get(part[val_idx][1])
if not path or del_val is None:
continue
+ updated_attrs.add(path[0])
# DELETE only accepts set operands (parse-time in AWS), and the
# existing attribute must be a set of the same type (runtime in AWS).
@@ -5507,8 +5508,6 @@ def _apply_delete(item, tokens, attr_values, attr_names, updated_attrs):
"Invalid UpdateExpression: Incorrect operand type for operator or function; "
f"operator: DELETE, operand type: {_AV_TYPE_NAMES.get(op_type, op_type)}, typeSet: ALLOWED_FOR_ADD_OPERAND")
- updated_attrs.add(path[0])
-
existing = _get_at_path(item, path)
if existing is None:
continue
diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py
index 30348a12..835cfec0 100644
--- a/tests/test_dynamodb.py
+++ b/tests/test_dynamodb.py
@@ -6937,3 +6937,138 @@ def test_dynamodb_delete_table_in_process_drops_backlog(ddb_stream_state):
assert name not in ddb_service._stream_records
assert ddb_service.stream_start_position(name) == 0
assert ddb_service.stream_end_position(name) == 0
+
+
+# ---------------------------------------------------------------------------
+# UpdateExpression key-attribute pre-check
+# ---------------------------------------------------------------------------
+
+
+def key_guard_table(ddb):
+ """Table whose key names are also usable as expression alias suffixes, so a
+ `#part` alias or `:part` value reference textually contains the key name."""
+ name = f"key-guard-{_uuid_mod.uuid4().hex[:8]}"
+ ddb.create_table(
+ TableName=name,
+ KeySchema=[
+ {"AttributeName": "part", "KeyType": "HASH"},
+ {"AttributeName": "sort", "KeyType": "RANGE"},
+ ],
+ AttributeDefinitions=[
+ {"AttributeName": "part", "AttributeType": "S"},
+ {"AttributeName": "sort", "AttributeType": "S"},
+ ],
+ BillingMode="PAY_PER_REQUEST",
+ )
+ ddb.put_item(
+ TableName=name,
+ Item={
+ "part": {"S": "p1"},
+ "sort": {"S": "s1"},
+ "other-part": {"S": "before"},
+ "tally": {"N": "1"},
+ },
+ )
+ try:
+ yield name
+ finally:
+ ddb.delete_table(TableName=name)
+
+
+_GUARD_KEY = {"part": {"S": "p1"}, "sort": {"S": "s1"}}
+
+
+def test_update_alias_named_after_key_targets_non_key_attribute(ddb, key_guard_table):
+ resp = ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ ReturnValues="UPDATED_NEW",
+ UpdateExpression="set #part = :part",
+ ExpressionAttributeNames={"#part": "other-part"},
+ ExpressionAttributeValues={":part": {"S": "after"}},
+ )
+ assert resp["Attributes"] == {"other-part": {"S": "after"}}
+
+ item = ddb.get_item(TableName=key_guard_table, Key=_GUARD_KEY)["Item"]
+ assert item["part"] == {"S": "p1"}
+ assert item["sort"] == {"S": "s1"}
+ assert item["other-part"] == {"S": "after"}
+
+
+def _assert_key_update_rejected(exc_info, key_name):
+ err = exc_info.value.response["Error"]
+ assert err["Code"] == "ValidationException"
+ assert err["Message"] == (
+ f"One or more parameter values were invalid: Cannot update attribute {key_name}. "
+ "This attribute is part of the key"
+ )
+ assert exc_info.value.response["ResponseMetadata"]["HTTPStatusCode"] == 400
+
+
+def test_update_rejects_direct_partition_key_set(ddb, key_guard_table):
+ with pytest.raises(ClientError) as ei:
+ ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ UpdateExpression="SET part = :v",
+ ExpressionAttributeValues={":v": {"S": "other"}},
+ )
+ _assert_key_update_rejected(ei, "part")
+
+
+def test_update_rejects_direct_sort_key_set(ddb, key_guard_table):
+ with pytest.raises(ClientError) as ei:
+ ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ UpdateExpression="SET sort = :v",
+ ExpressionAttributeValues={":v": {"S": "other"}},
+ )
+ _assert_key_update_rejected(ei, "sort")
+
+
+def test_update_rejects_alias_resolving_to_partition_key(ddb, key_guard_table):
+ with pytest.raises(ClientError) as ei:
+ ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ UpdateExpression="SET #k = :v",
+ ExpressionAttributeNames={"#k": "part"},
+ ExpressionAttributeValues={":v": {"S": "other"}},
+ )
+ _assert_key_update_rejected(ei, "part")
+
+
+def test_update_rejects_remove_of_key_attribute(ddb, key_guard_table):
+ with pytest.raises(ClientError) as ei:
+ ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ UpdateExpression="REMOVE #k",
+ ExpressionAttributeNames={"#k": "sort"},
+ )
+ _assert_key_update_rejected(ei, "sort")
+
+
+def test_update_rejects_add_on_key_attribute(ddb, key_guard_table):
+ with pytest.raises(ClientError) as ei:
+ ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ UpdateExpression="ADD part :v",
+ ExpressionAttributeValues={":v": {"N": "1"}},
+ )
+ _assert_key_update_rejected(ei, "part")
+
+
+def test_update_allows_value_reference_named_after_key(ddb, key_guard_table):
+ resp = ddb.update_item(
+ TableName=key_guard_table,
+ Key=_GUARD_KEY,
+ ReturnValues="UPDATED_NEW",
+ UpdateExpression="SET label = :sort ADD tally :one",
+ ExpressionAttributeValues={":sort": {"S": "v"}, ":one": {"N": "2"}},
+ )
+ assert resp["Attributes"]["label"] == {"S": "v"}
+ assert resp["Attributes"]["tally"] == {"N": "3"}