Dough Git

clove/doughGit

src/hooks.server.ts

back to src

/* src/hooks.server.ts
 * LICENCED DASL-1.0 (c) Clove Twilight
 */

import type { Handle, HandleServerError, ServerInit } from "@sveltejs/kit/hooks";
import { config, oidcEnabled } from "#lib/server/config.ts";
import { SESSION_COOKIE, oidcIssuer, sessionUser } from "#lib/server/auth.ts";
import { sweep } from "#lib/server/sweep.ts";

const SITE_ORIGIN = new URL(config.baseUrl).origin;

// Logging out POSTs to /auth/logout, which redirects on to the SSO's logout,
// and browsers check form-action against that redirect too
const SSO_ORIGIN = config.oidc.issuer ? new URL(config.oidc.issuer).origin : "";

const GIT_TRANSPORT = /\/(?:git-upload-pack|git-receive-pack|info\/refs)$/;

const SWEEP_EVERY_MS = 6 * 60 * 60 * 1000;

export const init: ServerInit = async () => {
  // Also once at boot: a restart resets the interval, and deploys can come more often than 6h
  void sweep();
  setInterval(() => void sweep(), SWEEP_EVERY_MS);

  console.log(`  oidc:       ${oidcEnabled ? "enabled" : "disabled"}`);
  console.log(
    `  trash:      ${config.trashDays > 0 ? `${config.trashDays}d, swept every 6h` : "kept forever"}`,
  );
  if (oidcEnabled) {
    try {
      console.log(`  oidc issuer (must equal the callback's iss): ${await oidcIssuer()}`);
      console.log(`  oidc redirect_uri: ${config.baseUrl}/auth/callback`);
    } catch (err) {
      console.error("  oidc discovery FAILED (check OIDC_ISSUER):", err instanceof Error ? err.message : err);
    }
  }
};

function sameOrigin(request: Request): boolean {
  const origin = request.headers.get("origin");
  if (origin) return origin === SITE_ORIGIN;

  const referer = request.headers.get("referer");
  if (!referer) return false;
  try {
    return new URL(referer).origin === SITE_ORIGIN;
  } catch {
    return false;
  }
}

export const handle: Handle = async ({ event, resolve }) => {
  const { request, url } = event;

  // Git transport never looks at the browser session, and skipping it keeps
  // SSO re-checks off that hot path. It authenticates with Basic auth instead
  if (GIT_TRANSPORT.test(url.pathname)) {
    event.locals.user = null;
    return resolve(event);
  }

  const unsafe = !["GET", "HEAD", "OPTIONS"].includes(request.method);
  if (unsafe && !sameOrigin(request)) {
    return new Response("cross-site request refused\n", { status: 403 });
  }

  const token = event.cookies.get(SESSION_COOKIE);
  event.locals.user = await sessionUser(token);
  if (token && !event.locals.user) event.cookies.delete(SESSION_COOKIE, { path: "/" });

  const response = await resolve(event);
  const headers = response.headers;

  const csp = headers.get("content-security-policy");
  if (csp && SSO_ORIGIN) {
    headers.set("content-security-policy", csp.replace("form-action 'self'", `form-action 'self' ${SSO_ORIGIN}`));
  }
  headers.set("X-Content-Type-Options", "nosniff");
  headers.set("Referrer-Policy", "same-origin");
  headers.set("X-Frame-Options", "DENY");
  if (SITE_ORIGIN.startsWith("https://")) {
    headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
  }
  return response;
};

// App errors (thrown with error()) keep their own title and message; this only softens unknown ones
export const handleError: HandleServerError = ({ kind, error, event }) => {
  if (kind !== "unknown") return;

  console.error("[error]", event.request.method, event.url.pathname, error);
  return {
    title: "something went wrong",
    message: "Something went wrong on our side. Please try again.",
  };
};