From a10e38f71d5541012a14940f2f3f7d88c777ee29 Mon Sep 17 00:00:00 2001 From: Afloat Date: Wed, 11 Nov 2026 12:38:35 +0800 Subject: [PATCH] fix: stabilize identity for requests carrying multiple credentials --- src/cache-ledger.ts | 15 ++++-- tests/credential-set-fingerprint.test.ts | 59 ++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/credential-set-fingerprint.test.ts diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index bf6ad6ce62..3393e4ae5e 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -658,7 +658,8 @@ export const CREDENTIAL_HEADER_NAMES = ["authorization", "x-api-key", "api-key", * returns undefined when the lane sent no recognized credential header. */ export function credentialFingerprint(headers: Record | undefined | null): string | undefined { if (!headers) return undefined; - let cred: string | undefined; + const values = new Set(); + const fields = new Set(); for (const [name, value] of Object.entries(headers)) { const n = name.toLowerCase(); if ((CREDENTIAL_HEADER_NAMES as readonly string[]).includes(n)) { @@ -677,12 +678,18 @@ export function credentialFingerprint(headers: Record | undefine // per request. The STABLE credential on that line is the // Access key id — fingerprint only that. const m = /^SDK-HMAC-SHA256\s+Access=([^\s,]+)/i.exec(v); - cred = m !== null ? `sdk-hmac:${m[1]!}` : v; - break; + const credential = m !== null ? `sdk-hmac:${m[1]!}` : v; + values.add(credential); + fields.add(JSON.stringify([n, credential])); } } } - if (cred === undefined) return undefined; + if (values.size === 0) return undefined; + // Keep existing single-key identities, including aliases carrying the + // same key. Distinct credentials form an order-independent identity so + // header iteration order cannot bypass or cross-clear a throttle hold. + const cred = values.size === 1 ? values.values().next().value! + : `credential-set:${JSON.stringify([...fields].sort())}`; return "sha256:" + createHash("sha256").update(cred, "utf8").digest("hex").slice(0, 12); } diff --git a/tests/credential-set-fingerprint.test.ts b/tests/credential-set-fingerprint.test.ts new file mode 100644 index 0000000000..493c661a6a --- /dev/null +++ b/tests/credential-set-fingerprint.test.ts @@ -0,0 +1,59 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { credentialFingerprint } from "../src/cache-ledger.js"; +import { _resetUpstreamThrottleForTest, noteUpstreamThrottle429, noteUpstreamThrottleResolved, upstreamThrottleGate } from "../src/upstream-throttle.js"; + +const first = { authorization: "Bearer gateway-key", "x-api-key": "account-a" }; +const reordered = { "X-Api-Key": "account-a", Authorization: "Bearer gateway-key" }; +const other = { authorization: "Bearer gateway-key", "x-api-key": "account-b" }; + +test("credential-set identity is independent of header insertion order and casing", () => { + assert.equal(credentialFingerprint(first), credentialFingerprint(reordered)); +}); + +test("every distinct credential contributes to identity", () => { + assert.notEqual(credentialFingerprint(first), credentialFingerprint(other)); + assert.notEqual(credentialFingerprint(first), credentialFingerprint({ authorization: "Bearer account-a", "x-api-key": "gateway-key" })); +}); + +test("duplicate aliases for one credential preserve the single-key fingerprint", () => { + const single = credentialFingerprint({ authorization: "Bearer same" }); + assert.equal(single, credentialFingerprint({ "x-api-key": "same", Authorization: " Bearer same ", "x-goog-api-key": "same" })); + assert.equal(single, credentialFingerprint({ authorization: "Bearer same", "x-api-key": "", "content-type": "application/json" })); + assert.equal(credentialFingerprint({ "x-api-key": " " }), undefined); +}); + +test("multi-header SDK-HMAC fingerprints ignore request signatures while preserving account isolation", () => { + const signed = (key: string, signature: string) => `SDK-HMAC-SHA256 Access=${key}, SignedHeaders=content-type;x-sdk-date, Signature=${signature}`; + const a = credentialFingerprint({ authorization: signed("AK_A", "first"), "api-key": "gateway" }); + const b = credentialFingerprint({ "Api-Key": "gateway", Authorization: signed("AK_A", "second") }); + const c = credentialFingerprint({ authorization: signed("AK_B", "second"), "api-key": "gateway" }); + assert.equal(a, b); + assert.notEqual(a, c); +}); + +test("a 429 hold survives header reordering and cannot block or be cleared by another credential set", () => { + const keys = ["BILI_THROTTLE_429", "BILI_THROTTLE_429_BASE_DELAY_MS", "BILI_THROTTLE_429_MAX_DELAY_MS", "BILI_THROTTLE_429_MAX_CONSECUTIVE"] as const; + const saved = keys.map((key) => process.env[key]); + try { + process.env.BILI_THROTTLE_429 = "1"; + process.env.BILI_THROTTLE_429_BASE_DELAY_MS = "2000"; + process.env.BILI_THROTTLE_429_MAX_DELAY_MS = "30000"; + process.env.BILI_THROTTLE_429_MAX_CONSECUTIVE = "5"; + _resetUpstreamThrottleForTest(); + const url = "https://multi-key.example/v1/messages"; + noteUpstreamThrottle429(url, first, null, 1000000); + assert.equal(upstreamThrottleGate(url, reordered, 1000001).reject?.status, 429); + assert.equal(upstreamThrottleGate(url, other, 1000001).reject, undefined); + noteUpstreamThrottleResolved(url, other, 200, 1000002); + assert.equal(upstreamThrottleGate(url, first, 1000003).reject?.status, 429); + noteUpstreamThrottleResolved(url, reordered, 200, 1000004); + assert.equal(upstreamThrottleGate(url, first, 1000005).reject, undefined); + } finally { + _resetUpstreamThrottleForTest(); + keys.forEach((key, index) => { + if (saved[index] === undefined) delete process.env[key]; + else process.env[key] = saved[index]; + }); + } +});