@nifrajs/cli 5.7.0 → 5.9.0

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 (253) hide show
  1. package/CHANGELOG.md +150 -0
  2. package/dist/agent-files.d.ts +0 -22
  3. package/dist/agent-files.d.ts.map +1 -1
  4. package/dist/agent-files.js +3 -1
  5. package/dist/agent-files.js.map +1 -1
  6. package/dist/assure-hydration.d.ts.map +1 -1
  7. package/dist/assure-hydration.js +2 -10
  8. package/dist/assure-hydration.js.map +1 -1
  9. package/dist/assure.d.ts.map +1 -1
  10. package/dist/assure.js +3 -3
  11. package/dist/assure.js.map +1 -1
  12. package/dist/check-scan.d.ts +2 -0
  13. package/dist/check-scan.d.ts.map +1 -1
  14. package/dist/check-scan.js +54 -86
  15. package/dist/check-scan.js.map +1 -1
  16. package/dist/check.d.ts +3 -0
  17. package/dist/check.d.ts.map +1 -1
  18. package/dist/check.js +20 -23
  19. package/dist/check.js.map +1 -1
  20. package/dist/cli.d.ts +8 -0
  21. package/dist/cli.d.ts.map +1 -1
  22. package/dist/cli.js +42 -13
  23. package/dist/cli.js.map +1 -1
  24. package/dist/command-catalog.d.ts.map +1 -1
  25. package/dist/command-catalog.js +20 -30
  26. package/dist/command-catalog.js.map +1 -1
  27. package/dist/config-target.js +1 -1
  28. package/dist/config-target.js.map +1 -1
  29. package/dist/db-child.js +3 -10
  30. package/dist/db-child.js.map +1 -1
  31. package/dist/db-config.d.ts +2 -0
  32. package/dist/db-config.d.ts.map +1 -1
  33. package/dist/db-config.js +2 -0
  34. package/dist/db-config.js.map +1 -1
  35. package/dist/doctor.d.ts +14 -3
  36. package/dist/doctor.d.ts.map +1 -1
  37. package/dist/doctor.js +91 -6
  38. package/dist/doctor.js.map +1 -1
  39. package/dist/frontend-guidance.d.ts +19 -0
  40. package/dist/frontend-guidance.d.ts.map +1 -1
  41. package/dist/frontend-guidance.js +267 -23
  42. package/dist/frontend-guidance.js.map +1 -1
  43. package/dist/hand-built-manifest.d.ts.map +1 -1
  44. package/dist/hand-built-manifest.js +1 -1
  45. package/dist/hand-built-manifest.js.map +1 -1
  46. package/dist/internal/control-character.d.ts +3 -0
  47. package/dist/internal/control-character.d.ts.map +1 -0
  48. package/dist/internal/control-character.js +10 -0
  49. package/dist/internal/control-character.js.map +1 -0
  50. package/dist/internal/escape-regexp.d.ts +3 -0
  51. package/dist/internal/escape-regexp.d.ts.map +1 -0
  52. package/dist/internal/escape-regexp.js +5 -0
  53. package/dist/internal/escape-regexp.js.map +1 -0
  54. package/dist/internal/line-at.d.ts +3 -0
  55. package/dist/internal/line-at.d.ts.map +1 -0
  56. package/dist/internal/line-at.js +5 -0
  57. package/dist/internal/line-at.js.map +1 -0
  58. package/dist/internal/nifra-project.d.ts.map +1 -1
  59. package/dist/internal/nifra-project.js +35 -41
  60. package/dist/internal/nifra-project.js.map +1 -1
  61. package/dist/internal/posix-path.d.ts +3 -0
  62. package/dist/internal/posix-path.d.ts.map +1 -0
  63. package/dist/internal/posix-path.js +5 -0
  64. package/dist/internal/posix-path.js.map +1 -0
  65. package/dist/internal/project-specifiers.d.ts.map +1 -1
  66. package/dist/internal/project-specifiers.js +3 -10
  67. package/dist/internal/project-specifiers.js.map +1 -1
  68. package/dist/internal/realpath-or-self.d.ts +3 -0
  69. package/dist/internal/realpath-or-self.d.ts.map +1 -0
  70. package/dist/internal/realpath-or-self.js +11 -0
  71. package/dist/internal/realpath-or-self.js.map +1 -0
  72. package/dist/internal/record.d.ts +3 -0
  73. package/dist/internal/record.d.ts.map +1 -0
  74. package/dist/internal/record.js +5 -0
  75. package/dist/internal/record.js.map +1 -0
  76. package/dist/internal/source-facts.d.ts.map +1 -1
  77. package/dist/internal/source-facts.js +28 -25
  78. package/dist/internal/source-facts.js.map +1 -1
  79. package/dist/introspect.d.ts +0 -22
  80. package/dist/introspect.d.ts.map +1 -1
  81. package/dist/introspect.js +11 -151
  82. package/dist/introspect.js.map +1 -1
  83. package/dist/levels-tool.d.ts +2 -0
  84. package/dist/levels-tool.d.ts.map +1 -1
  85. package/dist/levels-tool.js +54 -64
  86. package/dist/levels-tool.js.map +1 -1
  87. package/dist/load.d.ts +6 -1
  88. package/dist/load.d.ts.map +1 -1
  89. package/dist/load.js.map +1 -1
  90. package/dist/local-cli.d.ts +7 -0
  91. package/dist/local-cli.d.ts.map +1 -1
  92. package/dist/local-cli.js +13 -0
  93. package/dist/local-cli.js.map +1 -1
  94. package/dist/mcp-docs-tools.d.ts +1 -1
  95. package/dist/mcp-docs-tools.d.ts.map +1 -1
  96. package/dist/mcp-docs-tools.js +46 -3
  97. package/dist/mcp-docs-tools.js.map +1 -1
  98. package/dist/mcp-http.d.ts +1 -1
  99. package/dist/mcp-http.js +2 -2
  100. package/dist/mcp-io.d.ts +16 -0
  101. package/dist/mcp-io.d.ts.map +1 -1
  102. package/dist/mcp-io.js +63 -0
  103. package/dist/mcp-io.js.map +1 -1
  104. package/dist/mcp-isolate.d.ts.map +1 -1
  105. package/dist/mcp-isolate.js +2 -1
  106. package/dist/mcp-isolate.js.map +1 -1
  107. package/dist/mcp-project-child.d.ts.map +1 -1
  108. package/dist/mcp-project-child.js +5 -12
  109. package/dist/mcp-project-child.js.map +1 -1
  110. package/dist/mcp-render.d.ts.map +1 -1
  111. package/dist/mcp-render.js +9 -51
  112. package/dist/mcp-render.js.map +1 -1
  113. package/dist/mcp-root.js +1 -1
  114. package/dist/mcp-root.js.map +1 -1
  115. package/dist/mcp-run.d.ts.map +1 -1
  116. package/dist/mcp-run.js +3 -46
  117. package/dist/mcp-run.js.map +1 -1
  118. package/dist/mcp-ws.d.ts.map +1 -1
  119. package/dist/mcp-ws.js +2 -18
  120. package/dist/mcp-ws.js.map +1 -1
  121. package/dist/migrate-layout.d.ts.map +1 -1
  122. package/dist/migrate-layout.js +36 -57
  123. package/dist/migrate-layout.js.map +1 -1
  124. package/dist/output-schemas.d.ts.map +1 -1
  125. package/dist/output-schemas.js +7 -7
  126. package/dist/output-schemas.js.map +1 -1
  127. package/dist/port.d.ts.map +1 -1
  128. package/dist/port.js +1 -2
  129. package/dist/port.js.map +1 -1
  130. package/dist/ppr-read-fix.js +2 -2
  131. package/dist/ppr-read-fix.js.map +1 -1
  132. package/dist/recipes/5.8.0.d.ts +9 -0
  133. package/dist/recipes/5.8.0.d.ts.map +1 -0
  134. package/dist/recipes/5.8.0.js +20 -0
  135. package/dist/recipes/5.8.0.js.map +1 -0
  136. package/dist/recipes/5.9.0.d.ts +10 -0
  137. package/dist/recipes/5.9.0.d.ts.map +1 -0
  138. package/dist/recipes/5.9.0.js +41 -0
  139. package/dist/recipes/5.9.0.js.map +1 -0
  140. package/dist/recipes/index.d.ts +5 -0
  141. package/dist/recipes/index.d.ts.map +1 -1
  142. package/dist/recipes/index.js +4 -0
  143. package/dist/recipes/index.js.map +1 -1
  144. package/dist/review.d.ts.map +1 -1
  145. package/dist/review.js +9 -13
  146. package/dist/review.js.map +1 -1
  147. package/dist/rules/contract-visibility.d.ts.map +1 -1
  148. package/dist/rules/contract-visibility.js +3 -9
  149. package/dist/rules/contract-visibility.js.map +1 -1
  150. package/dist/rules/data-guard.d.ts.map +1 -1
  151. package/dist/rules/data-guard.js +3 -10
  152. package/dist/rules/data-guard.js.map +1 -1
  153. package/dist/rules/pages.d.ts.map +1 -1
  154. package/dist/rules/pages.js +41 -58
  155. package/dist/rules/pages.js.map +1 -1
  156. package/dist/rules/routes.d.ts.map +1 -1
  157. package/dist/rules/routes.js +20 -27
  158. package/dist/rules/routes.js.map +1 -1
  159. package/dist/rules/security.d.ts.map +1 -1
  160. package/dist/rules/security.js +206 -313
  161. package/dist/rules/security.js.map +1 -1
  162. package/dist/rules/zones.d.ts.map +1 -1
  163. package/dist/rules/zones.js +3 -10
  164. package/dist/rules/zones.js.map +1 -1
  165. package/dist/runtime-differences.d.ts +62 -0
  166. package/dist/runtime-differences.d.ts.map +1 -0
  167. package/dist/runtime-differences.js +222 -0
  168. package/dist/runtime-differences.js.map +1 -0
  169. package/dist/sdk.d.ts.map +1 -1
  170. package/dist/sdk.js +43 -43
  171. package/dist/sdk.js.map +1 -1
  172. package/dist/smoke.d.ts.map +1 -1
  173. package/dist/smoke.js +4 -12
  174. package/dist/smoke.js.map +1 -1
  175. package/dist/source-rewrites.d.ts +27 -0
  176. package/dist/source-rewrites.d.ts.map +1 -0
  177. package/dist/source-rewrites.js +402 -0
  178. package/dist/source-rewrites.js.map +1 -0
  179. package/dist/stylex-migrate.d.ts.map +1 -1
  180. package/dist/stylex-migrate.js +1 -1
  181. package/dist/stylex-migrate.js.map +1 -1
  182. package/dist/symptom-search.d.ts +5 -0
  183. package/dist/symptom-search.d.ts.map +1 -0
  184. package/dist/symptom-search.js +21 -0
  185. package/dist/symptom-search.js.map +1 -0
  186. package/dist/upgrade.d.ts +28 -3
  187. package/dist/upgrade.d.ts.map +1 -1
  188. package/dist/upgrade.js +153 -64
  189. package/dist/upgrade.js.map +1 -1
  190. package/dist/verification-plan.d.ts.map +1 -1
  191. package/dist/verification-plan.js +5 -3
  192. package/dist/verification-plan.js.map +1 -1
  193. package/docs/examples.json +32 -2
  194. package/docs/llms-full.txt +749 -95
  195. package/docs/types.json +300 -61
  196. package/package.json +11 -11
  197. package/src/agent-files.ts +4 -1
  198. package/src/assure-hydration.ts +2 -9
  199. package/src/assure.ts +3 -4
  200. package/src/check-scan.ts +65 -82
  201. package/src/check.ts +25 -23
  202. package/src/cli.ts +61 -13
  203. package/src/command-catalog.ts +22 -32
  204. package/src/config-target.ts +1 -1
  205. package/src/db-child.ts +3 -10
  206. package/src/db-config.ts +4 -0
  207. package/src/doctor.ts +128 -12
  208. package/src/frontend-guidance.ts +344 -25
  209. package/src/hand-built-manifest.ts +1 -2
  210. package/src/internal/control-character.ts +8 -0
  211. package/src/internal/escape-regexp.ts +4 -0
  212. package/src/internal/line-at.ts +4 -0
  213. package/src/internal/nifra-project.ts +39 -35
  214. package/src/internal/posix-path.ts +4 -0
  215. package/src/internal/project-specifiers.ts +3 -10
  216. package/src/internal/realpath-or-self.ts +10 -0
  217. package/src/internal/record.ts +4 -0
  218. package/src/internal/source-facts.ts +39 -29
  219. package/src/introspect.ts +16 -156
  220. package/src/levels-tool.ts +71 -78
  221. package/src/load.ts +6 -1
  222. package/src/local-cli.ts +12 -0
  223. package/src/mcp-docs-tools.ts +51 -3
  224. package/src/mcp-http.ts +2 -2
  225. package/src/mcp-io.ts +77 -0
  226. package/src/mcp-isolate.ts +2 -3
  227. package/src/mcp-project-child.ts +5 -14
  228. package/src/mcp-render.ts +11 -59
  229. package/src/mcp-root.ts +1 -1
  230. package/src/mcp-run.ts +5 -54
  231. package/src/mcp-ws.ts +2 -19
  232. package/src/migrate-layout.ts +52 -72
  233. package/src/output-schemas.ts +7 -8
  234. package/src/port.ts +1 -4
  235. package/src/ppr-read-fix.ts +2 -2
  236. package/src/recipes/5.8.0.ts +21 -0
  237. package/src/recipes/5.9.0.ts +42 -0
  238. package/src/recipes/index.ts +9 -0
  239. package/src/review.ts +12 -15
  240. package/src/rules/contract-visibility.ts +3 -8
  241. package/src/rules/data-guard.ts +3 -10
  242. package/src/rules/pages.ts +47 -58
  243. package/src/rules/routes.ts +21 -27
  244. package/src/rules/security.ts +224 -255
  245. package/src/rules/zones.ts +3 -10
  246. package/src/runtime-differences.ts +302 -0
  247. package/src/sdk.ts +55 -58
  248. package/src/smoke.ts +4 -13
  249. package/src/source-rewrites.ts +463 -0
  250. package/src/stylex-migrate.ts +1 -3
  251. package/src/symptom-search.ts +27 -0
  252. package/src/upgrade.ts +194 -74
  253. package/src/verification-plan.ts +10 -4
