Files
splatoon3.ink/workers/updater/access.spec.mjs
Matt Isenhower c2666b27ca Add an admin panel to the updater Worker
A small Vue + Tailwind app in workers/updater/admin, served by the updater
Worker on its own hostname (admin.splatoon3.ink, admin.dev.splatoon3.ink)
behind Cloudflare Access:

- Timeline: both lanes, credential renewals, and social posts on one time
  axis, a filterable run list, run details with per-updater timings and
  the run's own log lines, lane controls (run now, pause, scheduling),
  credential renewal, and a combined text log.
- Social: the post log with errors, per-account posting switches, when
  each status last went to each account, and a preview that can post one
  status by hand after a confirmation.
- Data: freshness of the core data files, the published file list, a
  check that the site serves the new data, and a cache purge.

Worker side:

- src/admin: the Access JWT check (jose; fails closed and only answers on
  ADMIN_HOSTNAME) and the API. Actions must be same-origin JSON POSTs, and
  the app is served with a strict CSP from the ADMIN_ASSETS binding.
- Runs capture their console output (AsyncLocalStorage), so log lines
  belong to the run that printed them. The Scheduler keeps a post log and
  serves run details and log lines; RegionAuth keeps its last 24 renewals.
- A manual run from the panel only posts to social when asked to.

Shared code:

- StatusGeneratorManager records what was posted or failed, and skips
  accounts turned off in private settings/social.json (the Node runner
  honours this too). app/social/admin.mjs previews and posts one status.
- Updater results include their duration.

Also: npm run admin:dev runs the panel against a simulated API, deploy
scripts build the panel first, the dev environment's assets move to
dev-assets.splatoon3.ink, and the lockfile drops entries nothing depends
on any more.

Not yet run against a deployed Worker or a real Access application.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-10 12:59:36 -08:00

66 lines
2.6 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import { generateKeyPair, SignJWT } from 'jose';
import { verifyAccess } from './src/admin/access.mjs';
const settings = {
ADMIN_HOSTNAME: 'admin.test',
ACCESS_TEAM_DOMAIN: 'team.cloudflareaccess.com',
ACCESS_AUD: 'audience-tag',
};
const keys = await generateKeyPair('RS256');
const resolveKey = async () => keys.publicKey;
function token({ issuer = 'https://team.cloudflareaccess.com', audience = 'audience-tag', expires = '5m', key = keys.privateKey, email = 'admin@example.test' } = {}) {
return new SignJWT({ email })
.setProtectedHeader({ alg: 'RS256' })
.setIssuer(issuer)
.setAudience(audience)
.setSubject('user-1')
.setIssuedAt()
.setExpirationTime(expires)
.sign(key);
}
function request(jwt, url = 'https://admin.test/') {
return new Request(url, { headers: jwt ? { 'Cf-Access-Jwt-Assertion': jwt } : {} });
}
describe('Cloudflare Access check', () => {
it('accepts a token for this application signed by the team', async () => {
expect(await verifyAccess(request(await token()), settings, resolveKey)).toEqual({ email: 'admin@example.test' });
});
it('refuses requests without a token', async () => {
expect(await verifyAccess(request(null), settings, resolveKey)).toBeNull();
});
it('refuses tokens for another application, another team, or past their expiry', async () => {
expect(await verifyAccess(request(await token({ audience: 'another-app' })), settings, resolveKey)).toBeNull();
expect(await verifyAccess(request(await token({ issuer: 'https://other.cloudflareaccess.com' })), settings, resolveKey)).toBeNull();
expect(await verifyAccess(request(await token({ expires: '-1m' })), settings, resolveKey)).toBeNull();
});
it('refuses a token signed with another key', async () => {
let other = await generateKeyPair('RS256');
expect(await verifyAccess(request(await token({ key: other.privateKey })), settings, resolveKey)).toBeNull();
});
it('refuses a valid token on any other hostname', async () => {
expect(await verifyAccess(request(await token(), 'https://updater.test/'), settings, resolveKey)).toBeNull();
});
it('stays closed until every Access setting is present, and only trusts Access team domains', async () => {
let jwt = await token();
for (let name of Object.keys(settings)) {
expect(await verifyAccess(request(jwt), { ...settings, [name]: undefined }, resolveKey)).toBeNull();
}
let lookalike = await token({ issuer: 'https://team.example.com' });
expect(await verifyAccess(request(lookalike), { ...settings, ACCESS_TEAM_DOMAIN: 'team.example.com' }, resolveKey)).toBeNull();
});
});