Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Origin-allowlist helper for the form-widget submission endpoint.
|
||||
*
|
||||
* Supports exact-match (`https://example.com`), bare hostnames (`example.com`)
|
||||
* and wildcard subdomain patterns (`*.example.com`). Schemes default to https
|
||||
* when not provided. Ports must match exactly when present.
|
||||
*
|
||||
* Returns `true` only when at least one allowed entry matches. An empty
|
||||
* allowlist always returns `false` — a form with no allowed origins is
|
||||
* non-submittable from any browser, by design.
|
||||
*/
|
||||
|
||||
export function normalizeOrigin(input: string | null | undefined): string | null {
|
||||
if (!input) return null;
|
||||
const trimmed = input.trim();
|
||||
if (trimmed === "") return null;
|
||||
// Browsers send Origin as scheme + host[:port], no path. Strip trailing slash.
|
||||
try {
|
||||
const url = new URL(trimmed.includes("://") ? trimmed : `https://${trimmed}`);
|
||||
return `${url.protocol}//${url.host}`.toLowerCase();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function parseAllowedEntry(entry: string): { host: string; wildcard: boolean; port?: string; scheme?: string } | null {
|
||||
const trimmed = entry.trim().toLowerCase();
|
||||
if (trimmed === "") return null;
|
||||
let scheme: string | undefined;
|
||||
let rest = trimmed;
|
||||
if (rest.includes("://")) {
|
||||
const [s, r] = rest.split("://", 2) as [string, string];
|
||||
scheme = s;
|
||||
rest = r;
|
||||
}
|
||||
// host[:port]
|
||||
const wildcard = rest.startsWith("*.");
|
||||
const hostAndPort = wildcard ? rest.slice(2) : rest;
|
||||
const [host, port] = hostAndPort.split(":", 2);
|
||||
if (!host) return null;
|
||||
return { host, wildcard, port, scheme };
|
||||
}
|
||||
|
||||
export function matchAllowedOrigin(originHeader: string | null | undefined, allowed: readonly string[]): boolean {
|
||||
if (!allowed || allowed.length === 0) return false;
|
||||
const normalized = normalizeOrigin(originHeader);
|
||||
if (!normalized) return false;
|
||||
let parsed: URL;
|
||||
try {
|
||||
parsed = new URL(normalized);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const reqHost = parsed.hostname.toLowerCase();
|
||||
const reqPort = parsed.port; // empty string when default
|
||||
const reqScheme = parsed.protocol.replace(":", "");
|
||||
|
||||
for (const entry of allowed) {
|
||||
const candidate = parseAllowedEntry(entry);
|
||||
if (!candidate) continue;
|
||||
if (candidate.scheme && candidate.scheme !== reqScheme) continue;
|
||||
if (candidate.port !== undefined && candidate.port !== reqPort) continue;
|
||||
if (candidate.wildcard) {
|
||||
// *.example.com matches foo.example.com but NOT example.com
|
||||
if (reqHost === candidate.host) continue;
|
||||
if (reqHost.endsWith(`.${candidate.host}`)) return true;
|
||||
} else if (reqHost === candidate.host) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user