Skip to content

docs(server): ctx_metadata credential-leak guidance (#1343) - #1400

Merged
bokelley merged 1 commit into
mainfrom
bokelley/ctx-metadata-safety
May 3, 2026
Merged

bokelley merged 1 commit into
mainfrom
bokelley/ctx-metadata-safety

Conversation

@bokelley

@bokelley bokelley commented May 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes the documentation half of #1343 — the "what goes in ctx_metadata and what doesn't" guide that adopters need to avoid embedding credentials in the framework's adopter-internal bag. The wire-strip behavior already exists; this PR documents why it isn't enough and the recommended re-derive-per-request pattern.

Problem

ctx_metadata is the framework's adopter-internal opaque bag on every DecisioningPlatform resource. The framework strips it from wire responses to buyers. But the strip-on-wire is necessary, not sufficient — three leak surfaces persist:

  1. Adopter-generated error messages — throw new Error(\failed for ${JSON.stringify(account)}`)puts everyctx_metadatafield into the error, which then flows to logs unconditionally and to the buyer whenexposeErrorDetails: true`.
  2. Info-level structured logs — logger.info('resolving', { account: ctx.account }) serializes the whole object verbatim into Datadog / CloudWatch / wherever your pipeline forwards.
  3. Heap dumps / process inspection — credentials at rest in process memory are recoverable from a core dump.

The framework's redactCredentialPatterns is pattern-based whack-a-mole; it catches Bearer <token> shapes in messages but can't see into a JSON.stringify(account) blob whose key happens to be accessToken.

What ships

  • docs/guides/CTX-METADATA-SAFETY.md — full guide with safe / unsafe field lists, the three leak-surface walkthrough, recommended re-derive pattern, forward-compat note
  • JSDoc warning on Account.ctx_metadata — points to the guide and names the alternative (Account.authInfo for credentials, framework round-trips it via refreshToken)
  • Cross-references from CLAUDE.md and AGENTS.md — AI agents scaffolding new platforms see the warning before they reach for ctx_metadata.accessToken
  • Sanity test for the wire-strip — adopters can copy the pattern to verify their own platforms don't accidentally spread ctx_metadata into a response shape

Deferred

The optional Zod / standard-schema enforcement piece (the original issue's Stage 2) is deferred — schema-driven structural redaction would be a separate, larger PR that wires into the logger seam. The docs side lands first since it's high-leverage and zero-blast-radius.

Test plan

  • 3 new tests in test/server-decisioning-ctx-metadata-strip.test.js pass
  • npm run build:lib clean
  • npm run format:check clean
  • Empty changeset (JSDoc + docs + test only, no library behavior change)

🤖 Generated with Claude Code

Closes the documentation half of #1343 — the "what goes in ctx_metadata
and what doesn't" guide adopters need to avoid embedding credentials in
the framework's adopter-internal bag. The wire-strip behavior already
exists; this PR documents WHY it isn't enough and the recommended
re-derive-per-request pattern.

Three leak surfaces beyond the wire response:
1. Adopter-generated error messages (`JSON.stringify(account)` in a
   throw)
2. Info-level structured logs that serialize the resolved Account
3. Heap dumps and process inspection

Recommendation: keep upstream IDs in `ctx_metadata`, put bearers on
`Account.authInfo` (which the framework round-trips through
`refreshToken`) or in your own per-principal token cache.

What ships:
- `docs/guides/CTX-METADATA-SAFETY.md` — full guide with safe / unsafe
  field lists, leak-surface walkthrough, recommended re-derive pattern,
  forward-compat note
- JSDoc warning on `Account.ctx_metadata` (account.ts) pointing to the
  guide and naming the alternative (`authInfo`)
- Cross-references from `CLAUDE.md` and `AGENTS.md` so AI agents
  scaffolding new platforms see the warning before they reach for
  `ctx_metadata.accessToken`
- Sanity test asserting the wire-strip works for `toWireAccount`

The optional Zod / standard-schema enforcement piece (Stage 2 of the
issue) is deferred — adopter ergonomics for the docs side land first;
the schema-driven structural redaction would be a separate, larger PR
that wires into the logger seam.

Test-only behavior change (JSDoc + sanity test) — empty changeset.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit ebaa4b3 into main May 3, 2026
10 checks passed
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