From 0a2d954357a1593b2684f69e103f232a15ff3ede Mon Sep 17 00:00:00 2001 From: ismaelrumzan Date: Fri, 20 Nov 2026 15:45:28 +0300 Subject: [PATCH 1/2] Redact credentials from SDK debug logs When a caller passes `debugLogger`, `logRequest` and `logResponse` in the generated `src/lib/sdks.ts` print every request header and every JSON response body verbatim. That includes `authorization: Bearer ` on every call and secrets returned in response bodies, such as the webhook signing `secret` from `createWebhook` (Vercel security finding 01M7V6G0R1RSPX4T3Y2KKPZ5HK). Mechanism: `src/hooks/debug-logger.ts` (hand-written, never generated) exports `redactDebugLogger`, which wraps the caller's logger. Header and multipart lines (`name: value` strings) for authorization, cookie, set-cookie, proxy-authorization, x-api-key, or any secret-like name are replaced with `name: [REDACTED]`. Plain-object and array arguments are copied with string values under secret-like keys (suffix secret, token, password, apikey, privatekey, ignoring case, `-` and `_`) replaced. The SDK's return values are untouched; only what reaches the logger changes. `src/lib/sdks.ts` is generated, so the two-line edit that applies the wrapper is also stored as `.speakeasy/patches/src/lib/sdks.ts.patch`. Speakeasy applies files in `.speakeasy/patches/` after every generation, so the nightly regen keeps the redaction. If the generated context ever drifts, generation fails with conflict markers instead of dropping the patch silently. Tests: `tests/debug-logger-redaction.test.mjs` runs `createWebhook` through the real SDK with a stub fetcher and a capturing logger. It failed before the fix (the log contained the bearer token and the webhook secret) and passes after. It also asserts the patch file and the gen.yaml test script exist, so removing either fails CI. The script is added to gen.yaml additionalScripts so regeneration keeps it in package.json. Verified by running `speakeasy run` with CLI 1.810.0 (the version the generation workflow uses) from a pristine generated `sdks.ts`: the patch was re-applied, `debug-logger.ts` was kept, and `npm test` passed on the regenerated tree. Known gap: secrets sent in request bodies under generic keys (for example an environment variable's `value`) are still logged. Review: review-loop round 1 with gpt-6.1-sol, approved, no findings. Co-Authored-By: Claude Opus 5.5 --- .speakeasy/gen.yaml | 3 +- .speakeasy/patches/src/lib/sdks.ts.patch | 21 ++++++ package.json | 5 +- src/hooks/debug-logger.ts | 84 ++++++++++++++++++++++++ src/lib/sdks.ts | 3 +- tests/debug-logger-redaction.test.mjs | 82 +++++++++++++++++++++++ 6 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 .speakeasy/patches/src/lib/sdks.ts.patch create mode 100644 src/hooks/debug-logger.ts create mode 100644 tests/debug-logger-redaction.test.mjs diff --git a/.speakeasy/gen.yaml b/.speakeasy/gen.yaml index 3e469677c0..cbc079a8fb 100644 --- a/.speakeasy/gen.yaml +++ b/.speakeasy/gen.yaml @@ -42,8 +42,9 @@ typescript: additionalPackageJSON: license: Apache-2.0 additionalScripts: - test: npm run test:connect-environments + test: npm run test:connect-environments && npm run test:debug-logger test:connect-environments: npm run build && node --test tests/connect-integration-resource-to-project.test.mjs + test:debug-logger: npm run build && node --test tests/debug-logger-redaction.test.mjs alwaysIncludeInboundAndOutbound: false apiPromiseHelpers: false author: Speakeasy diff --git a/.speakeasy/patches/src/lib/sdks.ts.patch b/.speakeasy/patches/src/lib/sdks.ts.patch new file mode 100644 index 0000000000..b3e1d73e7e --- /dev/null +++ b/.speakeasy/patches/src/lib/sdks.ts.patch @@ -0,0 +1,21 @@ +diff --git a/src/lib/sdks.ts b/src/lib/sdks.ts +index 19d9cf8ea..759c95f7a 100644 +--- a/src/lib/sdks.ts ++++ b/src/lib/sdks.ts +@@ -2,6 +2,7 @@ + * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. + */ + ++import { redactDebugLogger } from "../hooks/debug-logger.js"; + import { SDKHooks } from "../hooks/hooks.js"; + import { HookContext } from "../hooks/types.js"; + import { +@@ -110,7 +111,7 @@ export class ClientSDK { + + this._options = { ...options, hooks: this.#hooks }; + +- this.#logger = this._options.debugLogger; ++ this.#logger = redactDebugLogger(this._options.debugLogger); + } + + public _createRequest( diff --git a/package.json b/package.json index a091fc2e15..ef0502dd0a 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,9 @@ "lint": "oxlint --max-warnings=0 --deny-warnings --no-error-on-unmatched-pattern src", "build": "tsgo", "prepublishOnly": "npm run build", - "test": "npm run test:connect-environments", - "test:connect-environments": "npm run build && node --test tests/connect-integration-resource-to-project.test.mjs" + "test": "npm run test:connect-environments && npm run test:debug-logger", + "test:connect-environments": "npm run build && node --test tests/connect-integration-resource-to-project.test.mjs", + "test:debug-logger": "npm run build && node --test tests/debug-logger-redaction.test.mjs" }, "peerDependencies": { diff --git a/src/hooks/debug-logger.ts b/src/hooks/debug-logger.ts new file mode 100644 index 0000000000..e287b077e2 --- /dev/null +++ b/src/hooks/debug-logger.ts @@ -0,0 +1,84 @@ +import { Logger } from "../lib/logger.js"; + +/* + * This file is not generated. `.speakeasy/patches/src/lib/sdks.ts.patch` + * wraps the caller's `debugLogger` with `redactDebugLogger` so the request and + * response logging in `src/lib/sdks.ts` never prints credentials. + */ + +const REDACTED = "[REDACTED]"; + +const SENSITIVE_HEADERS = new Set([ + "authorization", + "cookie", + "proxy-authorization", + "set-cookie", + "x-api-key", +]); + +const SENSITIVE_KEY_SUFFIXES = [ + "apikey", + "password", + "privatekey", + "secret", + "token", +]; + +// Header and multipart field lines are logged as a single "name: value" string. +const NAME_VALUE_LINE = /^([A-Za-z0-9_.-]+):\s/; + +function isSensitiveKey(key: string): boolean { + const normalized = key.toLowerCase().replace(/[-_]/g, ""); + return SENSITIVE_KEY_SUFFIXES.some((suffix) => normalized.endsWith(suffix)); +} + +function redactLine(line: string): string { + const match = NAME_VALUE_LINE.exec(line); + const name = match?.[1]; + if (!name) { + return line; + } + if (SENSITIVE_HEADERS.has(name.toLowerCase()) || isSensitiveKey(name)) { + return `${name}: ${REDACTED}`; + } + return line; +} + +function redactValue(value: unknown): unknown { + if (Array.isArray(value)) { + return value.map(redactValue); + } + if (value === null || typeof value !== "object") { + return value; + } + const prototype = Object.getPrototypeOf(value); + if (prototype !== Object.prototype && prototype !== null) { + return value; + } + const redacted: Record = {}; + for (const [key, entry] of Object.entries(value)) { + redacted[key] = typeof entry === "string" && isSensitiveKey(key) + ? REDACTED + : redactValue(entry); + } + return redacted; +} + +function redactArg(arg: unknown): unknown { + return typeof arg === "string" ? redactLine(arg) : redactValue(arg); +} + +export function redactDebugLogger( + logger: Logger | undefined, +): Logger | undefined { + if (!logger) { + return undefined; + } + return { + group: (label?: string) => + logger.group(label === undefined ? label : redactLine(label)), + groupEnd: () => logger.groupEnd(), + log: (message: any, ...args: any[]) => + logger.log(redactArg(message), ...args.map(redactArg)), + }; +} diff --git a/src/lib/sdks.ts b/src/lib/sdks.ts index 19d9cf8ea0..759c95f7a6 100644 --- a/src/lib/sdks.ts +++ b/src/lib/sdks.ts @@ -2,6 +2,7 @@ * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT. */ +import { redactDebugLogger } from "../hooks/debug-logger.js"; import { SDKHooks } from "../hooks/hooks.js"; import { HookContext } from "../hooks/types.js"; import { @@ -110,7 +111,7 @@ export class ClientSDK { this._options = { ...options, hooks: this.#hooks }; - this.#logger = this._options.debugLogger; + this.#logger = redactDebugLogger(this._options.debugLogger); } public _createRequest( diff --git a/tests/debug-logger-redaction.test.mjs b/tests/debug-logger-redaction.test.mjs new file mode 100644 index 0000000000..a1a251c9ac --- /dev/null +++ b/tests/debug-logger-redaction.test.mjs @@ -0,0 +1,82 @@ +import assert from "node:assert/strict"; +import { readFile } from "node:fs/promises"; +import { inspect } from "node:util"; +import test from "node:test"; +import { Vercel } from "../esm/index.js"; +import { HTTPClient } from "../esm/lib/http.js"; + +const API_TOKEN = "vercel_api_token_1234567890"; +const WEBHOOK_SECRET = "webhook_signing_secret_1234567890"; + +function captureLogger() { + const lines = []; + const record = (...args) => { + lines.push(args.map((arg) => inspect(arg, { depth: null })).join(" ")); + }; + return { + lines, + logger: { group: record, groupEnd: () => {}, log: record }, + }; +} + +function webhookClient() { + return new HTTPClient({ + fetcher: async () => + new Response( + JSON.stringify({ + id: "account_hook_1234567890", + url: "https://example.com/webhook", + events: ["deployment.created"], + ownerId: "team_1234567890", + createdAt: 1234567890123, + updatedAt: 1234567890123, + secret: WEBHOOK_SECRET, + }), + { + status: 200, + headers: { + "content-type": "application/json", + "set-cookie": `session=${API_TOKEN}`, + }, + }, + ), + }); +} + +test("redacts the API token and webhook secret from debug logs", async () => { + const { lines, logger } = captureLogger(); + const vercel = new Vercel({ + bearerToken: API_TOKEN, + httpClient: webhookClient(), + debugLogger: logger, + }); + + const result = await vercel.webhooks.createWebhook({ + teamId: "team_1234567890", + requestBody: { + url: "https://example.com/webhook", + events: ["deployment.created"], + }, + }); + + assert.equal(result.secret, WEBHOOK_SECRET); + + const output = lines.join("\n"); + assert.match(output, /authorization: \[REDACTED\]/i); + assert.match(output, /set-cookie: \[REDACTED\]/i); + assert.match(output, /secret: '\[REDACTED\]'/); + assert.match(output, /https:\/\/example\.com\/webhook/); + assert.doesNotMatch(output, new RegExp(API_TOKEN)); + assert.doesNotMatch(output, new RegExp(WEBHOOK_SECRET)); +}); + +test("preserves the debug logger redaction across generations", async () => { + const patch = await readFile( + ".speakeasy/patches/src/lib/sdks.ts.patch", + "utf8", + ); + assert.match(patch, /redactDebugLogger\(this\._options\.debugLogger\)/); + + const generatorConfig = await readFile(".speakeasy/gen.yaml", "utf8"); + assert.match(generatorConfig, /tests\/debug-logger-redaction\.test\.mjs/); +}); From 5ec7d0e60af3351be89959adb09b1e88719e19ee Mon Sep 17 00:00:00 2001 From: ismaelrumzan Date: Tue, 24 Nov 2026 11:25:58 +0300 Subject: [PATCH 2/2] Redact any value under a secret-like key in debug logs Speakeasy review feedback: `redactValue` only replaced string values under secret-like keys, so `{ secret: 12345 }`, `{ token: ["t1", "t2"] }`, and `{ clientSecret: { value: "..." } }` reached the logger unchanged. Arrays and objects under those keys were walked recursively, but their elements and nested keys did not match the key list, so they passed through. Any non-null value under a key that ends in secret, token, password, apikey, or privatekey is now replaced with `[REDACTED]`. Null and undefined still log as-is so a missing field is visible. Test: a new case in `tests/debug-logger-redaction.test.mjs` sends a response with a numeric `secret`, an array `token`, and an object `clientSecret` through `createWebhook`. It failed before this change (the numeric secret appeared in the log) and passes now. Co-Authored-By: Claude Opus 5.5 --- src/hooks/debug-logger.ts | 2 +- tests/debug-logger-redaction.test.mjs | 36 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/hooks/debug-logger.ts b/src/hooks/debug-logger.ts index e287b077e2..e92b03acce 100644 --- a/src/hooks/debug-logger.ts +++ b/src/hooks/debug-logger.ts @@ -57,7 +57,7 @@ function redactValue(value: unknown): unknown { } const redacted: Record = {}; for (const [key, entry] of Object.entries(value)) { - redacted[key] = typeof entry === "string" && isSensitiveKey(key) + redacted[key] = entry != null && isSensitiveKey(key) ? REDACTED : redactValue(entry); } diff --git a/tests/debug-logger-redaction.test.mjs b/tests/debug-logger-redaction.test.mjs index a1a251c9ac..0f6ceacc73 100644 --- a/tests/debug-logger-redaction.test.mjs +++ b/tests/debug-logger-redaction.test.mjs @@ -70,6 +70,42 @@ test("redacts the API token and webhook secret from debug logs", async () => { assert.doesNotMatch(output, new RegExp(WEBHOOK_SECRET)); }); +test("redacts non-string values under secret-like keys", async () => { + const { lines, logger } = captureLogger(); + const vercel = new Vercel({ + bearerToken: API_TOKEN, + httpClient: new HTTPClient({ + fetcher: async () => + new Response( + JSON.stringify({ + secret: 9876543210, + token: ["token_value_1", "token_value_2"], + clientSecret: { value: "nested_secret_value_1" }, + url: "https://example.com/webhook", + }), + { status: 200, headers: { "content-type": "application/json" } }, + ), + }), + debugLogger: logger, + }); + + await vercel.webhooks + .createWebhook({ + teamId: "team_1234567890", + requestBody: { + url: "https://example.com/webhook", + events: ["deployment.created"], + }, + }) + .catch(() => {}); + + const output = lines.join("\n"); + assert.match(output, /https:\/\/example\.com\/webhook/); + assert.doesNotMatch(output, /9876543210/); + assert.doesNotMatch(output, /token_value_/); + assert.doesNotMatch(output, /nested_secret_value_1/); +}); + test("preserves the debug logger redaction across generations", async () => { const patch = await readFile( ".speakeasy/patches/src/lib/sdks.ts.patch",