flybrain/services/bridge/tests/redact.test.ts
acamilo d06e1cf809 bridge: refresh stored tokens on load before giving up, persist both roles
A stored access token that has expired (obtainmentTimestamp + expiresIn) or
that validate rejects with 401 is now refreshed once through its role's
RefreshingAuthProvider before startup fails, for the bot and broadcaster
roles alike. Refreshes are persisted under the role whose provider made
them (no more scope-set guessing), through one serialized writer, so both
roles reach tokens.json even when they are one account; writes stay
tmp + rename, 0600.

Only a refresh Twitch refuses (4xx, e.g. 400 Invalid refresh token) is
fatal, with one line naming the role and the runbook's re-authorize step.
Twitch errors are logged by status and message only: twurple's
HttpStatusCodeError message carries the refresh grant URL, client secret
and refresh token included, so the entrypoint and EventSub log lines now
redact it.

Tests mock Twitch's validate and token endpoints. Runbook and B1 updated.
2026-09-28 23:47:48 +00:00

22 lines
1.1 KiB
TypeScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { redactSecrets, safeErrorText } from '../src/redact';
void test('redactSecrets strips OAuth secrets from a twurple HttpStatusCodeError-shaped message', () => {
const message =
'Encountered HTTP status code 400: Bad Request\n\nURL: token?grant_type=refresh_token&client_id=abc' +
'&client_secret=s3cr3t&refresh_token=r3fr3sh\nMethod: POST';
const out = redactSecrets(message);
assert.doesNotMatch(out, /s3cr3t|r3fr3sh/);
assert.match(out, /client_secret=<redacted>&refresh_token=<redacted>/);
assert.match(out, /client_id=abc/, 'the client id is not a secret and stays readable');
});
void test('redactSecrets strips Authorization header values', () => {
assert.equal(redactSecrets('Authorization: OAuth abcdef0123456789'), 'Authorization: OAuth <redacted>');
});
void test('safeErrorText redacts the cause chain too', () => {
const error = new Error('outer', { cause: new Error('URL: token?client_secret=s3cr3t') });
assert.doesNotMatch(safeErrorText(error), /s3cr3t/);
});