Skip to content

fix(core): redact sensitive fields when a response fails to parse - #839

Open
mattespapier wants to merge 5 commits into
alchemy-run:mainfrom
mattespapier:fix/redact-sensitive-parse-errors
Open

mattespapier wants to merge 5 commits into
alchemy-run:mainfrom
mattespapier:fix/redact-sensitive-parse-errors

Conversation

@mattespapier

@mattespapier mattespapier commented Oct 7, 2026 •

Copy link
Copy Markdown

In strict mode, when a 2xx response doesn't match the spec, <Sdk>ParseError.body carries the whole body, including fields the spec marks sensitive, and in the default lenient mode a body that isn't JSON comes back as the success value. Here is Polar's webhooksGetWebhookEndpoint (its secret is sensitive), a real call to the Polar sandbox through the real SDK, with the reply cut off mid-secret by a fetch wrapper on our side:

// 200 {"created_at":"…",…,"id":"5a61…",…,"secret":"<first half of the secret>
webhooksGetWebhookEndpoint({ id });
 {
-  _tag: 'Success',
-  value: '{"created_at":"2026-10-09T15:46:04.941999Z",…,"secret":"<first half of the secret>'
+  _tag: 'Failure',
+  failure: {
+    _tag: 'PolarParseError',
+    body: '[REDACTED]',
+    cause: 'Invalid JSON response'
+  }
 }

wrapSensitive only runs on the success value; the error carried the body as read, and a mismatched body may hold the secret at an unmodeled key, so the whole body is withheld.

  • For outputs where hasSensitiveMember(ast), makeRestProtocol's parse errors report body: "[REDACTED]"; others are unchanged.
  • For those outputs, lenient mode now fails a non-JSON 2xx body instead of returning the text.
  • hasSensitiveMember looks inside map values and list elements, so a secret inside a map (Meilisearch getembedders, *.apiKey) or a marked list element counts. No generated output marks a list element yet.
  • Stripe's protocol and Fly.io's GraphQL and Sprites decoders, which wire validateResponse themselves, do the same; Fly's Sprites token mint always reports "[REDACTED]".

Each case below is a real call through the real SDK, with the reply changed on our side before the SDK reads it: cut off in the middle of the secret, or with one field given the wrong type. On main:

  • cut off, default (lenient) mode: the call succeeds and returns the cut-off text, secret included;
  • cut off, strict mode: the call fails and the error's body holds that text;
  • wrong type, strict mode: the call fails and the error's body holds the whole secret, which prints when the error is logged or serialized.

On this branch the secret appears in none of them. A wrong type in lenient mode is unchanged (see "Not covered"). Tested on:

  • Polar (sandbox): webhooksGetWebhookEndpoint, secret, on an endpoint created for the run and deleted after.
  • Neon: getProjectBranchRolePassword, password.
  • Stripe (test mode): CreateWebhookEndpoint, secret (returned only at creation), each endpoint deleted after.
  • Fly.io: GraphQL addOn on a Fly Redis add-on, password, read once and replayed (the GraphQL decoder); one Sprites token minted by listSprites, its mint reply replayed through listSprites (the token mint) and createOrganizationToken (the Sprites decoder), token.

Not covered: the other protocols that wire validateResponse themselves (Azure, Coinbase, MongoDB Atlas, Slack, STACKIT, Supabase and ZeroSSL), AWS's own client, error responses (an HTTP error status, or a GraphQL errors reply), and debug HTTP logging. In lenient mode a sensitive member that arrives with the wrong type (Neon's password as ["…"]) is still returned as read, unwrapped, since wrapSensitive wraps strings only. On success, a secret under a map value or in a marked list element is still returned as read, since wrapSensitive wraps object members only. Fly's GraphQL decoder doesn't call wrapSensitive on success, so a successful addOn returns password and publicUrl as plain strings, on main and here. Out of scope: the distilled-sdk skill still says lenient mode returns a non-JSON body as text.

On main, 13 of the 14 new cases fail (6 of 7 in core, both in Stripe, all 5 in Fly.io); the 14th checks a matching body still succeeds with the member redacted.

