Files
reverse-engineering-anything/src/application/HopperDownload.test.ts
T
8575dfe370 fix(hopper): refuse downloads redirected to another origin (#6700)
* fix(hopper): refuse downloads redirected to another origin

* test(hopper): cover download redirect origins

* Check redirect protocol before origin so downgrades keep non_https

* style: apply oxfmt to HopperDownload.ts

---------

Co-authored-by: Dextheking1 <Dextheking1@users.noreply.github.com>
Co-authored-by: benjaminkincses <4175097+benjaminkincses@users.noreply.github.com>
Co-authored-by: Ysopwnable <161784452+N0zoM1z0@users.noreply.github.com>
2026-11-27 04:21:36 +08:00

265 lines
8.2 KiB
TypeScript

import { createServer } from "node:http";
import { once } from "node:events";
import { afterEach, describe, expect, it, vi } from "vitest";
import { downloadHopperPackage } from "./HopperDownload.js";
afterEach(() => {
vi.unstubAllGlobals();
vi.useRealTimers();
});
it.each(["physical", "caller", "node-wrapper", "default-abort"] as const)(
"attributes fetch rejection to the operation that failed (%s)",
async (kind) => {
const controller = new AbortController();
const reason = { caller: "stop download" };
const physical = new TypeError("network read failed");
const wrapped = Object.assign(
new Error("operation aborted", { cause: reason }),
{
name: "AbortError",
code: "ABORT_ERR",
},
);
vi.stubGlobal("fetch", async (_url: string, options: RequestInit) => {
if (kind === "default-abort") controller.abort();
else controller.abort(reason);
if (kind === "physical") throw physical;
if (kind === "node-wrapper") throw wrapped;
throw options.signal?.reason;
});
const download = downloadHopperPackage("https://fixture.invalid/archive", {
signal: controller.signal,
});
if (kind === "physical")
expect(await download).toMatchObject({
ok: false,
error: { kind: "network", detail: physical.message },
});
else
await expect(download).rejects.toBe(
kind === "node-wrapper" ? wrapped : controller.signal.reason,
);
},
);
it.each([false, true])(
"attributes an expired download deadline to its own rejection (%s)",
async (interrupted) => {
vi.useFakeTimers();
vi.stubGlobal("fetch", async (_url: string, options: RequestInit) => {
await new Promise<void>((resolve) => setTimeout(resolve, 2));
throw interrupted
? options.signal?.reason
: new TypeError("physical network failure");
});
const download = downloadHopperPackage("https://fixture.invalid/archive", {
timeoutMs: 1,
});
await vi.advanceTimersByTimeAsync(2);
expect(await download).toMatchObject({
ok: false,
error: interrupted
? { kind: "timeout", limit: "deadline", timeoutMs: 1 }
: { kind: "network", detail: "physical network failure" },
});
},
);
it.each([null, false, 0, ""] as const)(
"retains a falsy caller abort reason when the response body is cancelled (%j)",
async (reason) => {
const controller = new AbortController();
vi.stubGlobal(
"fetch",
async (_url: string, options: RequestInit) =>
new Response(
new ReadableStream({
pull(stream) {
controller.abort(reason);
stream.error(options.signal?.reason);
},
}),
{ status: 200 },
),
);
await expect(
downloadHopperPackage("https://fixture.invalid/archive", {
signal: controller.signal,
}),
).rejects.toBe(reason);
},
);
it("does not treat an AbortError body failure as cancellation when the caller did not abort", async () => {
const failure = new DOMException("The operation was aborted", "AbortError");
vi.stubGlobal(
"fetch",
async () =>
new Response(
new ReadableStream({
pull(stream) {
stream.error(failure);
},
}),
{ status: 200 },
),
);
await expect(
downloadHopperPackage("https://fixture.invalid/archive"),
).rejects.toMatchObject({ name: "AbortError", code: DOMException.ABORT_ERR });
});
it.each([null, false, 0, "", { caller: "stop download" }] as const)(
"preserves an independent body failure when the caller also aborts (%j)",
async (reason) => {
const controller = new AbortController();
const failure = new DOMException(
"independent response failure",
"AbortError",
);
vi.stubGlobal(
"fetch",
async () =>
new Response(
new ReadableStream<Uint8Array>({
pull(stream) {
controller.abort(reason);
stream.error(failure);
},
}),
),
);
await expect(
downloadHopperPackage("https://fixture.invalid/archive", {
signal: controller.signal,
}),
).rejects.toBe(failure);
},
);
it.each([null, false, { caller: "cancel" }] as const)(
"retains the exact caller reason through a native response body read (%j)",
async (reason) => {
const server = createServer((_request, response) => {
response.writeHead(200);
response.write("first chunk");
});
server.listen(0, "127.0.0.1");
await once(server, "listening");
const address = server.address();
if (address === null || typeof address === "string")
throw new Error("Expected a TCP listener");
const controller = new AbortController();
const nativeFetch = globalThis.fetch;
vi.stubGlobal("fetch", async (_url: string, options: RequestInit) => {
const response = await nativeFetch(
`http://127.0.0.1:${address.port}/`,
options,
);
setImmediate(() => controller.abort(reason));
return response;
});
try {
await expect(
downloadHopperPackage("https://fixture.invalid/archive", {
signal: controller.signal,
}),
).rejects.toBe(reason);
} finally {
server.closeAllConnections();
await new Promise<void>((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve())),
);
}
},
);
describe("downloadHopperPackage redirect origin", () => {
const redirectResponse = (location: string): Response =>
new Response(null, { status: 302, headers: { location } });
it("returns the bytes for a direct response", async () => {
const fetch = vi
.fn<typeof globalThis.fetch>()
.mockResolvedValue(new Response("payload", { status: 200 }));
vi.stubGlobal("fetch", fetch);
const result = await downloadHopperPackage(
"https://www.hopperapp.com/download",
);
expect(result).toEqual({ ok: true, value: Buffer.from("payload") });
expect(fetch).toHaveBeenCalledTimes(1);
});
it("returns the bytes when redirects stay on the requested origin", async () => {
const fetch = vi
.fn<typeof globalThis.fetch>()
.mockResolvedValueOnce(redirectResponse("/releases/pkg.deb"))
.mockResolvedValueOnce(
redirectResponse("https://www.hopperapp.com/final/pkg.deb"),
)
.mockResolvedValueOnce(new Response("payload", { status: 200 }));
vi.stubGlobal("fetch", fetch);
const result = await downloadHopperPackage(
"https://www.hopperapp.com/download",
);
expect(result).toEqual({ ok: true, value: Buffer.from("payload") });
expect(fetch.mock.calls.map(([input]) => input)).toEqual([
"https://www.hopperapp.com/download",
"https://www.hopperapp.com/releases/pkg.deb",
"https://www.hopperapp.com/final/pkg.deb",
]);
});
it("refuses a redirect to another origin without requesting it", async () => {
const fetch = vi
.fn<typeof globalThis.fetch>()
.mockResolvedValueOnce(
redirectResponse("https://cdn.example.net/pkg.deb"),
)
.mockResolvedValueOnce(new Response("payload", { status: 200 }));
vi.stubGlobal("fetch", fetch);
const result = await downloadHopperPackage(
"https://www.hopperapp.com/download",
);
expect(result).toEqual({
ok: false,
error: {
url: "https://www.hopperapp.com/download",
kind: "url_refused",
refusal: "redirect_cross_origin",
},
});
expect(fetch).toHaveBeenCalledTimes(1);
});
it("refuses a later hop that leaves the requested origin", async () => {
const fetch = vi
.fn<typeof globalThis.fetch>()
.mockResolvedValueOnce(redirectResponse("/releases/pkg.deb"))
.mockResolvedValueOnce(
redirectResponse("https://cdn.example.net/pkg.deb"),
)
.mockResolvedValueOnce(new Response("payload", { status: 200 }));
vi.stubGlobal("fetch", fetch);
const result = await downloadHopperPackage(
"https://www.hopperapp.com/download",
);
expect(result).toEqual({
ok: false,
error: {
url: "https://www.hopperapp.com/download",
kind: "url_refused",
refusal: "redirect_cross_origin",
},
});
expect(fetch).toHaveBeenCalledTimes(2);
});
});