Skip to content

Commit 95761e7

Browse files
committed
fix(daemon): refuse host path install sources through the remote proxy
The `parseInstallSource` gate covered one of the two RPC methods that can carry an install source. The generic `agent_device.command` method copies `params.meta` wholesale and `commandRpcParamsSchema` types `meta` as an opaque object, so `meta.installSource` reached the install handler unparsed — and that is the method the client actually uses. Gating it at the daemon's HTTP boundary would take local callers with it: that server binds loopback, and `agent-device proxy` puts the local daemon in HTTP mode, so a local `install <path>` on a proxy host crosses the same boundary with the same wire shape. The proxy is the seam that separates the daemon's host from callers who are not on it, so the refusal belongs there — where it also covers both RPC methods at once, because the proxy reads the body before the method. An uploaded artifact still backs a path source across the proxy: the daemon substitutes the uploaded file and never reads the wire path, and an upload id the caller does not own throws rather than falling back to it. That fallback is what would turn the carve-out into the hole, so it is now pinned by a test. This refuses `install remote:<path>` through a proxy. Naming a file on the daemon's host from a remote client is the reported vulnerability, so the affordance cannot survive the fix; the client-side prefix is left in place. Refs #2097
1 parent 68a5d51 commit 95761e7

3 files changed

Lines changed: 206 additions & 0 deletions

File tree

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

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,134 @@ function sendUploadProxyFinalize(
513513
res.setHeader('content-type', 'application/json');
514514
res.end(JSON.stringify({ ok: true, uploadId: 'tracked-upload-1' }));
515515
}
516+
517+
type ProxyRpcOutcome = {
518+
status: number;
519+
body: { error?: { code?: number; message?: string; data?: { code?: string } } };
520+
upstreamCalls: number;
521+
};
522+
523+
async function postInstallRpcThroughProxy(
524+
params: Record<string, unknown>,
525+
): Promise<ProxyRpcOutcome> {
526+
let upstreamCalls = 0;
527+
const upstream = http.createServer((req, res) => {
528+
upstreamCalls += 1;
529+
let body = '';
530+
req.setEncoding('utf8');
531+
req.on('data', (chunk) => {
532+
body += chunk;
533+
});
534+
req.on('end', () => {
535+
res.setHeader('content-type', 'application/json');
536+
res.end(
537+
JSON.stringify({
538+
jsonrpc: '2.0',
539+
id: (JSON.parse(body) as { id?: unknown }).id,
540+
result: { ok: true, data: { reached: 'upstream' } },
541+
}),
542+
);
543+
});
544+
});
545+
const proxy = createDaemonProxyServer({
546+
upstreamBaseUrl: `http://127.0.0.1:${await listenOnLoopback(upstream)}`,
547+
upstreamToken: 'daemon-secret',
548+
clientToken: 'proxy-secret',
549+
});
550+
551+
try {
552+
const proxyPort = await listenOnLoopback(proxy);
553+
const response = await fetch(`http://127.0.0.1:${proxyPort}/agent-device/rpc`, {
554+
method: 'POST',
555+
headers: { 'content-type': 'application/json', authorization: 'Bearer proxy-secret' },
556+
body: JSON.stringify({ jsonrpc: '2.0', id: 'req-1', ...params }),
557+
});
558+
return {
559+
status: response.status,
560+
body: (await response.json()) as ProxyRpcOutcome['body'],
561+
upstreamCalls,
562+
};
563+
} finally {
564+
await closeLoopbackServer(proxy);
565+
await closeLoopbackServer(upstream);
566+
}
567+
}
568+
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.
572+
test('proxy refuses a host path install source on the generic command method', async (t) => {
573+
if (await skipWhenLoopbackUnavailable(t)) return;
574+
575+
const { status, body, upstreamCalls } = await postInstallRpcThroughProxy({
576+
method: 'agent_device.command',
577+
params: {
578+
token: 'proxy-secret',
579+
command: 'install_source',
580+
positionals: [],
581+
flags: { platform: 'android' },
582+
meta: { installSource: { kind: 'path', path: '/etc/passwd' } },
583+
},
584+
});
585+
586+
assert.equal(status, 400);
587+
assert.equal(body.error?.code, -32602);
588+
assert.equal(body.error?.data?.code, 'INVALID_ARGS');
589+
assert.equal(upstreamCalls, 0, 'the daemon must never see a proxied host path source');
590+
});
591+
592+
test('proxy refuses a host path install source on the install_from_source method', async (t) => {
593+
if (await skipWhenLoopbackUnavailable(t)) return;
594+
595+
const { status, body, upstreamCalls } = await postInstallRpcThroughProxy({
596+
method: 'agent_device.install_from_source',
597+
params: {
598+
token: 'proxy-secret',
599+
platform: 'android',
600+
source: { kind: 'path', path: '/etc/passwd' },
601+
},
602+
});
603+
604+
assert.equal(status, 400);
605+
assert.equal(body.error?.data?.code, 'INVALID_ARGS');
606+
assert.equal(upstreamCalls, 0);
607+
});
608+
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.
611+
test('proxy forwards a path source backed by an uploaded artifact', async (t) => {
612+
if (await skipWhenLoopbackUnavailable(t)) return;
613+
614+
const { status, upstreamCalls } = await postInstallRpcThroughProxy({
615+
method: 'agent_device.command',
616+
params: {
617+
token: 'proxy-secret',
618+
command: 'install_source',
619+
positionals: [],
620+
flags: { platform: 'android' },
621+
meta: {
622+
installSource: { kind: 'path', path: '/Users/dev/Downloads/Sample.apk' },
623+
uploadedArtifactId: 'upload-1',
624+
},
625+
},
626+
});
627+
628+
assert.equal(status, 200);
629+
assert.equal(upstreamCalls, 1);
630+
});
631+
632+
test('proxy forwards url install sources unchanged', async (t) => {
633+
if (await skipWhenLoopbackUnavailable(t)) return;
634+
635+
const { status, upstreamCalls } = await postInstallRpcThroughProxy({
636+
method: 'agent_device.install_from_source',
637+
params: {
638+
token: 'proxy-secret',
639+
platform: 'android',
640+
source: { kind: 'url', url: 'https://example.com/app.apk' },
641+
},
642+
});
643+
644+
assert.equal(status, 200);
645+
assert.equal(upstreamCalls, 1);
646+
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@ test('resolveInstallSource rejects GitHub Actions artifact sources on the local
7676
),
7777
).toThrow(/compatible remote daemon/i);
7878
});
79+
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.
83+
test('resolveInstallSource refuses an unknown upload id rather than reading the wire path', () => {
84+
expect(() =>
85+
resolveInstallSource(
86+
makeRequest({
87+
uploadedArtifactId: 'not-a-real-upload',
88+
installSource: { kind: 'path', path: '/etc/passwd' },
89+
}),
90+
),
91+
).toThrow();
92+
});

‎src/remote/daemon-proxy.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ async function handleProxyRequest(
8080
return;
8181
}
8282

83+
if (carriesUnbackedHostPathInstallSource(rpcBody)) {
84+
sendHostPathInstallSourceRefused(res, readJsonRpcId(rpcBody));
85+
return;
86+
}
87+
8388
await forwardProxyRequest({ req, res, route, options, rpcBody });
8489
}
8590

@@ -371,6 +376,62 @@ function rewriteRpcToken(body: string, upstreamToken: string): string {
371376
return JSON.stringify(parsed);
372377
}
373378

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+
374435
function readJsonRpcToken(body: string): string {
375436
try {
376437
const parsed = JSON.parse(body) as { params?: { token?: unknown } };

0 commit comments

Comments
 (0)