Skip to content

Commit fc7df6c

Browse files
fix(cli): refuse a non-URL install-from-source source up front (#3166)
* fix(cli): refuse a non-URL install-from-source source up front install-from-source treats its positional as a URL. A local path is only rejected later, when the download layer fails to parse it as a source URL, and the error does not say what to run instead. The CLI now refuses a positional that is not an http(s) URL with INVALID_ARGS before any work and points at `install <app> <path>` for local builds. The deprecated SDK `isTrustedInstallSourceUrl` reports an unparsable source as INVALID_ARGS instead of throwing a TypeError. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): parse the install-from-source URL instead of matching its prefix The prefix check let `http://`, `https://` and `http://exa mple.com` through, so they still failed later in the download layer without the `install <app> <path>` hint. Parse the source with URL.parse and require an http or https protocol, so every non-URL source is refused up front. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(provision-kit): share the invalid source URL refusal The download layer and isTrustedInstallSourceUrl each built the same INVALID_ARGS "Invalid source URL" error. Build it in one place so the two refusals cannot drift, and test that both reject an unparsable source with the same code and message. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 642da8f commit fc7df6c

6 files changed

Lines changed: 100 additions & 3 deletions

File tree

‎packages/provision-kit/src/install-source-download.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Readable } from 'node:stream';
55
import { test, vi } from 'vitest';
66
import { mkdtempForTest } from './tmp-dir.fixtures.ts';
77
import { downloadInstallSource } from './install-source-download.ts';
8+
import { isTrustedInstallSourceUrl } from './install-source.ts';
89
import * as networkTransport from './install-source-network-transport.ts';
910

1011
test('download redirects revalidate destinations and strip sensitive cross-origin headers', async () => {
@@ -242,3 +243,17 @@ function response(
242243
close: async () => {},
243244
};
244245
}
246+
247+
test('download and the trusted-source check refuse an unparsable source alike', async () => {
248+
const source = '/abs/path/app.zip';
249+
const expected = { code: 'INVALID_ARGS', message: 'Invalid source URL' };
250+
await assert.rejects(
251+
downloadInstallSource({
252+
tempDir: '/unused',
253+
url: source,
254+
signal: new AbortController().signal,
255+
}),
256+
expected,
257+
);
258+
assert.throws(() => isTrustedInstallSourceUrl(source), expected);
259+
});

‎packages/provision-kit/src/install-source-download.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,15 @@ function parseSourceUrl(raw: string): URL {
208208
try {
209209
parsed = new URL(raw);
210210
} catch {
211-
throw new AppError('INVALID_ARGS', 'Invalid source URL');
211+
throw invalidSourceUrlError();
212212
}
213213

214214
if (parsed.username || parsed.password) {
215215
throw new AppError('INVALID_ARGS', 'Source URL credentials are not allowed');
216216
}
217217
return parsed;
218218
}
219+
220+
export function invalidSourceUrlError(): AppError {
221+
return new AppError('INVALID_ARGS', 'Invalid source URL');
222+
}

‎packages/provision-kit/src/install-source.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
noteInstallArtifactArchiveDepth,
1313
} from './install-artifact-archive-context.ts';
1414
import { approveDownloadSourceUrl } from './install-source-network.ts';
15-
import { downloadInstallSource } from './install-source-download.ts';
15+
import { downloadInstallSource, invalidSourceUrlError } from './install-source-download.ts';
1616

