Skip to content

fix(federation)!: stop key holders printing or encoding private keys - #986

Merged
osanderson merged 8 commits into
mainfrom
fix/key-render-redact
Nov 25, 2026
Merged

osanderson merged 8 commits into
mainfrom
fix/key-render-redact

Conversation

@osanderson

Copy link
Copy Markdown
Collaborator

Breaking, but only for code that encodes a federation issuer's dependencies.

This closes the private-key rendering leaks that the key-material matrix in fapitest (#968) recorded as skipped cases. It also fixes one it didn't list, the federation issuers themselves. Every case in the matrix now runs.

Leaks closed

Commit Type Leak
fix(keys) NewKeyManagerFromSigners An Ed25519 key printed under %v, because ed25519.PrivateKey is a byte slice.
fix(keys) NewInMemoryECDH The key bytes printed under %s and %q.
fix(keys) ephemeral.NewKeyManager Its Ed25519 signing keys printed.
fix(federation)! The five *IssueDependencies structs An ECDSA or RSA private key, D included, encoded through encoding/json, encoding/xml and slog's JSON handler.
fix(federation) The five issuers, *SelfIssuer, *ResolveIssuer, *SubordinateIssuer, *TrustMarkIssuer and *HistoricalKeysIssuer An Ed25519 signer printed through their unexported deps field. The matrix's field guard couldn't see this.

How each is fixed:

  • Every type above now has a redacting Format, handling every verb and flag, and LogValue, in the style keys.SignerSpec and the pairwise derivers already use. NewInMemoryRSA gets the same for consistency; it wasn't leaking.
  • The key managers and the issuers now hold their backends or dependencies behind a pointer. fmt's %w and other unsupported verbs call no methods, so without the pointer they would still print the key.
  • The breaking change: the five *IssueDependencies structs also gain MarshalJSON and MarshalText, which refuse with the new ErrIssueDependenciesSerialization. MarshalText also covers encoding/xml. Encoding one of these structs now fails instead of writing the key. Nothing in this repo, its examples or the interop client encodes them.

UPGRADING.md has two sections under v0.86.0. One covers the breaking encoding refusal and what to encode instead. The other covers the redacted fmt and slog output, which changes nothing that should fail.

Still possible, and why

A keys.SignerSpec holding an Ed25519 key in its exported Signer field can still be printed through fmt's reflection fallback, which calls no methods: %p, %w outside Errorf, or a SignerSpec inside an unexported field. The same goes for an *IssueDependencies struct holding an Ed25519 key. Closing this would need Signer to change type, which would break the API. The matrix case accepts only that fallback, as TestSignerSpecPrintsNoKey already did.

Tests

  • Matrix: every knownLeak marker is removed, so each case runs, and a new "federation issuers" case is added. On main, five of these cases fail.
  • Unit tests: each type's Format and LogValue have their own tests. The encoding refusals are checked with errors.Is for json and xml. Each test fails without its fix.
  • Mutants: 35 new, all killed.
  • Suites: the full race suite, sqlstore, every example module and the interop client pass, and lint is clean.

Nothing on the wire changes.

🤖 Generated with Claude Code

osanderson and others added 8 commits November 26, 2026 00:30
… keys

The KeyManager NewKeyManagerFromSigners builds printed whatever fmt
makes of each signer: an ed25519.PrivateKey is a byte slice, which fmt
prints byte by byte under %v, %x, %d and the rest, at any depth. The
in-memory ECDH backend printed its *ecdh.PrivateKey's bytes under %s
and %q, since a verb a pointer field doesn't take makes fmt print what
it points to.

Both KeyManagers (with and without declared custody) and the
NewInMemoryECDH and NewInMemoryRSA backends now implement fmt.Formatter
and slog.LogValuer, printing a shadow in which the key is [REDACTED]
and the purposes, algorithms, kids and public keys are as before. The
signer KeyManager also holds each backend behind a pointer, so fmt's
reflection fallback, which calls no Format method (%w outside Errorf,
say), prints only an address where the signer is.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An ephemeral KeyManager holding an EdDSA key printed it whenever fmt
or log/slog rendered the manager: an ed25519.PrivateKey is a byte
slice, which fmt prints byte by byte under %v, %x, %d and the rest.

KeyManager now implements fmt.Formatter and slog.LogValuer, printing
each signing and decryption purpose's kid and [REDACTED] for its key.
Each signer is also held behind a pointer, so fmt's reflection
fallback, which calls no Format method (%w outside Errorf, say),
prints only an address where the key is. The key-material matrix in
fapitest now covers the signer where it moved, and reads the keys it
looks for through the pointer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The five issuers' dependencies (HistoricalKeysIssueDependencies,
ResolveIssueDependencies, SelfIssueDependencies,
SubordinateIssueDependencies and TrustMarkIssueDependencies) hold the
entity's Signer, often the private key itself, in an exported field.
encoding/json, encoding/xml and log/slog's JSON handler encoded an
*ecdsa.PrivateKey or *rsa.PrivateKey there with its private exponent
included, and fmt printed an ed25519.PrivateKey byte by byte.

