Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/infrastructure/source/http_source_downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export class HttpSourceDownloader implements SourceDownloader {
* For version tags, uses tags/{version}.tar.gz
*/
protected getArchiveUrl(version: string): string {
if (!/^[a-zA-Z0-9._-]+$/.test(version)) {
throw new UserError(
`Invalid version string "${version}": must contain only alphanumeric characters, dots, hyphens, and underscores.`,
);
}

if (version === "main") {
return `${HttpSourceDownloader.GITHUB_BASE}/heads/main.tar.gz`;
}
Expand Down
70 changes: 70 additions & 0 deletions src/infrastructure/source/http_source_downloader_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,76 @@ Deno.test("downloadAndExtract skips symlinks escaping via relative path", async
}
});

Deno.test("downloadAndExtract rejects version with path traversal", async () => {
const downloader = new HttpSourceDownloader();
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
await assertRejects(
() => downloader.downloadAndExtract("../malicious", tempDir),
UserError,
"Invalid version string",
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});

Deno.test("downloadAndExtract rejects version with nested path traversal", async () => {
const downloader = new HttpSourceDownloader();
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
await assertRejects(
() => downloader.downloadAndExtract("foo/../../heads/main", tempDir),
UserError,
"Invalid version string",
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});

Deno.test("downloadAndExtract rejects version with URL-encoded characters", async () => {
const downloader = new HttpSourceDownloader();
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
await assertRejects(
() => downloader.downloadAndExtract("version%2F..", tempDir),
UserError,
"Invalid version string",
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});

Deno.test("downloadAndExtract rejects version with query string", async () => {
const downloader = new HttpSourceDownloader();
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
await assertRejects(
() => downloader.downloadAndExtract("v1.0?ref=main", tempDir),
UserError,
"Invalid version string",
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});

Deno.test("downloadAndExtract rejects version with fragment", async () => {
const downloader = new HttpSourceDownloader();
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
await assertRejects(
() => downloader.downloadAndExtract("v1.0#fragment", tempDir),
UserError,
"Invalid version string",
);
} finally {
await Deno.remove(tempDir, { recursive: true });
}
});

Deno.test("downloadAndExtract skips symlinks escaping via absolute path", async () => {
const tempDir = await Deno.makeTempDir({ prefix: "swamp-test-" });
try {
Expand Down