All files / src middleware.ts

100% Statements 58/58
94.87% Branches 37/39
100% Functions 8/8
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                                                                                          1x                                                                                 65x 65x 65x             65x       65x                                                                 25x       1x   24x 18x 3x 15x           9x   6x       662x       69x       69x 4x 1x                 3x     65x 65x 65x         65x             65x         4x 4x 4x 4x       4x 4x 4x 4x 4x       61x 50x 50x 25x                     25x       25x   25x 25x 19x 28x 9x                     27x 27x 27x   27x 27x 27x 27x               34x 34x     1x                                
import { NextResponse, type NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
import {
  STATIC_SITE_URL,
  apiAnswersOffline,
  isLiveDemoOffline,
  pageAnswersOffline,
  staticSiteUrl,
} from "@/lib/offline-mode";
 
/**
 * Role-based route protection + per-request Content-Security-Policy.
 *
 * IMPORTANT: we do NOT wrap this in `withAuth()` from next-auth/middleware.
 * `withAuth` intercepts the response and rebuilds it internally, which strips
 * the `request: { headers }` option we pass to `NextResponse.next()`. That
 * option is how Next.js 15 discovers our per-request nonce and injects it
 * into its own streaming `<script>` tags. Without it, every framework inline
 * script is blocked by our CSP. So we do the auth check manually via
 * `getToken()` and fully own the response chain.
 *
 * Route rules (auth required):
 *   /admin/policies            → requires HDAB_AUTHORITY or EDC_ADMIN
 *   /admin/audit               → requires HDAB_AUTHORITY or EDC_ADMIN
 *   /admin/*  (other)          → requires EDC_ADMIN
 *   /compliance                → requires HDAB_AUTHORITY or EDC_ADMIN
 *   /patient/profile           → requires PATIENT or EDC_ADMIN
 *   /patient/research          → requires PATIENT or EDC_ADMIN
 *   /patient/insights          → requires PATIENT or EDC_ADMIN
 *   /patient/programmes        → requires PATIENT or EDC_ADMIN
 *   /onboarding/*              → any authenticated
 *   /credentials               → any authenticated
 *   /settings                  → any authenticated
 *   /data/*                    → any authenticated
 *   /negotiate                 → any authenticated
 *   All other UI routes        → public (CSP still applied)
 *
 * CSP:
 *   Every HTML response gets a per-request nonce so we can drop
 *   'unsafe-inline' from script-src. Next.js automatically propagates the
 *   nonce to its framework scripts when it reads the `Content-Security-Policy`
 *   REQUEST header we set on the forwarded request. style-src-attr still
 *   needs 'unsafe-inline' for React's `style={}` prop (CSP3 separation).
 */
 
const PROTECTED_PATHS = [
  "/admin",
  "/compliance",
  "/onboarding",
  "/credentials",
  "/settings",
  "/data",
  "/negotiate",
  // #357 reopened: the patient landing page is gated too, so an anonymous
  // visitor is sent to sign in rather than shown a page whose only data call
  // now answers 401. The three below stay listed because requiresRole() keys
  // off them for PATIENT / EDC_ADMIN; "/patient" alone needs a session and no
  // particular role, which matches what /api/patient enforces.
  "/patient",
  "/patient/profile",
  "/patient/research",
  "/patient/insights",
  "/patient/programmes",
  "/requests",
  "/applications",
  "/supervision",
  "/overview",
  // #404: every API route needs a session (ADR-044), so the pages whose data
  // was public until then send an anonymous visitor to sign in too.
  "/graph",
  "/permits",
  "/information",
  "/activity-report",
  // Their APIs already needed a session, so a signed-out visitor saw empty
  // panels; now they are sent to sign in, like /patient (#357).
  "/catalog",
  "/analytics",
  "/query",
  "/eehrxf",
  "/tasks",
  // #782: the national implementation profiles; their API needs a session.
  "/jurisdictions",
] as const;
 
function generateNonce(): string {
  // Edge runtime has globalThis.crypto
  const bytes = new Uint8Array(16);
  crypto.getRandomValues(bytes);
  return btoa(String.fromCharCode(...bytes));
}
 
function buildCsp(nonce: string, isDev: boolean): string {
  // 'strict-dynamic' lets the initial nonced script load further scripts
  // without needing them individually allowlisted. In dev we allow
  // 'unsafe-eval' because Next.js's Hot Module Replacement needs it.
  const scriptSrc = isDev
    ? `'self' 'nonce-${nonce}' 'strict-dynamic' 'unsafe-eval'`
    : `'self' 'nonce-${nonce}' 'strict-dynamic'`;
 
  return [
    `default-src 'self'`,
    `script-src ${scriptSrc}`,
    // script-src-elem falls back to script-src when omitted, so we don't
    // set it — keeping one source of truth avoids divergence where the elem
    // directive is stricter than the parent and blocks nonced inline scripts.
    // style-src: NO nonce here. When a nonce and 'unsafe-inline' both appear
    // in style-src, browsers ignore 'unsafe-inline' — and Next.js injects
    // <style> tags for CSS-in-JS that aren't nonced, so they'd get blocked.
    // Keeping 'unsafe-inline' is the pragmatic trade-off (CSS injection is
    // far lower risk than JS injection) and is still CSP3-compliant.
    `style-src 'self' 'unsafe-inline'`,
    `style-src-attr 'unsafe-inline'`,
    `img-src 'self' data: blob:`,
    `font-src 'self' data:`,
    // connect-src: same-origin for /api/*, plus websocket for Next.js HMR in dev
    `connect-src 'self'${isDev ? " ws: wss:" : ""}`,
    // The site search runs Pagefind in a worker from /pagefind/ (ADR-082).
    `worker-src 'self'`,
    `frame-src 'none'`,
    `frame-ancestors 'none'`,
    `object-src 'none'`,
    `base-uri 'self'`,
    `form-action 'self'`,
    `upgrade-insecure-requests`,
  ].join("; ");
}
 
function requiresRole(pathname: string): string[] | null {
  // HDAB-shared admin pages: policies (Art. 46 ODRL) and audit (Art. 37/38/53)
  // are part of the regulator's supervision toolkit. Navigation.tsx exposes
  // them to HDAB_AUTHORITY, so the middleware must match. All other /admin/*
  // routes (tenants, components, etc.) stay EDC_ADMIN only.
  if (
    pathname.startsWith("/admin/policies") ||
    pathname.startsWith("/admin/audit")
  ) {
    return ["HDAB_AUTHORITY", "EDC_ADMIN"];
  }
  if (pathname.startsWith("/admin")) return ["EDC_ADMIN"];
  if (pathname.startsWith("/compliance"))
    return ["HDAB_AUTHORITY", "EDC_ADMIN"];
  if (
    pathname.startsWith("/patient/profile") ||
    pathname.startsWith("/patient/research") ||
    pathname.startsWith("/patient/insights") ||
    pathname.startsWith("/patient/programmes")
  ) {
    return ["PATIENT", "EDC_ADMIN"];
  }
  return null;
}
 
function isProtected(pathname: string): boolean {
  return PROTECTED_PATHS.some((p) => pathname.startsWith(p));
}
 
export default async function middleware(req: NextRequest) {
  const { pathname } = req.nextUrl;
 
  // API routes are matched only for off-hours mode (ADR-053); online they
  // pass through untouched, without a CSP, as they always did.
  if (pathname.startsWith("/api/")) {
    if (isLiveDemoOffline() && !apiAnswersOffline(pathname)) {
      return NextResponse.json(
        {
          error:
            "The live demo is offline outside office hours; the static demo is always available",
          staticSite: `${STATIC_SITE_URL}/`,
        },
        { status: 503, headers: { "Retry-After": "3600" } },
      );
    }
    return NextResponse.next();
  }
 
  const nonce = generateNonce();
  const isDev = process.env.NODE_ENV !== "production";
  const csp = buildCsp(nonce, isDev);
 
  // The static site is the address search engines index; every page here
  // names its copy there (an HTTP header, as these pages are rendered per
  // request).
  const canonical = `<${staticSiteUrl(pathname)}>; rel="canonical"`;
 
  // Off hours (ADR-053): every backend is stopped, so every page is the
  // offline notice. A rewrite keeps the visitor's URL, which the notice reads
  // to link the same page on the static site. It answers 503 with
  // Retry-After, so a crawler comes back later instead of indexing the
  // notice in place of the page.
  if (
    isLiveDemoOffline() &&
    pathname !== "/offline" &&
    !pageAnswersOffline(pathname)
  ) {
    const offHeaders = new Headers(req.headers);
    offHeaders.set("x-nonce", nonce);
    offHeaders.set("Content-Security-Policy", csp);
    const res = NextResponse.rewrite(new URL("/offline", req.url), {
      request: { headers: offHeaders },
      status: 503,
    });
    res.headers.set("Content-Security-Policy", csp);
    res.headers.set("Cache-Control", "no-store");
    res.headers.set("Retry-After", "3600");
    res.headers.set("Link", canonical);
    return res;
  }
 
  // Auth gate for protected routes.
  if (isProtected(pathname)) {
    const token = await getToken({ req });
    if (!token) {
      const signInUrl = new URL("/auth/signin", req.url);
      // Path-relative, NOT req.url. Inside the container req.url resolves to
      // the internal bind address, so this emitted
      // `callbackUrl=https://0.0.0.0:3000/admin` on production — deep-linking
      // to a protected page and signing in sent the user to an unreachable
      // host instead of where they were going. Found running the issue #5
      // pentest checklist (J840-J842).
      //
      // A relative path is also the safer shape: NextAuth resolves it against
      // NEXTAUTH_URL, so it cannot be used to bounce a signed-in user to an
      // external origin.
      signInUrl.searchParams.set(
        "callbackUrl",
        `${req.nextUrl.pathname}${req.nextUrl.search}`,
      );
      return withCspResponse(NextResponse.redirect(signInUrl), csp, nonce);
    }
    const required = requiresRole(pathname);
    if (required) {
      const roles = (token.roles as string[] | undefined) ?? [];
      if (!required.some((r) => roles.includes(r))) {
        return withCspResponse(
          NextResponse.redirect(new URL("/auth/unauthorized", req.url)),
          csp,
          nonce,
        );
      }
    }
  }
 
  // Forward the nonce via request headers so Next.js injects it into
  // framework inline scripts (streaming RSC payload).
  const reqHeaders = new Headers(req.headers);
  reqHeaders.set("x-nonce", nonce);
  reqHeaders.set("Content-Security-Policy", csp);
 
  const res = NextResponse.next({ request: { headers: reqHeaders } });
  res.headers.set("Content-Security-Policy", csp);
  res.headers.set("Link", canonical);
  return res;
}
 
function withCspResponse(
  res: NextResponse,
  csp: string,
  _nonce: string,
): NextResponse {
  res.headers.set("Content-Security-Policy", csp);
  return res;
}
 
export const config = {
  // The first pattern runs on every page except Next.js static assets and
  // mock JSON fixtures. CSP is applied to HTML responses; auth checks
  // only fire on the PROTECTED_PATHS list.
  matcher: [
    // presentations/ holds self-contained reveal.js decks and poc/ the
    // static prototypes, both served as files; like swagger-ui they carry
    // their own scripts, which the nonce CSP would block. pagefind/ is the
    // site search index (ADR-082): its worker compiles WebAssembly, which the
    // page's CSP would forbid it, and it answers off hours like any file.
    "/((?!api|_next/static|_next/image|favicon.ico|swagger-ui|presentations|poc|mock|static|pagefind).*)",
    // Off-hours mode answers data routes with a 503 (ADR-053); the handler
    // passes them straight through otherwise.
    "/api/:path*",
  ],
};