app/lib/origin.mjs
29 lines and 1 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| originVerdict | 15 to 29 | 6 | 100% | WatchCRAP 6 | No data: no mutants in this code |
Imports
It imports no other module of this repo. Imported by app/routes/admin.tsx, app/routes/search.ask.ts, app/routes/theme.ts, app/routes/workspace.tsx.
Source
/*
* Absent Origin is allowed: only non-browser clients omit it, and they spend their
* own IP's allowance; a hostile page cannot omit it. The string "null" (sandboxed
* iframe, opaque origin) is refused, unlike a missing header.
*/
/**
* Compared against the request's own origin, not `SITE_ORIGIN`, so it keeps
* working on both workers.dev and the apex across the cutover.
*
* @param {string | null | undefined} origin the `Origin` header, verbatim
* @param {string} requestUrl the request's own URL
* @returns {{ ok: boolean, reason: string }}
*/
export function originVerdict(origin, requestUrl) {
if (origin === null || origin === undefined || origin === "") {
return { ok: true, reason: "absent" };
}
let self;
try {
self = new URL(requestUrl).origin;
} catch {
return { ok: false, reason: "unparseable-request-url" };
}
if (origin === self) return { ok: true, reason: "same-origin" };
return { ok: false, reason: "cross-origin" };
}
export const ASK_ORIGIN_REFUSAL = "Ask does not take cross-origin requests.";
// Deliberately says nothing about what the route does or why it refused.
export const ORIGIN_REFUSAL = "Cross-origin requests are not accepted.";