Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: wsl image download through the app fetch

Samrat Talukdar35a31a6303a0
diff --git a/apps/server/src/box/backends/wsl.ts b/apps/server/src/box/backends/wsl.ts
index 7d0f012c..72eab237 100644
--- a/apps/server/src/box/backends/wsl.ts
+++ b/apps/server/src/box/backends/wsl.ts
@@ -28,11 +28,14 @@
import { spawn, type ChildProcess } from "node:child_process";
import { createHash } from "node:crypto";
import { createReadStream, createWriteStream, existsSync, mkdirSync, readdirSync, renameSync, rmSync, statSync } from "node:fs";
-import https from "node:https";
import path from "node:path";
+import { Readable } from "node:stream";
+import { pipeline } from "node:stream/promises";
+import type { ReadableStream as NodeReadableStream } from "node:stream/web";
import { GOARCH, containerName, containerRoot, firstLine, helperPath, readJson, writeJson, wslPath, type BoxLog, type BoxRuntime } from "../common.js";
import { runCommand, type RunCommand, type RunResult } from "../run.js";
+import { externalFetch } from "../../server-fetch.js";
import type { RuntimeResult } from "../runtime.js";
import bundledPins from "../wsl.json" with { type: "json" };
import { IDLE_MINUTES, newMeta, upArgs, type BoxMeta } from "./bwrap.js";
@@ -159,29 +162,23 @@ export function sha256File(file: string): Promise<string> {
});
}
-/** Download over https (redirects followed) to `dest`, checked against `sha256`. */
-export async function download(url: string, dest: string, sha256: string | null, { maxRedirects = 5 }: { maxRedirects?: number } = {}): Promise<string> {
+/** Download over https (redirects followed, through externalFetch: the app's proxy and CA settings) to `dest`, checked against `sha256`. */
+export async function download(url: string, dest: string, sha256: string | null, { fetcher = externalFetch }: { fetcher?: (input: string, init?: RequestInit) => Promise<Response> } = {}): Promise<string> {
+ if (!/^https:\/\//.test(url)) throw new Error("download: https only");
mkdirSync(path.dirname(dest), { recursive: true });
const tmp = `${dest}.${process.pid}.part`;
- await new Promise<void>((resolve, reject) => {
- const fetch = (u: string, left: number) => {
- https.get(u, { headers: { "user-agent": "omnirush" } }, (res) => {
- if ([301, 302, 303, 307, 308].includes(res.statusCode ?? 0) && res.headers.location && left > 0) {
- res.resume();
- fetch(new URL(res.headers.location, u).toString(), left - 1);
- return;
- }
- if (res.statusCode !== 200) { res.resume(); reject(new Error(`download: HTTP ${res.statusCode}`)); return; }
- const out = createWriteStream(tmp);
- res.pipe(out);
- out.on("finish", () => resolve());
- out.on("error", reject);
- res.on("error", reject);
- }).on("error", reject);
- };
- if (!/^https:\/\//.test(url)) { reject(new Error("download: https only")); return; }
- fetch(url, maxRedirects);
- });
+ const res = await fetcher(url, { headers: { "user-agent": "omnirush" }, redirect: "follow" });
+ if (res.status !== 200 || !res.body) {
+ await res.body?.cancel().catch(() => undefined);
+ throw new Error(`download: HTTP ${res.status}`);
+ }
+ if (res.url && !res.url.startsWith("https://")) throw new Error("download: https only");
+ try {
+ await pipeline(Readable.fromWeb(res.body as unknown as NodeReadableStream<Uint8Array>), createWriteStream(tmp));
+ } catch (error) {
+ rmSync(tmp, { force: true });
+ throw error;
+ }
const got = await sha256File(tmp);
if (sha256 && got !== sha256) { rmSync(tmp, { force: true }); throw new Error(`download: sha256 mismatch (${got.slice(0, 12)}…)`); }
renameSync(tmp, dest);
diff --git a/apps/server/src/box/box-backends.test.ts b/apps/server/src/box/box-backends.test.ts
index 847bedb4..4718fdad 100644
--- a/apps/server/src/box/box-backends.test.ts
+++ b/apps/server/src/box/box-backends.test.ts
@@ -7,6 +7,7 @@
// the service's status and setting, files-used through a non-container box.
import { afterEach, describe, expect, test } from "bun:test";
import { ChildProcess } from "node:child_process";
+import { createHash } from "node:crypto";
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -16,7 +17,7 @@ import { OPT_IN, BACKENDS, backendsFor, detectBackend, driverFor, normalizeBacke
import { apparmorProfile, boxdEnv, boxrunPath, explainProbe, parseOut, probeBwrap, upArgs } from "./backends/bwrap.js";
import { createAppleArgs, findDigest, macosMajor, parseVersions, probeApple, snapshotInfo, SUPPORTED as APPLE } from "./backends/apple.js";
import { versionAtLeast } from "./backends/shared.js";
-import { decodeWsl, ENABLE_HINT, inDistro, parseWslList, probeWslDistro, startKeeper } from "./backends/wsl.js";
+import { decodeWsl, download, ENABLE_HINT, inDistro, parseWslList, probeWslDistro, startKeeper } from "./backends/wsl.js";
import { createWslcArgs, labelsOf, parseJsonRows, parseWslcVersion, probeWslc, UPDATE_HINT } from "./backends/wslc.js";
import { Box, setToolsRoot } from "./box.js";
import { askedBackend, readBackend, rememberServerDefault, saveBackend, serverBackends } from "./launch.js";
@@ -286,6 +287,24 @@ class FakeChild extends ChildProcess {
const fakeChild = () => new FakeChild();
describe("box backends: Windows", () => {
+ test("WSL rootfs download: through the app's fetch, https only, checked against its sha256", async () => {
+ const dir = tmp();
+ const body = Buffer.from("rootfs bytes");
+ const sum = createHash("sha256").update(body).digest("hex");
+ const asked: Array<[string, RequestInit | undefined]> = [];
+ const fetcher = async (url: string, init?: RequestInit) => {
+ asked.push([url, init]);
+ return Object.defineProperty(new Response(body, { status: 200 }), "url", { value: url });
+ };
+ const dest = path.join(dir, "rootfs.tar.gz");
+ expect(await download("https://example.invalid/r.tar.gz", dest, sum, { fetcher })).toBe(dest);
+ expect(readFileSync(dest, "utf8")).toBe("rootfs bytes");
+ expect(asked[0]?.[1]?.redirect).toBe("follow");
+ await expect(download("https://example.invalid/r.tar.gz", path.join(dir, "bad"), "0".repeat(64), { fetcher })).rejects.toThrow(/sha256 mismatch/);
+ await expect(download("http://example.invalid/r.tar.gz", path.join(dir, "plain"), null, { fetcher })).rejects.toThrow(/https only/);
+ await expect(download("https://example.invalid/missing", path.join(dir, "m"), null, { fetcher: async () => new Response("no", { status: 404 }) })).rejects.toThrow(/HTTP 404/);
+ });
+
test("wsl.exe output: UTF-16LE with a BOM or UTF-8, the verbose list with its default marker", () => {
const list = " NAME STATE VERSION\r\n* Ubuntu-24.04 Running 2\r\n OmniRushBox Stopped 2\r\n Legacy Distro Stopped 1\r\n";
expect(decodeWsl(utf16(list))).toBe(decodeWsl(Buffer.from(list)));