@@ -9,7 +9,7 @@ import {
9
9
  commentBlockHasMarker,
10
10
  MISSING_EXPECTATION_REASON,
11
11
  } from "./comment-markers.ts"
12
- import type { CheckRule, SourceIndex } from "./index.ts"
12
+ import type { CheckRule, RuleContext, SourceIndex } from "./index.ts"
13
13
  import { loadRuleTypeScript } from "./typescript.ts"
14
14
 
15
15
  /**
@@ -158,6 +158,51 @@ function isPresenceComparison(ts: typeof TSApi, node: TSApi.BinaryExpression): b
158
158
  return false
159
159
  }
160
160
 
161
+ /** A source file a scan reads: its path, tree and lines. */
162
+ interface ScannedFile {
163
+ readonly file: string
164
+ readonly tree: TSApi.SourceFile
165
+ readonly lines: readonly string[]
166
+ }
167
+
168
+ /**
169
+ * Runs `onFile` on every parsed source file, or reports that the scan could not run because TypeScript
170
+ * is missing.
171
+ */
172
+ async function scanFiles(
173
+ ctx: RuleContext,
174
+ code: string,
175
+ title: string,
176
+ onFile: (ts: typeof TSApi, source: ScannedFile, findings: Diagnostic[]) => void,
177
+ ): Promise<readonly Diagnostic[]> {
178
+ const loaded = await loadRuleTypeScript(ctx)
179
+ if (loaded.diagnostics !== undefined) return loaded.diagnostics
180
+ const ts = loaded.compiler
181
+ if (ts === undefined) return didNotRun(code, title)
182
+ const findings: Diagnostic[] = []
183
+ for (const file of ctx.project.source.files) {
184
+ const parsed = parsedFile(ts, ctx.project.source, file)
185
+ if (parsed !== undefined) onFile(ts, { file, ...parsed }, findings)
186
+ }
187
+ return findings
188
+ }
189
+
190
+ /** {@link scanFiles} over every node of each file, children included. */
191
+ function scanNodes(
192
+ ctx: RuleContext,
193
+ code: string,
194
+ title: string,
195
+ onNode: (ts: typeof TSApi, node: TSApi.Node, source: ScannedFile, findings: Diagnostic[]) => void,
196
+ ): Promise<readonly Diagnostic[]> {
197
+ return scanFiles(ctx, code, title, (ts, source, findings) => {
198
+ const visit = (node: TSApi.Node): void => {
199
+ onNode(ts, node, source, findings)
200
+ ts.forEachChild(node, visit)
201
+ }
202
+ visit(source.tree)
203
+ })
204
+ }
205
+
161
206
  function parse(ts: typeof TSApi, file: string, source: string): TSApi.SourceFile {
162
207
  return ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true, scriptKindOf(ts, file))
