Skip to content

Commit 96092af

Browse files
committed
refactor(proxy): give the host-path refusal its own module
Review: the narration I added carried what names, a module boundary, and test names should carry, and `daemon-proxy.ts` had held no such comments before. `proxy-install-source-admission.ts` now owns the rule, so the boundary states it instead of a comment; `isBackedByUploadedArtifact` names what "unbacked" means in `carriesUnbackedHostPathInstallSource`. The proxy owner returns to 443 lines from 495. Refs #2097
1 parent 95761e7 commit 96092af

6 files changed

Lines changed: 55 additions & 71 deletions

File tree

‎src/__tests__/daemon-proxy.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,6 @@ async function postInstallRpcThroughProxy(
566566
}
567567
}
568568

569-
// #2097: the daemon's own HTTP server binds loopback, so the proxy is the seam between
570-
// the daemon's host and callers that are not on it. A `path` install source names a file
571-
// on that host, so it must not cross — through either rpc method that can carry one.
572569
test('proxy refuses a host path install source on the generic command method', async (t) => {
573570
if (await skipWhenLoopbackUnavailable(t)) return;
574571

@@ -606,8 +603,6 @@ test('proxy refuses a host path install source on the install_from_source method
606603
assert.equal(upstreamCalls, 0);
607604
});
608605

609-
// The sanctioned remote install route: the client uploads the artifact first, and the
610-
// daemon substitutes the uploaded file for the wire path, so the path is never read.
611606
test('proxy forwards a path source backed by an uploaded artifact', async (t) => {
612607
if (await skipWhenLoopbackUnavailable(t)) return;
613608

‎src/daemon/__tests__/http-server-rpc-validation.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,6 @@ async function withInstallFromSourceRpcServer(
172172
}
173173
}
174174

175-
// #2097: honoring `source.kind: "path"` from the wire would hand any authenticated
176-
// caller a read of whatever the daemon user can read, through the install pipeline.
177175
test('install_from_source rejects a host path source at the rpc boundary', async (t) => {
178176
await withInstallFromSourceRpcServer(async (post) => {
179177
const { status, body, dispatched } = await post({ kind: 'path', path: '/etc/passwd' });

‎src/daemon/__tests__/install-source-resolution.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ test('resolveInstallSource rejects GitHub Actions artifact sources on the local
7777
).toThrow(/compatible remote daemon/i);
7878
});
7979

80-
// #2097: the proxy lets a path source cross only when an upload backs it, which is safe
81-
// solely because an upload id the caller does not own throws here instead of falling
82-
// back to the path from the wire. A fallback would turn that carve-out into the hole.
8380
test('resolveInstallSource refuses an unknown upload id rather than reading the wire path', () => {
8481
expect(() =>
8582
resolveInstallSource(

‎src/daemon/server/http-server.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ type HttpAuthDecision =
7676
| { ok: true; tenantId?: string }
7777
| { ok: false; statusCode: number; response: JsonRpcResponse };
7878

79-
/**
80-
* The install sources the daemon's HTTP surface admits. A `path` source names a file
81-
* on the daemon's own host, so it stays an in-process affordance for callers that
82-
* already carry the daemon's authority (#2097).
83-
*/
8479
type HttpInstallSource = Exclude<DaemonInstallSource, { kind: 'path' }>;
8580

8681
const MAX_HTTP_RPC_BODY_BYTES = 1024 * 1024;

‎src/remote/daemon-proxy.ts‎

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import {
1212
buildDaemonHttpUrl,
1313
} from '../daemon/http-contract.ts';
1414
import { buildDaemonHealthPayload } from '../daemon/http-health.ts';
15+
import {
16+
carriesUnbackedHostPathInstallSource,
17+
sendHostPathInstallSourceRefused,
18+
} from './proxy-install-source-admission.ts';
1519

1620
export type DaemonProxyOptions = {
1721
upstreamBaseUrl: string;
@@ -376,62 +380,6 @@ function rewriteRpcToken(body: string, upstreamToken: string): string {
376380
return JSON.stringify(parsed);
377381
}
378382

379-
/**
380-
* A `path` install source names a file on the daemon's own host. The daemon's HTTP
381-
* server binds loopback, so this proxy is the seam between that host and callers who
382-
* are not on it, and no such source may cross it (#2097). An uploaded artifact backs
383-
* the only legitimate crossing: the daemon substitutes the uploaded file and never
384-
* reads the path from the wire, and it rejects an upload id that is not the caller's.
385-
*/
386-
function carriesUnbackedHostPathInstallSource(rpcBody: string | undefined): boolean {
387-
const params = readRpcParams(rpcBody);
388-
if (!params) return false;
389-
const meta = readRecord(params.meta);
390-
if (hasUploadedArtifactId(meta)) return false;
391-
return isHostPathInstallSource(params.source) || isHostPathInstallSource(meta?.installSource);
392-
}
393-
394-
function readRpcParams(rpcBody: string | undefined): Record<string, unknown> | undefined {
395-
if (!rpcBody) return undefined;
396-
try {
397-
return readRecord(readRecord(JSON.parse(rpcBody))?.params);
398-
} catch {
399-
return undefined;
400-
}
401-
}
402-
403-
function hasUploadedArtifactId(meta: Record<string, unknown> | undefined): boolean {
404-
const uploadedArtifactId = meta?.uploadedArtifactId;
405-
return typeof uploadedArtifactId === 'string' && uploadedArtifactId.trim().length > 0;
406-
}
407-
408-
function isHostPathInstallSource(source: unknown): boolean {
409-
return readRecord(source)?.kind === 'path';
410-
}
411-
412-
function readRecord(value: unknown): Record<string, unknown> | undefined {
413-
return value && typeof value === 'object' && !Array.isArray(value)
414-
? (value as Record<string, unknown>)
415-
: undefined;
416-
}
417-
418-
function sendHostPathInstallSourceRefused(res: ServerResponse, rpcId: unknown): void {
419-
const error = new AppError(
420-
'INVALID_ARGS',
421-
'Invalid params: an install source of kind "path" names a file on the daemon host and is not accepted through the proxy',
422-
{ hint: 'Upload the artifact, or use a "url" or "github-actions-artifact" source.' },
423-
);
424-
res.statusCode = 400;
425-
res.setHeader('content-type', 'application/json');
426-
res.end(
427-
JSON.stringify({
428-
jsonrpc: '2.0',
429-
id: rpcId,
430-
error: { code: -32602, message: error.message, data: normalizeError(error) },
431-
}),
432-
);
433-
}
434-
435383
function readJsonRpcToken(body: string): string {
436384
try {
437385
const parsed = JSON.parse(body) as { params?: { token?: unknown } };
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { ServerResponse } from 'node:http';
2+
import { AppError, normalizeError } from '@agent-device/kernel/errors';
3+
4+
export function carriesUnbackedHostPathInstallSource(rpcBody: string | undefined): boolean {
5+
const params = readRpcParams(rpcBody);
6+
if (!params) return false;
7+
const meta = readRecord(params.meta);
8+
if (isBackedByUploadedArtifact(meta)) return false;
9+
return isHostPathInstallSource(params.source) || isHostPathInstallSource(meta?.installSource);
10+
}
11+
12+
export function sendHostPathInstallSourceRefused(res: ServerResponse, rpcId: unknown): void {
13+
const error = new AppError(
14+
'INVALID_ARGS',
15+
'Invalid params: an install source of kind "path" names a file on the daemon host and is not accepted through the proxy',
16+
{ hint: 'Upload the artifact, or use a "url" or "github-actions-artifact" source.' },
17+
);
18+
res.statusCode = 400;
19+
res.setHeader('content-type', 'application/json');
20+
res.end(
21+
JSON.stringify({
22+
jsonrpc: '2.0',
23+
id: rpcId,
24+
error: { code: -32602, message: error.message, data: normalizeError(error) },
25+
}),
26+
);
27+
}
28+
29+
function readRpcParams(rpcBody: string | undefined): Record<string, unknown> | undefined {
30+
if (!rpcBody) return undefined;
31+
try {
32+
return readRecord(readRecord(JSON.parse(rpcBody))?.params);
33+
} catch {
34+
return undefined;
35+
}
36+
}
37+
38+
function isBackedByUploadedArtifact(meta: Record<string, unknown> | undefined): boolean {
39+
const uploadedArtifactId = meta?.uploadedArtifactId;
40+
return typeof uploadedArtifactId === 'string' && uploadedArtifactId.trim().length > 0;
41+
}
42+
43+
function isHostPathInstallSource(source: unknown): boolean {
44+
return readRecord(source)?.kind === 'path';
45+
}
46+
47+
function readRecord(value: unknown): Record<string, unknown> | undefined {
48+
return value && typeof value === 'object' && !Array.isArray(value)
49+
? (value as Record<string, unknown>)
50+
: undefined;
51+
}

0 commit comments

Comments
 (0)