@nifrajs/cli 5.17.0 → 5.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/dist/check-diagnostics.d.ts +7 -0
  3. package/dist/check-diagnostics.d.ts.map +1 -1
  4. package/dist/check-diagnostics.js +3 -0
  5. package/dist/check-diagnostics.js.map +1 -1
  6. package/dist/check-scan-sql.d.ts +95 -0
  7. package/dist/check-scan-sql.d.ts.map +1 -0
  8. package/dist/check-scan-sql.js +1190 -0
  9. package/dist/check-scan-sql.js.map +1 -0
  10. package/dist/check-scan.d.ts +5 -93
  11. package/dist/check-scan.d.ts.map +1 -1
  12. package/dist/check-scan.js +20 -1207
  13. package/dist/check-scan.js.map +1 -1
  14. package/dist/check.d.ts.map +1 -1
  15. package/dist/check.js +9 -0
  16. package/dist/check.js.map +1 -1
  17. package/dist/cli.d.ts +2 -6
  18. package/dist/cli.d.ts.map +1 -1
  19. package/dist/cli.js +249 -80
  20. package/dist/cli.js.map +1 -1
  21. package/dist/command-catalog.d.ts +3 -0
  22. package/dist/command-catalog.d.ts.map +1 -1
  23. package/dist/command-catalog.js +18 -4
  24. package/dist/command-catalog.js.map +1 -1
  25. package/dist/frontend-guidance.js +1 -1
  26. package/dist/frontend-guidance.js.map +1 -1
  27. package/dist/internal/a11y-element.d.ts +33 -0
  28. package/dist/internal/a11y-element.d.ts.map +1 -0
  29. package/dist/internal/a11y-element.js +124 -0
  30. package/dist/internal/a11y-element.js.map +1 -0
  31. package/dist/internal/a11y-jsx.d.ts +12 -0
  32. package/dist/internal/a11y-jsx.d.ts.map +1 -0
  33. package/dist/internal/a11y-jsx.js +42 -0
  34. package/dist/internal/a11y-jsx.js.map +1 -0
  35. package/dist/internal/a11y-template.d.ts +11 -0
  36. package/dist/internal/a11y-template.d.ts.map +1 -0
  37. package/dist/internal/a11y-template.js +56 -0
  38. package/dist/internal/a11y-template.js.map +1 -0
  39. package/dist/internal/aria-data.d.ts +23 -0
  40. package/dist/internal/aria-data.d.ts.map +1 -0
  41. package/dist/internal/aria-data.js +220 -0
  42. package/dist/internal/aria-data.js.map +1 -0
  43. package/dist/internal/edit-distance.d.ts +3 -0
  44. package/dist/internal/edit-distance.d.ts.map +1 -0
  45. package/dist/internal/edit-distance.js +12 -0
  46. package/dist/internal/edit-distance.js.map +1 -0
  47. package/dist/internal/imported-as.d.ts +10 -0
  48. package/dist/internal/imported-as.d.ts.map +1 -0
  49. package/dist/internal/imported-as.js +23 -0
  50. package/dist/internal/imported-as.js.map +1 -0
  51. package/dist/internal/source-facts.d.ts +3 -0
  52. package/dist/internal/source-facts.d.ts.map +1 -1
  53. package/dist/internal/source-facts.js +2 -0
  54. package/dist/internal/source-facts.js.map +1 -1
  55. package/dist/internal/template-markup.d.ts +58 -0
  56. package/dist/internal/template-markup.d.ts.map +1 -0
  57. package/dist/internal/template-markup.js +221 -0
  58. package/dist/internal/template-markup.js.map +1 -0
  59. package/dist/internal/typescript-import.d.ts.map +1 -1
  60. package/dist/internal/typescript-import.js +11 -0
  61. package/dist/internal/typescript-import.js.map +1 -1
  62. package/dist/mcp-exec.d.ts.map +1 -1
  63. package/dist/mcp-exec.js +37 -23
  64. package/dist/mcp-exec.js.map +1 -1
  65. package/dist/mcp-http.js +1 -1
  66. package/dist/mcp-io.d.ts.map +1 -1
  67. package/dist/mcp-io.js +5 -3
  68. package/dist/mcp-io.js.map +1 -1
  69. package/dist/mcp-root.js +1 -1
  70. package/dist/mcp-root.js.map +1 -1
  71. package/dist/mcp-ws.d.ts.map +1 -1
  72. package/dist/mcp-ws.js +3 -2
  73. package/dist/mcp-ws.js.map +1 -1
  74. package/dist/migrate-layout-config.d.ts +63 -0
  75. package/dist/migrate-layout-config.d.ts.map +1 -0
  76. package/dist/migrate-layout-config.js +507 -0
  77. package/dist/migrate-layout-config.js.map +1 -0
  78. package/dist/migrate-layout-split.d.ts +46 -0
  79. package/dist/migrate-layout-split.d.ts.map +1 -0
  80. package/dist/migrate-layout-split.js +687 -0
  81. package/dist/migrate-layout-split.js.map +1 -0
  82. package/dist/migrate-layout.d.ts +4 -24
  83. package/dist/migrate-layout.d.ts.map +1 -1
  84. package/dist/migrate-layout.js +8 -1183
  85. package/dist/migrate-layout.js.map +1 -1
  86. package/dist/output-schemas.d.ts.map +1 -1
  87. package/dist/output-schemas.js +78 -10
  88. package/dist/output-schemas.js.map +1 -1
  89. package/dist/recipes/5.18.0.d.ts +9 -0
  90. package/dist/recipes/5.18.0.d.ts.map +1 -0
  91. package/dist/recipes/5.18.0.js +56 -0
  92. package/dist/recipes/5.18.0.js.map +1 -0
  93. package/dist/recipes/index.d.ts.map +1 -1
  94. package/dist/recipes/index.js +2 -0
  95. package/dist/recipes/index.js.map +1 -1
  96. package/dist/review.d.ts.map +1 -1
  97. package/dist/review.js +6 -0
  98. package/dist/review.js.map +1 -1
  99. package/dist/rules/a11y.d.ts +5 -0
  100. package/dist/rules/a11y.d.ts.map +1 -0
  101. package/dist/rules/a11y.js +78 -0
  102. package/dist/rules/a11y.js.map +1 -0
  103. package/dist/rules/codes.d.ts +6 -0
  104. package/dist/rules/codes.d.ts.map +1 -1
  105. package/dist/rules/codes.js +6 -0
  106. package/dist/rules/codes.js.map +1 -1
  107. package/dist/rules/comment-markers.js +1 -1
  108. package/dist/rules/comment-markers.js.map +1 -1
  109. package/dist/rules/explanations.d.ts.map +1 -1
  110. package/dist/rules/explanations.js +30 -0
  111. package/dist/rules/explanations.js.map +1 -1
  112. package/dist/rules/host-exposure.d.ts +23 -0
  113. package/dist/rules/host-exposure.d.ts.map +1 -0
  114. package/dist/rules/host-exposure.js +380 -0
  115. package/dist/rules/host-exposure.js.map +1 -0
  116. package/dist/rules/internal-links.d.ts.map +1 -1
  117. package/dist/rules/internal-links.js +16 -141
  118. package/dist/rules/internal-links.js.map +1 -1
  119. package/dist/rules/islands.d.ts +54 -0
  120. package/dist/rules/islands.d.ts.map +1 -1
  121. package/dist/rules/islands.js +577 -80
  122. package/dist/rules/islands.js.map +1 -1
  123. package/dist/rules/security.d.ts +1 -0
  124. package/dist/rules/security.d.ts.map +1 -1
  125. package/dist/rules/security.js +34 -7
  126. package/dist/rules/security.js.map +1 -1
  127. package/dist/runtime-differences.js +3 -3
  128. package/dist/runtime-differences.js.map +1 -1
  129. package/dist/scaffold.d.ts +1 -1
  130. package/dist/scaffold.d.ts.map +1 -1
  131. package/dist/scaffold.js +2 -2
  132. package/dist/scaffold.js.map +1 -1
  133. package/dist/source-rewrites.d.ts.map +1 -1
  134. package/dist/source-rewrites.js +2 -19
  135. package/dist/source-rewrites.js.map +1 -1
  136. package/docs/examples.json +4 -4
  137. package/docs/llms-full.txt +422 -42
  138. package/docs/types.json +466 -56
  139. package/package.json +12 -12
  140. package/src/check-diagnostics.ts +11 -0
  141. package/src/check-scan-sql.ts +1746 -0
  142. package/src/check-scan.ts +34 -1754
  143. package/src/check.ts +10 -1
  144. package/src/cli.ts +258 -98
  145. package/src/command-catalog.ts +21 -4
  146. package/src/frontend-guidance.ts +1 -1
  147. package/src/internal/a11y-element.ts +227 -0
  148. package/src/internal/a11y-jsx.ts +54 -0
  149. package/src/internal/a11y-template.ts +74 -0
  150. package/src/internal/aria-data.ts +242 -0
  151. package/src/internal/edit-distance.ts +16 -0
  152. package/src/internal/imported-as.ts +30 -0
  153. package/src/internal/source-facts.ts +4 -0
  154. package/src/internal/template-markup.ts +270 -0
  155. package/src/internal/typescript-import.ts +15 -0
  156. package/src/mcp-exec.ts +43 -22
  157. package/src/mcp-http.ts +1 -1
  158. package/src/mcp-io.ts +5 -3
  159. package/src/mcp-root.ts +1 -1
  160. package/src/mcp-ws.ts +3 -2
  161. package/src/migrate-layout-config.ts +615 -0
  162. package/src/migrate-layout-split.ts +870 -0
  163. package/src/migrate-layout.ts +37 -1468
  164. package/src/output-schemas.ts +85 -17
  165. package/src/recipes/5.18.0.ts +63 -0
  166. package/src/recipes/index.ts +2 -0
  167. package/src/review.ts +6 -0
  168. package/src/rules/a11y.ts +119 -0
  169. package/src/rules/codes.ts +6 -0
  170. package/src/rules/comment-markers.ts +2 -2
  171. package/src/rules/explanations.ts +30 -0
  172. package/src/rules/host-exposure.ts +531 -0
  173. package/src/rules/internal-links.ts +21 -154
  174. package/src/rules/islands.ts +698 -86
  175. package/src/rules/security.ts +48 -7
  176. package/src/runtime-differences.ts +3 -3
  177. package/src/scaffold.ts +3 -3
  178. package/src/source-rewrites.ts +2 -15
