Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
327 lines (291 loc) · 11.6 KB
/
Copy pathproxy.ts
File metadata and controls
327 lines (291 loc) · 11.6 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
import { getAdminPathAlias } from "@/lib/admin-path";
function normalizeConfiguredHost(value: string) {
return String(value || "")
.trim()
.replace(/^https?:\/\//, "")
.replace(/\/.*$/, "")
.replace(/:\d+$/, "")
.toLowerCase();
}
function normalizeRequestHost(value: string) {
const raw = String(value || "")
.split(",")[0]
.trim()
.toLowerCase();
if (!raw) return "";
const bracketedMatch = raw.match(/^\[([^\]]+)\](?::\d+)?$/);
const hostOnly = bracketedMatch ? bracketedMatch[1] : raw.replace(/:\d+$/, "");
if (hostOnly === "::1" || hostOnly === "127.0.0.1" || hostOnly === "0.0.0.0") {
return "localhost";
}
return hostOnly;
}
function isTruthy(value: string) {
return ["1", "true", "yes", "on"].includes(value.toLowerCase());
}
function isDebugModeEdge() {
const raw = process.env.DEBUG_MODE || process.env.NEXT_PUBLIC_DEBUG_MODE || "";
if (isTruthy(raw)) return true;
return process.env.NODE_ENV === "development";
}
function getTraceTier() {
const configured = (process.env.TRACE_PROFILE || "").trim().toLowerCase();
if (configured === "test") return "Test";
if (configured === "prod" || configured === "production") return "Prod";
if (configured === "dev" || configured === "development") return "Dev";
if (process.env.NODE_ENV === "production") return "Prod";
if (process.env.NODE_ENV === "test") return "Test";
return "Dev";
}
function shouldEmitTraceToConsole(tier: string) {
if (tier !== "Test") return true;
const raw = String(process.env.TRACE_TEST_CONSOLE || "").trim().toLowerCase();
return ["1", "true", "yes", "on"].includes(raw);
}
function traceEdge(scope: string, message: string, payload?: unknown) {
if (!isDebugModeEdge()) return;
const tier = getTraceTier();
if (!shouldEmitTraceToConsole(tier)) return;
if (payload === undefined) {
console.debug(`[trace:${tier}:${scope}] ${message}`);
return;
}
console.debug(`[trace:${tier}:${scope}] ${message}`, payload);
}
function hasSessionCookie(req: NextRequest) {
return Boolean(
req.cookies.get("__Secure-next-auth.session-token")?.value ||
req.cookies.get("next-auth.session-token")?.value ||
req.cookies.get("__Secure-authjs.session-token")?.value ||
req.cookies.get("authjs.session-token")?.value,
);
}
function readLastSiteId(req: NextRequest) {
return String(
req.cookies.get("cms_last_site_id")?.value ||
req.cookies.get("tooty_last_site_id")?.value ||
"",
).trim();
}
function readLastAppPath(req: NextRequest) {
const value = String(
req.cookies.get("cms_last_admin_path")?.value ||
req.cookies.get("tooty_last_app_path")?.value ||
"",
).trim();
if (!value.startsWith("/site/")) return "";
return value;
}
function setLastSiteId(res: NextResponse, siteId: string) {
const normalized = String(siteId || "").trim();
if (!normalized) return;
res.cookies.set("cms_last_site_id", normalized, {
path: "/",
maxAge: 60 * 60 * 24 * 30,
sameSite: "lax",
secure: false,
});
}
function setLastAppPath(res: NextResponse, appPath: string) {
const normalized = String(appPath || "").trim();
if (!normalized.startsWith("/site/")) return;
res.cookies.set("cms_last_admin_path", normalized, {
path: "/",
maxAge: 60 * 60 * 24 * 30,
sameSite: "lax",
secure: false,
});
}
function normalizeAdminAliasPath(path: string, adminPathAlias: string) {
const raw = String(path || "").trim() || "/";
if (raw === "/" || raw === "/app" || raw === `/${adminPathAlias}`) return "/";
if (raw.startsWith("/app/")) return raw.slice(4) || "/";
if (raw.startsWith(`/${adminPathAlias}/`)) return raw.slice(adminPathAlias.length + 1) || "/";
return raw;
}
export const config = {
matcher: [
"/((?!api/|_next/|_static/|_vercel|media/|sitemap\\.xml|robots\\.txt|.*\\..*).*)",
],
};
export default async function proxy(req: NextRequest) {
const url = req.nextUrl;
const path = url.pathname;
const searchParams = url.searchParams.toString();
const fullPath = `${path}${searchParams ? `?${searchParams}` : ""}`;
const traceId = req.headers.get("x-trace-id") || crypto.randomUUID();
const requestHeaders = new Headers(req.headers);
requestHeaders.set("x-trace-id", traceId);
const rewriteWithTrace = (to: string) => {
const res = NextResponse.rewrite(new URL(to, req.url), {
request: { headers: requestHeaders },
});
res.headers.set("x-trace-id", traceId);
return res;
};
const nextWithTrace = () => {
const res = NextResponse.next({
request: { headers: requestHeaders },
});
res.headers.set("x-trace-id", traceId);
return res;
};
const preserveWithTrace = (to: string) =>
process.env.NODE_ENV === "test" ? rewriteWithTrace(to) : nextWithTrace();
const redirectWithTrace = (to: string) => {
const res = NextResponse.redirect(new URL(to, req.url));
res.headers.set("x-trace-id", traceId);
return res;
};
const markAdminResponseNoStore = (res: NextResponse) => {
res.headers.set("Cache-Control", "no-store, no-cache, must-revalidate");
res.headers.set("Pragma", "no-cache");
res.headers.set("Expires", "0");
return res;
};
const normalizedRootDomain =
normalizeConfiguredHost(process.env.NEXT_PUBLIC_ROOT_DOMAIN || "") || "localhost";
const adminPathAlias = getAdminPathAlias();
traceEdge("middleware", "incoming request", {
traceId,
path,
search: searchParams,
host: req.headers.get("host"),
});
const forwardedHost = req.headers.get("x-forwarded-host") || "";
const hostHeader = forwardedHost || req.headers.get("host") || "";
let hostname = normalizeRequestHost(hostHeader);
// Normalize local subdomains for any localhost port.
if (hostname.endsWith(".localhost")) {
hostname = hostname.replace(".localhost", `.${normalizedRootDomain}`);
}
// In local dev/e2e we route *.test through localhost semantics.
if (normalizedRootDomain === "localhost" && hostname.endsWith(".test")) {
hostname = hostname.replace(/\.test$/, ".localhost");
}
// Normalize Vercel preview deployment URLs.
// Handles both "<branch>---<project>.vercel.app" and
// "<project>-<hash>-<scope>.vercel.app" URL shapes by treating preview hosts
// as root-domain requests.
const vercelSuffix = process.env.NEXT_PUBLIC_VERCEL_DEPLOYMENT_SUFFIX || "vercel.app";
if (hostname.endsWith(`.${vercelSuffix}`)) {
if (hostname.includes("---")) {
hostname = `${hostname.split("---")[0]}.${normalizedRootDomain}`;
} else {
hostname = normalizedRootDomain;
}
}
const MAIN_SUBDOMAIN = "main";
const mainDomainKey = `${MAIN_SUBDOMAIN}.${normalizedRootDomain}`;
const isMainAliasDomain = hostname === mainDomainKey;
if (isMainAliasDomain) {
// Treat main.<root> as root-domain alias to avoid cross-host redirect loops in middleware.
hostname = normalizedRootDomain === "localhost" ? "localhost" : normalizedRootDomain;
}
const isRootDomain =
hostname === "localhost" || hostname === normalizedRootDomain;
// ✅ Keep setup route reachable on root domain.
if (isRootDomain && path.startsWith("/setup")) {
traceEdge("middleware", "preserve setup path on root", { traceId, to: fullPath });
return preserveWithTrace(fullPath);
}
const externalAdminBase = `/app/${adminPathAlias}`;
const isExternalAdminPath =
path === externalAdminBase || path.startsWith(`${externalAdminBase}/`);
if (isRootDomain && isExternalAdminPath) {
const appPath = normalizeAdminAliasPath(path.replace(/^\/app/, ""), adminPathAlias);
const appFullPath = `${appPath}${searchParams ? `?${searchParams}` : ""}`;
traceEdge("middleware", "rewrite external admin alias path on root", { traceId, to: `/app${appPath === "/" ? "" : appFullPath}` });
const hasCookie = hasSessionCookie(req);
const sessionToken = hasCookie
? await getToken({
req,
secret: process.env.NEXTAUTH_SECRET,
})
: null;
const isAuthenticated = Boolean(sessionToken);
const forcePasswordChange = Boolean((sessionToken as any)?.forcePasswordChange);
const isPublicBridgePath = appPath === "/theme-bridge-client" || appPath === "/theme-bridge-start";
if (!isAuthenticated && appPath !== "/login" && !isPublicBridgePath) {
traceEdge("middleware", "redirect unauthenticated admin-path user", { traceId, to: `${externalAdminBase}/login` });
return markAdminResponseNoStore(redirectWithTrace(`${externalAdminBase}/login`));
}
if (appPath === "/login") {
traceEdge("middleware", "preserve admin login path for server auth resolution", {
traceId,
hasCookie,
isAuthenticated,
});
return markAdminResponseNoStore(
rewriteWithTrace(`/app/login${searchParams ? `?${searchParams}` : ""}`),
);
}
if (isAuthenticated && (appPath === "/" || appPath === "")) {
traceEdge("middleware", "preserve admin-path root for server dashboard routing", {
traceId,
to: "/app",
});
return markAdminResponseNoStore(rewriteWithTrace("/app"));
}
if (
isAuthenticated &&
forcePasswordChange &&
appPath !== "/settings/profile" &&
!appPath.startsWith("/settings/profile?")
) {
traceEdge("middleware", "force password change redirect", {
traceId,
to: `${externalAdminBase}/settings/profile?forcePasswordChange=1`,
});
return markAdminResponseNoStore(
redirectWithTrace(`${externalAdminBase}/settings/profile?forcePasswordChange=1`),
);
}
const rewritten = rewriteWithTrace(`/app${appPath === "/" ? "" : appFullPath}`);
const siteMatch = appPath.match(/^\/site\/([^/?#]+)/);
if (siteMatch?.[1]) {
setLastSiteId(rewritten, decodeURIComponent(siteMatch[1]));
setLastAppPath(rewritten, appPath);
}
return markAdminResponseNoStore(rewritten);
}
if (isRootDomain && (path === "/app" || path.startsWith("/app/"))) {
const cleanPath = normalizeAdminAliasPath(path, adminPathAlias);
const target = `/app/${adminPathAlias}${cleanPath === "/" ? "" : cleanPath}${searchParams ? `?${searchParams}` : ""}`;
traceEdge("middleware", "redirect internal app path to admin alias", { traceId, to: target });
return markAdminResponseNoStore(redirectWithTrace(target));
}
if (isRootDomain && path === "/login") {
const target = `${externalAdminBase}/login${searchParams ? `?${searchParams}` : ""}`;
traceEdge("middleware", "redirect root login path to canonical admin login", {
traceId,
to: target,
});
return markAdminResponseNoStore(redirectWithTrace(target));
}
// Preserve direct dashboard routes on localhost/root domain.
// ✅ Root homepage serves primary site directly.
if (isRootDomain && path === "/") {
traceEdge("middleware", "preserve root homepage", { traceId, to: "/" });
return preserveWithTrace("/");
}
// Keep /home namespace reachable directly.
if (isRootDomain && path.startsWith("/home")) {
traceEdge("middleware", "preserve /home path on root", { traceId, to: fullPath });
return preserveWithTrace(fullPath);
}
// 🧭 Root site rewrites non-app paths to the primary site content.
// Example: /welcome-to-tooty -> /main/welcome-to-tooty
if (isRootDomain) {
traceEdge("middleware", "rewrite root to primary site", {
traceId,
to: `/${mainDomainKey}${fullPath}`,
});
return rewriteWithTrace(`/${mainDomainKey}${fullPath}`);
}
// 🌐 All other subdomains use dynamic site routing
traceEdge("middleware", "rewrite tenant domain", { traceId, to: `/${hostname}${fullPath}` });
return rewriteWithTrace(`/${hostname}${fullPath}`);
}