๐Ÿ“ฆ cloudflare / vinext

๐Ÿ“„ middleware-codegen.ts ยท 193 lines
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/**
 * Shared middleware matching code generator.
 *
 * Both the App Router RSC entry (app-dev-server.ts) and the Pages Router
 * production entry (index.ts) need middleware matching logic inlined as
 * generated JavaScript strings. This module provides a single source of
 * truth to prevent the implementations from diverging.
 *
 * The regex detection guard (checking for "(" or "\\") is critical.
 * Without it, dot-escaping corrupts regex patterns like
 * /((?!api|_next).*), causing middleware to silently skip paths.
 */

/**
 * Returns the generated JavaScript source for `__isSafeRegex` and `__safeRegExp`.
 *
 * @param style - "modern" emits const/let (for RSC entry), "es5" emits var (for prod entry)
 */
export function generateSafeRegExpCode(style: "modern" | "es5" = "modern"): string {
  const v = style === "modern" ? "const" : "var";
  const l = style === "modern" ? "let" : "var";
  return `
function __isSafeRegex(pattern) {
  ${v} quantifierAtDepth = [];
  ${l} depth = 0;
  ${l} i = 0;
  while (i < pattern.length) {
    ${v} ch = pattern[i];
    if (ch === "\\\\") { i += 2; continue; }
    if (ch === "[") {
      i++;
      while (i < pattern.length && pattern[i] !== "]") {
        if (pattern[i] === "\\\\") i++;
        i++;
      }
      i++;
      continue;
    }
    if (ch === "(") {
      depth++;
      if (quantifierAtDepth.length <= depth) quantifierAtDepth.push(false);
      else quantifierAtDepth[depth] = false;
      i++;
      continue;
    }
    if (ch === ")") {
      ${v} hadQ = depth > 0 && quantifierAtDepth[depth];
      if (depth > 0) depth--;
      ${v} next = pattern[i + 1];
      if (next === "+" || next === "*" || next === "{") {
        if (hadQ) return false;
        if (depth >= 0 && depth < quantifierAtDepth.length) quantifierAtDepth[depth] = true;
      }
      i++;
      continue;
    }
    if (ch === "+" || ch === "*") {
      if (depth > 0) quantifierAtDepth[depth] = true;
      i++;
      continue;
    }
    if (ch === "?") {
      ${v} prev = i > 0 ? pattern[i - 1] : "";
      if (prev !== "+" && prev !== "*" && prev !== "?" && prev !== "}") {
        if (depth > 0) quantifierAtDepth[depth] = true;
      }
      i++;
      continue;
    }
    if (ch === "{") {
      ${l} j = i + 1;
      while (j < pattern.length && /[\\d,]/.test(pattern[j])) j++;
      if (j < pattern.length && pattern[j] === "}" && j > i + 1) {
        if (depth > 0) quantifierAtDepth[depth] = true;
        i = j + 1;
        continue;
      }
    }
    i++;
  }
  return true;
}
function __safeRegExp(pattern, flags) {
  if (!__isSafeRegex(pattern)) {
    console.warn("[vinext] Ignoring potentially unsafe regex pattern (ReDoS risk): " + pattern);
    return null;
  }
  try { return new RegExp(pattern, flags); } catch { return null; }
}`;
}

/**
 * Returns the generated JavaScript source for `__normalizePath`.
 *
 * This must be kept in sync with `normalizePath()` in `normalize-path.ts`.
 * The inline version is used by codegen entries that can't import modules.
 *
 * @param style - "modern" emits const/let, "es5" emits var
 */
export function generateNormalizePathCode(style: "modern" | "es5" = "modern"): string {
  const v = style === "modern" ? "const" : "var";
  const l = style === "modern" ? "let" : "var";
  return `
function __normalizePath(pathname) {
  if (
    pathname === "/" ||
    (pathname.length > 1 &&
      pathname[0] === "/" &&
      !pathname.includes("//") &&
      !pathname.includes("/./") &&
      !pathname.includes("/../") &&
      !pathname.endsWith("/.") &&
      !pathname.endsWith("/.."))
  ) {
    return pathname;
  }
  ${v} segments = pathname.split("/");
  ${v} resolved = [];
  for (${l} i = 0; i < segments.length; i++) {
    ${v} seg = segments[i];
    if (seg === "" || seg === ".") continue;
    if (seg === "..") { resolved.pop(); }
    else { resolved.push(seg); }
  }
  return "/" + resolved.join("/");
}`;
}

/**
 * Returns the generated JavaScript source for middleware pattern matching.
 *
 * This includes:
 * - `matchMiddlewarePattern(pathname, pattern)` โ€” matches a single pattern
 * - `matchesMiddleware(pathname, matcher)` โ€” matches the full matcher config
 *
 * The generated code depends on `__safeRegExp` being defined in the same scope
 * (use `generateSafeRegExpCode` to emit it).
 *
 * @param style - "modern" emits const/let/arrow functions, "es5" emits var/function
 */
export function generateMiddlewareMatcherCode(style: "modern" | "es5" = "modern"): string {
  const v = style === "modern" ? "const" : "var";
  const l = style === "modern" ? "let" : "var";
  const fn = style === "modern"
    ? (params: string, body: string) => `(${params}) => { ${body} }`
    : (params: string, body: string) => `function(${params}) { ${body} }`;

  // The pattern matching logic must be identical to matchPattern() in
  // packages/vinext/src/server/middleware.ts. Any changes here must be
  // mirrored there and vice versa.
  return `
function matchMiddlewarePattern(pathname, pattern) {
  // Regex patterns: if the pattern contains "(" or "\\" it's a regex โ€”
  // pass it through to RegExp directly WITHOUT dot-escaping.
  // This guard prevents regex pattern corruption from dot-escaping.
  if (pattern.includes("(") || pattern.includes("\\\\")) {
    ${v} re = __safeRegExp("^" + pattern + "$");
    if (re) return re.test(pathname);
  }
  // Single-pass tokenizer (avoids chained .replace() flagged by CodeQL as
  // incomplete sanitization โ€” later passes could re-process earlier outputs).
  ${l} regexStr = "";
  ${v} tokenRe = /\\/:([\\w]+)\\*|\\/:([\\w]+)\\+|:([\\w]+)|[.]|[^/:.]+|./g;
  ${l} tok;
  while ((tok = tokenRe.exec(pattern)) !== null) {
    if (tok[1] !== undefined) { regexStr += "(?:/.*)?"; }
    else if (tok[2] !== undefined) { regexStr += "(?:/.+)"; }
    else if (tok[3] !== undefined) { regexStr += "([^/]+)"; }
    else if (tok[0] === ".") { regexStr += "\\\\."; }
    else { regexStr += tok[0]; }
  }
  ${v} re2 = __safeRegExp("^" + regexStr + "$");
  return re2 ? re2.test(pathname) : pathname === pattern;
}

function matchesMiddleware(pathname, matcher) {
  if (!matcher) {
    return true;
  }
  ${v} patterns = [];
  if (typeof matcher === "string") { patterns.push(matcher); }
  else if (Array.isArray(matcher)) {
    for (${v} m of matcher) {
      if (typeof m === "string") patterns.push(m);
      else if (m && typeof m === "object" && "source" in m) patterns.push(m.source);
    }
  }
  return patterns.some(${fn("p", "return matchMiddlewarePattern(pathname, p);")});
}`;
}