Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

11.2 KB
Raw Blame
import test from "node:test";
import assert from "node:assert/strict";
import {
  containsSensitiveErrorCredential,
  redactSensitiveErrorText,
  sanitizeErrorMessage,
  sanitizeUpstreamDetails,
} from "../../open-sse/utils/errorSanitization.ts";

test("sanitizeErrorMessage removes bearer credentials and image data URLs", () => {
  const raw =
    "upstream echoed Authorization: Bearer eyJ.secret.token and data:image/png;charset=utf-8;base64,iVBORw0KGgoAAAANSUhEUgAAAAE=";
  const safe = sanitizeErrorMessage(raw);

  assert.doesNotMatch(safe, /eyJ\.secret\.token/);
  assert.doesNotMatch(safe, /iVBORw0KGgo/);
  assert.match(safe, /\[REDACTED\]/);
  // Authorization labels are fail-closed: once a credential label is seen,
  // the sanitizer may discard the remaining untrusted tail instead of
  // preserving a marker for each later secret.
  assert.equal(safe, "upstream echoed Authorization: [REDACTED]");
});

test("sanitizeErrorMessage redacts common JSON credential fields", () => {
  const safe = sanitizeErrorMessage(
    '{"api_key":"sk-sensitive","access_token":"oauth-sensitive","cookie":"session=sensitive; secondary=also-sensitive","authorization":"Basic dXNlcjpwYXNz"}'
  );

  assert.doesNotMatch(
    safe,
    /sk-sensitive|oauth-sensitive|session=sensitive|also-sensitive|dXNlcjpwYXNz/
  );
  assert.match(safe, /\[REDACTED\]/);
});

