fix(engine): bound the proxied host pre-check by the download's timeout (#8048)

The local lookup that runs before a proxied download took no abort signal,
so a stalled resolver held the request past its timeout. It now settles
when the request's signal aborts.

Its pass-through list now matches what dns.lookup reports: ENOTFOUND,
EAI_AGAIN and EAI_FAIL go to the proxy; ENODATA and ESERVFAIL, which the
lookup never returns, are gone.

Adds a test that runs the real GIF step on a source saved in Studio's
private downloads folder.
This commit is contained in:
Michael Wang
2026-11-17 06:54:23 +00:00
committed by GitHub
parent 7b27fbc84a
commit 06b632017c
3 changed files with 55 additions and 6 deletions
@@ -6,6 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
const answers = vi.hoisted(() => ({
list: [] as Array<{ address: string; family: number }>,
errorCode: undefined as string | undefined,
hang: false,
}));
vi.mock("node:dns", () => {
const lookup = (
@@ -13,9 +14,11 @@ vi.mock("node:dns", () => {
_options: unknown,
callback: (error: NodeJS.ErrnoException | null, addresses: typeof answers.list) => void,
) =>
answers.errorCode
? callback(Object.assign(new Error("lookup failed"), { code: answers.errorCode }), [])
: callback(null, answers.list);
answers.hang
? undefined
: answers.errorCode
? callback(Object.assign(new Error("lookup failed"), { code: answers.errorCode }), [])
: callback(null, answers.list);
return { default: { lookup }, lookup };
});
@@ -24,6 +27,7 @@ import { downloadToTemp, fetchPublicHttpsText, UrlDownloadError } from "./urlDow
const dirs: string[] = [];
afterEach(() => {
answers.errorCode = undefined;
answers.hang = false;
vi.unstubAllGlobals();
vi.unstubAllEnvs();
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true });
@@ -90,6 +94,27 @@ describe("downloads refuse hosts that resolve to private addresses", () => {
expect(viaProxy).toHaveBeenCalledOnce();
});
it("ends a proxied download whose local lookup never answers when the request times out", async () => {
answers.hang = true;
const viaProxy = useEnvProxy();
await expect(
fetchPublicHttpsText("https://assets.example.test/x.html", { maxBytes: 1024, timeoutMs: 20 }),
).rejects.toMatchObject({ kind: "timeout" });
expect(viaProxy).not.toHaveBeenCalled();
});
it.each(["ENOTFOUND", "EAI_AGAIN", "EAI_FAIL"])(
"leaves a name this machine cannot resolve (%s) to the env proxy",
async (code) => {
answers.errorCode = code;
const viaProxy = useEnvProxy();
await expect(
fetchPublicHttpsText("https://assets.example.test/x.html", { maxBytes: 1024 }),
).resolves.toBe("<html></html>");
expect(viaProxy).toHaveBeenCalledOnce();
},
);
it("leaves a name this machine cannot resolve to the env proxy", async () => {
answers.errorCode = "ENOTFOUND";
const viaProxy = useEnvProxy();
+7 -3
View File
@@ -509,13 +509,17 @@ function undiciTransport(lookup: LookupFunction): Promise<DownloadFetch> {
return transport;
}
const UNRESOLVED_HOST_CODES = new Set(["ENOTFOUND", "EAI_AGAIN", "ENODATA", "ESERVFAIL"]);
const UNRESOLVED_HOST_CODES = new Set(["ENOTFOUND", "EAI_AGAIN", "EAI_FAIL"]);
/** Behind an env proxy, refuses a host this machine resolves to a non-public address; the proxy resolves the rest. */
function assertProxiedHostResolvesPublic(url: string): Promise<void> {
function assertProxiedHostResolvesPublic(url: string, signal?: AbortSignal | null): Promise<void> {
const hostname = new URL(url).hostname.replace(/^\[|\]$/g, "");
return new Promise((resolvePromise, reject) => {
const abort = () => reject(signal?.reason);
if (signal?.aborted) return abort();
signal?.addEventListener("abort", abort, { once: true });
dns.lookup(hostname, { all: true }, (error, answers) => {
signal?.removeEventListener("abort", abort);
if (error) {
return UNRESOLVED_HOST_CODES.has(error.code ?? "") ? resolvePromise() : reject(error);
}
@@ -530,7 +534,7 @@ function assertProxiedHostResolvesPublic(url: string): Promise<void> {
const fetchPublic: DownloadFetch = async (url, init) => {
if (resolveRestrictPageNetwork()) return (await undiciTransport(capturePolicyLookup))(url, init);
if (fetchGoesThroughEnvProxy()) {
await assertProxiedHostResolvesPublic(url);
await assertProxiedHostResolvesPublic(url, init.signal);
return fetch(url, init);
}
return (await undiciTransport(publicOnlyLookup))(url, init);
@@ -286,6 +286,26 @@ describe("prepareAnimatedGifInputs", () => {
expect(calls).toHaveLength(1);
});
it("reads a remote GIF that Studio saved in the private downloads folder", async () => {
const projectDir = makeProject();
const downloadDir = join(projectDir, ".hyperframes", "prepared-assets", "downloads");
const sourceUrl = "https://cdn.example.com/loop.gif";
const sourcePath = join(downloadDir, "download_0123456789ab.gif");
mkdirSync(downloadDir, { recursive: true });
writeFileSync(sourcePath, gif([...frame(10), ...frame(10)], 0));
const options = { projectDir, downloadDir, sourceAssets: new Map([[sourceUrl, sourcePath]]) };
try {
expect(resolveLocalImageSourcePath(sourceUrl, options)).toBe(sourcePath);
const result = await prepareAnimatedGifInputs(
`<img data-start="0" data-duration="2" src="${sourceUrl}" />`,
{ ...options, transcode: async (request) => writeFileSync(request.outputPath, "webm") },
);
expect(result.preparedGifs[0]?.sourceSrc).toBe(sourceUrl);
} finally {
rmSync(projectDir, { recursive: true, force: true });
}
});
it("uses source asset mappings for remote GIF URLs", async () => {
const projectDir = makeProject();
const sourceUrl = "https://cdn.example.com/reaction.gif";