@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
@@ -9,6 +9,7 @@ import {
9
9
  commentBlockHasMarker,
10
10
  MISSING_EXPECTATION_REASON,
11
11
  } from "./comment-markers.ts"
12
+ import { createHostExposureRule } from "./host-exposure.ts"
12
13
  import type { CheckRule, RuleContext, SourceIndex } from "./index.ts"
13
14
  import { loadRuleTypeScript } from "./typescript.ts"
14
15
 
@@ -469,15 +470,48 @@ function timingSafeHelper(file: string): string {
469
470
  'import { timingSafeEqual as nifraTimingSafeEqualBytes } from "node:crypto"',
470
471
  "",
471
472
  typed
472
- ? "function nifraTimingSafeEqual(left: string, right: string): boolean {"
473
+ ? "function nifraTimingSafeEqual(left: unknown, right: unknown): boolean {"
473
474
  : "function nifraTimingSafeEqual(left, right) {",
474
- " const leftBytes = Buffer.from(left)",
475
- " const rightBytes = Buffer.from(right)",
476
- " return leftBytes.length === rightBytes.length && nifraTimingSafeEqualBytes(leftBytes, rightBytes)",
475
+ ' if (typeof left !== "string" || typeof right !== "string") return left === right',
476
+ " if (left.length !== right.length) return false",
477
+ " const leftUnits = new Uint16Array(left.length)",
478
+ " const rightUnits = new Uint16Array(right.length)",
479
+ " for (let index = 0; index < left.length; index++) {",
480
+ " leftUnits[index] = left.charCodeAt(index)",
481
+ " rightUnits[index] = right.charCodeAt(index)",
482
+ " }",
483
+ " return nifraTimingSafeEqualBytes(leftUnits, rightUnits)",
477
484
  "}",
478
485
  ].join("\n")
479
486
  }
480
487
 
