@clawchatsai/connector 0.1.49 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -6
- package/dist/auth-handler.d.ts +41 -20
- package/dist/auth-handler.js +136 -106
- package/dist/channel-binding.d.ts +57 -0
- package/dist/channel-binding.js +73 -0
- package/dist/config-store.d.ts +14 -6
- package/dist/config-store.js +58 -11
- package/dist/devices-cli.d.ts +16 -0
- package/dist/devices-cli.js +90 -0
- package/dist/index.js +76 -16
- package/dist/plugin-config.d.ts +12 -0
- package/dist/session-token.d.ts +6 -39
- package/dist/session-token.js +6 -95
- package/dist/shim.js +13 -10
- package/dist/totp.d.ts +8 -0
- package/dist/totp.js +16 -0
- package/dist/webrtc-peer.d.ts +2 -0
- package/dist/webrtc-peer.js +15 -1
- package/package.json +1 -1
- package/server/approval-client.test.js +1 -1
- package/server/controllers/title.js +3 -1
- package/server/controllers/title.test.js +8 -1
- package/server/gateway.js +21 -3
- package/server/gateway.test.js +44 -0
- package/server/history-export.js +5 -1
- package/server/history-export.test.js +63 -0
- package/server/index.js +4 -1
- package/server/peer/guest-agent.js +41 -8
- package/server/peer/guest-agent.test.js +50 -1
- package/server/peer/keys.js +12 -0
- package/server/peer/sharing.js +17 -3
- package/server/peer/sharing.test.js +42 -4
- package/server/peer/wordlist.js +3 -0
- package/server/team.js +15 -6
- package/server/team.test.js +48 -4
- package/server/util/gateway-retry.js +58 -0
- package/server/util/gateway-retry.test.js +78 -0
- package/server/util/redact.js +84 -0
- package/server/util/redact.test.js +97 -0
- package/dist/google-jwt.d.ts +0 -34
- package/dist/google-jwt.js +0 -87
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Retry for the connector's OWN gateway requests (team chat, sharing, guest agents, the session lens), never for
|
|
2
|
+
// frames a browser relays (the frontend retries those itself).
|
|
3
|
+
//
|
|
4
|
+
// 2026.11.4+ refuses write RPCs (chat.send, sessions.create/patch/send, agent, ...) with a retryable error while the
|
|
5
|
+
// gateway is starting or its agent DB is preparing: { code: 'UNAVAILABLE', retryable: true, retryAfterMs,
|
|
6
|
+
// details: { reason: 'startup-sidecars' } }. chat.send also answers retryable UNAVAILABLE while the same request is
|
|
7
|
+
// being admitted, the transcript rebuilds, or the model catalog races. Older gateways never send these, so the
|
|
8
|
+
// wrapper is a pass-through there. gateway.js request() keeps the error fields (gatewayCode, retryable, ...).
|
|
9
|
+
|
|
10
|
+
const MAX_TOTAL_MS = 60_000; // cap on time spent waiting between attempts
|
|
11
|
+
const MIN_DELAY_MS = 100;
|
|
12
|
+
const MAX_DELAY_MS = 2_000; // same bounds as the gateway's own client
|
|
13
|
+
const DEFAULT_DELAY_MS = 500;
|
|
14
|
+
// Retryable refusals a retry makes worse or cannot fix: the socket drops on a restart anyway; a busy goal needs the user.
|
|
15
|
+
const NEVER_RETRY_REASONS = new Set(['gateway-restarting', 'goal-session-busy']);
|
|
16
|
+
|
|
17
|
+
const wait = ms => new Promise(r => setTimeout(r, ms));
|
|
18
|
+
|
|
19
|
+
/** The refusal that happens before the method does any work (safe to repeat for every method). */
|
|
20
|
+
export function isAdmissionRefusal(e) {
|
|
21
|
+
if (e?.gatewayCode !== 'UNAVAILABLE' || e.retryable !== true) return false;
|
|
22
|
+
return e.details?.reason === 'startup-sidecars' || e.details?.code === 'agent-database-inspection-pending';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Whether `method` failing with `e` is worth repeating unchanged. */
|
|
26
|
+
export function isRetryableRefusal(method, e) {
|
|
27
|
+
if (e?.gatewayCode !== 'UNAVAILABLE' || e.retryable !== true) return false;
|
|
28
|
+
if (NEVER_RETRY_REASONS.has(e.details?.reason)) return false;
|
|
29
|
+
if (isAdmissionRefusal(e)) return true;
|
|
30
|
+
// chat.send carries an idempotencyKey, so repeating it can't send twice ("retry the same request").
|
|
31
|
+
return method === 'chat.send';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function retryDelay(e) {
|
|
35
|
+
const ms = Number.isFinite(e.retryAfterMs) ? Math.floor(e.retryAfterMs) : DEFAULT_DELAY_MS;
|
|
36
|
+
return Math.min(Math.max(ms, MIN_DELAY_MS), MAX_DELAY_MS);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Call `request(method, params, timeoutMs)`, repeating it after a retryable refusal, after the gateway's retryAfterMs,
|
|
41
|
+
* until `maxMs` of waiting is used up; then the last refusal is thrown. Other errors pass straight through.
|
|
42
|
+
*/
|
|
43
|
+
export async function requestWithRetry(request, method, params, timeoutMs, { maxMs = MAX_TOTAL_MS, sleep = wait } = {}) {
|
|
44
|
+
let waited = 0;
|
|
45
|
+
for (;;) {
|
|
46
|
+
try { return await request(method, params, timeoutMs); }
|
|
47
|
+
catch (e) {
|
|
48
|
+
if (!isRetryableRefusal(method, e)) throw e;
|
|
49
|
+
const delay = retryDelay(e);
|
|
50
|
+
if (waited + delay > maxMs) throw e;
|
|
51
|
+
waited += delay;
|
|
52
|
+
await sleep(delay);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** `request` with the retry above; same signature. */
|
|
58
|
+
export const withGatewayRetry = (request, opts) => (method, params, timeoutMs) => requestWithRetry(request, method, params, timeoutMs, opts);
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { requestWithRetry, withGatewayRetry, isRetryableRefusal } from './gateway-retry.js';
|
|
4
|
+
|
|
5
|
+
const refusal = (extra = {}) => Object.assign(new Error('chat.send unavailable during gateway startup'), {
|
|
6
|
+
gatewayCode: 'UNAVAILABLE', retryable: true, retryAfterMs: 500, details: { reason: 'startup-sidecars', method: 'chat.send' }, ...extra,
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
/** A request that fails with `errors` in turn, then answers. Records the sleeps. */
|
|
10
|
+
function scripted(errors, answer = { ok: true }) {
|
|
11
|
+
const calls = [];
|
|
12
|
+
const sleeps = [];
|
|
13
|
+
const request = async (method, params, t) => { calls.push({ method, params, t }); const e = errors[calls.length - 1]; if (e) throw e; return answer; };
|
|
14
|
+
return { request, calls, sleeps, sleep: async ms => { sleeps.push(ms); } };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
test('a startup refusal is retried after retryAfterMs, then the answer comes back', async () => {
|
|
18
|
+
const s = scripted([refusal(), refusal({ retryAfterMs: 1200 })]);
|
|
19
|
+
const res = await requestWithRetry(s.request, 'sessions.create', { key: 'k' }, 7000, { sleep: s.sleep });
|
|
20
|
+
assert.deepEqual(res, { ok: true });
|
|
21
|
+
assert.equal(s.calls.length, 3);
|
|
22
|
+
assert.deepEqual(s.calls.map(c => c.params), [{ key: 'k' }, { key: 'k' }, { key: 'k' }]); // same request each time
|
|
23
|
+
assert.equal(s.calls[0].t, 7000);
|
|
24
|
+
assert.deepEqual(s.sleeps, [500, 1200]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('the wait is bounded: delays are clamped, and the last refusal is thrown once ~60 s are used up', async () => {
|
|
28
|
+
const s = scripted(Array.from({ length: 1000 }, () => refusal({ retryAfterMs: 60_000 })));
|
|
29
|
+
const err = await requestWithRetry(s.request, 'chat.send', {}, 1000, { sleep: s.sleep }).catch(e => e);
|
|
30
|
+
assert.equal(err.gatewayCode, 'UNAVAILABLE');
|
|
31
|
+
assert.ok(s.sleeps.every(ms => ms === 2000)); // retryAfterMs clamped to 2 s
|
|
32
|
+
assert.equal(s.sleeps.reduce((a, b) => a + b, 0), 60_000);
|
|
33
|
+
assert.equal(s.calls.length, 31);
|
|
34
|
+
const s2 = scripted(Array.from({ length: 10 }, () => refusal({ retryAfterMs: undefined })));
|
|
35
|
+
await requestWithRetry(s2.request, 'chat.send', {}, 1000, { sleep: s2.sleep });
|
|
36
|
+
assert.deepEqual(s2.sleeps, Array(10).fill(500)); // no hint: the gateway's default
|
|
37
|
+
const s3 = scripted(Array.from({ length: 10 }, () => refusal({ retryAfterMs: 5 })));
|
|
38
|
+
await requestWithRetry(s3.request, 'chat.send', {}, 1000, { sleep: s3.sleep });
|
|
39
|
+
assert.ok(s3.sleeps.every(ms => ms === 100));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('other errors are not retried: plain failures, non-retryable UNAVAILABLE, persisted config writes, restarts', async () => {
|
|
43
|
+
const cases = [
|
|
44
|
+
['chat.send', new Error('boom')],
|
|
45
|
+
['chat.send', Object.assign(new Error('x'), { gatewayCode: 'INVALID_REQUEST', retryable: true })],
|
|
46
|
+
['chat.send', Object.assign(new Error('x'), { gatewayCode: 'UNAVAILABLE' })], // retryable missing
|
|
47
|
+
['config.patch', Object.assign(new Error('persisted'), { gatewayCode: 'UNAVAILABLE', details: { persistedConfig: {} } })],
|
|
48
|
+
['chat.send', refusal({ details: { reason: 'gateway-restarting' } })],
|
|
49
|
+
['chat.send', refusal({ details: { reason: 'goal-session-busy' } })],
|
|
50
|
+
['config.patch', Object.assign(new Error('x'), { gatewayCode: 'UNAVAILABLE', retryable: true, details: { reason: 'something-else' } })], // not an admission refusal, not chat.send
|
|
51
|
+
];
|
|
52
|
+
for (const [method, error] of cases) {
|
|
53
|
+
const s = scripted([error]);
|
|
54
|
+
const err = await requestWithRetry(s.request, method, {}, 1000, { sleep: s.sleep }).catch(e => e);
|
|
55
|
+
assert.equal(err, error);
|
|
56
|
+
assert.equal(s.calls.length, 1, `${method}: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('chat.send is also retried for its other retryable refusals (same idempotencyKey, so it can not send twice)', async () => {
|
|
61
|
+
const admitting = Object.assign(new Error('Chat input is being admitted; retry the same request.'), { gatewayCode: 'UNAVAILABLE', retryable: true });
|
|
62
|
+
const rebuilding = Object.assign(new Error('session transcript is rebuilding; retry shortly'), { gatewayCode: 'UNAVAILABLE', retryable: true, retryAfterMs: 250, details: { method: 'chat.send' } });
|
|
63
|
+
const s = scripted([admitting, rebuilding]);
|
|
64
|
+
await requestWithRetry(s.request, 'chat.send', { idempotencyKey: 'r1' }, 1000, { sleep: s.sleep });
|
|
65
|
+
assert.deepEqual(s.sleeps, [500, 250]);
|
|
66
|
+
assert.ok(s.calls.every(c => c.params.idempotencyKey === 'r1'));
|
|
67
|
+
assert.equal(isRetryableRefusal('sessions.patch', admitting), false); // only chat.send carries an idempotency key
|
|
68
|
+
// An agent DB still being inspected refuses before any work, for every method.
|
|
69
|
+
const dbPending = Object.assign(new Error('agent database inspection pending'), { gatewayCode: 'UNAVAILABLE', retryable: true, retryAfterMs: 250, details: { code: 'agent-database-inspection-pending' } });
|
|
70
|
+
assert.equal(isRetryableRefusal('sessions.create', dbPending), true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('withGatewayRetry keeps the request signature and passes a healthy gateway through untouched', async () => {
|
|
74
|
+
const seen = [];
|
|
75
|
+
const request = withGatewayRetry(async (m, p, t) => { seen.push([m, p, t]); return { m }; });
|
|
76
|
+
assert.deepEqual(await request('agents.list', {}, 5000), { m: 'agents.list' });
|
|
77
|
+
assert.deepEqual(seen, [['agents.list', {}, 5000]]);
|
|
78
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Secret redaction for text that leaves this gateway (guest replies to other people, exported history).
|
|
2
|
+
//
|
|
3
|
+
// Since 2026.10.10 (#169063) the gateway no longer masks credentials in tool arguments and results, so an agent that
|
|
4
|
+
// read a .env or printed a token can pass it on in plain text. This is a conservative port of the common patterns from
|
|
5
|
+
// the gateway's own redactor (src/logging/redact-patterns.ts, 2026.11.6): vendor token prefixes, bearer/Authorization
|
|
6
|
+
// values, URL credentials, private key blocks, and secret-named assignments. It favours not touching ordinary prose
|
|
7
|
+
// over catching every possible secret; it is a safety net, not a vault.
|
|
8
|
+
|
|
9
|
+
export const REDACTED = '[REDACTED]';
|
|
10
|
+
|
|
11
|
+
// Private keys first: the body is base64 that other patterns would otherwise cut into pieces. An unterminated block
|
|
12
|
+
// (a truncated tool result) is redacted to the end of the text.
|
|
13
|
+
const PEM = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g;
|
|
14
|
+
|
|
15
|
+
// Token shapes with a distinctive prefix. Group 1 is the secret (some patterns keep a leading boundary character).
|
|
16
|
+
const VENDOR_TOKENS = [
|
|
17
|
+
/\b(sk-[A-Za-z0-9_-]{8,})\b/g, // OpenAI, Anthropic (sk-ant-...), OpenRouter, ...
|
|
18
|
+
/(gh[pousr]_[A-Za-z0-9]{10,})/g, // GitHub ghp_ gho_ ghu_ ghs_ ghr_
|
|
19
|
+
/(github_pat_[A-Za-z0-9_]{10,})/g,
|
|
20
|
+
/(glpat-[A-Za-z0-9._=-]{20,})/g, // GitLab
|
|
21
|
+
/(xox[baprs]-[A-Za-z0-9-]{10,})/g, // Slack
|
|
22
|
+
/(xapp-[A-Za-z0-9-]{10,})/g,
|
|
23
|
+
/(https:\/\/hooks\.slack\.com\/(?:services|workflows)\/[A-Za-z0-9/]{20,})/g,
|
|
24
|
+
/(https:\/\/discord(?:app)?\.com\/api\/webhooks\/[0-9]{17,20}\/[A-Za-z0-9_-]{40,})/g,
|
|
25
|
+
/(gsk_[A-Za-z0-9_-]{10,})/g, // Groq
|
|
26
|
+
/(AIza[0-9A-Za-z_-]{30,})/g, // Google API keys (39 chars)
|
|
27
|
+
/(ya29\.[0-9A-Za-z_./+=-]{10,})/g, // Google OAuth access tokens
|
|
28
|
+
/(GOCSPX-[A-Za-z0-9_-]{20,})/g,
|
|
29
|
+
/(eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})/g, // JWTs
|
|
30
|
+
/(pplx-[A-Za-z0-9_-]{10,})/g,
|
|
31
|
+
/(xai-[A-Za-z0-9]{30,})/g,
|
|
32
|
+
/(hf_[A-Za-z0-9]{10,})/g, // Hugging Face
|
|
33
|
+
/(npm_[A-Za-z0-9]{10,})/g,
|
|
34
|
+
/(pypi-[A-Za-z0-9_-]{10,})/g,
|
|
35
|
+
/([sr]k_(?:live|test)_[A-Za-z0-9]{10,})/g, // Stripe
|
|
36
|
+
/(SG\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,})/g, // SendGrid
|
|
37
|
+
/((?:^|[^A-Za-z0-9])(?:AKIA|ASIA)[A-Z0-9]{16}(?![A-Za-z0-9]))/g, // AWS access key ids
|
|
38
|
+
/(\d{8,10}:AA[A-Za-z0-9_-]{33})/g, // Telegram bot tokens
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
// Authorization: Bearer xxx / Basic xxx / Token xxx (headers, curl -H "...", JSON "Authorization": "..."): keep the scheme.
|
|
42
|
+
const AUTH_HEADER = /(authorization["']?\s*[:=]\s*["']?(?:bearer|basic|token|bot)\s+)([A-Za-z0-9._~+/=-]{8,})/gi;
|
|
43
|
+
// A bare "Bearer <long token>" outside a header. 18+ characters: shorter is prose ("bearer of bad news").
|
|
44
|
+
const BEARER = /\b(bearer\s+)([A-Za-z0-9._~+/=-]{18,})(?![A-Za-z0-9._~+/=-])/gi;
|
|
45
|
+
// user:password@ in a URL: keep the scheme, user and host.
|
|
46
|
+
const URL_CREDENTIALS = /\b([a-z][a-z0-9+.-]{1,15}:\/\/[^\s/@:"'<>]+:)([^\s/@"'<>]{1,200})(@)/gi;
|
|
47
|
+
// ENV_STYLE_NAME=value / "NAME": "value" where the name ends in KEY/TOKEN/SECRET/PASSWORD: upper-case names only, so
|
|
48
|
+
// "monkey: 3" or "the token: here" stay.
|
|
49
|
+
const ENV_ASSIGNMENT = /\b((?:[A-Z][A-Z0-9]*_)*(?:API_?KEY|ACCESS_?KEY|SECRET_?KEY|PRIVATE_?KEY|AUTH_?TOKEN|ACCESS_?TOKEN|REFRESH_?TOKEN|TOKEN|SECRET|PASSWORD|PASSWD|PASSPHRASE|CLIENT_?SECRET)\b["']?\s*[=:]\s*)(["']?)(?!\[REDACTED\])([^\s"'\\,;]{6,})\2/g;
|
|
50
|
+
// JSON/YAML keys naming a secret, with a quoted value ("apiKey": "...", \"password\": \"...\" inside an escaped string).
|
|
51
|
+
const JSON_FIELD = /(\\*"(?:api[-_]?key|apikey|x-api-key|access[-_]?token|refresh[-_]?token|id[-_]?token|auth[-_]?token|client[-_]?secret|private[-_]?key|secret|password|passwd|passphrase|token|bearer[-_]?token|authorization|proxy-authorization|cookie|set-cookie)\\*"\s*:\s*\\*")(?!(?:bearer\s+|basic\s+|token\s+|bot\s+)?\[REDACTED\])([^"\\]{3,})/gi;
|
|
52
|
+
// key=value in query strings, CLI flags and config lines (lower-case): the value stops at whitespace, &, quotes.
|
|
53
|
+
const KEY_VALUE = /((?:^|[\s?&,;({[])(?:--?)?(?:access[-_]?token|refresh[-_]?token|id[-_]?token|api[-_]?key|apikey|client[-_]?secret|secret[-_]?key|private[-_]?key|password|passwd|passphrase|secret|token)=)(["'`]?)(?!\[REDACTED\])([^\s&#"'`<>]{4,})/gi;
|
|
54
|
+
// "password: hunter2" in a config or a chat: only a value that looks like a credential (has a digit or symbol), so
|
|
55
|
+
// "password: required" or "token: none" in prose stay.
|
|
56
|
+
const COLON_PASSWORD = /(\b(?:password|passwd|passphrase)\s*:\s*)(["'`]?)(?!\[REDACTED\])([^\s"'`,;]{6,})/gi;
|
|
57
|
+
|
|
58
|
+
const looksLikeCredential = v => /[^A-Za-z]/.test(v);
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* `text` with credentials replaced by [REDACTED]. Non-strings pass through. `onRedact` (optional) is called once per
|
|
62
|
+
* replacement, for callers that count.
|
|
63
|
+
* @param {unknown} text
|
|
64
|
+
* @param {() => void} [onRedact]
|
|
65
|
+
*/
|
|
66
|
+
export function redactSecrets(text, onRedact) {
|
|
67
|
+
if (typeof text !== 'string' || text.length < 8) return text;
|
|
68
|
+
const hit = () => { onRedact?.(); return REDACTED; };
|
|
69
|
+
let out = text.replace(PEM, hit);
|
|
70
|
+
out = out.replace(AUTH_HEADER, (_m, head) => head + hit());
|
|
71
|
+
out = out.replace(BEARER, (_m, head) => head + hit());
|
|
72
|
+
out = out.replace(URL_CREDENTIALS, (_m, head, _pw, at) => head + hit() + at);
|
|
73
|
+
for (const re of VENDOR_TOKENS) {
|
|
74
|
+
out = out.replace(re, (match, secret) => {
|
|
75
|
+
const bare = secret.replace(/^[^A-Za-z0-9]/, '');
|
|
76
|
+
return match.replace(bare, hit);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
out = out.replace(ENV_ASSIGNMENT, (_m, head, quote, _v) => head + quote + hit() + quote);
|
|
80
|
+
out = out.replace(JSON_FIELD, (_m, head) => head + hit());
|
|
81
|
+
out = out.replace(KEY_VALUE, (_m, head, quote) => head + quote + hit());
|
|
82
|
+
out = out.replace(COLON_PASSWORD, (m, head, quote, value) => (looksLikeCredential(value) ? head + quote + hit() : m));
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { redactSecrets, REDACTED } from './redact.js';
|
|
4
|
+
|
|
5
|
+
const R = REDACTED;
|
|
6
|
+
const same = text => assert.equal(redactSecrets(text), text, `should be untouched: ${text}`);
|
|
7
|
+
|
|
8
|
+
test('vendor token prefixes are redacted', () => {
|
|
9
|
+
// Fake tokens, assembled at runtime so GitHub push protection doesn't take the fixtures for real secrets.
|
|
10
|
+
const secrets = [
|
|
11
|
+
['sk-ant-', 'api03-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789'],
|
|
12
|
+
['sk-proj-', 'abcdefghijklmnop1234'],
|
|
13
|
+
['ghp_', 'abcdefghijklmnopqrstuvwxyz0123456789'],
|
|
14
|
+
['github_pat_', '11ABCDEFG0abcdefghijkl_mnopqrstuvwxyz'],
|
|
15
|
+
['xox', 'b-123456789012-abcdefghijklmnop'],
|
|
16
|
+
['xapp-', '1-A0123456789-abcdefghij'],
|
|
17
|
+
['AIza', 'SyA1234567890abcdefghijklmnopqrstuv'],
|
|
18
|
+
['gsk_', 'abcdefghijklmnopqrstuvwx'],
|
|
19
|
+
['npm_', 'abcdefghijklmnopqrstuvwxyz0123'],
|
|
20
|
+
['hf_', 'abcdefghijklmnopqrstuv'],
|
|
21
|
+
['sk_live_', 'abcdefghijklmnop1234'],
|
|
22
|
+
['AKIA', 'IOSFODNN7EXAMPLE'],
|
|
23
|
+
['eyJhbGciOiJIUzI1NiJ9.', 'eyJzdWIiOiIxMjM0NTY3ODkwIn0.dBjftJeZ4CVPmB92K27uhbUJU1p1r_wW1gFWFOEjXk'],
|
|
24
|
+
].map(p => p.join(''));
|
|
25
|
+
for (const s of secrets) {
|
|
26
|
+
const out = redactSecrets(`the key is ${s} ok`);
|
|
27
|
+
assert.equal(out, `the key is ${R} ok`, s);
|
|
28
|
+
}
|
|
29
|
+
assert.equal(redactSecrets('use AKIAIOSFODNN7EXAMPLE here'), `use ${R} here`);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('private key blocks are redacted, also when truncated', () => {
|
|
33
|
+
const block = '-----BEGIN RSA PRIVATE KEY-----\nMIIEowIBAAKCAQEA1234\nabcdEFGH\n-----END RSA PRIVATE KEY-----';
|
|
34
|
+
assert.equal(redactSecrets(`before\n${block}\nafter`), `before\n${R}\nafter`);
|
|
35
|
+
assert.equal(redactSecrets('-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAA\nQyNTUxOQAAACD'), R);
|
|
36
|
+
same('-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A\n-----END PUBLIC KEY-----');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('Authorization headers and bearer tokens keep their scheme', () => {
|
|
40
|
+
assert.equal(redactSecrets('curl -H "Authorization: Bearer abcdef1234567890XYZ" https://x.test'), `curl -H "Authorization: Bearer ${R}" https://x.test`);
|
|
41
|
+
assert.equal(redactSecrets('Authorization: Basic dXNlcjpwYXNzd29yZA=='), `Authorization: Basic ${R}`);
|
|
42
|
+
assert.equal(redactSecrets('{"authorization":"Bearer tok_1234567890abcdef"}'), `{"authorization":"Bearer ${R}"}`);
|
|
43
|
+
assert.equal(redactSecrets('sent Bearer abcdefghijklmnopqrstuvwxyz0123456789 upstream'), `sent Bearer ${R} upstream`);
|
|
44
|
+
same('He was a bearer of bad news and a Bearer token is how you authenticate.');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('passwords in URLs are redacted, hosts and ports are not', () => {
|
|
48
|
+
assert.equal(redactSecrets('postgres://admin:s3cr3tPass@db.internal:5432/app'), `postgres://admin:${R}@db.internal:5432/app`);
|
|
49
|
+
assert.equal(redactSecrets('git clone https://bob:ghs_x@github.com/o/r'), `git clone https://bob:${R}@github.com/o/r`);
|
|
50
|
+
same('https://example.com:8080/path?a=b@c');
|
|
51
|
+
same('git@github.com:org/repo.git');
|
|
52
|
+
same('ssh://git@host:22/repo');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('secret-named assignments are redacted', () => {
|
|
56
|
+
assert.equal(redactSecrets('ANTHROPIC_API_KEY=abc123def456ghi'), `ANTHROPIC_API_KEY=${R}`);
|
|
57
|
+
assert.equal(redactSecrets('export DB_PASSWORD="hunter2hunter2"'), `export DB_PASSWORD="${R}"`);
|
|
58
|
+
assert.equal(redactSecrets('{"apiKey": "abcd1234efgh", "name": "x"}'), `{"apiKey": "${R}", "name": "x"}`);
|
|
59
|
+
assert.equal(redactSecrets('{\\"password\\": \\"hunter22\\"}'), `{\\"password\\": \\"${R}\\"}`);
|
|
60
|
+
assert.equal(redactSecrets('https://api.test/v1?token=abcd1234efgh&x=1'), `https://api.test/v1?token=${R}&x=1`);
|
|
61
|
+
assert.equal(redactSecrets('run --api-key=abcd1234efgh now'), `run --api-key=${R} now`);
|
|
62
|
+
assert.equal(redactSecrets('password: hunter2!'), `password: ${R}`);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('ordinary text is left alone', () => {
|
|
66
|
+
same('The password policy requires 12 characters and a token refresh every hour.');
|
|
67
|
+
same('password: required');
|
|
68
|
+
same('token: none');
|
|
69
|
+
same('monkey: bananas, donkey: carrot, key: value');
|
|
70
|
+
same('Set MAX_RETRIES=5 and TIMEOUT_MS=3000 in the config.');
|
|
71
|
+
same('sk-short');
|
|
72
|
+
same('Please review src/auth/token.ts and the secret santa list.');
|
|
73
|
+
same('const hash = "abcdef0123456789abcdef0123456789abcdef01"; // a git sha, not a key');
|
|
74
|
+
same('see https://example.com/docs#password-reset');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('non-strings pass through and replacements are counted once', () => {
|
|
78
|
+
assert.equal(redactSecrets(undefined), undefined);
|
|
79
|
+
assert.equal(redactSecrets(42), 42);
|
|
80
|
+
assert.equal(redactSecrets(''), '');
|
|
81
|
+
let n = 0;
|
|
82
|
+
const out = redactSecrets('TOKEN=abcdef123456 and Authorization: Bearer abcdef1234567890 and ghp_abcdefghijklmnopqrstuvwxyz', () => n++);
|
|
83
|
+
assert.equal(out, `TOKEN=${R} and Authorization: Bearer ${R} and ${R}`);
|
|
84
|
+
assert.equal(n, 3);
|
|
85
|
+
assert.equal(redactSecrets(out), out); // idempotent
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('a large text without secrets stays fast', () => {
|
|
89
|
+
const text = 'lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(20_000);
|
|
90
|
+
const t0 = Date.now();
|
|
91
|
+
assert.equal(redactSecrets(text), text);
|
|
92
|
+
assert.ok(Date.now() - t0 < 2000, 'redaction is linear');
|
|
93
|
+
const noise = 'a1b2'.repeat(100_000);
|
|
94
|
+
const t1 = Date.now();
|
|
95
|
+
redactSecrets(noise);
|
|
96
|
+
assert.ok(Date.now() - t1 < 2000, 'long token-like runs are linear');
|
|
97
|
+
});
|
package/dist/google-jwt.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* google-jwt.ts — Google ID Token verification
|
|
3
|
-
*
|
|
4
|
-
* Verifies Google-signed JWTs against Google's public JWKS.
|
|
5
|
-
* Uses `jose` for cryptographic verification.
|
|
6
|
-
*
|
|
7
|
-
* Spec: datachannel-auth-totp.md §5.1
|
|
8
|
-
*/
|
|
9
|
-
export interface GoogleIdTokenClaims {
|
|
10
|
-
iss: string;
|
|
11
|
-
sub: string;
|
|
12
|
-
aud: string;
|
|
13
|
-
email: string;
|
|
14
|
-
email_verified: boolean;
|
|
15
|
-
exp: number;
|
|
16
|
-
iat: number;
|
|
17
|
-
}
|
|
18
|
-
export interface VerifyGoogleIdTokenOptions {
|
|
19
|
-
clientId: string;
|
|
20
|
-
authorizedSub: string;
|
|
21
|
-
authorizedEmail: string;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Verify a Google ID token JWT.
|
|
25
|
-
*
|
|
26
|
-
* @param idToken The raw JWT string from the browser
|
|
27
|
-
* @param options Expected clientId, sub, and email
|
|
28
|
-
* @returns The verified claims, or throws on failure
|
|
29
|
-
*/
|
|
30
|
-
export declare function verifyGoogleIdToken(idToken: string, options: VerifyGoogleIdTokenOptions): Promise<GoogleIdTokenClaims>;
|
|
31
|
-
/**
|
|
32
|
-
* Invalidate the JWKS cache (e.g., on key rotation failure).
|
|
33
|
-
*/
|
|
34
|
-
export declare function clearJWKSCache(): void;
|
package/dist/google-jwt.js
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* google-jwt.ts — Google ID Token verification
|
|
3
|
-
*
|
|
4
|
-
* Verifies Google-signed JWTs against Google's public JWKS.
|
|
5
|
-
* Uses `jose` for cryptographic verification.
|
|
6
|
-
*
|
|
7
|
-
* Spec: datachannel-auth-totp.md §5.1
|
|
8
|
-
*/
|
|
9
|
-
import * as https from 'node:https';
|
|
10
|
-
// jose is imported dynamically to allow graceful failure if not installed
|
|
11
|
-
let joseModule = null;
|
|
12
|
-
async function getJose() {
|
|
13
|
-
if (!joseModule) {
|
|
14
|
-
joseModule = await import('jose');
|
|
15
|
-
}
|
|
16
|
-
return joseModule;
|
|
17
|
-
}
|
|
18
|
-
let jwksCache = null;
|
|
19
|
-
const GOOGLE_JWKS_URI = 'https://www.googleapis.com/oauth2/v3/certs';
|
|
20
|
-
const GOOGLE_ISSUERS = ['https://accounts.google.com', 'accounts.google.com'];
|
|
21
|
-
/**
|
|
22
|
-
* Fetch Google's JWKS, with caching based on Cache-Control header.
|
|
23
|
-
*/
|
|
24
|
-
async function fetchJWKS() {
|
|
25
|
-
if (jwksCache && Date.now() < jwksCache.expiresAt) {
|
|
26
|
-
return jwksCache.keys;
|
|
27
|
-
}
|
|
28
|
-
const jose = await getJose();
|
|
29
|
-
return new Promise((resolve, reject) => {
|
|
30
|
-
https.get(GOOGLE_JWKS_URI, (res) => {
|
|
31
|
-
let data = '';
|
|
32
|
-
res.on('data', (chunk) => { data += chunk; });
|
|
33
|
-
res.on('end', () => {
|
|
34
|
-
try {
|
|
35
|
-
const keys = JSON.parse(data);
|
|
36
|
-
// Parse Cache-Control max-age
|
|
37
|
-
const cacheControl = res.headers['cache-control'] || '';
|
|
38
|
-
const maxAgeMatch = cacheControl.match(/max-age=(\d+)/);
|
|
39
|
-
const maxAge = maxAgeMatch ? parseInt(maxAgeMatch[1], 10) : 3600;
|
|
40
|
-
jwksCache = {
|
|
41
|
-
keys,
|
|
42
|
-
expiresAt: Date.now() + maxAge * 1000,
|
|
43
|
-
};
|
|
44
|
-
resolve(keys);
|
|
45
|
-
}
|
|
46
|
-
catch (e) {
|
|
47
|
-
reject(new Error(`Failed to parse Google JWKS: ${e.message}`));
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}).on('error', reject);
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Verify a Google ID token JWT.
|
|
55
|
-
*
|
|
56
|
-
* @param idToken The raw JWT string from the browser
|
|
57
|
-
* @param options Expected clientId, sub, and email
|
|
58
|
-
* @returns The verified claims, or throws on failure
|
|
59
|
-
*/
|
|
60
|
-
export async function verifyGoogleIdToken(idToken, options) {
|
|
61
|
-
const jose = await getJose();
|
|
62
|
-
// Fetch JWKS
|
|
63
|
-
const jwksData = await fetchJWKS();
|
|
64
|
-
const JWKS = jose.createLocalJWKSet(jwksData);
|
|
65
|
-
// Verify signature and decode
|
|
66
|
-
const { payload } = await jose.jwtVerify(idToken, JWKS, {
|
|
67
|
-
issuer: GOOGLE_ISSUERS,
|
|
68
|
-
audience: options.clientId,
|
|
69
|
-
});
|
|
70
|
-
const claims = payload;
|
|
71
|
-
// Verify sub matches authorized user
|
|
72
|
-
if (claims.sub !== options.authorizedSub) {
|
|
73
|
-
throw new Error(`Google account mismatch: expected sub ${options.authorizedSub}, ` +
|
|
74
|
-
`got ${claims.sub} (${claims.email})`);
|
|
75
|
-
}
|
|
76
|
-
// Verify email matches (belt + suspenders)
|
|
77
|
-
if (claims.email !== options.authorizedEmail) {
|
|
78
|
-
throw new Error(`Google email mismatch: expected ${options.authorizedEmail}, got ${claims.email}`);
|
|
79
|
-
}
|
|
80
|
-
return claims;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Invalidate the JWKS cache (e.g., on key rotation failure).
|
|
84
|
-
*/
|
|
85
|
-
export function clearJWKSCache() {
|
|
86
|
-
jwksCache = null;
|
|
87
|
-
}
|