diff --git a/package-lock.json b/package-lock.json index aac5370..9af9f3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@withone/cli", - "version": "2.15.0", + "version": "2.15.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@withone/cli", - "version": "2.15.0", + "version": "2.15.1", "dependencies": { "@clack/prompts": "^0.9.1", "commander": "^13.1.0", diff --git a/package.json b/package.json index 297b8bb..43f4759 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@withone/cli", - "version": "2.15.0", + "version": "2.15.1", "description": "CLI for managing One", "type": "module", "files": [ diff --git a/src/lib/api-redirect.test.ts b/src/lib/api-redirect.test.ts new file mode 100644 index 0000000..35d7308 --- /dev/null +++ b/src/lib/api-redirect.test.ts @@ -0,0 +1,259 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import http, { type IncomingMessage, type ServerResponse } from 'node:http'; +import { OneApi } from './api.js'; +import type { ActionDetails } from './types.js'; + +const SECRET = 'sk_test_redirect_only'; +const CONNECTION = 'test_connection'; +const action = { _id: 'test-action', title: 'Test action', method: 'POST', path: '/items', tags: [] } as ActionDetails; + +type SeenRequest = { method: string; headers: http.IncomingHttpHeaders; body: string }; + +async function listen(handler: (req: IncomingMessage, res: ServerResponse) => void): Promise { + const server = http.createServer(handler); + await new Promise((resolve, reject) => { + server.once('error', reject); + server.listen(0, '127.0.0.1', resolve); + }); + return server; +} + +function origin(server: http.Server): string { + const address = server.address(); + assert.ok(address && typeof address !== 'string'); + return `http://127.0.0.1:${address.port}`; +} + +async function close(server: http.Server): Promise { + server.closeAllConnections(); + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); +} + +async function readRequest(req: IncomingMessage): Promise { + let body = ''; + for await (const chunk of req) body += chunk.toString(); + return { method: req.method ?? '', headers: req.headers, body }; +} + +describe('OneApi redirect credentials', () => { + it('does not send metadata credentials to a different origin after a redirect', async () => { + const received: SeenRequest[] = []; + const other = await listen((req, res) => { + void readRequest(req).then(value => { received.push(value); res.end('{}'); }); + }); + const apiServer = await listen((_req, res) => { + res.writeHead(302, { Location: `${origin(other)}/capture` }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(apiServer)}/v1`, {}); + let error: unknown; + try { await api.whoami(); } catch (caught) { error = caught; } + assert.equal(received.length, 0, 'a foreign origin must not receive a request'); + assert.match(String(error), /redirect|origin/i); + assert.ok(String(error).includes(origin(other)), 'the error names the refused origin'); + assert.ok(!String(error).includes('/capture'), 'but never its path, which can carry tokens'); + } finally { + await close(apiServer); + await close(other); + } + }); + + it('does not send passthrough credentials or caller headers to a different origin', async () => { + const received: SeenRequest[] = []; + const other = await listen((req, res) => { + void readRequest(req).then(value => { received.push(value); res.end('{}'); }); + }); + const apiServer = await listen((_req, res) => { + res.writeHead(307, { Location: `${origin(other)}/capture` }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(apiServer)}/v1`, {}); + let error: unknown; + try { + await api.executePassthroughRequest({ + platform: 'test', actionId: action._id, connectionKey: CONNECTION, + data: { value: 1 }, headers: { 'x-client-note': 'dummy' }, + }, action); + } catch (caught) { error = caught; } + assert.equal(received.length, 0, 'a foreign origin must not receive a request'); + assert.match(String(error), /redirect|origin/i); + } finally { + await close(apiServer); + await close(other); + } + }); + + it('follows same-origin metadata redirects with the credential intact', async () => { + const received: SeenRequest[] = []; + const server = await listen((req, res) => { + if (req.url === '/v1/users/whoami') { + res.writeHead(302, { Location: '/v1/users/current' }); + res.end(); + } else { + void readRequest(req).then(value => { received.push(value); res.end('{"ok":true}'); }); + } + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + assert.deepEqual(await api.whoami(), { ok: true }); + assert.equal(received.length, 1); + assert.equal(received[0].headers['x-one-secret'], SECRET); + } finally { + await close(server); + } + }); + + it('resolves relative redirects against the current URL', async () => { + const paths: string[] = []; + const server = await listen((req, res) => { + paths.push(req.url ?? ''); + if (req.url === '/v1/users/whoami') { + res.writeHead(302, { Location: '../users/current?view=short' }); + res.end(); + } else { + res.end('{"ok":true}'); + } + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + assert.deepEqual(await api.whoami(), { ok: true }); + assert.deepEqual(paths, ['/v1/users/whoami', '/v1/users/current?view=short']); + } finally { + await close(server); + } + }); + + it('rejects URL-embedded credentials before a second request', async () => { + let requests = 0; + const server = await listen((_req, res) => { + requests++; + res.writeHead(302, { Location: `http://user:pass@127.0.0.1:${new URL(origin(server)).port}/capture` }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + await assert.rejects(api.whoami(), /credentialed API redirect/); + assert.equal(requests, 1); + } finally { + await close(server); + } + }); + + it('rejects malformed Location values', async () => { + let requests = 0; + const server = await listen((_req, res) => { + requests++; + res.writeHead(302, { Location: 'http://[' }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + await assert.rejects(api.whoami(), /Invalid API redirect URL/); + assert.equal(requests, 1); + } finally { + await close(server); + } + }); + + it('stops a redirect loop after the native fetch limit', async () => { + let requests = 0; + const server = await listen((_req, res) => { + requests++; + res.writeHead(302, { Location: '/v1/users/whoami' }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + await assert.rejects(api.whoami(), /Too many API redirects/); + assert.equal(requests, 21, 'the original request plus 20 redirects, as native fetch does'); + } finally { + await close(server); + } + }); + + it('rejects a hostname hidden behind URL userinfo', async () => { + let received = 0; + const other = await listen((_req, res) => { received++; res.end('{}'); }); + const apiServer = await listen((_req, res) => { + res.writeHead(302, { Location: `//127.0.0.1:${new URL(origin(apiServer)).port}@127.0.0.1:${new URL(origin(other)).port}/capture` }); + res.end(); + }); + try { + const api = new OneApi(SECRET, `${origin(apiServer)}/v1`, {}); + await assert.rejects(api.whoami(), /redirect/); + assert.equal(received, 0); + } finally { + await close(apiServer); + await close(other); + } + }); + + it('blocks an HTTPS-to-HTTP downgrade before fetch sees the target', async (t) => { + const calls: string[] = []; + t.mock.method(globalThis, 'fetch', async (url: string | URL | Request) => { + calls.push(String(url)); + return new Response(null, { status: 302, headers: { Location: 'http://api.example.test/v1/users/current' } }); + }); + const api = new OneApi(SECRET, 'https://api.example.test/v1', {}); + await assert.rejects(api.whoami(), /cross-origin/); + assert.deepEqual(calls, ['https://api.example.test/v1/users/whoami']); + }); + + it('keeps headers on a GET redirected with 303', async () => { + let received: SeenRequest | undefined; + const server = await listen((req, res) => { + if (req.url === '/v1/users/whoami') { + res.writeHead(303, { Location: '/v1/users/current' }); + res.end(); + } else { + void readRequest(req).then(value => { received = value; res.end('{}'); }); + } + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + await api.whoami(); + assert.equal(received?.method, 'GET'); + assert.equal(received?.headers['content-type'], 'application/json'); + assert.equal(received?.headers['x-one-secret'], SECRET); + } finally { + await close(server); + } + }); + + for (const [status, expectedMethod, expectedBody] of [ + [301, 'GET', ''], [302, 'GET', ''], [303, 'GET', ''], + [307, 'POST', '{"value":1}'], [308, 'POST', '{"value":1}'], + ] as const) { + it(`preserves same-origin POST redirect semantics for ${status}`, async () => { + const received: SeenRequest[] = []; + const server = await listen((req, res) => { + if (req.url === '/v1/passthrough/items') { + res.writeHead(status, { Location: '/v1/passthrough/moved' }); + res.end(); + } else { + void readRequest(req).then(value => { received.push(value); res.end('{"ok":true}'); }); + } + }); + try { + const api = new OneApi(SECRET, `${origin(server)}/v1`, {}); + const result = await api.executePassthroughRequest({ + platform: 'test', actionId: action._id, connectionKey: CONNECTION, + data: { value: 1 }, headers: { 'x-client-note': 'dummy' }, + }, action); + assert.deepEqual(result.responseData, { ok: true }); + assert.equal(received.length, 1); + assert.equal(received[0].method, expectedMethod); + assert.equal(received[0].body, expectedBody); + assert.equal(received[0].headers['content-type'], expectedMethod === 'POST' ? 'application/json' : undefined); + assert.equal(received[0].headers['x-one-secret'], SECRET); + assert.equal(received[0].headers['x-one-connection-key'], CONNECTION); + assert.equal(received[0].headers['x-client-note'], 'dummy'); + } finally { + await close(server); + } + }); + } +}); diff --git a/src/lib/api.ts b/src/lib/api.ts index df0cb07..d81a8e3 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -88,6 +88,47 @@ function parseRetryAfter(value: string | null): number | undefined { return undefined; } +/** Follow ordinary API redirects without sending credentials to another origin. */ +async function fetchApi(url: string, init: RequestInit): Promise { + let currentUrl = url; + let request = { ...init, redirect: 'manual' as const }; + for (let redirects = 0; ; redirects++) { + const response = await fetch(currentUrl, request); + if (![301, 302, 303, 307, 308].includes(response.status)) return response; + const location = response.headers.get('location'); + if (!location) return response; + let nextUrl: URL; + try { + nextUrl = new URL(location, currentUrl); + } catch { + await response.body?.cancel(); + throw new ApiError(response.status, 'Invalid API redirect URL'); + } + if (nextUrl.username || nextUrl.password || nextUrl.origin !== new URL(currentUrl).origin) { + await response.body?.cancel(); + // Name the origin only: a redirect's path and query can carry tokens. + const target = nextUrl.origin === 'null' ? nextUrl.protocol : nextUrl.origin; + throw new ApiError(response.status, `Refusing cross-origin or credentialed API redirect to ${target}`); + } + // Native fetch follows 20 redirects and fails on the 21st. + if (redirects >= 20) { + await response.body?.cancel(); + throw new ApiError(response.status, 'Too many API redirects'); + } + const method = (request.method ?? 'GET').toUpperCase(); + if ((response.status === 303 && method !== 'GET' && method !== 'HEAD') || + ((response.status === 301 || response.status === 302) && method === 'POST')) { + const headers = new Headers(request.headers); + for (const name of ['content-type', 'content-length', 'content-encoding', 'content-language', 'content-location']) { + headers.delete(name); + } + request = { ...request, method: 'GET', body: undefined, headers }; + } + await response.body?.cancel(); + currentUrl = nextUrl.href; + } +} + export class OneApi { private apiBase: string; @@ -140,7 +181,7 @@ export class OneApi { fetchOpts.body = JSON.stringify(opts.body); } - const response = await fetch(url, fetchOpts); + const response = await fetchApi(url, fetchOpts); if (!response.ok) { const text = await response.text(); @@ -397,7 +438,7 @@ export class OneApi { fetchOpts.body = JSON.stringify(opts.body); } - const response = await fetch(url, fetchOpts); + const response = await fetchApi(url, fetchOpts); if (response.status === 304) { return { data: null as T, etag: opts.ifNoneMatch ?? null, status: 304 }; @@ -586,7 +627,7 @@ export class OneApi { }; } - const response = await fetch(fullUrl, fetchOpts); + const response = await fetchApi(fullUrl, fetchOpts); if (!response.ok) { const text = await response.text(); diff --git a/src/lib/find.test.ts b/src/lib/find.test.ts index 68f54b9..063440e 100644 --- a/src/lib/find.test.ts +++ b/src/lib/find.test.ts @@ -1,5 +1,6 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; +import http from 'node:http'; import { EXECUTE_GUIDE, @@ -268,6 +269,7 @@ describe('misplaced intent', () => { assert.ok(pick.knowledge.includes(LARGE), 'the whole document, not a digest'); assert.ok(pick.knowledge.includes('URL: https://api.withone.ai/v1/passthrough/v1/custom')); assert.ok(pick.knowledge.includes('This is a custom action')); + assert.match(pick.knowledge, /redirect: "manual"/); assert.deepEqual(result.answers[0].alsoSelected.map((a) => a.actionId), ['b']); assert.match(result.guide ?? '', /INTEGRATION CODE GUIDE/); }); @@ -344,6 +346,81 @@ describe('actions load', () => { const result = await load(['x'], {}, unplaced, settings({ connectionKeys: ['k'], knowledgeAgent: true })); assert.match((result.loaded[0] as DocumentedAction).knowledge, /ONE_PLATFORM_CONNECTION_KEY/); + assert.match((result.loaded[0] as DocumentedAction).knowledge, /redirect: "manual"/); + }); + + it('generated fetch code refuses a foreign redirect without sending credentials', async () => { + let sourceRequests = 0; + let targetRequests = 0; + let sourceHeaders: http.IncomingHttpHeaders | undefined; + const target = http.createServer((_req, res) => { targetRequests++; res.end('{}'); }); + await new Promise(resolve => target.listen(0, '127.0.0.1', resolve)); + const targetAddress = target.address(); + assert.ok(targetAddress && typeof targetAddress !== 'string'); + const source = http.createServer((req, res) => { + sourceRequests++; + sourceHeaders = req.headers; + res.writeHead(307, { Location: `http://127.0.0.1:${targetAddress.port}/capture` }); + res.end(); + }); + try { + await new Promise(resolve => source.listen(0, '127.0.0.1', resolve)); + const sourceAddress = source.address(); + assert.ok(sourceAddress && typeof sourceAddress !== 'string'); + const unplaced: FindDeps = { + ...deps([]), + getActionDetails: async (id: string): Promise => ({ _id: id, title: 'X', method: 'GET', path: '/x', knowledge: '# X' }), + }; + const result = await load(['x'], {}, unplaced, settings({ + knowledgeAgent: true, + apiBase: `http://127.0.0.1:${sourceAddress.port}/v1`, + })); + const guide = (result.loaded[0] as DocumentedAction).knowledge; + const snippet = guide.match(/```typescript\n([\s\S]*?)\n```/)?.[1]; + assert.ok(snippet); + const run = new Function('fetch', 'process', `return (async () => { ${snippet} return response; })()`); + await assert.rejects( + run(fetch, { env: { ONE_SECRET: 'dummy-secret', ONE_PLATFORM_CONNECTION_KEY: 'dummy-connection' } }), + /Unexpected redirect from the One API: 307/, + ); + assert.equal(sourceRequests, 1); + assert.equal(sourceHeaders?.['x-one-secret'], 'dummy-secret'); + assert.equal(sourceHeaders?.['x-one-connection-key'], 'dummy-connection'); + assert.equal(targetRequests, 0); + } finally { + source.closeAllConnections(); + target.closeAllConnections(); + await Promise.all([ + new Promise(resolve => source.close(() => resolve())), + new Promise(resolve => target.close(() => resolve())), + ]); + } + }); + + it('generated fetch code returns an ordinary response unchanged', async () => { + const source = http.createServer((_req, res) => { res.end('{"ok":true}'); }); + try { + await new Promise(resolve => source.listen(0, '127.0.0.1', resolve)); + const sourceAddress = source.address(); + assert.ok(sourceAddress && typeof sourceAddress !== 'string'); + const unplaced: FindDeps = { + ...deps([]), + getActionDetails: async (id: string): Promise => ({ _id: id, title: 'X', method: 'GET', path: '/x', knowledge: '# X' }), + }; + const result = await load(['x'], {}, unplaced, settings({ + knowledgeAgent: true, + apiBase: `http://127.0.0.1:${sourceAddress.port}/v1`, + })); + const snippet = (result.loaded[0] as DocumentedAction).knowledge.match(/```typescript\n([\s\S]*?)\n```/)?.[1]; + assert.ok(snippet); + const run = new Function('fetch', 'process', `return (async () => { ${snippet} return response; })()`); + const response = await run(fetch, { env: { ONE_SECRET: 'dummy-secret', ONE_PLATFORM_CONNECTION_KEY: 'dummy-connection' } }); + assert.equal(response.status, 200); + assert.deepEqual(await response.json(), { ok: true }); + } finally { + source.closeAllConnections(); + await new Promise(resolve => source.close(() => resolve())); + } }); it('serves the first document it actually returns, however large, even after a refused entry', async () => { diff --git a/src/lib/find.ts b/src/lib/find.ts index c152739..02304e9 100644 --- a/src/lib/find.ts +++ b/src/lib/find.ts @@ -580,6 +580,7 @@ Environment variables: ONE_SECRET (their One API key) and ${connEnv} (the key of \`\`\`typescript const response = await fetch("${base}/passthrough${path}", { method: "${method}", +redirect: "manual", // never carry the One secret to another origin headers: { "x-one-secret": process.env.ONE_SECRET, "x-one-connection-key": process.env.${connEnv}, @@ -588,6 +589,9 @@ headers: { }, // body: JSON.stringify(...) - only for non-GET requests }); +if (response.status >= 300 && response.status < 400) { +throw new Error("Unexpected redirect from the One API: " + response.status); +} \`\`\``; }