163
208
  }
@@ -249,6 +294,21 @@ function bindsName(ts: typeof TSApi, binding: TSApi.BindingName, name: string):
249
294
  )
250
295
  }
251
296
 
297
+ /** Whether `node` is a loop or a catch clause that declares `name` for its body. */
298
+ function loopOrCatchBinds(ts: typeof TSApi, node: TSApi.Node, name: string): boolean {
299
+ if (ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node))
300
+ return (
301
+ node.initializer !== undefined &&
302
+ ts.isVariableDeclarationList(node.initializer) &&
303
+ node.initializer.declarations.some((d) => bindsName(ts, d.name, name))
304
+ )
305
+ return (
306
+ ts.isCatchClause(node) &&
307
+ node.variableDeclaration !== undefined &&
308
+ bindsName(ts, node.variableDeclaration.name, name)
309
+ )
310
+ }
311
+
252
312
  /**
253
313
  * Whether `id` names a plain parameter of an enclosing function, not a value the file builds: the
254
314
  * nearest declaration of the name, walking out, must be that parameter. A loop or catch variable, a
@@ -265,19 +325,7 @@ function isParameterRef(ts: typeof TSApi, id: TSApi.Identifier): boolean {
265
325
  }
266
326
  if ((ts.isBlock(node) || ts.isSourceFile(node)) && declaresIn(ts, node.statements, id.text))
267
327
  return false
268
- if (
269
- (ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node)) &&
270
- node.initializer !== undefined &&
271
- ts.isVariableDeclarationList(node.initializer) &&
272
- node.initializer.declarations.some((d) => bindsName(ts, d.name, id.text))
273
- )
274
- return false
275
- if (
276
- ts.isCatchClause(node) &&
277
- node.variableDeclaration !== undefined &&
278
- bindsName(ts, node.variableDeclaration.name, id.text)
279
- )
280
- return false
328
+ if (loopOrCatchBinds(ts, node, id.text)) return false
281
329
  }
282
330
  return false
283
331
  }
@@ -330,19 +378,7 @@ function constInitializer(ts: typeof TSApi, id: TSApi.Identifier): TSApi.Express
330
378
  for (let node: TSApi.Node | undefined = id.parent; node !== undefined; node = node.parent) {
331
379
  if (ts.isFunctionLike(node) && node.parameters.some((p) => bindsName(ts, p.name, id.text)))
332
380
  return undefined
333
- if (
334
- (ts.isForStatement(node) || ts.isForOfStatement(node) || ts.isForInStatement(node)) &&
335
- node.initializer !== undefined &&
336
- ts.isVariableDeclarationList(node.initializer) &&
337
- node.initializer.declarations.some((d) => bindsName(ts, d.name, id.text))
338
- )
339
- return undefined
340
- if (
341
- ts.isCatchClause(node) &&
342
- node.variableDeclaration !== undefined &&
343
- bindsName(ts, node.variableDeclaration.name, id.text)
344
- )
345
- return undefined
381
+ if (loopOrCatchBinds(ts, node, id.text)) return undefined
346
382
  if (!ts.isBlock(node) && !ts.isSourceFile(node)) continue
347
383
  for (const statement of node.statements) {
348
384
  if (!ts.isVariableStatement(statement)) continue
@@ -491,69 +527,60 @@ export function rewriteSecretComparisons(
491
527
  export const secretComparisonRule: CheckRule = {
492
528
  code: "NF-S002",
493
529
  title: "Non-constant-time secret comparison",
494
- async scan(ctx) {
495
- const loaded = await loadRuleTypeScript(ctx)
496
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
497
- const ts = loaded.compiler
498
- if (ts === undefined) return didNotRun("NF-S002", "Non-constant-time secret comparison scan")
499
- const findings: Diagnostic[] = []
530
+ scan(ctx) {
500
531
  const webDirs = createWebAppProbe(ctx.root)
501
- for (const file of ctx.project.source.files) {
502
- const parsed = parsedFile(ts, ctx.project.source, file)
503
- if (parsed === undefined) continue
504
- const { tree, lines } = parsed
505
- const browser = provenBrowserFile(ctx.root, file, webDirs)
506
- for (const { left, right, line } of secretComparisons(ts, tree, browser)) {
507
- const reviewed = expectedReason(lines, line, "NF-S002") !== undefined
508
- findings.push(
509
- diagnostic({
510
- code: "NF-S002",
511
- severity: reviewed ? "info" : browser ? "warn" : "error",
512
- file,
513
- line,
514
- message: reviewed
515
- ? "secret comparison is expected here"
516
- : unexpectedMessage(
517
- "secret-like values must use a length check and timing-safe comparison",
518
- lines,
519
- line,
520
- "NF-S002",
521
- ),
522
- evidence: [
523
- left ?? right ?? "secret comparison",
524
- ...expectationEvidence(lines, line, "NF-S002"),
525
- ],
526
- ...(reviewed
527
- ? {}
528
- : {
529
- fix: {
530
- recipe: "security.timing-safe-equal",
531
- command: "nifra fix --code NF-S002",
532
- },
533
- }),
534
- verify: "nifra check --lints-only",
535
- }),
536
- )
537
- }
538
- }
539
- return findings
532
+ return scanFiles(
533
+ ctx,
534
+ "NF-S002",
535
+ "Non-constant-time secret comparison scan",
536
+ (ts, { file, tree, lines }, findings) => {
537
+ const browser = provenBrowserFile(ctx.root, file, webDirs)
538
+ for (const { left, right, line } of secretComparisons(ts, tree, browser)) {
539
+ const reviewed = expectedReason(lines, line, "NF-S002") !== undefined
540
+ findings.push(
541
+ diagnostic({
542
+ code: "NF-S002",
543
+ severity: reviewed ? "info" : browser ? "warn" : "error",
544
+ file,
545
+ line,
546
+ message: reviewed
547
+ ? "secret comparison is expected here"
548
+ : unexpectedMessage(
549
+ "secret-like values must use a length check and timing-safe comparison",
550
+ lines,
551
+ line,
552
+ "NF-S002",
553
+ ),
554
+ evidence: [
555
+ left ?? right ?? "secret comparison",
556
+ ...expectationEvidence(lines, line, "NF-S002"),
557
+ ],
558
+ ...(reviewed
559
+ ? {}
560
+ : {
561
+ fix: {
562
+ recipe: "security.timing-safe-equal",
563
+ command: "nifra fix --code NF-S002",
564
+ },
565
+ }),
566
+ verify: "nifra check --lints-only",
567
+ }),
568
+ )
569
+ }
570
+ },
571
+ )
540
572
  },
541
573
  }
542
574
 
543
575
  export const piiLogRule: CheckRule = {
544
576
  code: "NF-S003",
545
577
  title: "Sensitive value in log call",
546
- async scan(ctx) {
547
- const loaded = await loadRuleTypeScript(ctx)
548
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
549
- const ts = loaded.compiler
550
- if (ts === undefined) return didNotRun("NF-S003", "Sensitive-value-in-log scan")
551
- const findings: Diagnostic[] = []
552
- for (const file of ctx.project.source.files) {
553
- const parsed = parsedFile(ts, ctx.project.source, file)
554
- if (parsed === undefined) continue
555
- const { tree, lines } = parsed
556
- const visit = (node: TSApi.Node): void => {
578
+ scan: (ctx) =>
579
+ scanNodes(
580
+ ctx,
581
+ "NF-S003",
582
+ "Sensitive-value-in-log scan",
583
+ (ts, node, { file, tree, lines }, findings) => {
557
584
  if (ts.isCallExpression(node)) {
558
585
  const expression = node.expression
559
586
  const callee = ts.isPropertyAccessExpression(expression)
@@ -587,12 +614,8 @@ export const piiLogRule: CheckRule = {
587
614
  }
588
615
  }
589
616
  }
590
- ts.forEachChild(node, visit)
591
- }
592
- visit(tree)
593
- }
594
- return findings
595
- },
617
+ },
618
+ ),
596
619
  }
597
620
 
598
621
  /**
@@ -720,59 +743,55 @@ function isDenial(ts: typeof TSApi, expression: TSApi.Expression, authenticator:
720
743
  export const failOpenGateRule: CheckRule = {
721
744
  code: "NF-S001",
722
745
  title: "Fail-open gate",
723
- async scan(ctx) {
724
- const loaded = await loadRuleTypeScript(ctx)
725
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
726
- const ts = loaded.compiler
727
- if (ts === undefined) return didNotRun("NF-S001", "Fail-open gate scan")
728
- const findings: Diagnostic[] = []
746
+ scan(ctx) {
729
747
  const webDirs = createWebAppProbe(ctx.root)
730
- for (const file of ctx.project.source.files) {
731
- const parsed = parsedFile(ts, ctx.project.source, file)
732
- if (parsed === undefined) continue
733
- const { tree, lines } = parsed
734
- let browser: boolean | undefined
735
- const visit = (node: TSApi.Node): void => {
736
- if (ts.isCatchClause(node)) {
737
- let parent: TSApi.Node | undefined = node.parent
738
- while (parent !== undefined && !ts.isFunctionLike(parent)) parent = parent.parent
739
- const name = parent === undefined ? undefined : functionName(ts, parent)
740
- if (name !== undefined && GATE_NAMES.some((gate) => gate.test(name))) {
741
- if (!failsClosed(ts, node.block, name)) {
742
- const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
743
- const reviewed = expectedReason(lines, line, "NF-S001") !== undefined
744
- browser ??= provenBrowserFile(ctx.root, file, webDirs)
745
- findings.push(
746
- diagnostic({
747
- code: "NF-S001",
748
- severity: reviewed ? "info" : browser ? "warn" : "error",
749
- file,
750
- line,
751
- message: reviewed
752
- ? "gate catch block is expected here"
753
- : unexpectedMessage(
754
- "gate catch blocks must rethrow or return an explicit denial",
755
- lines,
756
- line,
757
- "NF-S001",
758
- ),
759
- evidence: [
760
- name,
761
- "catch block has no denial or rethrow",
762
- ...(browser ? ["browser code: the server's own gate decides"] : []),
763
- ...expectationEvidence(lines, line, "NF-S001"),
764
- ],
765
- verify: "nifra check --lints-only",
766
- }),
767
- )
748
+ return scanFiles(
749
+ ctx,
750
+ "NF-S001",
751
+ "Fail-open gate scan",
752
+ (ts, { file, tree, lines }, findings) => {
753
+ let browser: boolean | undefined
754
+ const visit = (node: TSApi.Node): void => {
755
+ if (ts.isCatchClause(node)) {
756
+ let parent: TSApi.Node | undefined = node.parent
757
+ while (parent !== undefined && !ts.isFunctionLike(parent)) parent = parent.parent
758
+ const name = parent === undefined ? undefined : functionName(ts, parent)
759
+ if (name !== undefined && GATE_NAMES.some((gate) => gate.test(name))) {
760
+ if (!failsClosed(ts, node.block, name)) {
761
+ const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
762
+ const reviewed = expectedReason(lines, line, "NF-S001") !== undefined
763
+ browser ??= provenBrowserFile(ctx.root, file, webDirs)
764
+ findings.push(
765
+ diagnostic({
766
+ code: "NF-S001",
767
+ severity: reviewed ? "info" : browser ? "warn" : "error",
768
+ file,
769
+ line,
770
+ message: reviewed
771
+ ? "gate catch block is expected here"
772
+ : unexpectedMessage(
773
+ "gate catch blocks must rethrow or return an explicit denial",
774
+ lines,
775
+ line,
776
+ "NF-S001",
777
+ ),
778
+ evidence: [
779
+ name,
780
+ "catch block has no denial or rethrow",
781
+ ...(browser ? ["browser code: the server's own gate decides"] : []),
782
+ ...expectationEvidence(lines, line, "NF-S001"),
783
+ ],
784
+ verify: "nifra check --lints-only",
785
+ }),
786
+ )
787
+ }
768
788
  }
769
789
  }
790
+ ts.forEachChild(node, visit)
770
791
  }
771
- ts.forEachChild(node, visit)
772
- }
773
- visit(tree)
774
- }
775
- return findings
792
+ visit(tree)
793
+ },
794
+ )
776
795
  },
777
796
  }
778
797
 
@@ -794,17 +813,12 @@ function usesParam(ts: typeof TSApi, body: TSApi.Node, param: string): boolean {
794
813
  export const corsOriginPredicateRule: CheckRule = {
795
814
  code: "NF-S004",
796
815
  title: "CORS origin predicate ignores the origin",
797
- async scan(ctx) {
798
- const loaded = await loadRuleTypeScript(ctx)
799
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
800
- const ts = loaded.compiler
801
- if (ts === undefined) return didNotRun("NF-S004", "CORS origin predicate scan")
802
- const findings: Diagnostic[] = []
803
- for (const file of ctx.project.source.files) {
804
- const parsed = parsedFile(ts, ctx.project.source, file)
805
- if (parsed === undefined) continue
806
- const { tree, lines } = parsed
807
- const visit = (node: TSApi.Node): void => {
816
+ scan: (ctx) =>
817
+ scanNodes(
818
+ ctx,
819
+ "NF-S004",
820
+ "CORS origin predicate scan",
821
+ (ts, node, { file, tree, lines }, findings) => {
808
822
  if (ts.isCallExpression(node) && nameOf(ts, node.expression) === "cors") {
809
823
  const arg = node.arguments[0]
810
824
  if (arg !== undefined && ts.isObjectLiteralExpression(arg)) {
@@ -854,28 +868,19 @@ export const corsOriginPredicateRule: CheckRule = {
854
868
  }
855
869
  }
856
870
  }
857
- ts.forEachChild(node, visit)
858
- }
859
- visit(tree)
860
- }
861
- return findings
862
- },
871
+ },
872
+ ),
863
873
  }
864
874
 
865
875
  export const externalRedirectRule: CheckRule = {
866
876
  code: "NF-S005",
867
877
  title: "External redirect opt-out",
868
- async scan(ctx) {
869
- const loaded = await loadRuleTypeScript(ctx)
870
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
871
- const ts = loaded.compiler
872
- if (ts === undefined) return didNotRun("NF-S005", "External redirect opt-out scan")
873
- const findings: Diagnostic[] = []
874
- for (const file of ctx.project.source.files) {
875
- const parsed = parsedFile(ts, ctx.project.source, file)
876
- if (parsed === undefined) continue
877
- const { tree, lines } = parsed
878
- const visit = (node: TSApi.Node): void => {
878
+ scan: (ctx) =>
879
+ scanNodes(
880
+ ctx,
881
+ "NF-S005",
882
+ "External redirect opt-out scan",
883
+ (ts, node, { file, tree, lines }, findings) => {
879
884
  if (ts.isCallExpression(node) && nameOf(ts, node.expression) === "redirect") {
880
885
  for (const arg of node.arguments) {
881
886
  if (!ts.isObjectLiteralExpression(arg)) continue
@@ -909,12 +914,8 @@ export const externalRedirectRule: CheckRule = {
909
914
  }
910
915
  }
911
916
  }
912
- ts.forEachChild(node, visit)
913
- }
914
- visit(tree)
915
- }
916
- return findings
917
- },
917
+ },
918
+ ),
918
919
  }
919
920
 
920
921
  const ESCAPE_HATCHES: Readonly<Record<string, string>> = {
@@ -929,17 +930,12 @@ const ESCAPE_HATCHES: Readonly<Record<string, string>> = {
929
930
  export const assuranceEscapeHatchRule: CheckRule = {
930
931
  code: "NF-S006",
931
932
  title: "Security escape hatch enabled",
932
- async scan(ctx) {
933
- const loaded = await loadRuleTypeScript(ctx)
934
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
935
- const ts = loaded.compiler
936
- if (ts === undefined) return didNotRun("NF-S006", "Security escape hatch scan")
937
- const findings: Diagnostic[] = []
938
- for (const file of ctx.project.source.files) {
939
- const parsed = parsedFile(ts, ctx.project.source, file)
940
- if (parsed === undefined) continue
941
- const { tree, lines } = parsed
942
- const visit = (node: TSApi.Node): void => {
933
+ scan: (ctx) =>
934
+ scanNodes(
935
+ ctx,
936
+ "NF-S006",
937
+ "Security escape hatch scan",
938
+ (ts, node, { file, tree, lines }, findings) => {
943
939
  if (ts.isPropertyAssignment(node) && node.initializer.kind === ts.SyntaxKind.TrueKeyword) {
944
940
  const name = nameOf(ts, node.name)
945
941
  const consequence = name !== undefined ? ESCAPE_HATCHES[name] : undefined
@@ -961,12 +957,8 @@ export const assuranceEscapeHatchRule: CheckRule = {
961
957
  )
962
958
  }
963
959
  }
964
- ts.forEachChild(node, visit)
965
- }
966
- visit(tree)
967
- }
968
- return findings
969
- },
960
+ },
961
+ ),
970
962
  }
971
963
 
972
964
  const COOKIE_PREFIX = /^__(?:Host|Secure)-/
@@ -974,59 +966,45 @@ const COOKIE_PREFIX = /^__(?:Host|Secure)-/
974
966
  export const unprefixedSecureCookieRule: CheckRule = {
975
967
  code: "NF-S007",
976
968
  title: "Secure cookie without a __Host-/__Secure- prefix",
977
- async scan(ctx) {
978
- const loaded = await loadRuleTypeScript(ctx)
979
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
980
- const ts = loaded.compiler
981
- if (ts === undefined) return didNotRun("NF-S007", "Secure cookie prefix scan")
982
- const findings: Diagnostic[] = []
983
- for (const file of ctx.project.source.files) {
984
- const parsed = parsedFile(ts, ctx.project.source, file)
985
- if (parsed === undefined) continue
986
- const { tree } = parsed
987
- const visit = (node: TSApi.Node): void => {
988
- if (ts.isCallExpression(node)) {
989
- const callee = nameOf(ts, node.expression)
990
- if (callee === "cookie" || callee === "serializeCookie") {
991
- const nameArg = node.arguments[0]
992
- const hasPrefix =
993
- nameArg !== undefined &&
994
- ts.isStringLiteralLike(nameArg) &&
995
- COOKIE_PREFIX.test(nameArg.text)
996
- const secure = node.arguments.some(
997
- (arg) =>
998
- ts.isObjectLiteralExpression(arg) &&
999
- arg.properties.some(
1000
- (prop) =>
1001
- ts.isPropertyAssignment(prop) &&
1002
- nameOf(ts, prop.name) === "secure" &&
1003
- prop.initializer.kind === ts.SyntaxKind.TrueKeyword,
1004
- ),
969
+ scan: (ctx) =>
970
+ scanNodes(ctx, "NF-S007", "Secure cookie prefix scan", (ts, node, { file, tree }, findings) => {
971
+ if (ts.isCallExpression(node)) {
972
+ const callee = nameOf(ts, node.expression)
973
+ if (callee === "cookie" || callee === "serializeCookie") {
974
+ const nameArg = node.arguments[0]
975
+ const hasPrefix =
976
+ nameArg !== undefined &&
977
+ ts.isStringLiteralLike(nameArg) &&
978
+ COOKIE_PREFIX.test(nameArg.text)
979
+ const secure = node.arguments.some(
980
+ (arg) =>
981
+ ts.isObjectLiteralExpression(arg) &&
982
+ arg.properties.some(
983
+ (prop) =>
984
+ ts.isPropertyAssignment(prop) &&
985
+ nameOf(ts, prop.name) === "secure" &&
986
+ prop.initializer.kind === ts.SyntaxKind.TrueKeyword,
987
+ ),
988
+ )
989
+ // Only literal names are judged - a dynamic name can't be checked for a prefix.
990
+ if (secure && !hasPrefix && nameArg !== undefined && ts.isStringLiteralLike(nameArg)) {
991
+ const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
992
+ findings.push(
993
+ diagnostic({
994
+ code: "NF-S007",
995
+ severity: "info",
996
+ file,
997
+ line,
998
+ message:
999
+ "Secure cookie without a __Host-/__Secure- prefix - a prefix stops subdomains and insecure contexts from shadowing it",
1000
+ evidence: [nameArg.text],
1001
+ verify: "nifra check --lints-only",
1002
+ }),
1005
1003
  )
1006
- // Only literal names are judged - a dynamic name can't be checked for a prefix.
1007
- if (secure && !hasPrefix && nameArg !== undefined && ts.isStringLiteralLike(nameArg)) {
1008
- const line = tree.getLineAndCharacterOfPosition(node.getStart(tree)).line + 1
1009
- findings.push(
1010
- diagnostic({
1011
- code: "NF-S007",
1012
- severity: "info",
1013
- file,
1014
- line,
1015
- message:
1016
- "Secure cookie without a __Host-/__Secure- prefix - a prefix stops subdomains and insecure contexts from shadowing it",
1017
- evidence: [nameArg.text],
1018
- verify: "nifra check --lints-only",
1019
- }),
1020
- )
1021
- }
1022
1004
  }
1023
1005
  }
1024
- ts.forEachChild(node, visit)
1025
1006
  }
1026
- visit(tree)
1027
- }
1028
- return findings
1029
- },
1007
+ }),
1030
1008
  }
1031
1009
 
1032
1010
  /** Reads the backend the contract-lock check loaded; this rule runs no project code of its own. */