@@ -0,0 +1,531 @@
1
+ import type * as TSApi from "typescript"
2
+ import type { HostTopology } from "../check-diagnostics.ts"
3
+ import { type Diagnostic, diagnostic } from "../diagnostics.ts"
4
+ import { importedAs } from "../internal/imported-as.ts"
5
+ import { commentBlockExpectation, MISSING_EXPECTATION_REASON } from "./comment-markers.ts"
6
+ import type { CheckRule, SourceIndex } from "./index.ts"
7
+ import { loadRuleTypeScript } from "./typescript.ts"
8
+
9
+ const EXPECTATION = ["NF-S010", "host-exposure"]
10
+ /** The one option a Deno or Bun listener reads: core has no `allowedHosts` or `canonicalHost`, those are the Node adapters'. */
11
+ const HOST_OPTION = "validateHost"
12
+ const LISTENER_HINT = /\b(?:serve|listen)\b/
13
+ const SCRIPT_FILE = /\.[cm]?[jt]sx?$/
14
+ /** Options that mean the listener terminates TLS itself, so no proxy in front is handling Host. */
15
+ const TLS_OPTIONS: ReadonlySet<string> = new Set(["tls", "cert"])
16
+
17
+ type Runtime = "deno" | "bun"
18
+
19
+ /** Whether the listener serves a Nifra app (which takes `validateHost`) or a bare runtime handler (which does not). */
20
+ type Surface = "app" | "native"
21
+
22
+ const CONSEQUENCE: Readonly<Record<Runtime, string>> = {
23
+ deno: "Deno builds the request URL from Host, so a Host such as `evil.test/admin?` routes the request to `/admin`, past any path rule in front of the app",
24
+ bun: "Bun may leave `c.req.url` relative, or build it on a Host the URL parser refuses, so `new URL(c.req.url)` can throw in a handler",
25
+ }
26
+
27
+ const LABEL: Readonly<Record<Runtime, string>> = {
28
+ deno: "Deno",
29
+ bun: "Bun",
30
+ }
31
+
32
+ function propertyKey(ts: typeof TSApi, prop: TSApi.ObjectLiteralElementLike): string | undefined {
33
+ if (ts.isShorthandPropertyAssignment(prop)) return prop.name.text
34
+ if (
35
+ !ts.isPropertyAssignment(prop) &&
36
+ !ts.isMethodDeclaration(prop) &&
37
+ !ts.isGetAccessorDeclaration(prop) &&
38
+ !ts.isSetAccessorDeclaration(prop)
39
+ )
40
+ return undefined
41
+ const name = prop.name
42
+ if (ts.isIdentifier(name) || ts.isStringLiteralLike(name)) return name.text
43
+ return undefined
44
+ }
45
+
46
+ type Bindings = ReadonlyMap<string, readonly TSApi.Expression[]>
47
+
48
+ /** `node` without the wrappers that change only its type: parentheses, `as`, `satisfies`, `!`. */
49
+ function unwrapped(ts: typeof TSApi, node: TSApi.Expression): TSApi.Expression {
50
+ let inner = node
51
+ while (isTypeWrapper(ts, inner)) inner = inner.expression
52
+ return inner
53
+ }
54
+
55
+ function isTypeWrapper(
56
+ ts: typeof TSApi,
57
+ node: TSApi.Node,
58
+ ): node is
59
+ | TSApi.ParenthesizedExpression
60
+ | TSApi.AsExpression
61
+ | TSApi.SatisfiesExpression
62
+ | TSApi.NonNullExpression {
63
+ return (
64
+ ts.isParenthesizedExpression(node) ||
65
+ ts.isAsExpression(node) ||
66
+ ts.isSatisfiesExpression(node) ||
67
+ ts.isNonNullExpression(node)
68
+ )
69
+ }
70
+
71
+ /** Whether `node` only names a member (`request.options`, `{ options: 1 }`) and refers to no binding. */
72
+ function namesMember(ts: typeof TSApi, node: TSApi.Identifier): boolean {
73
+ const parent = node.parent
74
+ if (ts.isBindingElement(parent)) return parent.propertyName === node
75
+ if (ts.isQualifiedName(parent)) return parent.right === node
76
+ return (
77
+ (ts.isPropertyAccessExpression(parent) ||
78
+ ts.isPropertyAssignment(parent) ||
79
+ ts.isPropertyDeclaration(parent) ||
80
+ ts.isPropertySignature(parent) ||
81
+ ts.isMethodDeclaration(parent) ||
82
+ ts.isMethodSignature(parent) ||
83
+ ts.isGetAccessorDeclaration(parent) ||
84
+ ts.isSetAccessorDeclaration(parent) ||
85
+ ts.isEnumMember(parent)) &&
86
+ parent.name === node
87
+ )
88
+ }
89
+
90
+ /**
91
+ * What a spread can copy from, `null` standing for a branch that writes nothing, or `undefined` when
92
+ * one of them cannot be read.
93
+ */
94
+ function spreadSources(
95
+ ts: typeof TSApi,
96
+ from: TSApi.Expression,
97
+ bindings: Bindings,
98
+ seen: ReadonlySet<TSApi.Node>,
99
+ ): readonly (TSApi.ObjectLiteralExpression | null)[] | undefined {
100
+ if (isTypeWrapper(ts, from)) return spreadSources(ts, from.expression, bindings, seen)
101
+ if (ts.isObjectLiteralExpression(from)) return [from]
102
+ if (
103
+ from.kind === ts.SyntaxKind.NullKeyword ||
104
+ (ts.isIdentifier(from) && from.text === "undefined")
105
+ )
106
+ return [null]
107
+ let branches: readonly TSApi.Expression[] | undefined
108
+ if (ts.isConditionalExpression(from)) branches = [from.whenTrue, from.whenFalse]
109
+ else if (
110
+ ts.isBinaryExpression(from) &&
111
+ from.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken
112
+ ) {
113
+ const right = spreadSources(ts, from.right, bindings, seen)
114
+ return right === undefined ? undefined : [...right, null]
115
+ } else if (ts.isIdentifier(from)) branches = bindings.get(from.text)
116
+ if (branches === undefined) return undefined
117
+ const found: (TSApi.ObjectLiteralExpression | null)[] = []
118
+ for (const branch of branches) {
119
+ // Each node is followed once, so a name bound to itself ends here.
120
+ if (seen.has(branch)) return undefined
121
+ const sources = spreadSources(ts, branch, bindings, new Set([...seen, branch]))
122
+ if (sources === undefined) return undefined
123
+ found.push(...sources)
124
+ }
125
+ return found
126
+ }
127
+
128
+ /**
129
+ * What an object literal leaves `validateHost` at, the last writer winning: `true` for a value that
130
+ * is the literal `true`, `undefined` when nothing writes it. A spread that cannot be read may
131
+ * write it, so it is no guarantee.
132
+ */
133
+ function hostSetting(
134
+ ts: typeof TSApi,
135
+ object: TSApi.ObjectLiteralExpression,
136
+ bindings: Bindings,
137
+ seen: ReadonlySet<TSApi.Node> = new Set(),
138
+ ): boolean | undefined {
139
+ let setting: boolean | undefined
140
+ for (const prop of object.properties) {
141
+ if (ts.isSpreadAssignment(prop)) {
142
+ const sources = spreadSources(ts, prop.expression, bindings, seen)
143
+ if (sources === undefined) {
144
+ setting = false
145
+ continue
146
+ }
147
+ const results = sources.map((source) =>
148
+ source === null ? undefined : hostSetting(ts, source, bindings, new Set([...seen, source])),
149
+ )
150
+ if (results.includes(false)) setting = false
151
+ else if (results.every((result) => result === true)) setting = true
152
+ } else if (prop.name !== undefined && ts.isComputedPropertyName(prop.name)) {
153
+ setting = false
154
+ } else if (propertyKey(ts, prop) === HOST_OPTION) {
155
+ setting =
156
+ ts.isPropertyAssignment(prop) &&
157
+ unwrapped(ts, prop.initializer).kind === ts.SyntaxKind.TrueKeyword
158
+ }
159
+ }
160
+ return setting
161
+ }
162
+
163
+ /** The local names `@nifrajs/deno` exports `serve` under in this file. */
164
+ function serveImports(ts: typeof TSApi, tree: TSApi.SourceFile): ReadonlyMap<string, Runtime> {
165
+ const { locals, namespaces } = importedAs(ts, tree, (from) => from === "@nifrajs/deno", "serve")
166
+ const found = new Map<string, Runtime>()
167
+ for (const name of locals) found.set(name, "deno")
168
+ for (const name of namespaces) found.set(`${name}.serve`, "deno")
169
+ return found
170
+ }
171
+
172
+ /** Local names under which this file has `server` from `@nifrajs/core`, and namespace imports of it. */
173
+ interface CoreNames {
174
+ readonly factories: ReadonlySet<string>
175
+ readonly namespaces: ReadonlySet<string>
176
+ readonly bindings: Bindings
177
+ }
178
+
179
+ function coreNames(ts: typeof TSApi, tree: TSApi.SourceFile): CoreNames {
180
+ const { locals: factories, namespaces } = importedAs(
181
+ ts,
182
+ tree,
183
+ (from) => from === "@nifrajs/core" || from.startsWith("@nifrajs/core/"),
184
+ "server",
185
+ )
186
+ return { factories, namespaces, bindings: bindingsIn(ts, tree) }
187
+ }
188
+
189
+ /** Every initializer each name in the file is declared with. */
190
+ function bindingsIn(ts: typeof TSApi, tree: TSApi.SourceFile): Bindings {
191
+ const bindings = new Map<string, TSApi.Expression[]>()
192
+ const collect = (node: TSApi.Node): void => {
193
+ if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name) && node.initializer) {
194
+ const found = bindings.get(node.name.text)
195
+ if (found === undefined) bindings.set(node.name.text, [node.initializer])
196
+ else found.push(node.initializer)
197
+ }
198
+ ts.forEachChild(node, collect)
199
+ }
200
+ collect(tree)
201
+ return bindings
202
+ }
203
+
204
+ function isServerFactory(ts: typeof TSApi, callee: TSApi.Expression, names: CoreNames): boolean {
205
+ return ts.isIdentifier(callee)
206
+ ? names.factories.has(callee.text)
207
+ : ts.isPropertyAccessExpression(callee) &&
208
+ ts.isIdentifier(callee.expression) &&
209
+ names.namespaces.has(callee.expression.text) &&
210
+ callee.name.text === "server"
211
+ }
212
+
213
+ function constantBindings(ts: typeof TSApi, tree: TSApi.SourceFile): Bindings {
214
+ const bindings = new Map<string, TSApi.Expression[]>()
215
+ for (const statement of tree.statements) {
216
+ if (
217
+ !ts.isVariableStatement(statement) ||
218
+ (statement.declarationList.flags & ts.NodeFlags.Const) === 0
219
+ )
220
+ continue
221
+ for (const declaration of statement.declarationList.declarations) {
222
+ if (ts.isIdentifier(declaration.name) && declaration.initializer !== undefined)
223
+ bindings.set(declaration.name.text, [declaration.initializer])
224
+ }
225
+ }
226
+ return bindings
227
+ }
228
+
229
+ /** Whether no scope between `node` and the file can declare a name that shadows a module one. */
230
+ function isModuleScope(ts: typeof TSApi, node: TSApi.Node): boolean {
231
+ for (let parent: TSApi.Node | undefined = node.parent; parent; parent = parent.parent) {
232
+ if (
233
+ ts.isBlock(parent) ||
234
+ ts.isFunctionLike(parent) ||
235
+ ts.isForStatement(parent) ||
236
+ ts.isForInStatement(parent) ||
237
+ ts.isForOfStatement(parent) ||
238
+ ts.isCaseBlock(parent) ||
239
+ ts.isModuleBlock(parent)
240
+ )
241
+ return false
242
+ }
243
+ return true
244
+ }
245
+
246
+ function unescapedOptions(
247
+ ts: typeof TSApi,
248
+ tree: TSApi.SourceFile,
249
+ names: CoreNames,
250
+ constants: Bindings,
251
+ ): Bindings {
252
+ const options = new Map(constants)
253
+ const visit = (node: TSApi.Node): void => {
254
+ if (ts.isIdentifier(node) && options.has(node.text) && !namesMember(ts, node)) {
255
+ let value: TSApi.Node = node
256
+ while (
257
+ isTypeWrapper(ts, value.parent) ||
258
+ (ts.isConditionalExpression(value.parent) && value !== value.parent.condition) ||
259
+ (ts.isBinaryExpression(value.parent) &&
260
+ value.parent.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken &&
261
+ value === value.parent.right)
262
+ ) {
263
+ value = value.parent
264
+ }
265
+ const use = value.parent
266
+ const declares = ts.isVariableDeclaration(use) && use.name === node
267
+ const spreads = ts.isSpreadAssignment(use)
268
+ const configures =
269
+ ts.isCallExpression(use) &&
270
+ use.arguments[0] === value &&
271
+ isModuleScope(ts, use) &&
272
+ isServerFactory(ts, use.expression, names)
273
+ // Property writes, aliases, and calls to unknown code may change the options object.
274
+ if (!declares && !spreads && !configures) options.delete(node.text)
275
+ }
276
+ ts.forEachChild(node, visit)
277
+ }
278
+ visit(tree)
279
+ return options
280
+ }
281
+
282
+ function hasHostGuard(
283
+ ts: typeof TSApi,
284
+ listener: TSApi.CallExpression,
285
+ app: TSApi.Expression,
286
+ names: CoreNames,
287
+ constants: Bindings,
288
+ optionsBindings: Bindings,
289
+ ): boolean {
290
+ // A nested scope may shadow a module-level app, option, or factory.
291
+ if (!isModuleScope(ts, listener)) return false
292
+ const seen = new Set<TSApi.Expression>()
293
+ let receiver: TSApi.Expression | undefined = app
294
+ while (receiver !== undefined && !seen.has(receiver)) {
295
+ seen.add(receiver)
296
+ if (ts.isIdentifier(receiver)) receiver = constants.get(receiver.text)?.[0]
297
+ else if (isTypeWrapper(ts, receiver)) receiver = receiver.expression
298
+ else if (ts.isCallExpression(receiver)) {
299
+ const callee = receiver.expression
300
+ if (isServerFactory(ts, callee, names)) {
301
+ const options = receiver.arguments[0]
302
+ if (options === undefined) return false
303
+ return (
304
+ spreadSources(ts, options, optionsBindings, new Set())?.every(
305
+ (source) => source !== null && hostSetting(ts, source, optionsBindings) === true,
306
+ ) === true
307
+ )
308
+ }
309
+ receiver =
310
+ ts.isPropertyAccessExpression(callee) &&
311
+ /^(?:get|post|put|patch|delete|head|options|route|use|derive|decorate|group|guard)$/.test(
312
+ callee.name.text,
313
+ )
314
+ ? callee.expression
315
+ : undefined
316
+ } else return false
317
+ }
318
+ return false
319
+ }
320
+
321
+ /**
322
+ * Whether `receiver` can be a Nifra server: a `server(...)` call or a chain from one, or a name this
323
+ * file binds to one. A name it does not bind (an import, a parameter) is given the benefit of the
324
+ * doubt; a name bound to something else (`const tcp = net.createServer()`) is not.
325
+ */
326
+ function isNifraServer(
327
+ ts: typeof TSApi,
328
+ receiver: TSApi.Expression,
329
+ names: CoreNames,
330
+ lenient = true,
331
+ hops = 0,
332
+ ): boolean {
333
+ // Only following a binding counts: a chain or a wrapper is finite, a binding can name itself.
334
+ if (hops > 6) return false
335
+ if (
336
+ ts.isParenthesizedExpression(receiver) ||
337
+ ts.isAwaitExpression(receiver) ||
338
+ ts.isNonNullExpression(receiver) ||
339
+ ts.isAsExpression(receiver)
340
+ )
341
+ return isNifraServer(ts, receiver.expression, names, lenient, hops)
342
+ if (ts.isIdentifier(receiver)) {
343
+ const inits = names.bindings.get(receiver.text)
344
+ if (inits === undefined) return lenient
345
+ return inits.some((init) => isNifraServer(ts, init, names, true, hops + 1))
346
+ }
347
+ if (ts.isCallExpression(receiver)) {
348
+ const callee = receiver.expression
349
+ if (isServerFactory(ts, callee, names)) return true
350
+ if (ts.isPropertyAccessExpression(callee)) {
351
+ // A method call on a name this file does not bind (`net.createServer()`) proves nothing.
352
+ return isNifraServer(ts, callee.expression, names, false, hops)
353
+ }
354
+ return false
355
+ }
356
+ // `this.app`, `ctx.app`: not traceable here.
357
+ return (
358
+ lenient && (ts.isPropertyAccessExpression(receiver) || ts.isElementAccessExpression(receiver))
359
+ )
360
+ }
361
+
362
+ function calleeName(ts: typeof TSApi, callee: TSApi.Expression): string | undefined {
363
+ if (ts.isIdentifier(callee)) return callee.text
364
+ if (ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.expression))
365
+ return `${callee.expression.text}.${callee.name.text}`
366
+ return undefined
367
+ }
368
+
369
+ interface Kind {
370
+ readonly runtime: Runtime
371
+ readonly surface: Surface
372
+ /** Which argument holds the listener's options: an adapter or `listen()` takes them second, the runtime's own call first. */
373
+ readonly optionsAt: "second" | "first"
374
+ readonly app?: TSApi.Expression | undefined
375
+ }
376
+
377
+ function listenerKind(
378
+ ts: typeof TSApi,
379
+ call: TSApi.CallExpression,
380
+ imports: ReadonlyMap<string, Runtime>,
381
+ names: CoreNames,
382
+ ): Kind | undefined {
383
+ const name = calleeName(ts, call.expression)
384
+ if (name !== undefined) {
385
+ const imported = imports.get(name)
386
+ if (imported !== undefined)
387
+ return { runtime: imported, surface: "app", optionsAt: "second", app: call.arguments[0] }
388
+ if (name === "Deno.serve") return { runtime: "deno", surface: "native", optionsAt: "first" }
389
+ if (name === "Bun.serve") return { runtime: "bun", surface: "native", optionsAt: "first" }
390
+ }
391
+ if (
392
+ ts.isPropertyAccessExpression(call.expression) &&
393
+ call.expression.name.text === "listen" &&
394
+ (names.factories.size > 0 || names.namespaces.size > 0) &&
395
+ isNifraServer(ts, call.expression.expression, names)
396
+ )
397
+ return { runtime: "bun", surface: "app", optionsAt: "second", app: call.expression.expression }
398
+ return undefined
399
+ }
400
+
401
+ /** Whether the options a listener is given hold TLS: the listener terminates it, with no proxy in front. */
402
+ function terminatesTls(ts: typeof TSApi, call: TSApi.CallExpression, kind: Kind): boolean {
403
+ const candidate =
404
+ kind.optionsAt === "second"
405
+ ? call.arguments[1]
406
+ : call.arguments.find(ts.isObjectLiteralExpression)
407
+ if (candidate === undefined || !ts.isObjectLiteralExpression(candidate)) return false
408
+ return candidate.properties.some((prop) => {
409
+ const key = propertyKey(ts, prop)
410
+ if (key === undefined || !TLS_OPTIONS.has(key)) return false
411
+ if (!ts.isPropertyAssignment(prop)) return true
412
+ const value = prop.initializer
413
+ // `tls: undefined` is how a caller leaves TLS out.
414
+ return !(
415
+ (ts.isIdentifier(value) && value.text === "undefined") ||
416
+ value.kind === ts.SyntaxKind.NullKeyword ||
417
+ value.kind === ts.SyntaxKind.FalseKeyword
418
+ )
419
+ })
420
+ }
421
+
422
+ /** A parsed source file, read once and shared with every other rule that scans it. */
423
+ export interface ParsedSource {
424
+ readonly tree: TSApi.SourceFile
425
+ readonly lines: readonly string[]
426
+ }
427
+
428
+ /** Where in a file a listener sits, and the diagnostic it reports once its line is known. */
429
+ interface Listener {
430
+ readonly position: number
431
+ readonly runtime: Runtime
432
+ readonly surface: Surface
433
+ }
434
+
435
+ function listenersIn(
436
+ ts: typeof TSApi,
437
+ tree: TSApi.SourceFile,
438
+ topology: HostTopology | undefined,
439
+ ): readonly Listener[] {
440
+ const imports = serveImports(ts, tree)
441
+ const names = coreNames(ts, tree)
442
+ const constants = constantBindings(ts, tree)
443
+ const optionBindings = unescapedOptions(ts, tree, names, constants)
444
+ const found: Listener[] = []
445
+ const visit = (node: TSApi.Node): void => {
446
+ if (ts.isCallExpression(node)) {
447
+ const kind = listenerKind(ts, node, imports, names)
448
+ if (
449
+ kind !== undefined &&
450
+ (topology === "direct" || terminatesTls(ts, node, kind)) &&
451
+ (kind.app === undefined ||
452
+ !hasHostGuard(ts, node, kind.app, names, constants, optionBindings))
453
+ )
454
+ found.push({
455
+ position: node.expression.getStart(tree),
456
+ runtime: kind.runtime,
457
+ surface: kind.surface,
458
+ })
459
+ }
460
+ ts.forEachChild(node, visit)
461
+ }
462
+ visit(tree)
463
+ return found
464
+ }
465
+
466
+ /**
467
+ * NF-S010: a Deno or Bun listener that answers every request without checking `Host`, where
468
+ * nothing says a proxy does. The check is off by default on both, which costs a request
469
+ * nothing and is right behind a proxy that checks `Host`. It is not right for a server clients reach
470
+ * directly, and Deno routes on what `Host` carries.
471
+ *
472
+ * The rule never infers a proxy: a Dockerfile, an `nginx.conf` or a `fly.toml` says little about the
473
+ * network a server runs in. It fires on evidence of direct exposure (the listener terminates TLS
474
+ * itself) or on `"hostTopology": "direct"` in `nifra.check.json`, and stays silent on
475
+ * `"hostTopology": "proxied"`. A guard exempts the listener whose app can be traced to a local
476
+ * server factory with `validateHost: true`.
477
+ *
478
+ * `parsedFile` is the security rules' shared parse cache, so a file is read once for all of them.
479
+ */
480
+ export function createHostExposureRule(
481
+ parsedFile: (ts: typeof TSApi, sources: SourceIndex, file: string) => ParsedSource | undefined,
482
+ ): CheckRule {
483
+ return {
484
+ code: "NF-S010",
485
+ title: "Server answers requests with Host unchecked",
486
+ async scan(ctx): Promise<readonly Diagnostic[]> {
487
+ const topology = ctx.project.policies.checkConfig.hostTopology
488
+ if (topology === "proxied") return []
489
+ const loaded = await loadRuleTypeScript(ctx)
490
+ if (loaded.diagnostics !== undefined) return loaded.diagnostics
491
+ const ts = loaded.compiler
492
+ if (ts === undefined) return []
493
+ const sources = ctx.project.source
494
+ const findings: Diagnostic[] = []
495
+ for (const file of sources.files) {
496
+ if (!SCRIPT_FILE.test(file)) continue
497
+ const source = parsedFile(ts, sources, file)
498
+ if (source === undefined || !LISTENER_HINT.test(source.tree.text)) continue
499
+ const { tree, lines } = source
500
+ for (const listener of listenersIn(ts, tree, topology)) {
501
+ const line = tree.getLineAndCharacterOfPosition(listener.position).line + 1
502
+ const expected = commentBlockExpectation(lines, line, EXPECTATION)
503
+ if (expected?.reason !== undefined) continue
504
+ const remedy =
505
+ listener.surface === "app"
506
+ ? "Set `validateHost: true` on the server (one header read per request)"
507
+ : "Check `Host` in the handler before routing on it (this listener has no `validateHost` option)"
508
+ const message = `This ${LABEL[listener.runtime]} listener answers every request without checking Host: ${CONSEQUENCE[listener.runtime]}. ${remedy}, or say a proxy in front checks it with \`"hostTopology": "proxied"\` in nifra.check.json or a \`nifra-expect NF-S010: <reason>\` comment above the listener`
509
+ findings.push(
510
+ diagnostic({
511
+ code: "NF-S010",
512
+ severity: "warn",
513
+ file: tree.fileName,
514
+ line,
515
+ message:
516
+ expected === undefined ? message : `${message}. ${MISSING_EXPECTATION_REASON}`,
517
+ evidence: [
518
+ topology === "direct"
519
+ ? 'nifra.check.json declares "hostTopology": "direct"'
520
+ : "the listener terminates TLS itself",
521
+ "this listener has no provable Host guard",
522
+ ],
523
+ verify: "nifra check --lints-only",
524
+ }),
525
+ )
526
+ }
527
+ }
528
+ return findings
529
+ },
530
+ }
531
+ }