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
16 changes: 12 additions & 4 deletions docs/api-abuse-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@
[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.
# 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.
[[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"
Expand Down
21 changes: 1 addition & 20 deletions netlify/edge-functions/download-compatibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,7 @@ 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.
// 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"] },
};
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion shared/download-routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Expand Down
53 changes: 41 additions & 12 deletions test/download-routing.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import os from "node:os";
import { test } from "node:test";
import { createServer, preview } from "vite";
import { parseAllRedirects } from "@netlify/redirect-parser";
import { chromium } from "playwright";
import { parse as parseToml } from "smol-toml";

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");
Expand Down Expand Up @@ -39,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"); };
Expand Down Expand Up @@ -118,7 +112,42 @@ 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();
}
});

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();
}
});
Expand Down
Loading