From 94e39dab659e5a0271d502931187e749c2ecf241 Mon Sep 17 00:00:00 2001 From: Sourav Das Date: Sun, 8 Nov 2026 03:15:24 +0530 Subject: [PATCH 1/2] Return bodyless redirects for resumable Netlify downloads --- docs/api-abuse-protection.md | 16 ++++++-- netlify.toml | 11 ++++-- .../edge-functions/download-compatibility.ts | 4 +- package-lock.json | 1 + package.json | 1 + shared/download-routing.ts | 2 +- test/download-routing.test.mjs | 37 +++++++++++++++++++ 7 files changed, 61 insertions(+), 11 deletions(-) diff --git a/docs/api-abuse-protection.md b/docs/api-abuse-protection.md index 9cefd33..87fc736 100644 --- a/docs/api-abuse-protection.md +++ b/docs/api-abuse-protection.md @@ -9,7 +9,7 @@ compute and repeated requests, not a guarantee against distributed denial of ser | --- | --- | --- | | Repository and release JSON | Static `/github/repository.json` and `/github/latest-release.json` | 0 | | Retained raw `/.netlify/functions/*` aliases | One protected Edge compatibility resolver; old GitHub URLs redirect to static JSON | 0 | -| App downloads, Windows runtime, explicit runtime architectures | Build-generated CDN 302 redirects to GitHub | 0 | +| App downloads, Windows runtime, explicit runtime architectures | Build-generated CDN 302 redirects; requests with `Range` receive a bodyless 302 from the existing Edge resolver | 0 | | Canonical macOS/Linux runtime architecture detection | One `download-resolver` Node function, using bundled manifests | 0 | | Appcasts and release manifests | Existing static documents, unchanged | 0 | @@ -44,13 +44,17 @@ fallbacks remain available. This improves client behavior; bots can bypass it. `netlify/functions/download-resolver.ts` protects the two canonical dynamic runtime URLs. `netlify/edge-functions/download-compatibility.ts` protects the 16 exact retained raw function aliases, including the old app `?platform=` URL. +The same Edge function handles requests with a `Range` header on the nine fixed +public shortcuts. Netlify can return a slice of its static redirect text as a +`206`; the function returns an empty, non-cacheable `302` so downloaders follow +the redirect and resume at GitHub. Requests without `Range` use the static rules. Each resolver has a native limit of 60 requests per 60 seconds, aggregated by IP and domain. The Edge resolver runs before dispatch in Netlify's reserved functions namespace and shares the same pure resolver and validated catalog. It has no all-site wildcard, does not overlap the Node routes, and makes no upstream fetches. Netlify disables the implicit `/.netlify/functions/download-resolver` URL when custom paths are configured. Netlify rejects `/.netlify/*` as a CDN redirect -source. Public fixed `/download/*` URLs are static rules. The old GitHub aliases return cacheable 302s to the +source. The old GitHub aliases return cacheable 302s to the static JSON documents; standard fetch clients follow them automatically. Unknown paths never invoke the resolver. Non-GET/HEAD requests to the resolver return 405 without reading bodies or contacting upstream services. @@ -98,8 +102,12 @@ After deployment: 1. Check deploy post-processing logs for acceptance of both rate-limit rules. Confirm the Node `download-resolver` and Edge `download-compatibility` are deployed, with no per-alias Node functions or all-site Edge wildcard. -2. Smoke-test static JSON, the legacy JSON redirects and all download URLs with redirects disabled - (do not download installers). Check manifest versions against redirect targets. +2. Check static JSON, the legacy JSON redirects and all download URLs with redirects disabled. + For each fixed shortcut send a GET with `Range: bytes=65536-65567`: expect a + bodyless 302 to the published destination. Repeat after warming the ordinary CDN + redirect. Following the redirect for an available binary must return the same + 32 bytes and Content-Range as requesting that range directly from GitHub. + Check manifest versions against redirect targets. 3. On a deploy preview, make a capped single-IP test of 61 HEAD requests to a dynamic URL over about 30 seconds, allow 10 seconds for enforcement, then make at most three probes. Expect 429; stop immediately on 429 or service errors. Do not diff --git a/netlify.toml b/netlify.toml index e69a595..9020397 100644 --- a/netlify.toml +++ b/netlify.toml @@ -26,10 +26,13 @@ [headers.values] Cache-Control = "public, max-age=31536000, immutable" -# Download redirects are generated from validated manifests in dist/_redirects. -# Canonical dynamic runtime routes use the Node download-resolver.ts function. -# Retained function aliases use the Edge download-compatibility.ts resolver. -# Both use native rate limits. +# A cached static redirect has a text body that Netlify can slice into a 206. +# For resume requests, return an empty 302 before those CDN rules run. GitHub +# handles the original Range and If-Range headers on the actual file. +[[edge_functions]] + function = "download-compatibility" + pattern = "^/download/(windows|macos|linux|linux/deb|ai-runtime/(windows|(macos|linux)/(arm64|x64)))/latest/?$" + header = { range = true } [[redirects]] from = "/home" diff --git a/netlify/edge-functions/download-compatibility.ts b/netlify/edge-functions/download-compatibility.ts index ae30101..0e7edba 100644 --- a/netlify/edge-functions/download-compatibility.ts +++ b/netlify/edge-functions/download-compatibility.ts @@ -4,8 +4,8 @@ import { resolveDownload } from "../../shared/download-routing.ts"; export default (request: Request) => resolveDownload(request, downloadCatalog); -// Edge routing runs before dispatch in Netlify's reserved functions namespace. -// Restrict this compatibility resolver to the retained public aliases. +// These aliases always need the resolver. netlify.toml also binds it to the +// fixed public shortcuts only when the request carries a Range header. export const config: Config = { path: [ "/.netlify/functions/github-repo", diff --git a/package-lock.json b/package-lock.json index 490ab1b..f184a4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,7 @@ "react-markdown": "^10.1.0", "remark-gfm": "^4.0.1", "sharp": "^0.35.5", + "smol-toml": "1.9.0", "tailwindcss": "^3.4.17", "typescript": "^5.8.3", "typescript-eslint": "^8.38.0", diff --git a/package.json b/package.json index 27601b9..6806526 100644 --- a/package.json +++ b/package.json @@ -63,6 +63,7 @@ "react-markdown": "^10.1.0", "remark-gfm": "^4.0.1", "sharp": "^0.35.5", + "smol-toml": "1.9.0", "tailwindcss": "^3.4.17", "typescript": "^5.8.3", "typescript-eslint": "^8.38.0", diff --git a/shared/download-routing.ts b/shared/download-routing.ts index 7c8eceb..f3b5c6e 100644 --- a/shared/download-routing.ts +++ b/shared/download-routing.ts @@ -67,7 +67,7 @@ export function resolveDownload(request: Request, catalog: DownloadCatalog): Res } }); } const fixed = fixedDownloadRoutes(catalog).get(path); - if (fixed) return new Response(null, { status: 302, headers: { Location: fixed } }); + if (fixed) return new Response(null, { status: 302, headers: { Location: fixed, "Cache-Control": "no-store" } }); if (!isDynamicDownload(path)) return new Response(null, { status: 404 }); let target: string; if (path.includes("ai-runtime")) { diff --git a/test/download-routing.test.mjs b/test/download-routing.test.mjs index 4475caa..ae05af7 100644 --- a/test/download-routing.test.mjs +++ b/test/download-routing.test.mjs @@ -5,6 +5,7 @@ import os from "node:os"; import { test } from "node:test"; import { createServer, preview } from "vite"; import { parseAllRedirects } from "@netlify/redirect-parser"; +import { parse as parseToml } from "smol-toml"; import { chromium } from "playwright"; test("download compatibility, architecture precedence and bounded offline load", async () => { @@ -123,6 +124,42 @@ test("download compatibility, architecture precedence and bounded offline load", } }); +test("resume requests on every fixed public shortcut get an empty redirect before the CDN serves a byte range", async () => { + const config = parseToml(await readFile("netlify.toml", "utf8")); + const declaration = config.edge_functions.find(rule => rule.function === "download-compatibility" && rule.header?.range); + assert.deepEqual({ ...declaration.header }, { range: true }); + const paths = new RegExp(declaration.pattern); + const vite = await createServer({ configFile: false, logLevel: "silent", server: { middlewareMode: true, hmr: false } }); + try { + const { fixedDownloadRoutes } = await vite.ssrLoadModule("/shared/download-routing.ts"); + const { downloadCatalog } = await vite.ssrLoadModule("/shared/generatedDownloadCatalog.ts"); + const { default: redirect } = await vite.ssrLoadModule("/netlify/edge-functions/download-compatibility.ts"); + const fixed = [...fixedDownloadRoutes(downloadCatalog)].filter(([route]) => route.startsWith("/download/")); + for (const [route, url] of fixed) { + for (const path of [route, route + "/"]) { + assert.ok(paths.test(path), path); + for (const method of ["GET", "HEAD"]) { + const request = new Request("https://example.test" + path, { + method, headers: { Range: "bytes=65536-", "If-Range": '"file-etag"' }, + }); + const response = redirect(request); + assert.equal(response.status, 302); + assert.equal(response.headers.get("location"), url); + assert.equal(response.headers.get("cache-control"), "no-store"); + assert.equal(response.headers.get("content-range"), null); + assert.equal(response.body, null); + } + } + } + for (const path of ["/download", "/download/windows/latest/more", "/download/ai-runtime/linux/latest", + "/download/ai-runtime/macos/latest", "/download/ai-runtime/linux/other/latest", "/appcast/windows-stable.xml"]) { + assert.equal(paths.test(path), false, path); + } + } finally { + await vite.close(); + } +}); + test("preview uses built redirects and release inputs independently of the working catalog", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "openstudio-download-preview-")); const vite = await createServer({ configFile: false, logLevel: "silent", server: { middlewareMode: true } }); From 617742ef57aae84f86c319b08fa1bf500c0bc1ff Mon Sep 17 00:00:00 2001 From: Sourav Das Date: Sun, 8 Nov 2026 03:20:05 +0530 Subject: [PATCH 2/2] Keep legacy Edge aliases alongside range-only download routes --- netlify.toml | 6 ++++++ .../edge-functions/download-compatibility.ts | 21 +------------------ test/download-routing.test.mjs | 16 ++++---------- 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/netlify.toml b/netlify.toml index 9020397..6d6f558 100644 --- a/netlify.toml +++ b/netlify.toml @@ -26,6 +26,12 @@ [headers.values] Cache-Control = "public, max-age=31536000, immutable" +# Keep both route declarations here: a TOML route for a function replaces its +# inline path list. The historical aliases have no header condition. +[[edge_functions]] + function = "download-compatibility" + pattern = "^/[.]netlify/functions/(github-(repo|release)|download-latest(/(windows|macos|linux)|-(windows|macos|linux)|-ai-runtime-(windows|(macos|linux)(-(arm64|x64))?))?)/?$" + # A cached static redirect has a text body that Netlify can slice into a 206. # For resume requests, return an empty 302 before those CDN rules run. GitHub # handles the original Range and If-Range headers on the actual file. diff --git a/netlify/edge-functions/download-compatibility.ts b/netlify/edge-functions/download-compatibility.ts index 0e7edba..8afe0ce 100644 --- a/netlify/edge-functions/download-compatibility.ts +++ b/netlify/edge-functions/download-compatibility.ts @@ -4,26 +4,7 @@ import { resolveDownload } from "../../shared/download-routing.ts"; export default (request: Request) => resolveDownload(request, downloadCatalog); -// These aliases always need the resolver. netlify.toml also binds it to the -// fixed public shortcuts only when the request carries a Range header. +// netlify.toml owns both sets of routes; they share this native rate limit. export const config: Config = { - path: [ - "/.netlify/functions/github-repo", - "/.netlify/functions/github-release", - "/.netlify/functions/download-latest", - "/.netlify/functions/download-latest/windows", - "/.netlify/functions/download-latest/macos", - "/.netlify/functions/download-latest/linux", - "/.netlify/functions/download-latest-windows", - "/.netlify/functions/download-latest-macos", - "/.netlify/functions/download-latest-linux", - "/.netlify/functions/download-latest-ai-runtime-windows", - "/.netlify/functions/download-latest-ai-runtime-macos", - "/.netlify/functions/download-latest-ai-runtime-linux", - "/.netlify/functions/download-latest-ai-runtime-macos-arm64", - "/.netlify/functions/download-latest-ai-runtime-macos-x64", - "/.netlify/functions/download-latest-ai-runtime-linux-arm64", - "/.netlify/functions/download-latest-ai-runtime-linux-x64", - ], rateLimit: { windowLimit: 60, windowSize: 60, aggregateBy: ["ip", "domain"] }, }; diff --git a/test/download-routing.test.mjs b/test/download-routing.test.mjs index ae05af7..0158821 100644 --- a/test/download-routing.test.mjs +++ b/test/download-routing.test.mjs @@ -6,12 +6,10 @@ import { test } from "node:test"; import { createServer, preview } from "vite"; import { parseAllRedirects } from "@netlify/redirect-parser"; import { parse as parseToml } from "smol-toml"; -import { chromium } from "playwright"; test("download compatibility, architecture precedence and bounded offline load", async () => { const vite = await createServer({ logLevel: "silent", server: { middlewareMode: true } }); const originalFetch = globalThis.fetch; - let browser; try { const { resolveDownload, fixedDownloadRoutes, isDynamicDownload, githubAliases } = await vite.ssrLoadModule("/shared/download-routing.ts"); const { downloadCatalog } = await vite.ssrLoadModule("/shared/generatedDownloadCatalog.ts"); @@ -40,16 +38,11 @@ test("download compatibility, architecture precedence and bounded offline load", "/.netlify/functions/download-latest-ai-runtime-windows-extra", "/.netlify/functions/download-latest-ai-runtime-windows/extra", ]; - // Validate the actual declared ownership: exact retained aliases run at the - // Edge, canonical dynamic paths run in Node, and unknown paths run neither. - // Node 22 does not expose URLPattern globally. - browser = await chromium.launch(); - const page = await browser.newPage(); - const matches = await page.evaluate(({ patterns, paths }) => paths.map((path) => - patterns.some((pattern) => new URLPattern({ pathname: pattern }).test({ pathname: path }))), - { patterns: edgeConfig.path, paths: [...protectedPaths, ...unknownPaths, ...config.path] }); + const netlify = parseToml(await readFile("netlify.toml", "utf8")); + const alias = netlify.edge_functions.find(rule => rule.function === "download-compatibility" && !rule.header); + const aliasPaths = new RegExp(alias.pattern); + const matches = [...protectedPaths, ...unknownPaths, ...config.path].map(path => aliasPaths.test(path)); assert.deepEqual(matches, [...protectedPaths.map(() => true), ...unknownPaths.map(() => false), ...config.path.map(() => false)]); - assert.equal(edgeConfig.path.length, protectedPaths.length, "Edge declarations must not expand beyond retained aliases"); for (const path of protectedPaths) assert.ok(isDynamicDownload(path) || fixedDownloadRoutes(downloadCatalog).has(path) || githubAliases[path], path); let upstreamCalls = 0; globalThis.fetch = async () => { upstreamCalls++; throw new Error("Runtime network requests are forbidden"); }; @@ -119,7 +112,6 @@ test("download compatibility, architecture precedence and bounded offline load", assert.equal(upstreamCalls, 0, "5,000 handler calls must produce zero network requests"); } finally { globalThis.fetch = originalFetch; - if (browser) await browser.close(); await vite.close(); } });