Each now implements fmt.Formatter and slog.LogValuer, printing its
fields with [REDACTED] for Signer, and refuses encoding through
MarshalText and MarshalJSON, which return the new
ErrIssueDependenciesSerialization sentinel. MarshalText covers
encoding/xml too, which encodes an encoding.TextMarshaler as text
rather than walking its fields. This follows keys.SignerSpec, which
already refuses encoding with ErrSignerSpecSerialization.

BREAKING CHANGE: encoding any of the five federation issuer dependency
structs with encoding/json, encoding/xml or another encoder that uses
encoding.TextMarshaler now fails with ErrIssueDependenciesSerialization
instead of writing the signer's private key. Encode the entity's public
JWKS, or the fields you need, instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each federation issuer holds its dependencies, Signer included, in an
unexported field, where fmt's reflection fallback calls no Format
method: an issuer signing with an ed25519.PrivateKey printed every
byte of it under %v, and the dependencies' own redaction never ran.

The five issuers now implement fmt.Formatter and slog.LogValuer,
printing their config and their dependencies as the dependencies
print themselves, with [REDACTED] for Signer, and hold the
dependencies behind a pointer, so the fallback (%w outside Errorf,
say) prints only an address. The key-material matrix gains a case
for the issuers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The signer and ephemeral KeyManagers' EdDSA cases, the in-memory ECDH
backend and the federation issuers' dependencies no longer print their
keys, so their known-leak markers go, with the descriptions only they
used.

The SignerSpec EdDSA case runs too, accepting what fmt's reflection
fallback prints: SignerSpec is an input type whose exported Signer
field the caller fills, and the fallback, which calls none of its
methods, prints an ed25519.PrivateKey there as the byte slice it is.
Every rendering through SignerSpec's own methods, and every encoder,
is still checked. The new inputField records why, as SignerSpec's own
rendering test already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds UPGRADING.md sections for the federation issuer dependencies'
refusal to be encoded, and for the redacted fmt and log/slog output of
the key-holding types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the Format and LogValue of the signer KeyManager (with and
without declared custody), the in-memory ECDH and RSA backends, the
ephemeral KeyManager and the five federation issuers; the [REDACTED]
mask each key holder puts where a key would print, the federation
issuers' dependencies' Signer mask included; and the dependencies'
MarshalJSON and MarshalText refusals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Go 1.27 prints a json.RawMessage as its text, where earlier releases
printed its bytes, so the expected rendering of the dependencies' JWKS
field is built with fmt from the json.RawMessage itself rather than
from a []byte conversion.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@osanderson
osanderson force-pushed the fix/key-render-redact branch from 749d276 to 7f5706b Compare November 25, 2026 16:33
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.98795% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
federation/issuers_render.go 88.46% 2 Missing and 1 partial ⚠️
keys/ephemeral/render.go 93.33% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@sonarqubecloud

Copy link
Copy Markdown

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