Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
feat(api): allow listed local API origins [A2]
Corey Olson977bf0138a9c
diff --git a/src/api.js b/src/api.jsindex f1b53169..4be3e21e 100644--- a/src/api.js+++ b/src/api.js@@ -138,11 +138,12 @@ async function createRequest(root, body, isMain) {}/** Create the authenticated local adapter used by the HTTP server and other local transports. */-export function createLocalApiHandler({ secret, getPort, runCommand, projects = listApiProjects, isMain } = {}) {+export function createLocalApiHandler({ secret, getPort, runCommand, projects = listApiProjects, isMain, allowedOrigins = [] } = {}) {if (typeof getPort !== 'function') throw new TypeError('createLocalApiHandler needs a bound-port getter');if (typeof runCommand !== 'function') throw new TypeError('createLocalApiHandler needs the actual CLI entry point as runCommand');const codeIsMain = codeIsFromMain() && isMain !== false;const key = Buffer.from(secret);+ const permittedOrigins = new Set(allowedOrigins);return createApiHandler({authenticate: (req) => {const url = new URL(req.url ?? '/', 'http://' + ADDRESS);@@ -151,7 +152,7 @@ export function createLocalApiHandler({ secret, getPort, runCommand, projects =const bound = getPort();const ownHost = req.headers.host === ADDRESS + ':' + bound || req.headers.host === 'localhost:' + bound;if (!ownHost || given.length !== key.length || !timingSafeEqual(given, key)) throw new Refused('AUTH_REQUIRED', 'this API needs its session secret and own host; use the address pullboard serve printed');- if (req.headers.origin && req.headers.origin !== 'http://' + req.headers.host) throw new Refused('API_ORIGIN', 'use the API from its own origin');+ if (req.headers.origin && req.headers.origin !== 'http://' + req.headers.host && !permittedOrigins.has(req.headers.origin)) throw new Refused('API_ORIGIN', 'use the API from its own origin');return { local: true };},boards: () => apiBoardListing(projects, codeIsMain),diff --git a/test/api.test.js b/test/api.test.jsindex c26fbdf5..2c854deb 100644--- a/test/api.test.js+++ b/test/api.test.js@@ -1,5 +1,6 @@/** The public, versioned JSON surface of every pullboard command (A1). */import assert from 'node:assert/strict';+import { randomUUID } from 'node:crypto';import { runFixtureExecFile as execFileSync, startFixtureChild as spawn, runFixtureChild as spawnSync, reportFixtureChildFailure, runFixtureChild, runFixtureGit, safeFixtureDiagnostic } from './fixture-child.js';import { performance } from 'node:perf_hooks';import {@@ -17,9 +18,11 @@ import {import { tmpdir } from 'node:os';import { DatabaseSync } from 'node:sqlite';import { once } from 'node:events';+import { createServer } from 'node:http';import { join, dirname, resolve, basename, delimiter } from 'node:path';import { after, test } from 'node:test';-import { resultCommands } from '../src/cli.js';+import { main, resultCommands } from '../src/cli.js';+import { createLocalApiHandler } from '../src/api.js';import { allShouts, EVENT_LOG_VERSION, closeBoard, openBoard } from '../src/board.js';import { presentationShout, relayPresentation } from '../src/relay-presentation.js';import { JSON_SHAPES } from '../src/json.js';@@ -80,6 +83,34 @@ test('[O8] authenticated API state exposes model labels and configured stable diassert.equal(prefix.find((agent) => agent.agent_id === 'coordinator').displayName, 'unknown-coordinator');});+test('[A2] the local API admits only its listed origins', async (t) => {+ const box = project();+ const listedOrigin = 'https://app.pullboard.dev';+ const configured = await startLocalHandler(t, box, { allowedOrigins: [listedOrigin] });+ const listed = await apiFetch(configured, '/api/v1/boards', { headers: { origin: listedOrigin } });+ assert.equal(listed.status, 200);+ assert.equal((await listed.json()).boards.length, 1, 'the allowed origin reaches the regular board handler');++ for (const origin of [+ 'https://app.pullboard.dev.attacker.invalid',+ 'https://sub.app.pullboard.dev',+ 'https://app.pullboard.dev:444',+ 'https://different.invalid',+ ]) {+ const response = await apiFetch(configured, '/api/v1/boards', { headers: { origin } });+ assert.equal((await apiError(response, 403)).code, 'API_ORIGIN', `an unlisted origin is refused: ${origin}`);+ }++ const missingKey = await apiFetch(configured, '/api/v1/boards', { noSecret: true, headers: { origin: listedOrigin } });+ assert.equal((await apiError(missingKey, 401)).code, 'AUTH_REQUIRED', 'an allowed origin still needs the session key');+ const wrongKey = await apiFetch(configured, '/api/v1/boards', { headers: { origin: listedOrigin, 'x-pullboard-key': randomUUID() } });+ assert.equal((await apiError(wrongKey, 401)).code, 'AUTH_REQUIRED', 'an allowed origin does not replace the session key');++ const defaulted = await startLocalHandler(t, box);+ const defaultOrigin = await apiFetch(defaulted, '/api/v1/boards', { headers: { origin: listedOrigin } });+ assert.equal((await apiError(defaultOrigin, 403)).code, 'API_ORIGIN', 'omitting the option preserves same-origin-only behavior');+});+/** Quote a literal executable path for the fixture hook's POSIX shim. */function shellWord(value) {return "'" + value.replace(/'/g, "'\\''") + "'";@@ -1017,6 +1048,34 @@ test('[N26,A2] roadmap text, JSON and API state follow live local and registeredassert.deepEqual(state.state.milestones, document.milestones);});+/** Serve the local API adapter directly so tests can exercise its optional origin allowlist. */+async function startLocalHandler(t, box, options = {}) {+ const secret = randomUUID();+ let port = 0;+ const handler = createLocalApiHandler({+ secret,+ getPort: () => port,+ runCommand: main,+ projects: () => [{ root: box.repo, name: 'API fixture', project: '', added: '2026-01-01T00:00:00.000Z' }],+ ...(options.allowedOrigins === undefined ? {} : { allowedOrigins: options.allowedOrigins }),+ });+ const server = createServer(handler);+ await new Promise((resolveReady, rejectReady) => {+ server.once('error', rejectReady);+ server.listen(0, '127.0.0.1', () => {+ port = server.address().port;+ resolveReady();+ });+ });+ t.after(async () => {+ handler.close();+ const closed = new Promise((resolveClosed) => server.close(resolveClosed));+ server.closeAllConnections();+ await closed;+ });+ return { origin: `http://127.0.0.1:${port}`, secret };+}+/** Start the real local API server and stop it when its test finishes. */async function startApi(t, box, { traceStartup = false, traceFile: requestedTraceFile, readinessTimeoutMs = 10_000,holdPhase, phaseReachedFile, releaseFile } = {}) {