-
-
Notifications
You must be signed in to change notification settings - Fork 814
Expand file tree
/
Copy pathroute-rules.ts
More file actions
123 lines (115 loc) · 4.4 KB
/
route-rules.ts
File metadata and controls
123 lines (115 loc) · 4.4 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
import { HTTPError, proxyRequest, redirect as sendRedirect, requireBasicAuth } from "h3";
import type { BasicAuthOptions, EventHandler, Middleware } from "h3";
import type { MatchedRouteRule, NitroRouteRules } from "nitro/types";
import { joinURL, withQuery, withoutBase } from "ufo";
import { defineCachedHandler } from "./cache.ts";
// Note: Remember to update RuntimeRouteRules in src/build/virtual/routing.ts when adding new route rules
type RouteRuleCtor<T extends keyof NitroRouteRules> = ((m: MatchedRouteRule<T>) => Middleware) & {
order?: number;
};
// Headers route rule
export const headers: RouteRuleCtor<"headers"> = ((m) =>
function headersRouteRule(event) {
for (const [key, value] of Object.entries(m.options || {})) {
event.res.headers.set(key, value);
}
}) satisfies RouteRuleCtor<"headers">;
// Redirect route rule
export const redirect: RouteRuleCtor<"redirect"> = ((m) =>
function redirectRouteRule(event) {
let target = m.options?.to;
if (!target) {
return;
}
if (target.endsWith("/**")) {
let targetPath = event.url.pathname + event.url.search;
const strpBase = (m.options as any)._redirectStripBase;
if (strpBase) {
if (!isPathInScope(event.url.pathname, strpBase)) {
throw new HTTPError({ status: 400 });
}
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
return sendRedirect(target, m.options?.status);
}) satisfies RouteRuleCtor<"redirect">;
// Proxy route rule
export const proxy: RouteRuleCtor<"proxy"> = ((m) =>
function proxyRouteRule(event) {
let target = m.options?.to;
if (!target) {
return;
}
if (target.endsWith("/**")) {
let targetPath = event.url.pathname + event.url.search;
const strpBase = (m.options as any)._proxyStripBase;
if (strpBase) {
if (!isPathInScope(event.url.pathname, strpBase)) {
throw new HTTPError({ status: 400 });
}
targetPath = withoutBase(targetPath, strpBase);
}
target = joinURL(target.slice(0, -3), targetPath);
} else if (event.url.search) {
target = withQuery(target, Object.fromEntries(event.url.searchParams));
}
return proxyRequest(event, target, {
...m.options,
});
}) satisfies RouteRuleCtor<"proxy">;
// Cache route rule
export const cache: RouteRuleCtor<"cache"> = ((m) =>
function cacheRouteRule(event, next) {
if (!event.context.matchedRoute) {
return next();
}
const cachedHandlers: Map<string, EventHandler> = ((globalThis as any).__nitroCachedHandlers ??=
new Map());
const { handler, route } = event.context.matchedRoute;
const key = `${m.route}:${route}`;
let cachedHandler = cachedHandlers.get(key);
if (!cachedHandler) {
cachedHandler = defineCachedHandler(handler, {
group: "nitro/route-rules",
name: key,
...m.options,
});
cachedHandlers.set(key, cachedHandler);
}
return cachedHandler(event);
}) satisfies RouteRuleCtor<"cache">;
// basicAuth auth route rule
// Must run before `redirect`/`proxy`/`cache` so unauthorized requests are
// neither redirected nor proxied.
export const basicAuth: RouteRuleCtor<"auth"> = /* @__PURE__ */ Object.assign(
((m) =>
async function authRouteRule(event, next) {
if (!m.options) {
return;
}
await requireBasicAuth(event, m.options as BasicAuthOptions);
return next();
}) satisfies RouteRuleCtor<"auth">,
{ order: -1 }
);
// Check whether `pathname`, after canonicalization, stays within `base`.
// Prevents match/forward differentials where an encoded traversal like `..%2f`
// bypasses the `/**` scope at match time but escapes the base once the
// downstream (proxy upstream or redirect target) decodes `%2f` → `/`
// (GHSA-5w89-w975-hf9q).
//
// WHATWG URL keeps `%2F` and `%5C` opaque in paths, so we pre-decode those,
// then let `new URL` resolve `.`/`..`/`%2E%2E` segments and normalize `\`.
export function isPathInScope(pathname: string, base: string): boolean {
let canonical: string;
try {
const pre = pathname.replace(/%2f/gi, "/").replace(/%5c/gi, "\\");
canonical = new URL(pre, "http://_").pathname;
} catch {
return false;
}
return !base || canonical === base || canonical.startsWith(base + "/");
}