test("sanitizeErrorMessage redacts URL credentials while preserving safe URLs", () => {
  const safeUrl = "https://example.com/docs/error?lang=en#recovery";
  const projected = sanitizeErrorMessage(
    "proxy failed https://svc-user:[email protected]/v1 " +
      "then https://storage.example/blob?X-Amz-Credential=AKIAOPAQUE%2Fscope&" +
      "X-Amz-Signature=signature-secret&X-Amz-Expires=60 " +
      "and https://account.blob.core.windows.net/c?sv=2025-01-05&sig=sas-secret&se=soon " +
      "then https://vertex.example/predict?key=vertex-key-secret&mode=express " +
      "plus https://gateway.example/v1?api_key=query-api-secret&token=query-token-secret " +
      `see ${safeUrl}`
  );

  assert.doesNotMatch(
    projected,
    /svc-user|p4ss-opaque|AKIAOPAQUE|signature-secret|sas-secret|vertex-key-secret|query-api-secret|query-token-secret/i
  );
  assert.match(projected, /\[REDACTED\]/);
  assert.match(projected, /X-Amz-Expires=60/);
  assert.match(projected, /sv=2025-01-05/);
  assert.match(projected, /se=soon/);
  assert.match(projected, new RegExp(safeUrl.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")));
});

test("sanitizeErrorMessage redacts credentials hidden behind serialized whitespace", () => {
  const inputs = [
    String.raw`api_key\t=opaque-tab-secret-9382746`,
    String.raw`api_key\u0009=opaque-unicode-tab-9382746`,
    String.raw`Bearer\topaque-bearer-secret-9382746`,
    String.raw`api_key\\t=opaque-double-tab-secret-9382746`,
  ];

  for (const input of inputs) {
    const projected = sanitizeErrorMessage(input);
    assert.doesNotMatch(projected, /opaque-(?:tab|unicode-tab|bearer|double-tab)-secret/i);
    assert.match(projected, /\[REDACTED\]/);
  }
});

test("sanitizeErrorMessage redacts CLI credential flag values", () => {
  const inputs = [
    "spawn failed: helper --api-key opaque-cli-key-9382746 --mode check",
    'spawn failed: helper --token "opaque cli token 9382746" --mode check',
    "spawn failed: helper --password 'opaque-cli-password-9382746' --mode check",
  ];

  for (const input of inputs) {
    const projected = sanitizeErrorMessage(input);
    assert.doesNotMatch(projected, /opaque(?: cli|-cli)/i);
    assert.match(projected, /\[REDACTED\]/);
  }
});

test("sanitizeErrorMessage covers the canonical credential pattern catalog", () => {
  const credentials = [
    `AIza${"A".repeat(35)}`,
    `hf_${"A".repeat(34)}`,
    `r8_${"A".repeat(37)}`,
    `gho_${"A".repeat(36)}`,
    `ghu_${"A".repeat(36)}`,
    `ghs_${"A".repeat(36)}`,
    `ghr_${"A".repeat(36)}`,
    `lin_api_${"A".repeat(40)}`,
    `secret_${"A".repeat(43)}`,
    `npm_${"A".repeat(36)}`,
    `PMAK-1234abcd-${"a".repeat(32)}`,
    `rk_live_${"A".repeat(24)}`,
    `sq0atp-${"A".repeat(22)}`,
    `SK${"a".repeat(32)}`,
    `SG.${"A".repeat(22)}.${"B".repeat(43)}`,
    `key-${"a".repeat(32)}`,
    `M${"A".repeat(23)}.${"B".repeat(6)}.${"C".repeat(27)}`,
    "postgresql://db-user:[email protected]/app",
    // GHSA-r4q7-7f24-m29p — Groq and xAI keys had no catalog entry and no
    // STRONG_CREDENTIAL_TOKEN fallback, so they reached error bodies verbatim.
    `gsk_${"A".repeat(52)}`,
    `xai-${"A".repeat(80)}`,
    // House access tokens (`oma_live_` + 43 base64url chars) had no catalog
    // entry either and reached error bodies verbatim.
    `oma_live_${"A".repeat(43)}`,
  ];

  for (const credential of credentials) {
    const projected = sanitizeErrorMessage(`upstream echoed ${credential}`);
    assert.equal(projected.includes(credential), false, credential.slice(0, 16));
    assert.match(projected, /\[REDACTED(?::[^\]]+)?\]/);
  }
});

// House access tokens (`oma_live_` + base64url) had no catalog entry and no
// strong-token fallback, so they reached error bodies verbatim. The sanitizer
// stamps the generic `[REDACTED]` label (catalog names are dropped on this
// path); the guardrail keeps the specific `[REDACTED:oma]` label.
const OMA_SANITIZER_TOKEN = `oma_live_${"A".repeat(43)}`;

test("sanitizeErrorMessage masks house tokens on every detection path", () => {
  for (const input of [
    `upstream echoed ${OMA_SANITIZER_TOKEN}`,
    `request failed for key ${OMA_SANITIZER_TOKEN} on model foo`,
  ]) {
    const viaMessage = sanitizeErrorMessage(input);
    assert.equal(viaMessage.includes(OMA_SANITIZER_TOKEN), false, input);
    assert.match(viaMessage, /\[REDACTED/);

    const viaText = redactSensitiveErrorText(input);
    assert.equal(viaText.includes(OMA_SANITIZER_TOKEN), false, input);
    assert.match(viaText, /\[REDACTED/);

    assert.equal(containsSensitiveErrorCredential(input), true, input);
  }
});

test("sanitizeErrorMessage leaves neighboring verdicts and short look-alikes untouched", () => {
  // Neighboring `sk-ant-` label unchanged (gate criterion).
  const neighbor = `upstream echoed sk-ant-${"A".repeat(40)}`;
  assert.match(sanitizeErrorMessage(neighbor), /\[REDACTED/);

  // Short look-alikes and a bare prefix stay intact on every detection path.
  for (const benign of [
    "upstream error without any credential",
    "see oma_live_xxx in the setup guide",
    `rejected oma_live_${"A".repeat(8)} retry`,
    "oma_live_not_a_real_token",
    `rejected oma_live_${"A".repeat(19)} retry`,
  ]) {
    assert.equal(sanitizeErrorMessage(benign), benign, benign);
    assert.equal(redactSensitiveErrorText(benign), benign, benign);
    assert.equal(containsSensitiveErrorCredential(benign), false, benign);
  }

  // Lower bound: a 20-char suffix is masked on every detection path.
  const bound = `rejected oma_live_${"A".repeat(20)} retry`;
  assert.equal(sanitizeErrorMessage(bound).includes(`oma_live_${"A".repeat(20)}`), false);
  assert.match(sanitizeErrorMessage(bound), /\[REDACTED/);
  assert.equal(containsSensitiveErrorCredential(bound), true);
});

test("sanitizeErrorMessage handles glued prefixes, casing, encoding and off-charset tails", () => {
  // A letter or digit glued in front makes it part of a larger word: intact.
  for (const prefix of ["x", "9"]) {
    const input = `failed ${prefix}${OMA_SANITIZER_TOKEN} end`;
    assert.equal(sanitizeErrorMessage(input), input, input);
    assert.equal(containsSensitiveErrorCredential(input), false, input);
  }
  // A dash or space keeps the token standalone: masked.
  for (const prefix of ["-", " "]) {
    const input = `failed${prefix}${OMA_SANITIZER_TOKEN} end`;
    assert.equal(sanitizeErrorMessage(input).includes(OMA_SANITIZER_TOKEN), false, input);
    assert.match(sanitizeErrorMessage(input), /\[REDACTED/);
  }
  // An underscore glued in front hides it from the word-boundary catalog but
  // the strong-token fallback still catches it on sanitizer paths.
  const glued = `failed_${OMA_SANITIZER_TOKEN} end`;
  assert.equal(sanitizeErrorMessage(glued).includes(OMA_SANITIZER_TOKEN), false, glued);
  assert.equal(containsSensitiveErrorCredential(glued), true, glued);

  // Uppercased prefix: the strong-token fallback is case-insensitive, so the
  // sanitizer still masks it (the catalog-only guardrail does not).
  const upper = `failed OMA_LIVE_${"A".repeat(43)} end`;
  assert.match(sanitizeErrorMessage(upper), /\[REDACTED/);
  assert.equal(containsSensitiveErrorCredential(upper), true, upper);

  // Double-escaped token: escape normalization decodes it before the catalog
  // pass, so it is masked.
  const doubled = `failed oma_live_\\u0041${"A".repeat(42)} end`;
  assert.match(sanitizeErrorMessage(doubled), /\[REDACTED/);

  // Off-charset tail (`.` is outside base64url): matched up to the dot, like
  // the neighboring loose-bound patterns.
  const dotted = sanitizeErrorMessage(`failed ${OMA_SANITIZER_TOKEN}.suffix end`);
  assert.equal(dotted.includes(OMA_SANITIZER_TOKEN), false, dotted);

  // Bare prefix without a suffix stays intact.
  assert.equal(sanitizeErrorMessage("saw oma_live_ in logs"), "saw oma_live_ in logs");
});

test("upstream bodies echoing house tokens are refused passthrough", () => {
  assert.equal(
    containsSensitiveErrorCredential(`{"message":"bad key ${OMA_SANITIZER_TOKEN}}"}`),
    true
  );
});

test("sanitizeErrorMessage redacts credentials that cross the public length boundary", () => {
  const credential = `hf_${"A".repeat(34)}`;
  const projected = sanitizeErrorMessage(`${"x".repeat(4088)}${credential}`);
  const escapedPrefixProjected = sanitizeErrorMessage(
    `${String.raw`\t`}${"x".repeat(4088)}${credential}`
  );

  for (const output of [projected, escapedPrefixProjected]) {
    assert.equal(output.includes("hf_"), false);
    assert.equal(output.includes(credential), false);
    assert.match(output, /\[REDACTED(?::[^\]]+)?\]$/);
    assert.ok(output.length <= 4096);
  }
});

test("sanitizeErrorMessage redacts closed and unterminated PGP private-key armor", () => {
  const closed = sanitizeErrorMessage(
    "provider returned -----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
      "Version: test\n\npgp-private-material\n" +
      "-----END PGP PRIVATE KEY BLOCK----- after"
  );
  const unterminated = sanitizeErrorMessage(
    "provider returned -----BEGIN PGP PRIVATE KEY BLOCK-----\npgp-unterminated-material"
  );

  // Public exception messages fail closed at the first physical line; the
  // post-block suffix is intentionally not recovered from a multiline secret.
  assert.equal(closed, "provider returned [REDACTED]");
  assert.equal(unterminated, "provider returned [REDACTED]");
  assert.doesNotMatch(
    `${closed} ${unterminated}`,
    /pgp-private-material|pgp-unterminated-material/
  );
});

test("sanitizeUpstreamDetails drops credential headers and redacts data URLs", () => {
  const safe = sanitizeUpstreamDetails({
    authorization: "Bearer sensitive",
    cookie: "session=sensitive; refresh=also-sensitive",
    "set-cookie": "session=sensitive",
    error: "failed for data:image/webp;base64,UklGRgAAAAA=",
  }) as Record<string, unknown>;

  assert.equal("authorization" in safe, false);
  assert.equal("cookie" in safe, false);
  assert.equal("set-cookie" in safe, false);
  assert.equal(safe.error, "failed for [REDACTED_DATA_URL]");
});