@@ -1089,17 +1067,12 @@ function callsJsonParse(ts: typeof TSApi, node: TSApi.Node): boolean {
1089
1067
  export const unvalidatedWsFrameRule: CheckRule = {
1090
1068
  code: "NF-S009",
1091
1069
  title: "WebSocket frames parsed without a messageSchema",
1092
- async scan(ctx) {
1093
- const loaded = await loadRuleTypeScript(ctx)
1094
- if (loaded.diagnostics !== undefined) return loaded.diagnostics
1095
- const ts = loaded.compiler
1096
- if (ts === undefined) return didNotRun("NF-S009", "WebSocket messageSchema scan")
1097
- const findings: Diagnostic[] = []
1098
- for (const file of ctx.project.source.files) {
1099
- const parsed = parsedFile(ts, ctx.project.source, file)
1100
- if (parsed === undefined) continue
1101
- const { tree, lines } = parsed
1102
- const visit = (node: TSApi.Node): void => {
1070
+ scan: (ctx) =>
1071
+ scanNodes(
1072
+ ctx,
1073
+ "NF-S009",
1074
+ "WebSocket messageSchema scan",
1075
+ (ts, node, { file, tree, lines }, findings) => {
1103
1076
  if (
1104
1077
  ts.isCallExpression(node) &&
1105
1078
  ts.isPropertyAccessExpression(node.expression) &&
@@ -1136,12 +1109,8 @@ export const unvalidatedWsFrameRule: CheckRule = {
1136
1109
  }
1137
1110
  }
1138
1111
  }
1139
- ts.forEachChild(node, visit)
1140
- }
1141
- visit(tree)
1142
- }
1143
- return findings
1144
- },
1112
+ },
1113
+ ),
1145
1114
  }
