Skip to content

feat(cbor): a deterministic CBOR codec (#785) - #789

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/785-cbor-codec
Sep 10, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/785-cbor-codec

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

The first of three PRs teaching CloudWatch to speak the protocols its clients actually
speak. This one adds emulator/cbor.go and its tests and wires them to nothing, so
the codec can be reviewed against RFC 8949 and Smithy's protocol tests on its own.

Why substrate has its own codec

Substrate had no CBOR at all — the only CBOR in the repo was a hardcoded []byte{0xa0}
in getMetricData, so the other nine CloudWatch operations answered XML to a client that
had asked for CBOR. aws-sdk-go-v2 reports deserialization failed, expected map for struct, got major type 1: the < of <GetMetricDataResponse read as a CBOR head.

Rather than depend on github.com/aws/smithy-go: the root module has no AWS SDK
dependency, and adding one to emit a response format is a large surface for a small need.
More to the point, substrate's premise is that the same inputs produce the same bytes. A
library free to choose length form, member order or float width per call makes a recorded
response non-reproducible, and none offers a contract that says otherwise.

Writer: one output per input

Definite length with minimal arguments, smallest-representation integers, doubles always
0xfb, timestamps always tag 1 over a float64 at millisecond resolution, and member
order from an explicit ordered type. map[string]any is refused — sorting it would
give stable bytes in an order no model declares, and accepting it at all makes the ordered
type optional.

Reader: liberal exactly where the spec requires

Indefinite-length maps, arrays and strings; non-minimal length arguments (the AWS
query-compatible vectors encode a two-entry map as b9 00 02); a double arriving as
float16, float32, float64 or an integer; a tag 1 payload in either numeric form;
0xf7 as null; and an unrecognized tag decoding to its payload, so an unmodeled member is
consumed structurally rather than failing the request.

Refused: reserved additional information 28–30, a declared element count larger than the
input, and nesting past 64 levels — a body is attacker-controlled, and
9b ffffffffffffffff is nine bytes asking for 2⁶⁴ elements.

Tests assert wire bytes, not round trips

A codec that is self-consistently wrong passes every round trip and fails against every
real client. 115 subtests over RFC 8949 Appendix A and Smithy's normative rpcv2Cbor
vectors.

One table holds the literal output of smithy-go v1.28.1's encoding/cbor.Encode — the
encoder aws-sdk-go-v2 actually uses — so agreement with the peer is recorded rather than
inferred. Every one of substrate's own encodings was also fed back through cbor.Decode
and accepted.

That cross-check corrected a premise: smithy-go's encoder has no indefinite-length
path, so substrate's definite-length choice matches what the Go SDK puts on the wire
rather than diverging from it, and the two produce byte-identical timestamps
(c1fb41cc37db380fbe77). The indefinite forms come from the test vectors and other SDKs'
streaming writers — which is why the reader must take them and the writer need not produce
them. The doc comments and CHANGELOG say this rather than the reverse, which is what an
earlier draft claimed.

Verification

  • gofmt, go build ./..., go vet ./..., golangci-lint run ./... — all clean, 0 issues
  • make test on go1.27.1 and GOTOOLCHAIN=go1.26.6 — both exit 0, 0 failures
  • make coverage — emulator 83.5% (was 83.4%), total 83.0% (was 82.9%)
  • make docs-reference docs-reference-check docs-versions — clean, no drift
  • go vet -C test/e2e ./... and go test -C test/e2e ./... — exit 0

Part of #785; the plugin wiring follows in the next two PRs.

Smithy RPC v2 CBOR is one of the three protocols CloudWatch's model declares,
and substrate had no CBOR encoder or decoder at all — only a hardcoded
[]byte{0xa0} in GetMetricData, which left the other nine operations answering
XML to a client that had asked for CBOR.

This adds the codec on its own, wired to nothing, so it can be reviewed against
RFC 8949 and Smithy's protocol tests rather than alongside a plugin rewrite.

The writer has exactly one output for any input, which is what a replayable
event log needs: definite length with minimal arguments, smallest-representation
integers, doubles always 0xfb, timestamps always tag 1 over a float64, and
member order from an explicit ordered type rather than Go's randomized map
iteration. The reader is liberal where the specification requires it —
indefinite length, non-minimal arguments, a double as float16/32/64 or an
integer, 0xf7 as null, an unrecognized tag decoding to its payload — and
refuses reserved additional information, a declared count larger than the
input, and nesting past 64 levels before either can size an allocation or
recurse.

Tests assert wire bytes rather than round trips, since a self-consistently
wrong codec passes every round trip. One table holds smithy-go v1.28.1's own
encoder output, which corrected a premise: it has no indefinite-length path, so
substrate's definite-length choice matches what aws-sdk-go-v2 puts on the wire
instead of diverging from it, and the two agree on timestamps byte for byte.
@scttfrdmn scttfrdmn added this to the v0.110.0 milestone Sep 10, 2026
@codecov

codecov Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.88608% with 13 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/cbor.go 95.88% 13 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 7df0a2c into main Sep 10, 2026
14 checks passed
@scttfrdmn
scttfrdmn deleted the feat/785-cbor-codec branch September 10, 2026 23:34
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.

1 participant