1717
type MaterializeLocalSourceResult = {
1818
localPath: string;
@@ -158,7 +158,8 @@ export async function validateDownloadSourceUrl(parsedUrl: URL): Promise<void> {
158158
* whether a URL names a GitHub Actions or EAS artifact, which says nothing about who built it.
159159
*/
160160
export function isTrustedInstallSourceUrl(sourceUrl: string | URL): boolean {
161-
const parsed = sourceUrl instanceof URL ? sourceUrl : new URL(sourceUrl);
161+
const parsed = sourceUrl instanceof URL ? sourceUrl : URL.parse(sourceUrl);
162+
if (!parsed) throw invalidSourceUrlError();
162163
const hostname = parsed.hostname.toLowerCase();
163164
if (!hostname) return false;
164165
const pathname = parsed.pathname;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { AppError } from '@agent-device/kernel/errors';
4+
import type { AgentDeviceClient } from '../agent-device-client.ts';
5+
import { tryRunClientBackedCommand } from '../cli/commands/router.ts';
6+
7+
const REACHED_CLIENT = new Error('reached the client');
8+
9+
async function runInstallFromSource(source: string) {
10+
const client = {
11+
apps: {
12+
installFromSource: async () => {
13+
throw REACHED_CLIENT;
14+
},
15+
},
16+
} as unknown as AgentDeviceClient;
17+
return await tryRunClientBackedCommand({
18+
command: 'install-from-source',
19+
positionals: [source],
20+
flags: { json: false, help: false, version: false },
21+
client,
22+
});
23+
}
24+
25+
test('install-from-source refuses a local path with a typed error', async () => {
26+
await assert.rejects(
27+
() => runInstallFromSource('/abs/path/app.zip'),
28+
(error) =>
29+
error instanceof AppError &&
30+
error.code === 'INVALID_ARGS' &&
31+
error.message === 'install-from-source <url> must be an http(s) URL: /abs/path/app.zip' &&
32+
String(error.details?.hint).includes('install <app> <path>'),
33+
);
34+
});
35+
36+
for (const source of [
37+
'http://',
38+
'https://',
39+
'http://exa mple.com/app.zip',
40+
'ftp://example.com/app.zip',
41+
'example.com/app.zip',
42+
]) {
43+
test(`install-from-source refuses ${JSON.stringify(source)} before any work`, async () => {
44+
await assert.rejects(
45+
() => runInstallFromSource(source),
46+
(error) =>
47+
error instanceof AppError &&
48+
error.code === 'INVALID_ARGS' &&
49+
String(error.details?.hint).includes('install <app> <path>'),
50+
);
51+
});
52+
}
53+
54+
test('install-from-source passes an http(s) URL to the client', async () => {
55+
await assert.rejects(
56+
() => runInstallFromSource('HTTPS://example.com/app.zip'),
57+
(error) => error === REACHED_CLIENT,
58+
);
59+
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
withAppleToolProvider,
2525
} from '@agent-device/platform-apple/tool-provider';
2626
import { prepareIosInstallArtifact } from '@agent-device/platform-apple/install-artifact';
27+
import { AppError } from '@agent-device/kernel/errors';
2728
import { ANDROID_INSTALL_SOURCE_CONTRACT_EVIDENCE } from './install-source.coverage.ts';
2829
import { mkdtempForTest } from './test-utils/tmp-dir.ts';
2930
import * as networkTransport from '@agent-device/provision-kit/install-source-network-transport';
@@ -104,6 +105,13 @@ test('isTrustedInstallSourceUrl recognizes supported artifact services', () => {
104105
false,
105106
);
106107
assert.equal(isTrustedInstallSourceUrl('https://expo.dev/pricing'), false);
108+
assert.throws(
109+
() => isTrustedInstallSourceUrl('/abs/path/app.zip'),
110+
(error: unknown) =>
111+
error instanceof AppError &&
112+
error.code === 'INVALID_ARGS' &&
113+
error.message === 'Invalid source URL',
114+
);
107115
});
108116

109117
test.sequential('materializeInstallablePath extracts zip archives without ditto', async () => {

‎src/commands/management/install.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,23 @@ function resolveInstallSource(positionals: string[], flags: CliFlags) {
204204
}
205205
if (githubArtifactSource) return githubArtifactSource;
206206
if (configuredSource) return configuredSource;
207+
if (!isHttpUrl(url!)) {
208+
throw new AppError('INVALID_ARGS', `install-from-source <url> must be an http(s) URL: ${url}`, {
209+
hint: 'Install a local build with install <app> <path>.',
210+
});
211+
}
207212
return {
208213
kind: 'url' as const,
209214
url: url!,
210215
headers: parseInstallSourceHeaders(flags.header),
211216
};
212217
}
213218

219+
function isHttpUrl(value: string): boolean {
220+
const protocol = URL.parse(value)?.protocol;
221+
return protocol === 'http:' || protocol === 'https:';
222+
}
223+
214224
function parseInstallSourceHeaders(
215225
headerFlags: CliFlags['header'],
216226
): Record<string, string> | undefined {

0 commit comments

Comments
 (0)