488
+ function canRewriteSecretComparison(
489
+ ts: typeof TSApi,
490
+ tree: TSApi.SourceFile,
491
+ browser: boolean,
492
+ ): boolean {
493
+ const fileName = tree.fileName.replaceAll("\\", "/").toLowerCase()
494
+ return (
495
+ !browser &&
496
+ !`/${fileName}`.includes("/shared/") &&
497
+ !/\.shared\.[^./]+$/.test(fileName) &&
498
+ tree.statements.some(
499
+ (statement) =>
500
+ ts.isImportDeclaration(statement) &&
501
+ statement.importClause?.isTypeOnly !== true &&
502
+ ts.isStringLiteralLike(statement.moduleSpecifier) &&
503
+ statement.moduleSpecifier.text === "node:crypto",
504
+ )
505
+ )
506
+ }
507
+
508
+ function isStrictSecretComparison(ts: typeof TSApi, node: TSApi.BinaryExpression): boolean {
509
+ return (
510
+ node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsEqualsToken ||
511
+ node.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsEqualsToken
512
+ )
513
+ }
514
+
481
515
  /**
482
516
  * Rewrite every unreviewed NF-S002 comparison in one file to `nifraTimingSafeEqual(left, right)` and
483
517
  * add the helper once. Sites come from the syntax tree, not a reported line: the helper lands above
@@ -491,9 +525,12 @@ export function rewriteSecretComparisons(
491
525
  browser: boolean,
492
526
  ): string {
493
527
  const tree = parse(ts, file, source)
528
+ if (!canRewriteSecretComparison(ts, tree, browser)) return source
494
529
  const lines = tree.text.split("\n")
495
530
  const sites = secretComparisons(ts, tree, browser).filter(
496
- (site) => expectedReason(lines, site.line, "NF-S002") === undefined,
531
+ (site) =>
532
+ isStrictSecretComparison(ts, site.node) &&
533
+ expectedReason(lines, site.line, "NF-S002") === undefined,
497
534
  )
498
535
  // A comparison nested inside another (`(token === a) === flag`) is left for the next run, so every
499
536
  // edit below covers a disjoint range and applying them from the end keeps earlier offsets valid.
@@ -535,7 +572,8 @@ export const secretComparisonRule: CheckRule = {
535
572
  "Non-constant-time secret comparison scan",
536
573
  (ts, { file, tree, lines }, findings) => {
537
574
  const browser = provenBrowserFile(ctx.root, file, webDirs)
538
- for (const { left, right, line } of secretComparisons(ts, tree, browser)) {
575
+ const canRewrite = canRewriteSecretComparison(ts, tree, browser)
576
+ for (const { node, left, right, line } of secretComparisons(ts, tree, browser)) {
539
577
  const reviewed = expectedReason(lines, line, "NF-S002") !== undefined
540
578
  findings.push(
541
579
  diagnostic({
@@ -555,7 +593,7 @@ export const secretComparisonRule: CheckRule = {
555
593
  left ?? right ?? "secret comparison",
556
594
  ...expectationEvidence(lines, line, "NF-S002"),
557
595
  ],
558
- ...(reviewed
596
+ ...(reviewed || !canRewrite || !isStrictSecretComparison(ts, node)
559
597
  ? {}
560
598
  : {
561
599
  fix: {
@@ -1113,6 +1151,8 @@ export const unvalidatedWsFrameRule: CheckRule = {
1113
1151
  ),
1114
1152
  }
1115
1153
 
1154
+ export const hostExposureRule = createHostExposureRule(parsedFile)
1155
+
1116
1156
  export const securityRules = Object.freeze([
1117
1157
  failOpenGateRule,
1118
1158
  secretComparisonRule,
@@ -1123,4 +1163,5 @@ export const securityRules = Object.freeze([
1123
1163
  unprefixedSecureCookieRule,
1124
1164
  inputBeforeAuthenticationRule,
1125
1165
  unvalidatedWsFrameRule,
1166
+ hostExposureRule,
1126
1167
  ])
@@ -65,12 +65,12 @@ export const RUNTIME_DIFFERENCES: readonly RuntimeEntry[] = [
65
65
  },
66
66
  {
67
67
  id: "host-builds-url",
68
- runtimes: ["deno", "bun"],
68
+ runtimes: ["deno", "bun", "node"],
69
69
  symptom:
70
70
  "`c.req.url` points at a path the client did not ask for, or is a relative string, after a request with an odd `Host` header such as `Host: h.test/admin#`.",
71
71
  cause:
72
- "With `Host: h.test/admin#` and no options set, Deno builds `Request.url` from the Host as received and routes `/admin`, Bun leaves `c.req.url` relative (`/url`), and `@nifrajs/node` answers `400 Bad Request`.",
73
- fix: "Set `validateHost: true` so Nifra answers `400 invalid_host` for a Host the request target cannot safely be built on. Off by default: reading the Host header measured about 6% of a bare route on Bun.",
72
+ "With `Host: h.test/admin#` and no options set, Deno builds `Request.url` from the Host as received and routes `/admin`, Bun leaves `c.req.url` relative (`/url`), and `@nifrajs/node` and `@nifrajs/node-uws` answer `400 Bad Request` on their ordinary request paths (a fixed route that `@nifrajs/node-uws` registers with uWebSockets directly is answered before that check runs).",
73
+ fix: 'On Bun and Deno, set `validateHost: true` (`server({ validateHost: true })`) so Nifra answers `400 invalid_host` for a Host the request target cannot safely be built on. Off by default: reading the Host header measured about 6% of a bare route on Bun. The Node adapters already refuse these Hosts on their ordinary request paths. Behind a proxy that checks Host, leave it off and say so with `"hostTopology": "proxied"` in nifra.check.json: `nifra check` (NF-S010) warns about a listener that terminates TLS itself with no Host check.',
74
74
  verify:
75
75
  "`printf 'GET /url HTTP/1.1\\r\\nhost: h.test/admin#\\r\\n\\r\\n' | nc 127.0.0.1 <port>` against a route that echoes `c.req.url`.",
76
76
  seeAlso: "validateHost invalid_host Host header",
package/src/scaffold.ts CHANGED
@@ -20,7 +20,7 @@ export type Framework = "react" | "preact" | "solid" | "vue" | "svelte" | "vanil
20
20
  /** A scaffold flavour on top of the framework. `default` is the plain page stub; `stateful` only
21
21
  * applies to vanilla and emits a reactive island (`bindIsland` state + `data-bind-*` markup + keyed
22
22
  * `bindList`) instead of the static island stub - the small-app lane whose binding names, binding
23
- * hosts and list keys NF-C047/C048/C022 check statically. */
23
+ * hosts and list keys NF-C047/C048/C022/C064/C065/C066 check statically. */
24
24
  export type ScaffoldVariant = "default" | "stateful"