Checked: pnpm vitest run on core, Stripe and Fly.io (792 pass; 778 on main), the tests of the other seven packages above plus ACME, Polar, Neon, trigger.dev and Cloudflare (242 pass), format:check, lint, tsc -b --noCheck false on the whole repository (0 errors).

Related: #754's new parseError call in protocol-rest.ts should use hasSensitiveMember.

@mattespapier
mattespapier force-pushed the fix/redact-sensitive-parse-errors branch 2 times, most recently from 1d045ee to ca9023f Compare October 8, 2026 03:16
A 2xx body for an operation whose output has a sensitive member reached
the caller unredacted when it did not match the output schema. In strict
mode, `<Sdk>ParseError.body` carried the raw text or the parsed JSON, so a
response that drifted from the spec put the secret into an error that
gets logged. In lenient mode, a non-JSON body (one cut off mid-secret,
for example) came back as the success value.

`hasSensitiveMember(ast)`, beside `wrapSensitive` in `protocol-rest`,
reports whether any member reachable from an output is marked
`SensitiveValue`. It looks inside map values too. For such outputs
`makeRestProtocol` fails a non-JSON 2xx body in both modes and builds
every parse error with `body: "[REDACTED]"`, the sentinel ACME and
ZeroSSL already use; the cause stays the schema error. The whole body is
withheld because in a mismatched body the secret may sit at a key the
schema does not model. Outputs without sensitive members behave as
before.

Protocols that do not go through `makeRestProtocol` are unchanged.
`hasSensitiveMember` visited each list element but checked the
`SensitiveValue` mark only on an object's members, so an output whose
only secret is a list element (a stored header map typed
`Record<string, Array<string>>` whose elements are marked) counted as not
sensitive, and a parse failure reported its body unredacted. It now
checks the mark on each list element too, as it does on a member or a
map value.

No generated output marks a list element today, so no SDK changes until
the generator and `wrapSensitive` honor such a mark.
Stripe's hand-written protocol wires `validateResponse` itself, so the
core fix does not reach it. A 2xx body for an output with a sensitive
member (`CreateWebhookEndpoint`'s `secret`, `CreateEphemeralKey`'s
`secret`) that did not match the schema put the secret into
`StripeParseError.body` in strict mode, and a non-JSON body (one cut off
mid-secret) came back as the success value in lenient mode.

It now does what `makeRestProtocol` does: for an output where
`hasSensitiveMember(outputAst)`, a non-JSON 2xx body fails in every mode
and every parse error reports `body: "[REDACTED]"`. Outputs without
sensitive members behave as before.
Fly.io's GraphQL and Sprites decoders wire `validateResponse` themselves,
so the core fix does not reach them. A 2xx body for an output with a
sensitive member (`addOn`'s `password` and `publicUrl`,
`createOrganizationToken`'s `token`) that did not match the schema put
the secret into `FlyIoParseError.body` in strict mode, and a non-JSON
body (one cut off mid-secret) came back as the success value in lenient
mode.

Both now do what `makeRestProtocol` does: for an output where
`hasSensitiveMember(outputAst)`, a non-JSON 2xx body fails in every mode
and every parse error reports `body: "[REDACTED]"`. The Sprites token
mint that runs before every Sprites operation but
`createOrganizationToken` reported `json ?? text` when the reply carried
no usable token, so a reply cut off mid-token leaked it from an ordinary
call such as `deleteSprite`; it now always reports `"[REDACTED]"`, since
its reply exists only to carry the token. Outputs without sensitive
members behave as before.
Only core's REST protocol, Stripe and Fly.io fail a non-JSON 2xx body for an output with sensitive members; Azure, Coinbase, MongoDB Atlas, Slack, STACKIT, Supabase and ZeroSSL decode it themselves and still return it as text in lenient mode. The decoder comments now say what they do (fail a non-JSON body, withhold the body from parse errors) instead of claiming the body is never returned unredacted, which a secret under a map value still is on success.
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