mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* fix(brief): per-run slot URL so same-day digests link to distinct briefs
Digest emails at 8am and 1pm on the same day pointed to byte-identical
magazine URLs because the URL was keyed on YYYY-MM-DD in the user tz.
Each compose run overwrote the single daily envelope in place, and the
composer rolling 24h story window meant afternoon output often looked
identical to morning. Readers clicking an older email got whatever the
latest cron happened to write.
Slot format is now YYYY-MM-DD-HHMM (local tz, per compose run). The
magazine URL, carousel URLs, and Redis key all carry the slot, and each
digest dispatch gets its own frozen envelope that lives out the 7d TTL.
envelope.data.date stays YYYY-MM-DD for rendering "19 April 2026".
The digest cron also writes a brief:latest:{userId} pointer (7d TTL,
overwritten each compose) so the dashboard panel and share-url endpoint
can locate the most recent brief without knowing the slot. The
previous date-probing strategy does not work once keys carry HHMM.
No back-compat for the old YYYY-MM-DD format: the verifier rejects it,
the composer only ever writes the new shape, and any in-flight
notifications signed under the old format will 403 on click. Acceptable
at the rollout boundary per product decision.
* fix(brief): carve middleware bot allowlist to accept slot-format carousel path
BRIEF_CAROUSEL_PATH_RE in middleware.ts was still matching only the
pre-slot YYYY-MM-DD segment, so every slot-based carousel URL emitted
by the digest cron (YYYY-MM-DD-HHMM) would miss the social allowlist
and fall into the generic bot gate. Telegram/Slack/Discord/LinkedIn
image fetchers would 403 on sendMediaGroup, breaking previews for the
new digest links.
CI missed this because tests/middleware-bot-gate.test.mts still
exercised the old /YYYY-MM-DD/ path shape. Swap the fixture to the
slot format and add a regression asserting the pre-slot shape is now
rejected, so legacy links cannot silently leak the allowlist after
the rollout.
* fix(brief): preserve caller-requested slot + correct no-brief share-url error
Two contract bugs in the slot rollout that silently misled callers:
1. GET /api/latest-brief?slot=X where X has no envelope was returning
{ status: 'composing', issueDate: <today UTC> } — which reads as
"today's brief is composing" instead of "the specific slot you
asked about doesn't exist". A caller probing a known historical
slot would get a completely unrelated "today" signal. Now we echo
the requested slot back (issueSlot + issueDate derived from its
date portion) when the caller supplied ?slot=, and keep the
UTC-today placeholder only for the no-param path.
2. POST /api/brief/share-url with no slot and no latest-pointer was
falling into the generic invalid_slot_shape 400 branch. That is
not an input-shape problem; it is "no brief exists yet for this
user". Return 404 brief_not_found — the same code the
existing-envelope check returns — so callers get one coherent
contract: either the brief exists and is shareable, or it doesn't
and you get 404.
200 lines
7.4 KiB
JavaScript
200 lines
7.4 KiB
JavaScript
// HMAC-signed brief URL helpers.
|
|
//
|
|
// The signed token is the credential for the hosted magazine route —
|
|
// a recipient with the URL can read the brief, no other auth. These
|
|
// tests lock down the invariants that matter: deterministic signing,
|
|
// rejection of tampered inputs, and graceful rotation via a second
|
|
// accepted secret during overlap windows.
|
|
|
|
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
BriefUrlError,
|
|
signBriefToken,
|
|
signBriefUrl,
|
|
verifyBriefToken,
|
|
} from '../server/_shared/brief-url.ts';
|
|
|
|
const SECRET = 'primary-secret-for-tests-0123456789';
|
|
const PREV_SECRET = 'rotated-out-legacy-secret-abcdefghij';
|
|
const USER_ID = 'user_abc123';
|
|
// Slot format: YYYY-MM-DD-HHMM (per compose run).
|
|
const ISSUE_DATE = '2026-04-17-0800';
|
|
|
|
describe('signBriefToken + verifyBriefToken', () => {
|
|
it('round-trips: verify(sign) is true for matching inputs', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.equal(await verifyBriefToken(USER_ID, ISSUE_DATE, token, SECRET), true);
|
|
});
|
|
|
|
it('produces a 43-char base64url token (SHA-256 without padding)', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.match(token, /^[A-Za-z0-9_-]{43}$/);
|
|
});
|
|
|
|
it('is deterministic for the same input', async () => {
|
|
const a = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
const b = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.equal(a, b);
|
|
});
|
|
|
|
it('rejects a tampered token', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
// Flip one base64url char without changing shape.
|
|
const tampered = token.startsWith('A')
|
|
? `B${token.slice(1)}`
|
|
: `A${token.slice(1)}`;
|
|
assert.equal(await verifyBriefToken(USER_ID, ISSUE_DATE, tampered, SECRET), false);
|
|
});
|
|
|
|
it('rejects a token bound to a different userId', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.equal(await verifyBriefToken('user_xyz', ISSUE_DATE, token, SECRET), false);
|
|
});
|
|
|
|
it('rejects a token bound to a different issueSlot', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
// Same day, different slot (13:00) must NOT verify.
|
|
assert.equal(await verifyBriefToken(USER_ID, '2026-04-17-1300', token, SECRET), false);
|
|
});
|
|
|
|
it('rejects a token signed with a different secret', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, 'other-secret');
|
|
assert.equal(await verifyBriefToken(USER_ID, ISSUE_DATE, token, SECRET), false);
|
|
});
|
|
|
|
it('rejects a malformed token shape without touching crypto', async () => {
|
|
// Tokens of the wrong length short-circuit.
|
|
assert.equal(await verifyBriefToken(USER_ID, ISSUE_DATE, 'too-short', SECRET), false);
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, 'a'.repeat(44), SECRET),
|
|
false,
|
|
);
|
|
// Non-string token.
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, /** @type {any} */ (null), SECRET),
|
|
false,
|
|
);
|
|
});
|
|
|
|
it('rejects a malformed userId or issueDate on verify without throwing', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.equal(await verifyBriefToken('', ISSUE_DATE, token, SECRET), false);
|
|
assert.equal(await verifyBriefToken('user with spaces', ISSUE_DATE, token, SECRET), false);
|
|
assert.equal(await verifyBriefToken(USER_ID, '04/17/2026', token, SECRET), false);
|
|
});
|
|
|
|
it('throws BriefUrlError when signing with an empty secret', async () => {
|
|
await assert.rejects(
|
|
() => signBriefToken(USER_ID, ISSUE_DATE, ''),
|
|
(err) => err instanceof BriefUrlError && err.code === 'missing_secret',
|
|
);
|
|
});
|
|
|
|
it('throws BriefUrlError when verifying with an empty secret', async () => {
|
|
const token = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
await assert.rejects(
|
|
() => verifyBriefToken(USER_ID, ISSUE_DATE, token, ''),
|
|
(err) => err instanceof BriefUrlError && err.code === 'missing_secret',
|
|
);
|
|
});
|
|
|
|
it('throws BriefUrlError on malformed userId at sign time', async () => {
|
|
await assert.rejects(
|
|
() => signBriefToken('user with spaces', ISSUE_DATE, SECRET),
|
|
(err) => err instanceof BriefUrlError && err.code === 'invalid_user_id',
|
|
);
|
|
});
|
|
|
|
it('throws BriefUrlError on malformed issueDate at sign time', async () => {
|
|
await assert.rejects(
|
|
() => signBriefToken(USER_ID, '2026/04/17', SECRET),
|
|
(err) => err instanceof BriefUrlError && err.code === 'invalid_issue_date',
|
|
);
|
|
});
|
|
|
|
it('throws BriefUrlError when slot is missing the HHMM suffix', async () => {
|
|
// Bare YYYY-MM-DD is no longer a valid slot — cron must pass the
|
|
// full YYYY-MM-DD-HHMM. Guards against an accidental partial
|
|
// revert of the slot rollout.
|
|
await assert.rejects(
|
|
() => signBriefToken(USER_ID, '2026-04-17', SECRET),
|
|
(err) => err instanceof BriefUrlError && err.code === 'invalid_issue_date',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('secret rotation', () => {
|
|
it('accepts tokens signed with the previous secret during rotation', async () => {
|
|
const legacyToken = await signBriefToken(USER_ID, ISSUE_DATE, PREV_SECRET);
|
|
// Primary is rotated; prev is still accepted during the overlap.
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, legacyToken, SECRET, PREV_SECRET),
|
|
true,
|
|
);
|
|
});
|
|
|
|
it('still accepts tokens signed with the new primary after rotation', async () => {
|
|
const freshToken = await signBriefToken(USER_ID, ISSUE_DATE, SECRET);
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, freshToken, SECRET, PREV_SECRET),
|
|
true,
|
|
);
|
|
});
|
|
|
|
it('rejects a token signed with a third, unknown secret even when prev is set', async () => {
|
|
const strayToken = await signBriefToken(USER_ID, ISSUE_DATE, 'unknown-secret');
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, strayToken, SECRET, PREV_SECRET),
|
|
false,
|
|
);
|
|
});
|
|
|
|
it('rejects previous-secret tokens once prev is removed', async () => {
|
|
const legacyToken = await signBriefToken(USER_ID, ISSUE_DATE, PREV_SECRET);
|
|
assert.equal(
|
|
await verifyBriefToken(USER_ID, ISSUE_DATE, legacyToken, SECRET),
|
|
false,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('signBriefUrl', () => {
|
|
it('composes a URL with the expected path and token', async () => {
|
|
const url = await signBriefUrl({
|
|
userId: USER_ID,
|
|
issueDate: ISSUE_DATE,
|
|
baseUrl: 'https://worldmonitor.app',
|
|
secret: SECRET,
|
|
});
|
|
assert.match(
|
|
url,
|
|
new RegExp(`^https://worldmonitor\\.app/api/brief/${USER_ID}/${ISSUE_DATE}\\?t=[A-Za-z0-9_-]{43}$`),
|
|
);
|
|
});
|
|
|
|
it('trims trailing slash on baseUrl', async () => {
|
|
const url = await signBriefUrl({
|
|
userId: USER_ID,
|
|
issueDate: ISSUE_DATE,
|
|
baseUrl: 'https://worldmonitor.app/',
|
|
secret: SECRET,
|
|
});
|
|
assert.ok(url.startsWith('https://worldmonitor.app/api/brief/'));
|
|
assert.ok(!url.includes('.app//api/'));
|
|
});
|
|
|
|
it('URL-encodes the path components', async () => {
|
|
// Underscore + dash are legal userId chars; they must NOT be
|
|
// percent-encoded by the helper (encodeURIComponent preserves
|
|
// them), which keeps the URL readable in email clients.
|
|
const url = await signBriefUrl({
|
|
userId: 'user_abc-123',
|
|
issueDate: ISSUE_DATE,
|
|
baseUrl: 'https://worldmonitor.app',
|
|
secret: SECRET,
|
|
});
|
|
assert.ok(url.includes('/brief/user_abc-123/'));
|
|
});
|
|
});
|