-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (31 loc) · 1.98 KB
/
middleware.ts
File metadata and controls
39 lines (31 loc) · 1.98 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
import { updateSession } from "@/utils/supabase/middleware"
import { NextResponse, type NextRequest } from "next/server"
import { validateCsrfToken } from "./lib/csrf"
export async function middleware(request: NextRequest) {
const response = await updateSession(request)
// CSRF protection for state-changing requests
if (["POST", "PUT", "DELETE"].includes(request.method)) {
const csrfCookie = request.cookies.get("csrf_token")?.value
const headerToken = request.headers.get("x-csrf-token")
if (!csrfCookie || !headerToken || !validateCsrfToken(headerToken)) {
return new NextResponse("Invalid CSRF token", { status: 403 })
}
}
// CSP for development and production
const isDev = process.env.NODE_ENV === "development"
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseDomain = supabaseUrl ? new URL(supabaseUrl).origin : ""
response.headers.set(
"Content-Security-Policy",
isDev
? `default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdnjs.cloudflare.com https://assets.onedollarstats.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: blob:; connect-src 'self' wss: https://api.openai.com https://api.mistral.ai https://api.supabase.com ${supabaseDomain} https://api.github.com https://collector.onedollarstats.com;`
: `default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://cdnjs.cloudflare.com https://analytics.umami.is https://vercel.live https://assets.onedollarstats.com; frame-src 'self' https://vercel.live; style-src 'self' 'unsafe-inline'; img-src 'self' data: https: blob:; connect-src 'self' wss: https://api.openai.com https://api.mistral.ai https://api.supabase.com ${supabaseDomain} https://api-gateway.umami.dev https://api.github.com https://collector.onedollarstats.com;`
)
return response
}
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
runtime: "nodejs",
}