Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
// harden-portal-requests (F29): SameSite=Lax alone lets any *.babyboo.my page drive this portal's API with the
|
|
// viewer's cookie, because sibling subdomains are "same-site". A state-changing API request must therefore come
|
|
// from this portal's own origin. Runs in the middleware (edge runtime): no Node APIs.
|
|
|
|
const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
|
|
|
|
/**
|
|
* True when an unsafe-method request was sent by a browser from another origin.
|
|
*
|
|
* - `Sec-Fetch-Site` (set by the browser, not by scripts) wins: only `same-origin` passes.
|
|
* - Without it, the `Origin` host must equal the request host (`X-Forwarded-Host` from the ingress, else `Host`).
|
|
* - With neither header the caller is not a browser (server-to-server, CLI), so CSRF does not apply.
|
|
*/
|
|
export function isCrossSiteWrite(method: string, headers: Headers): boolean {
|
|
if (SAFE_METHODS.has(method.toUpperCase())) return false;
|
|
|
|
const site = headers.get("sec-fetch-site");
|
|
if (site !== null) return site !== "same-origin";
|
|
|
|
const origin = headers.get("origin");
|
|
if (origin === null) return false;
|
|
|
|
const host = (headers.get("x-forwarded-host") ?? headers.get("host") ?? "").split(",")[0].trim().toLowerCase();
|
|
if (!host) return true;
|
|
try {
|
|
const from = new URL(origin);
|
|
// Parse the request host with the Origin's scheme so a default port (":443" from the ingress) normalises away.
|
|
return from.host.toLowerCase() !== new URL(`${from.protocol}//${host}`).host.toLowerCase();
|
|
} catch {
|
|
return true; // "null" (opaque origin) or garbage
|
|
}
|
|
}
|