-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.ts
More file actions
69 lines (62 loc) · 2.2 KB
/
middleware.ts
File metadata and controls
69 lines (62 loc) · 2.2 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
import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
// Check if Supabase is configured
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY
if (supabaseUrl && supabaseAnonKey) {
try {
const { createMiddlewareClient } = await import("@supabase/auth-helpers-nextjs")
// Create a Supabase client configured to use cookies
const supabase = createMiddlewareClient({ req, res })
// Refresh session if expired - required for Server Components
await supabase.auth.getSession()
} catch (error) {
console.warn("Supabase middleware error:", error)
}
}
// Security Headers - OWASP Recommended
const securityHeaders = {
// Prevent XSS attacks
"X-XSS-Protection": "1; mode=block",
// Prevent MIME type sniffing
"X-Content-Type-Options": "nosniff",
// Prevent clickjacking
"X-Frame-Options": "DENY",
// Referrer policy
"Referrer-Policy": "strict-origin-when-cross-origin",
// Content Security Policy
"Content-Security-Policy": [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval'",
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: https:",
"font-src 'self'",
"connect-src 'self' https://*.supabase.co wss://*.supabase.co",
"frame-ancestors 'none'",
"base-uri 'self'",
"form-action 'self'",
].join("; "),
// Strict Transport Security (HTTPS only)
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
// Permissions Policy
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
}
// Apply security headers
Object.entries(securityHeaders).forEach(([key, value]) => {
res.headers.set(key, value)
})
return res
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
"/((?!_next/static|_next/image|favicon.ico).*)",
],
}