Skip to content

feat(persistence): add opt-in associated data to AESGCMCodec - #134

Merged
houko merged 2 commits into
mainfrom
feat/aesgcm-associated-data
Nov 14, 2026
Merged

houko merged 2 commits into
mainfrom
feat/aesgcm-associated-data

Conversation

@houko

@houko houko commented Nov 14, 2026

Copy link
Copy Markdown
Member

Summary

AESGCMCodec sealed without associated data. When two stores shared one key, a file saved by one store could be copied over the other's file and would decode as valid state for it. This PR adds an opt-in AESGCMCodec.AssociatedData field that binds each ciphertext to a store identity.

What

  • New field AssociatedData string on AESGCMCodec. It is passed as the GCM additional data on both Encode (Seal) and Decode (Open), under every key the KeyProvider returns, current or historical. It is authenticated but neither encrypted nor stored in the envelope.
  • The empty value seals with no associated data, byte for byte the format written before this change, so existing files keep loading.
  • The field is a string, not []byte, so AESGCMCodec stays comparable; apidiff treats a []byte field as an incompatible change.
  • The field doc, docs/architecture.md and docs/public-api.md cover four points:
    • Reads and writes must use the same value for as long as the data exists.
    • A stable logical name such as com.example.app/settings is the recommended identity.
    • A file path works only if it never changes. Moving or renaming the data directory, restoring onto another machine, or resolving a relative path from another working directory makes the primary and its backup fail like tampered data, so LoadWithRecovery returns a RecoveryError.
    • Adding or changing the value for existing data is a migration: load with the old codec, then save with the new one.

Tests

  • A ciphertext sealed with AD A fails to decode with AD B and with no AD.
  • A golden envelope produced by the previous code still decodes with an empty AD.
  • Round trip with AD.
  • Key rotation with AD: data sealed under the old key and under the new key opens only with the same AD.
  • Root package: two stores share one key and each uses its own logical name as AD. A file copied from one store over the other fails to load, and the store keeps its state.

Replacing the AD with nil makes the binding, rotation and swapped-file tests fail.

How tested

  • go test ./..., go test -race -count=1 ./..., go vet ./..., go mod tidy -diff, golangci-lint run ./...: all pass
  • scripts/check-api.sh against a clone of main (0defa75): only AESGCMCodec.AssociatedData: added, a compatible change

Compatibility

Additive and v1-compatible. The zero value keeps today's format and behaviour exactly. Applications that opt in must migrate existing data, as documented.

Trade-offs

  • The value is not stored in the envelope, so it cannot be recovered from a file. A changed or forgotten value can only be fixed by migrating with the old value. This is why the docs recommend a stable logical name over an absolute path.
  • The website reference (site/) will be updated in a separate site-only PR, per the repository rule that keeps website and library changes apart.

Screenshots

None

AESGCMCodec sealed without associated data, so with one key shared by two stores a file saved by one decoded as valid state for the other when copied over it. The new AESGCMCodec.AssociatedData field is authenticated with the ciphertext on Encode and Decode under every key the provider returns, current or historical, and is not stored in the envelope.

The field is a string rather than []byte so AESGCMCodec stays comparable; apidiff reports a []byte field as an incompatible change. The empty value seals with no associated data, byte for byte the format written before, which a golden envelope produced by the previous code verifies. Reads and writes must use the same value; adding or changing it for existing data needs a migration that loads with the old value and saves with the new one, as the doc comment, docs/architecture.md and docs/public-api.md now state.
The field doc, docs/architecture.md and docs/public-api.md now say the associated data must stay the same for the data's whole lifetime and recommend a stable logical name such as com.example.app/settings; a file path is mentioned only with the warning that moving or renaming it leaves the primary and backup unreadable until migrated. The root swapped-file test uses logical names instead of absolute temp paths.
@houko
houko merged commit 92416e3 into main Nov 14, 2026
23 checks passed
@houko
houko deleted the feat/aesgcm-associated-data branch November 14, 2026 03:08
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