25
25
 
26
26
  const EXT: Record<Framework, string> = {
@@ -274,7 +274,7 @@ export default function Page({ data }: Route.ComponentProps) {
274
274
  // mountIslands({ todos })
275
275
  //
276
276
  // Cross-island coordination: one createIslandBus() closed over in each enhancer. nifra_docs("islands")
277
- // has the full cookbook; \`nifra check\` runs NF-C047/C048/C022 over the page and client you write.
277
+ // has the full cookbook; \`nifra check\` runs NF-C047/C048/C022/C064/C065/C066 over the page and client you write.
278
278
  `
279
279
  }
280
280
 
@@ -315,7 +315,7 @@ export function scaffoldRoute(
315
315
  file,
316
316
  content: statefulStub(file),
317
317
  backend,
318
- note: `Create ${file} and ${backend.file} as a zero-runtime @nifrajs/web-vanilla route with a reactive island. ${ROUTE_CONTRACT}\nThe stub embeds the island client (bindIsland state + data-bind-* markup + keyed bindList); write it as the companion .client.ts. \`nifra check\` runs NF-C047/C048/C022 over the page and client; nifra_docs("islands") is the full cookbook.`,
318
+ note: `Create ${file} and ${backend.file} as a zero-runtime @nifrajs/web-vanilla route with a reactive island. ${ROUTE_CONTRACT}\nThe stub embeds the island client (bindIsland state + data-bind-* markup + keyed bindList); write it as the companion .client.ts. \`nifra check\` runs NF-C047/C048/C022/C064/C065/C066 over the page and client; nifra_docs("islands") is the full cookbook.`,
319
319
  }
320
320
  }
321
321
  if (framework === "react" || framework === "preact" || framework === "solid") {
@@ -6,6 +6,7 @@
6
6
  import { isSameOriginPath } from "@nifrajs/core/pattern"
7
7
  import type * as TS from "typescript"
8
8
  import { codeUnitOrder } from "./internal/code-unit-order.ts"
9
+ import { importedAs } from "./internal/imported-as.ts"
9
10
  import {
10
11
  applyEdits,
11
12
  type Edit,
@@ -449,21 +450,7 @@ function rewritePprOption(ts: typeof TS, file: string, text: string): RewriteRes
449
450
  const manual: ManualSite[] = []
450
451
  let count = 0
451
452
  // The names createWebApp is imported under, and the names @nifrajs/web is imported as a namespace.
452
- const locals = new Set<string>()
453
- const namespaces = new Set<string>()
454
- for (const statement of tree.statements) {
455
- if (!ts.isImportDeclaration(statement) || !ts.isStringLiteral(statement.moduleSpecifier))
456
- continue
457
- if (statement.moduleSpecifier.text !== WEB) continue
458
- const bindings = statement.importClause?.namedBindings
459
- if (bindings === undefined) continue
460
- if (ts.isNamespaceImport(bindings)) namespaces.add(bindings.name.text)
461
- else {
462
- for (const element of bindings.elements) {
463
- if ((element.propertyName ?? element.name).text === WEB_APP) locals.add(element.name.text)
464
- }
465
- }
466
- }
453
+ const { locals, namespaces } = importedAs(ts, tree, (from) => from === WEB, WEB_APP)
467
454
  const isCreateWebApp = (callee: TS.Expression): boolean =>
468
455
  (ts.isIdentifier(callee) && locals.has(callee.text)) ||
469
456
  (ts.isPropertyAccessExpression(callee) &&