Zo doen wijdat in oost

This commit is contained in:
2026-10-16 07:20:44 +02:00
parent 25d9cb1c7d
commit 6f80a820da
229 changed files with 23894 additions and 74 deletions
+128
View File
@@ -0,0 +1,128 @@
import {
ALLOWED_PORTS,
DESTINATIONS,
HTTPS_PORT,
LIMITS,
MODE_FETCH,
REFUSALS,
} from "./spec.js";
const HTTPS = "https:";
function isSubset(candidate, allowed) {
return Array.isArray(candidate) && candidate.every((item) => allowed.includes(item));
}
function normaliseHost(host) {
return String(host).trim().toLowerCase().replace(/\.+$/, "");
}
export class Policy {
constructor(destinations = DESTINATIONS, limits = LIMITS) {
this.destinations = destinations;
this.urlMaxChars = limits.url_max_chars;
}
static narrow(baked, refreshed) {
const accepted = [];
const rejected = [];
if (!Array.isArray(refreshed)) {
return { destinations: [], rejected: ["destinations is not a list"] };
}
for (const item of refreshed) {
const suffix = item && typeof item.suffix === "string" ? normaliseHost(item.suffix) : "";
const parent = baked.find(
(entry) => suffix === entry.suffix || suffix.endsWith("." + entry.suffix),
);
if (!parent) {
rejected.push(`${suffix || "?"}: not in the baked-in allowlist`);
continue;
}
const methods = Array.isArray(item.methods) ? item.methods.map((m) => String(m).toUpperCase()) : [];
const ports = Array.isArray(item.ports) ? item.ports : [];
const modes = Array.isArray(item.modes) ? item.modes : [];
if (!methods.length || !isSubset(methods, parent.methods)) {
rejected.push(`${suffix}: methods widen the baked-in list`);
continue;
}
if (!ports.length || !isSubset(ports, parent.ports) || !isSubset(ports, ALLOWED_PORTS)) {
rejected.push(`${suffix}: ports widen the baked-in list`);
continue;
}
if (!modes.includes(MODE_FETCH)) {
rejected.push(`${suffix}: not available in fetch mode`);
continue;
}
accepted.push({
suffix,
adapter: String(item.adapter || parent.adapter),
methods,
ports,
modes,
max_rps: typeof item.max_rps === "number" ? item.max_rps : parent.max_rps,
});
}
return { destinations: accepted, rejected };
}
static fromRefreshed(baked, refreshed, limits = LIMITS) {
const narrowed = Policy.narrow(baked, refreshed);
return { policy: new Policy(narrowed.destinations, limits), rejected: narrowed.rejected };
}
destinationFor(host) {
const name = normaliseHost(host);
return (
this.destinations.find((item) => name === item.suffix || name.endsWith("." + item.suffix)) || null
);
}
refuse(code, reason) {
return { ok: false, code, reason };
}
check(method, rawUrl) {
if (typeof rawUrl !== "string" || !rawUrl || rawUrl.length > this.urlMaxChars) {
return this.refuse(REFUSALS.HOST_NOT_ALLOWED, "url is missing or too long");
}
let url;
try {
url = new URL(rawUrl);
} catch (error) {
return this.refuse(REFUSALS.HOST_NOT_ALLOWED, "url does not parse");
}
if (url.protocol !== HTTPS) {
return this.refuse(REFUSALS.HOST_NOT_ALLOWED, "only https is allowed");
}
if (url.username || url.password) {
return this.refuse(REFUSALS.HOST_NOT_ALLOWED, "credentials in the url are refused");
}
const destination = this.destinationFor(url.hostname);
if (!destination) {
return this.refuse(REFUSALS.HOST_NOT_ALLOWED, `${url.hostname} is not on the allowlist`);
}
const port = url.port === "" ? HTTPS_PORT : Number(url.port);
if (port !== HTTPS_PORT || !destination.ports.includes(port) || !ALLOWED_PORTS.includes(port)) {
return this.refuse(REFUSALS.PORT_NOT_ALLOWED, `port ${port} is not allowed over https`);
}
const verb = String(method || "").toUpperCase();
if (!destination.methods.includes(verb)) {
return this.refuse(REFUSALS.METHOD_NOT_ALLOWED, `${verb || "?"} is not permitted`);
}
return { ok: true, url, destination, method: verb };
}
checkRedirect(location, base, method) {
let target;
try {
target = new URL(location, base);
} catch (error) {
return this.refuse(REFUSALS.REDIRECT_NOT_ALLOWED, "redirect target does not parse");
}
const verdict = this.check(method, target.href);
if (!verdict.ok) {
return this.refuse(REFUSALS.REDIRECT_NOT_ALLOWED, verdict.reason);
}
return verdict;
}
}