Skip to content

Commit f27001d

Browse files
amankansal-ltclaude
andcommitted
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]>
1 parent ea045da commit f27001d

3 files changed

Lines changed: 22 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: 2 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;
@@ -159,7 +159,7 @@ export async function validateDownloadSourceUrl(parsedUrl: URL): Promise<void> {
159159
*/
160160
export function isTrustedInstallSourceUrl(sourceUrl: string | URL): boolean {
161161
const parsed = sourceUrl instanceof URL ? sourceUrl : URL.parse(sourceUrl);
162-
if (!parsed) throw new AppError('INVALID_ARGS', 'Invalid source URL');
162+
if (!parsed) throw invalidSourceUrlError();
163163
const hostname = parsed.hostname.toLowerCase();
164164
if (!hostname) return false;
165165
const pathname = parsed.pathname;

0 commit comments

Comments
 (0)