Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/access.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
8 changes: 4 additions & 4 deletions scripts/capture-thumbnails.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
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';
Expand All @@ -28,7 +28,7 @@
}

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}` } });
Comment thread
tryskian marked this conversation as resolved.
Dismissed
const result = await response.json();
if (!response.ok || result.ok === false) throw new Error(result.error || `request_failed_${response.status}`);
return result;
Expand All @@ -50,7 +50,7 @@
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 });
Expand Down
9 changes: 8 additions & 1 deletion tests/access.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
});