1146
1115
 
1147
1116
  export const securityRules = Object.freeze([
@@ -7,7 +7,7 @@
7
7
  * file importing one in no zone, backend code importing frontend code, and shared code importing
8
8
  * anything but shared code. NF-C029 owns private environment reads in browser code.
9
9
  */
10
- import { existsSync, readFileSync, realpathSync } from "node:fs"
10
+ import { existsSync, readFileSync } from "node:fs"
11
11
  import { isAbsolute, join, relative } from "node:path"
12
12
  import {
13
13
  browserDenial,
@@ -37,6 +37,7 @@ import {
37
37
  withApiKindHint,
38
38
  } from "../internal/nifra-project.ts"
39
39
  import { createSpecifierResolver } from "../internal/project-specifiers.ts"
40
+ import { realpathOrSelf } from "../internal/realpath-or-self.ts"
40
41
  import { createSourceFacts } from "../internal/source-facts.ts"
41
42
  import type { CheckRule, RuleContext } from "./index.ts"
42
43
  import { loadRuleTypeScript } from "./typescript.ts"
@@ -147,18 +148,10 @@ export function zonedFiles(ctx: RuleContext): {
147
148
  return { files, classifierFor, configErrors }
148
149
  }
149
150
 
150
- const real = (path: string): string => {
151
- try {
152
- return realpathSync(path)
153
- } catch {
154
- return path
155
- }
156
- }
157
-
158
151
  /** Where a resolved import lands, as the project names it. Both sides are real paths: a checkout
159
152
  * reached through a symlink (`/var` -> `/private/var` on macOS) still names files project-relative. */
160
153
  function display(root: string, file: string): string {
161
- const rel = relative(real(root), real(file)).replaceAll("\\", "/")
154
+ const rel = relative(realpathOrSelf(root), realpathOrSelf(file)).replaceAll("\\", "/")
162
155
  return rel.startsWith("../") || isAbsolute(rel) ? file : rel
163
156
  }
164
157