Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/cache-ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> | undefined | null): string | undefined {
if (!headers) return undefined;
let cred: string | undefined;
const values = new Set<string>();
const fields = new Set<string>();
for (const [name, value] of Object.entries(headers)) {
const n = name.toLowerCase();
if ((CREDENTIAL_HEADER_NAMES as readonly string[]).includes(n)) {
Expand All @@ -677,12 +678,18 @@ export function credentialFingerprint(headers: Record<string, string> | 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);
}

Expand Down
59 changes: 59 additions & 0 deletions tests/credential-set-fingerprint.test.ts
Original file line number Diff line number Diff line change
@@ -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];
});
}
});
Loading