Skip to content

fix(sqs): warn on a stored attribute violation, still return the message (#491) - #513

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/491-sqs-stored-attribute-warning
Aug 2, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/491-sqs-stored-attribute-warning

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #491

ReceiveMessage now logs at WARN when a stored message's attributes violate a rule sqsCheckMessageAttributes enforces on send, naming the queue, the message ID and the rule that was violated. The message is still returned, in full, exactly as stored.

#491 is explicit that it is not a "validate on receive" bug report, and this PR is mostly the recorded decision it asks for.

The decision: warn, don't withhold

Returning the message is the decision, not an omission.

Substrate's core property is that replaying an event log reproduces the same observations, and a message written by an older substrate was accepted by the substrate that recorded it. Withholding or dropping it now would make a recorded run unreplayable, which is the one outcome #491 rules out.

A receive-time rejection also has no AWS behaviour to imitate: real SQS never accepted the message in the first place, so there is no real response to copy. Substrate would be inventing an observation.

Option 1 (do nothing) leaves an operator with a fixture that quietly predates the rules. The warning gives them the signal without changing what any caller observes.

Option 3 — a replay-time report of recorded data a current substrate would refuse — is the more principled home, because the violation is a property of the log rather than of any one request. It needs a surface in the replay machinery that does not exist today, so it is filed as #512 rather than dropped, and this PR says so in the CHANGELOG and in the code comment.

Two scope choices, both deliberate and both mutation-tested

The whole stored attribute set is checked, not the subset the request asked for. The violation is a property of what is in state. #461's selection rule means a request naming no attribute names gets no attributes back — so a subset check would hide the violation from exactly the caller most likely to be replaying an old log.

It fires on every receive, not once per message. A message redelivered after its visibility timeout is a fixture being exercised again, and remembering which messages have already warned would be per-process state that a replay could not reproduce.

Send is unchanged

No new run can produce this state — the send path still refuses an illegal attribute set with InvalidParameterValue. TestSQS_MessageAttributes_SendTimeRejectionIsUnchanged pins that, including that the queue depth stays at zero.

Tests

emulator/sqs_messageattrrules_test.go. Two helpers:

  • storeIllegalAttributes writes an illegal attribute set directly into state. Going around the API is the point rather than a shortcut: no send path stores an illegal attribute any more, so the only way this state exists is an event log recorded against a substrate that predates the rules.

  • newSQSServerCapturingLogs builds a server whose logger writes into a buffer at WARN, so the warning is asserted rather than assumed.

  • TestSQS_MessageAttributes_StoredAttributesAreReturnedNotWithheld — three violation families (a reserved AWS.trace name, an eleventh attribute, Age = Number/"abc"), each over both protocols, asserting the message ID, the body and that every stored attribute comes back with its stored value. This is the replay gate: it must fail if anyone later "fixes" receive to validate.

  • TestSQS_MessageAttributes_StoredViolationWarns — the warning names the message, the queue and the rule; a request naming no attributes still warns (with Empty(attributes) in the same subtest, since that is the case a subset check would miss); a legal message logs nothing; a message with no attributes logs nothing.

  • TestSQS_MessageAttributes_SendTimeRejectionIsUnchanged — 400 / InvalidParameterValue / the cast message / depth 0.

The old TestSQS_MessageAttributes_StoredAttributesAreNotRechecked is replaced: it asserted the absence of a check, which is now the weaker half of what is asserted.

Verification

From the repo root: gofmt -l . clean, go build ./..., go vet ./..., golangci-lint run ./... → 0 issues, make test (race) → ok, make docs-reference docs-reference-check docs-versions → no diff, npm --prefix docs run build → complete, go vet -C test/e2e ./... and go test -C test/e2e ./... → ok.

Mutation testing — 14 mutants over sqs_messageattrrules.go and sqs_plugin.go: 13 caught, 1 proven equivalent, 0 survived.

The four mutants #491 exists to forbid are all caught by the replay gate:

Mutant Caught by
withhold the message (continue) …AreReturnedNotWithheld
reject the whole ReceiveMessage …AreReturnedNotWithheld
strip the offending attributes …AreReturnedNotWithheld
blank the body as a "safe" redaction …AreReturnedNotWithheld

The warning mutants — never warn, warn unconditionally, drop the message ID, drop the queue, drop which rule was violated, downgrade to Debug — are all caught by …StoredViolationWarns. Both scope mutants (check only the requested subset; skip the check when nothing was requested) are caught by the "request naming no attributes still warns" subtest, which is why it exists. Softening the send path (sqsCheckMessageAttributes(nil)) is caught by …SendTimeRejectionIsUnchanged.

The one equivalent mutant is removing the len(msg.MessageAttributes) == 0 fast path: sqsCheckMessageAttributes returns nil for an empty or nil map, so no input distinguishes the two forms. It stays on cost grounds — most messages carry no attributes, and the guard skips a sort and an allocation per message in the receive loop — and the comment says that rather than implying correctness.

A first run had one survivor: a logger == nil guard. No other logger == nil guard exists in the package and receiveMessage already calls p.logger.Warn unguarded a few lines away, so it was inconsistent defensive code for an unreachable path. Resolved by deleting the guard, not by adding a test for it.

End to end

$ aws sqs send-message --queue-url .../replay-probe --message-body body \
    --message-attributes '{"trace":{"DataType":"String","StringValue":"v"}}'
c07a7c78-91ab-8e53-9175-dd70f0a3fd60

$ aws sqs send-message --queue-url .../replay-probe --message-body body \
    --message-attributes '{"Age":{"DataType":"Number","StringValue":"abc"}}'
An error occurred (InvalidParameterValue) when calling the SendMessage operation:
Can't cast the value of message (user) attribute 'Age' to a number.

$ aws sqs receive-message --queue-url .../replay-probe --message-attribute-names All
{"trace": {"StringValue": "v", "DataType": "String"}}      # attributes intact
# server log: 0 WARN lines

Send still refuses, a legal message warns nothing, and the illegal-stored case is only reachable by writing state directly — which is exactly the fixture the unit tests build.

Docs

docs/services.md §"Message attributes" gains a ### The rules apply on send, not on receive subsection: the message is returned in full with a WARN naming the queue, message ID and rule; the replay argument and the no-AWS-behaviour-to-imitate argument; that rejecting on receive is deliberately not done and the suite fails if anyone adds it; the whole-set-not-subset and every-receive rationales; and that send-time rejection is unchanged so no new run can produce this state.

…age (#491)

ReceiveMessage now logs at WARN when a stored message's attributes violate a
rule sqsCheckMessageAttributes enforces on send, naming the queue, the message
ID and the rule that was violated. The message is still returned, in full,
exactly as stored.

Returning it is the decision, not an omission. Substrate's core property is
that replaying an event log reproduces the same observations, and a message
written by an older substrate was accepted by the substrate that recorded it —
so withholding or dropping it now would make a recorded run unreplayable, which
is the one outcome #491 rules out. A receive-time rejection also has no AWS
behaviour to imitate: real SQS never accepted the message, so there is no real
response to copy. The warning gives an operator the signal that their fixture
predates the rules without changing what any caller observes.

Two scope choices are deliberate and tested. The whole stored attribute set is
checked rather than the subset the request asked for, because the violation is a
property of what is in state and #461's selection means a request naming no
attribute names would otherwise hide it entirely. And it fires on every receive
rather than once, because a message redelivered after its visibility timeout is
a fixture being exercised again, and remembering which messages have already
warned would be per-process state a replay could not reproduce.

Send-time rejection is unchanged, so no new run can produce this state. Option 3
from the issue — a replay-time report of recorded data a current substrate would
refuse — is the more principled home and is filed as #512.
@codecov

codecov Bot commented Aug 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit dcf8ffa into main Aug 2, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the feat/491-sqs-stored-attribute-warning branch August 2, 2026 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQS message-attribute rules are not applied to attributes already in state

1 participant