Repository navigation
Expand file tree
/
Copy pathmiddleware.ts
More file actions
193 lines (181 loc) · 7.26 KB
/
Copy pathmiddleware.ts
File metadata and controls
193 lines (181 loc) · 7.26 KB
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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
import {
matchRule,
addressCeilingMessage,
extractKey,
rateLimitHeaders,
mcpRateLimitMessage,
getBackend,
checkAddressCeiling,
effectiveMax,
} from "@/lib/api/rate-limit";
import { buildCsp } from "@/lib/security/headers";
import { stampClientIpHeader } from "@/lib/security/client-ip";
import { safeInviteNext } from "@/lib/auth/invite-next";
import { turnstileSiteKey } from "@/lib/config/env";
import { isPublicCacheableAuthPath } from "@/lib/auth/public-cache-paths";
const UUID_RE =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
/**
* Pages that render a Turnstile widget, and so need its origin in `frame-src`
* and `script-src`. Kept in step with the call sites of `turnstileProps()`.
*/
const TURNSTILE_WIDGET_PATHS: ReadonlySet<string> = new Set([
"/sign-in",
"/sign-up",
"/forgot-password",
"/verify-email",
]);
/**
* Generate a per-request CSP nonce. Edge-runtime compatible: avoids
* `Buffer` so the Cloudflare Workers / Edge build accepts the module.
*
* @returns Base64-encoded UUID v4 (122 bits of entropy).
*/
function generateNonce(): string {
return btoa(crypto.randomUUID());
}
/**
* Next.js middleware: session enforcement, rate limiting, request
* validation, and per-request CSP. API/MCP auth is delegated to route
* handlers. Runs in the Edge runtime so the OpenNext Cloudflare build
* accepts the module — Next 16's `proxy.ts` filename is locked to the
* Node.js runtime which workerd rejects.
*
* @param request - Incoming request.
* @returns Redirect, error response, or pass-through; all carry CSP headers.
*/
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const session = getSessionCookie(request);
const isProd = process.env.NODE_ENV === "production";
const nonce = isProd ? generateNonce() : undefined;
const wsScheme = request.nextUrl.protocol === "https:" ? "wss" : "ws";
const wsOrigin =
isProd && process.env.NEXT_PUBLIC_DEPLOY_TARGET === "cloudflare"
? `${wsScheme}://${request.nextUrl.host}`
: undefined;
// Keyed on the public site key, not the secret: middleware runs on the edge
// where only `NEXT_PUBLIC_*` is inlined, and the site key is exactly the
// signal for whether a Turnstile widget will render on this deploy. Scoped
// to the four pages that actually render one, so the rest of the app keeps
// `frame-src 'none'` rather than carrying a framing allowance it never uses.
const csp = buildCsp({
isProd,
nonce,
wsOrigin,
turnstile:
turnstileSiteKey() !== null && TURNSTILE_WIDGET_PATHS.has(pathname),
});
const withCsp = <T extends NextResponse>(response: T): T => {
response.headers.set("Content-Security-Policy", csp);
return response;
};
// Auth pages: redirect signed-in users to their validated invite
// destination when one is carried, else home.
if (session && (pathname === "/sign-in" || pathname === "/sign-up")) {
const next = safeInviteNext(request.nextUrl.searchParams.get("next"));
return withCsp(NextResponse.redirect(new URL(next ?? "/", request.url)));
}
// Protected app pages: redirect to sign-in if not authenticated.
// Only auth endpoints and MCP routes are public — all other API
// routes require a session cookie to prevent unauthenticated access.
const isPublicPath =
pathname === "/sign-in" ||
pathname === "/sign-up" ||
pathname === "/consent" ||
pathname === "/verify-email" ||
pathname === "/forgot-password" ||
pathname === "/reset-password" ||
pathname === "/account-deleted" ||
pathname === "/privacy" ||
pathname === "/terms" ||
pathname === "/impressum" ||
pathname === "/subprocessors" ||
pathname === "/dpa" ||
pathname.startsWith("/invitations/") ||
pathname.startsWith("/api/auth/") ||
pathname === "/api/mcp" ||
pathname.startsWith("/.well-known/");
if (!session && !isPublicPath) {
return withCsp(NextResponse.redirect(new URL("/sign-in", request.url)));
}
// Rate limiting — routed by rule.bindingKey so `/api/auth/*` paths hit the
// RATE_LIMIT_AUTH binding (defense-in-depth on top of Better-Auth's in-memory
// customRules) and the rest of `/api/*` hits RATE_LIMIT_API.
let rlHeaders: Record<string, string> | null = null;
const rule = matchRule(pathname);
if (rule) {
const key = await extractKey(request, rule.keyStrategy);
if (key) {
const limit = effectiveMax(rule.max, key);
const primary = await getBackend(rule.bindingKey).check(
`${rule.pattern}:${key}`,
limit,
rule.window,
);
// The ceiling is charged only for admitted requests, so a rejected
// primary cannot burn the shared per-address budget. The headroom
// comparison only does work on the memory backend: the Cloudflare
// binding reports a constant remaining, so the ceiling is selected
// there only once it has already rejected.
const ceiling = primary.allowed
? await checkAddressCeiling(request, rule, key)
: null;
const result =
ceiling && (!ceiling.allowed || ceiling.remaining < primary.remaining)
? ceiling
: primary;
rlHeaders = rateLimitHeaders(result, rule, limit);
if (!result.allowed) {
// A ceiling rejection is the shared per-address budget, not the
// caller's own rule budget — the body must name the right constraint.
const message =
ceiling && !ceiling.allowed
? addressCeilingMessage(result.resetIn)
: rule.bindingKey === "mcp"
? mcpRateLimitMessage(limit, rule.window, result.resetIn)
: "Too many requests. Please try again later.";
return withCsp(
NextResponse.json(
{ error: message },
{ status: 429, headers: rlHeaders },
),
);
}
}
}
// UUID validation for project routes
const match = pathname.match(/^\/api\/project\/([^/]+)/);
if (match && !UUID_RE.test(match[1])) {
return withCsp(
NextResponse.json({ error: "Invalid project ID" }, { status: 400 }),
);
}
// Forward `x-nonce` so the renderer auto-tags inline <script> elements,
// and stamp the resolved client address: `auth.api.*` dispatches read raw
// inbound headers, so Better Auth's resolver depends on this stamp.
const requestHeaders = new Headers(request.headers);
stampClientIpHeader(requestHeaders);
if (nonce) {
requestHeaders.set("x-nonce", nonce);
requestHeaders.set("Content-Security-Policy", csp);
}
const response = NextResponse.next({ request: { headers: requestHeaders } });
// Withheld on the shared-cacheable auth documents: RateLimit counters are
// per-caller state a shared cache would replay to other callers. The 429
// branch above keeps its headers; an error response is not stored.
if (rlHeaders && !isPublicCacheableAuthPath(pathname)) {
for (const [k, v] of Object.entries(rlHeaders)) {
response.headers.set(k, v);
}
}
return withCsp(response);
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon\\.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico|txt|xml|json|webmanifest)$).*)",
],
};