-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
185 lines (169 loc) · 6.62 KB
/
middleware.ts
File metadata and controls
185 lines (169 loc) · 6.62 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
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextRequest, NextResponse } from "next/server";
import { getDb } from "@/lib/db";
import { userSettings } from "@/lib/db/schema";
import { eq } from "drizzle-orm";
const isProtectedRoute = createRouteMatcher([
"/dashboard(.*)",
"/api/trpc(.*)",
"/api/feedback",
"/admin(.*)",
]);
const isAdminRoute = createRouteMatcher([
"/admin(.*)",
]);
const isPublicRoute = createRouteMatcher([
"/",
"/login",
"/auth/sign-in(.*)",
"/auth/sign-up(.*)",
"/donate/success",
"/api/stripe(.*)",
"/api/auth/sync-user",
"/api/clerk/webhook",
"/api/markdown",
"/privacy",
"/terms",
"/vitamin-k-foods-warfarin",
"/vitamin-k-calculator",
"/warfarin-food-chart",
"/warfarin-diet-tracker",
"/inr-vitamin-k-management",
"/install",
"/faq",
"/blog(.*)",
"/api-docs",
"/api/x402(.*)",
"/.well-known(.*)",
]);
function addSecurityHeaders(response: NextResponse) {
// Prevent clickjacking
response.headers.set("X-Frame-Options", "DENY");
// Prevent MIME type sniffing
response.headers.set("X-Content-Type-Options", "nosniff");
// XSS protection (legacy browsers)
response.headers.set("X-XSS-Protection", "1; mode=block");
// Referrer policy
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
// Disable unnecessary browser features
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=(), interest-cohort=()"
);
// HSTS — enforce HTTPS for 1 year with subdomains
if (process.env.NODE_ENV === "production") {
response.headers.set(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload"
);
}
// Content Security Policy
response.headers.set(
"Content-Security-Policy",
[
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://challenges.cloudflare.com",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"font-src 'self' https://fonts.gstatic.com data:",
"img-src 'self' data: blob: https://img.clerk.com https://*.clerk.com",
"connect-src 'self' https: wss:",
"frame-src https://challenges.cloudflare.com",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
].join("; ")
);
return response;
}
export default clerkMiddleware(async (auth, req: NextRequest) => {
// ── Markdown content negotiation for AI agents ──────────────────────
// When Accept: text/markdown is requested, rewrite to the markdown API route
const acceptHeader = req.headers.get("accept") || "";
if (acceptHeader.includes("text/markdown")) {
const path = req.nextUrl.pathname;
const mdRoutes = ["/", "/api-docs"];
if (mdRoutes.includes(path)) {
const mdUrl = new URL("/api/markdown", req.url);
mdUrl.searchParams.set("path", path);
return NextResponse.rewrite(mdUrl);
}
}
// Handle CORS for x402 API endpoints
if (req.nextUrl.pathname.startsWith("/api/x402/")) {
if (req.method === "OPTIONS") {
return new NextResponse(null, {
status: 204,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization, X-402-Version, X-402-Payment, X-402-Recipient",
"Access-Control-Max-Age": "86400",
},
});
}
}
// Handle CORS for .well-known endpoints (AI discovery)
if (req.nextUrl.pathname.startsWith("/.well-known/")) {
const response = NextResponse.next();
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Methods", "GET, OPTIONS");
response.headers.set("Access-Control-Allow-Headers", "Content-Type");
return response;
}
// Force HTTPS in production
if (process.env.NODE_ENV === "production" && req.headers.get("x-forwarded-proto") === "http") {
const httpsUrl = new URL(req.url);
httpsUrl.protocol = "https:";
return NextResponse.redirect(httpsUrl, 308);
}
const { userId } = await auth();
if (!isPublicRoute(req)) {
if (!userId) {
const signInUrl = new URL("/auth/sign-in", req.url);
signInUrl.searchParams.set("redirect_url", req.url);
return NextResponse.redirect(signInUrl);
}
// Check admin access for admin routes
if (isAdminRoute(req)) {
try {
const db = await getDb();
const settings = await db
.select({ role: userSettings.role })
.from(userSettings)
.where(eq(userSettings.userId, userId))
.get();
if (settings?.role !== "admin") {
return NextResponse.redirect(new URL("/dashboard", req.url));
}
} catch (error) {
console.error("[Middleware] Error checking admin role:", error);
return NextResponse.redirect(new URL("/dashboard", req.url));
}
}
}
// ── Apply security headers + Link headers ─────────────────────────────
const response = NextResponse.next();
addSecurityHeaders(response);
// RFC 8288 Link headers for agent discovery on homepage
if (req.nextUrl.pathname === "/") {
const baseUrl = process.env.NEXT_PUBLIC_APP_URL || "https://vitaktracker.com";
response.headers.set(
"Link",
[
`<${baseUrl}/.well-known/api-catalog>; rel="api-catalog"; type="application/linkset+json"; title="VitaK Tracker API Catalog"`,
`<${baseUrl}/.well-known/openapi.json>; rel="service-desc"; type="application/openapi+json"; title="OpenAPI 3.1 Specification"`,
`<${baseUrl}/api-docs>; rel="service-doc"; type="text/html"; title="API Documentation"`,
`<${baseUrl}/llms.txt>; rel="llms-txt"; type="text/plain"; title="VitaK Tracker LLMs.txt"`,
`<${baseUrl}/.well-known/ai-plugin.json>; rel="ai-plugin"; type="application/json"; title="ChatGPT Plugin Manifest"`,
`<${baseUrl}/.well-known/agent-skills>; rel="agent-skills"; type="application/json"; title="Agent Skills Discovery Index"`,
`<${baseUrl}/.well-known/mcp/server-card.json>; rel="mcp-server"; type="application/json"; title="MCP Server Card"`,
`<${baseUrl}/.well-known/oauth-protected-resource>; rel="oauth-protected-resource"; type="application/json"; title="OAuth Protected Resource Metadata"`,
`<${baseUrl}/.well-known/openid-configuration>; rel="openid-configuration"; type="application/json"; title="OpenID Connect Discovery"`,
].join(", ")
);
}
return response;
});
export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};