From 35a7bed83fc8d46dd58cb23b275d3f28c4e759ee Mon Sep 17 00:00:00 2001 From: krystian Date: Sat, 28 Nov 2026 16:51:39 -0500 Subject: [PATCH] fix: confine capture credentials to loopback --- lib/access.mjs | 11 +++++++++++ scripts/capture-thumbnails.mjs | 8 ++++---- tests/access.test.mjs | 9 ++++++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/access.mjs b/lib/access.mjs index 6c4561d..2816e07 100644 --- a/lib/access.mjs +++ b/lib/access.mjs @@ -54,3 +54,14 @@ export function captureUrl(value, origins) { if (!origin || !origins.includes(origin)) throw new Error('Page is not approved for capture.'); return new URL(value).href; } + +export function localServiceOrigin(value) { + const url = new URL(value); + if (url.protocol !== 'http:' || !['127.0.0.1', 'localhost'].includes(url.hostname) + || url.username || url.password || url.pathname !== '/' || url.search || url.hash + || !url.port) { + throw new Error('DevNotes API must be an HTTP loopback address with an explicit port.'); + } + // Use a literal loopback address even when the operator supplied localhost. + return `http://127.0.0.1:${url.port}`; +} diff --git a/scripts/capture-thumbnails.mjs b/scripts/capture-thumbnails.mjs index 6a1434d..ff7c17f 100644 --- a/scripts/capture-thumbnails.mjs +++ b/scripts/capture-thumbnails.mjs @@ -3,10 +3,10 @@ import { createRequire } from 'node:module'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { chromium as projectChromium } from 'playwright-core'; -import { captureUrl } from '../lib/access.mjs'; +import { captureUrl, localServiceOrigin } from '../lib/access.mjs'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); -const origin = process.env.DEVNOTES_ORIGIN || 'http://127.0.0.1:4347'; +const origin = localServiceOrigin(process.env.DEVNOTES_ORIGIN || 'http://127.0.0.1:4347'); const token = process.env.DEVNOTES_API_TOKEN || JSON.parse(await readFile(resolve(process.env.ANNOTATION_DATA_DIR || resolve(root, '.data'), 'access.json'), 'utf8')).token; const helperPath = process.env.DEVNOTES_CHROMIUM_HELPER || '/Users/tryskian/.codex/skills/dev-chromium/scripts/chromium.cjs'; @@ -28,7 +28,7 @@ async function browserRuntime() { } async function request(path, options) { - const response = await fetch(`${origin}${path}`, { ...options, headers: { ...options?.headers, Authorization: `Bearer ${token}` } }); + const response = await fetch(`${origin}${path}`, { ...options, redirect: 'error', headers: { ...options?.headers, Authorization: `Bearer ${token}` } }); const result = await response.json(); if (!response.ok || result.ok === false) throw new Error(result.error || `request_failed_${response.status}`); return result; @@ -50,7 +50,7 @@ if (!notes.length) { process.exit(0); } -const clientSource = await (await fetch(`${origin}/client.js`)).text(); +const clientSource = await (await fetch(`${origin}/client.js`, { redirect: 'error' })).text(); const runtime = await browserRuntime(); const browser = await runtime.chromium.launch({ headless: true, executablePath: runtime.executablePath }); const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, deviceScaleFactor: 1 }); diff --git a/tests/access.test.mjs b/tests/access.test.mjs index fceea84..65b2e1c 100644 --- a/tests/access.test.mjs +++ b/tests/access.test.mjs @@ -7,7 +7,7 @@ import { request as httpRequest } from 'node:http'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import test from 'node:test'; -import { captureUrl } from '../lib/access.mjs'; +import { captureUrl, localServiceOrigin } from '../lib/access.mjs'; test('notebook access requires a capability, approved origin and local host', async (context) => { const reservation = createServer(); @@ -75,3 +75,10 @@ test('capture only accepts approved HTTP origins', () => { assert.throws(() => captureUrl(value, allowed)); } }); + +test('capture credentials only go to a literal loopback service', () => { + assert.equal(localServiceOrigin('http://localhost:4347'), 'http://127.0.0.1:4347'); + for (const value of ['https://external.example:4347', 'http://external.example:4347', 'http://127.0.0.1:4347/redirect', 'http://user:pass@127.0.0.1:4347', 'file:///tmp/notes', 'http://127.0.0.1:4347?target=remote']) { + assert.throws(() => localServiceOrigin(value)); + } +});