tslaws 0.10.0 → 1.0.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 (122) hide show
  1. package/README.md +7 -2
  2. package/bin/tslaws.js +8 -2
  3. package/dist/browser.d.ts +1 -1
  4. package/dist/browser.js +25 -5
  5. package/dist/bundled.js +2 -2
  6. package/dist/bundlerPlugins.d.ts +2 -0
  7. package/dist/bundlerPlugins.js +7 -0
  8. package/dist/certificates.d.ts +139 -0
  9. package/dist/certificates.js +433 -0
  10. package/dist/chc.d.ts +85 -0
  11. package/dist/chc.js +298 -0
  12. package/dist/check.d.ts +13 -1
  13. package/dist/check.js +211 -27
  14. package/dist/cli.d.ts +2 -0
  15. package/dist/cli.js +175 -34
  16. package/dist/compile.d.ts +15 -5
  17. package/dist/compile.js +198 -69
  18. package/dist/coverage.d.ts +38 -1
  19. package/dist/coverage.js +94 -12
  20. package/dist/e2eApi.js +3 -1
  21. package/dist/generators.js +12 -2
  22. package/dist/github.d.ts +49 -5
  23. package/dist/github.js +107 -24
  24. package/dist/githubCli.d.ts +28 -0
  25. package/dist/githubCli.js +1039 -211
  26. package/dist/hints.js +2 -1
  27. package/dist/init.js +42 -0
  28. package/dist/isolate.d.ts +28 -0
  29. package/dist/isolate.js +262 -0
  30. package/dist/isolatedCheck.d.ts +1 -0
  31. package/dist/isolatedCheck.js +16 -0
  32. package/dist/lawHost.d.ts +50 -0
  33. package/dist/lawHost.js +199 -0
  34. package/dist/lawRuntime.d.ts +39 -0
  35. package/dist/lawRuntime.js +242 -0
  36. package/dist/lock.d.ts +49 -9
  37. package/dist/lock.js +132 -21
  38. package/dist/mcp.js +28 -7
  39. package/dist/mutate.d.ts +4 -0
  40. package/dist/mutate.js +7 -1
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +4 -0
  43. package/dist/pluginHooks.d.ts +16 -0
  44. package/dist/pluginHooks.js +48 -0
  45. package/dist/pluginHost.d.ts +5 -0
  46. package/dist/pluginHost.js +43 -0
  47. package/dist/pluginWorker.d.ts +1 -0
  48. package/dist/pluginWorker.js +40 -0
  49. package/dist/plural.d.ts +2 -0
  50. package/dist/plural.js +4 -0
  51. package/dist/pool.d.ts +80 -7
  52. package/dist/pool.js +222 -11
  53. package/dist/projects.d.ts +43 -1
  54. package/dist/projects.js +283 -11
  55. package/dist/promote.d.ts +56 -2
  56. package/dist/promote.js +429 -13
  57. package/dist/property.js +5 -1
  58. package/dist/propertyWorker.js +7 -0
  59. package/dist/proposals.d.ts +52 -4
  60. package/dist/proposals.js +484 -21
  61. package/dist/reporters.js +5 -1
  62. package/dist/runtimeEnv.d.ts +13 -5
  63. package/dist/runtimeEnv.js +48 -10
  64. package/dist/sandbox.d.ts +98 -0
  65. package/dist/sandbox.js +673 -0
  66. package/dist/sandboxGuard.d.ts +56 -0
  67. package/dist/sandboxGuard.js +249 -0
  68. package/dist/seal.d.ts +9 -0
  69. package/dist/seal.js +426 -0
  70. package/dist/security/cli.d.ts +6 -0
  71. package/dist/security/cli.js +39 -0
  72. package/dist/security/cwe.d.ts +14 -0
  73. package/dist/security/cwe.js +40 -0
  74. package/dist/security/deps/cache.d.ts +28 -0
  75. package/dist/security/deps/cache.js +107 -0
  76. package/dist/security/deps/config.d.ts +16 -0
  77. package/dist/security/deps/config.js +43 -0
  78. package/dist/security/deps/imports.d.ts +4 -0
  79. package/dist/security/deps/imports.js +33 -0
  80. package/dist/security/deps/lockfiles.d.ts +23 -0
  81. package/dist/security/deps/lockfiles.js +153 -0
  82. package/dist/security/deps/osv.d.ts +66 -0
  83. package/dist/security/deps/osv.js +137 -0
  84. package/dist/security/deps/run.d.ts +54 -0
  85. package/dist/security/deps/run.js +135 -0
  86. package/dist/security/deps/severity.d.ts +26 -0
  87. package/dist/security/deps/severity.js +78 -0
  88. package/dist/security/generators.d.ts +9 -0
  89. package/dist/security/generators.js +36 -0
  90. package/dist/security/index.d.ts +27 -0
  91. package/dist/security/index.js +18 -0
  92. package/dist/security/integers.d.ts +31 -0
  93. package/dist/security/integers.js +105 -0
  94. package/dist/security/law.d.ts +54 -0
  95. package/dist/security/law.js +54 -0
  96. package/dist/security/pathTraversal.d.ts +29 -0
  97. package/dist/security/pathTraversal.js +146 -0
  98. package/dist/security/prototypePollution.d.ts +30 -0
  99. package/dist/security/prototypePollution.js +142 -0
  100. package/dist/security/redirect.d.ts +21 -0
  101. package/dist/security/redirect.js +154 -0
  102. package/dist/security/redos.d.ts +21 -0
  103. package/dist/security/redos.js +93 -0
  104. package/dist/security/validation.d.ts +25 -0
  105. package/dist/security/validation.js +89 -0
  106. package/dist/security/xss.d.ts +21 -0
  107. package/dist/security/xss.js +90 -0
  108. package/dist/settings.d.ts +20 -0
  109. package/dist/settings.js +32 -0
  110. package/dist/smt.d.ts +29 -1
  111. package/dist/smt.js +239 -107
  112. package/dist/smtWorker.d.ts +1 -0
  113. package/dist/smtWorker.js +26 -0
  114. package/dist/solvers.d.ts +9 -0
  115. package/dist/solvers.js +11 -5
  116. package/dist/symbolic.d.ts +54 -1
  117. package/dist/symbolic.js +239 -13
  118. package/dist/tsconfigResolve.d.ts +28 -0
  119. package/dist/tsconfigResolve.js +141 -0
  120. package/dist/types.d.ts +28 -0
  121. package/hooks/claude-hook.mjs +2 -0
  122. package/package.json +18 -6
@@ -0,0 +1,141 @@
1
+ // The module resolution a package's own tsconfig describes (`paths`, `baseUrl`, `rootDirs`, ...), so laws can import
2
+ // the way the package's code does. Only resolution options are taken: tslaws picks the law files and decides what and
3
+ // how to emit, so `include`, `files`, `outDir`, `noEmit`, `module` and `moduleResolution` are never read from it.
4
+ import { existsSync as fsExists, statSync } from 'node:fs';
5
+ import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
6
+ import ts from 'typescript';
7
+ import { readConfig } from "./coverage.js";
8
+ const slash = (path) => path.replaceAll(String.fromCharCode(92), '/');
9
+ /** Options copied as they are. `allowImportingTsExtensions` is handled apart: it cannot be combined with emit. */
10
+ const copied = [
11
+ 'customConditions',
12
+ 'resolveJsonModule',
13
+ 'allowArbitraryExtensions',
14
+ 'moduleSuffixes',
15
+ 'resolvePackageJsonExports',
16
+ 'resolvePackageJsonImports',
17
+ ];
18
+ /** Whether `path` exists. A path the sandbox may not read (the directories above the project) is answered with no. */
19
+ function existsSync(path) {
20
+ try {
21
+ return fsExists(path);
22
+ }
23
+ catch {
24
+ return false;
25
+ }
26
+ }
27
+ /** `entry` made absolute against `base`, forward slashes, a trailing slash and any `*` kept. */
28
+ function absolute(entry, base) {
29
+ const full = slash(resolve(base, entry));
30
+ return /[\\/]$/.test(entry) && !full.endsWith('/') ? `${full}/` : full;
31
+ }
32
+ /**
33
+ * `paths` with every target absolute. A `baseUrl` is folded in rather than passed on: targets are relative to it when it
34
+ * is set, and the base itself becomes the final `*` fallback, which is what `baseUrl` means. The TypeScript 7 compiler
35
+ * has no `baseUrl` at all.
36
+ */
37
+ function foldPaths(paths, baseUrl, pathsBase) {
38
+ if (paths === undefined && baseUrl === undefined)
39
+ return undefined;
40
+ const base = baseUrl ?? pathsBase;
41
+ const out = {};
42
+ for (const [pattern, targets] of Object.entries(paths ?? {}))
43
+ out[pattern] = targets.map((target) => absolute(target, base));
44
+ if (baseUrl !== undefined)
45
+ out['*'] = [...(out['*'] ?? []), `${slash(baseUrl)}/*`];
46
+ return out;
47
+ }
48
+ /** The nearest `tsconfig.json` in `dir` or above it. */
49
+ export function nearestTsconfig(dir) {
50
+ for (let current = resolve(dir);; current = dirname(current)) {
51
+ const candidate = join(current, 'tsconfig.json');
52
+ if (existsSync(candidate))
53
+ return candidate;
54
+ if (dirname(current) === current)
55
+ return undefined;
56
+ }
57
+ }
58
+ /** The resolution options of `file` and everything it extends (relative files and packages in node_modules). */
59
+ export function readResolutionOf(file, strict) {
60
+ const read = ts.readConfigFile(file, ts.sys.readFile);
61
+ const message = (diagnostic) => ts.flattenDiagnosticMessageText(diagnostic.messageText, ' ');
62
+ if (read.error !== undefined) {
63
+ if (strict)
64
+ throw new Error(`tslaws.json "tsconfig": ${slash(file)}: ${message(read.error)}`);
65
+ return {};
66
+ }
67
+ const parsed = ts.parseJsonConfigFileContent(read.config, {
68
+ useCaseSensitiveFileNames: ts.sys.useCaseSensitiveFileNames,
69
+ // Tslaws picks the files itself; never walk the tree for `include`.
70
+ readDirectory: () => [],
71
+ fileExists: ts.sys.fileExists,
72
+ readFile: ts.sys.readFile,
73
+ }, dirname(file), undefined, file);
74
+ // A missing `extends` target (6053) or an unreadable one (5083) is the only problem that changes what resolves.
75
+ const broken = parsed.errors.find((diagnostic) => diagnostic.code === 6053 || diagnostic.code === 5083);
76
+ if (broken !== undefined && strict)
77
+ throw new Error(`tslaws.json "tsconfig": ${slash(file)}: ${message(broken)}`);
78
+ const found = parsed.options;
79
+ const out = {};
80
+ for (const key of copied)
81
+ if (found[key] !== undefined)
82
+ out[key] = found[key];
83
+ // Emit is on, so the extension-importing option becomes the one that rewrites `.ts` specifiers in the output.
84
+ if (found.allowImportingTsExtensions === true || found.rewriteRelativeImportExtensions === true)
85
+ out.rewriteRelativeImportExtensions = true;
86
+ if (found.rootDirs !== undefined)
87
+ out.rootDirs = found.rootDirs.map((dir) => slash(dir));
88
+ const paths = foldPaths(found.paths, found.baseUrl, found.pathsBasePath ?? dirname(file));
89
+ if (paths !== undefined)
90
+ out.paths = paths;
91
+ return out;
92
+ }
93
+ /**
94
+ * The tsconfig a project's laws are compiled with: `"tsconfig": "<path>"` in tslaws.json, else the nearest tsconfig.json
95
+ * above the project; none for `"tsconfig": false` or when there is no file. An explicit path that is missing or broken is
96
+ * an error; a discovered file that cannot be read is skipped.
97
+ */
98
+ export function readResolution(cwd) {
99
+ const setting = readConfig(cwd).tsconfig;
100
+ if (setting === false)
101
+ return undefined;
102
+ let file;
103
+ if (setting === undefined)
104
+ file = nearestTsconfig(cwd);
105
+ else {
106
+ const given = resolve(cwd, setting);
107
+ file = existsSync(given) && statSync(given).isDirectory() ? join(given, 'tsconfig.json') : given;
108
+ if (!existsSync(file))
109
+ throw new Error(`tslaws.json "tsconfig": no such file: ${slash(relative(cwd, file))}`);
110
+ }
111
+ if (file === undefined)
112
+ return undefined;
113
+ const options = readResolutionOf(file, setting !== undefined);
114
+ return Object.keys(options).length === 0 ? undefined : { file, options };
115
+ }
116
+ /** The same options as compiler options for the API (every value is plain JSON, so nothing needs converting). */
117
+ export function resolutionCompilerOptions(options) {
118
+ return { ...options };
119
+ }
120
+ /** Whether `file` lies inside `dir`. */
121
+ export function isInside(dir, file) {
122
+ const rel = relative(dir, file);
123
+ return rel !== '' && !rel.startsWith('..') && !isAbsolute(rel);
124
+ }
125
+ /**
126
+ * The directory the compiler's `rootDir` must be so that every source file a `paths` target can reach lies under it: the
127
+ * deepest common ancestor of the project and the directories the targets name (up to the first `*`). Targets on another
128
+ * drive are left out; the project itself when no target leaves it.
129
+ */
130
+ export function commonRoot(cwd, options) {
131
+ let root = resolve(cwd);
132
+ const paths = (options?.paths ?? {});
133
+ for (const target of Object.values(paths).flat()) {
134
+ const fixed = target.split('*')[0] ?? '';
135
+ const dir = /[\\/]$/.test(fixed) ? fixed : dirname(fixed);
136
+ // Another drive has no path from here: nothing to widen to.
137
+ while (!isAbsolute(relative(root, dir)) && relative(root, dir).startsWith('..') && dirname(root) !== root)
138
+ root = dirname(root);
139
+ }
140
+ return root;
141
+ }
package/dist/types.d.ts CHANGED
@@ -11,8 +11,14 @@ export interface ManifestLaw {
11
11
  body: string;
12
12
  /** The declaration from the law's name to its end. */
13
13
  text: string;
14
+ /**
15
+ * What the law depends on besides `text`, as a digest: the helpers and top-level code of its file that it reaches, the
16
+ * project modules those import, and the settings it runs under. Set by `readManifest` (see seal.ts); the lock hash covers it.
17
+ */
18
+ seal?: string;
14
19
  problems: string[];
15
20
  }
21
+ import type { Certificate, CertifyMode } from './certificates.ts';
16
22
  import type { Exercised } from './exercises.ts';
17
23
  export interface LawResult {
18
24
  file: string;
@@ -27,6 +33,8 @@ export interface LawResult {
27
33
  message?: string;
28
34
  /** The domain assumptions a `proved` or `bounded` result rests on, for the parameter types of this law only. */
29
35
  assumptions?: string[];
36
+ /** Set when certifying was on and Z3 proved the law (`proved` or `bounded`): whether a checker accepted a proof of it. */
37
+ certificate?: Certificate;
30
38
  /** Project-relative path of a screenshot of the page at the counterexample (browser laws). */
31
39
  screenshot?: string;
32
40
  /** The project functions the law reaches and where they are declared, for an editor to mark on both ends. */
@@ -46,9 +54,16 @@ export interface ProposalOutcome {
46
54
  method: string;
47
55
  counterexample?: string;
48
56
  message?: string;
57
+ /** The certificate of the proof, for `tslaws accept` to record with the law. */
58
+ certificate?: Certificate;
49
59
  }
50
60
  export interface CheckOptions {
51
61
  files: string[];
62
+ /**
63
+ * `files` is part of the project, named by the person: the lock is compared only for the laws of those files, so a
64
+ * locked law in any other file is neither removed nor checked. Unset, `files` is every laws file of the project.
65
+ */
66
+ partial?: boolean;
52
67
  cwd: string;
53
68
  runs: number;
54
69
  seed?: number;
@@ -57,6 +72,8 @@ export interface CheckOptions {
57
72
  lockFile: string;
58
73
  /** Skip the Z3 prover. */
59
74
  noProve: boolean;
75
+ /** Check each proof with cvc5 and Carcara; `"require"` makes a proof without a certificate an error. See certificates.ts. */
76
+ certify?: CertifyMode;
60
77
  /** Longest array, most loop iterations and deepest recursion a proof considers. */
61
78
  bound: number;
62
79
  proofTimeoutMs: number;
@@ -68,6 +85,17 @@ export interface CheckOptions {
68
85
  browser?: 'chromium' | 'firefox' | 'webkit';
69
86
  /** Flags the person gave on the command line, which beat `tslaws.json`: `runs`, `property-timeout`. */
70
87
  explicit?: ('runs' | 'property-timeout')[];
88
+ /**
89
+ * Run node laws in a child process under Node's permission model (see sandbox.ts). Default true; `false` is
90
+ * `--no-sandbox`, which lets laws and the helpers they load do anything this process can.
91
+ */
92
+ sandbox?: boolean;
93
+ /**
94
+ * Let end-to-end laws run. They drive a browser and start the app's server, which needs processes the sandbox refuses,
95
+ * so they run in this process with its full permissions: with the sandbox on they are refused unless this is set
96
+ * (`--allow-e2e`). Set it only for laws you wrote or reviewed. Ignored when `sandbox` is false.
97
+ */
98
+ allowE2e?: boolean;
71
99
  /** Skip the regression corpus: nothing is replayed and nothing is saved. */
72
100
  noRegressions?: boolean;
73
101
  /** Stored counterexamples to replay before the random search, as JSON text from `encodeExample`. Set by `check`. */
@@ -5,6 +5,8 @@
5
5
  // Exit status 2 with a message on stderr is how Claude Code feeds the reason back to the agent.
6
6
  import { spawnSync } from 'node:child_process';
7
7
  import { readFileSync } from 'node:fs';
8
+ // createRequire is only a resolver here: Node has no other synchronous way to find a package from the project's
9
+ // directory (`import.meta.resolve` starts from this file). No CommonJS code is loaded.
8
10
  import { createRequire } from 'node:module';
9
11
  import { dirname, join } from 'node:path';
10
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tslaws",
3
- "version": "0.10.0",
3
+ "version": "1.0.0",
4
4
  "description": "Runs and checks `law` declarations compiled by tsc-laws: property tests, SMT proofs, tamper lock.",
5
5
  "bin": {
6
6
  "tslaws": "bin/tslaws.js"
@@ -17,30 +17,42 @@
17
17
  "exports": {
18
18
  ".": {
19
19
  "types": "./dist/api.d.ts",
20
+ "import": "./dist/api.js",
20
21
  "default": "./dist/api.js"
21
22
  },
22
23
  "./browser": {
23
24
  "types": "./dist/browserApi.d.ts",
25
+ "import": "./dist/browserApi.js",
24
26
  "default": "./dist/browserApi.js"
25
27
  },
26
28
  "./e2e": {
27
29
  "types": "./dist/e2eApi.d.ts",
30
+ "import": "./dist/e2eApi.js",
28
31
  "default": "./dist/e2eApi.js"
29
32
  },
33
+ "./security": {
34
+ "types": "./dist/security/index.d.ts",
35
+ "import": "./dist/security/index.js",
36
+ "default": "./dist/security/index.js"
37
+ },
30
38
  "./adapters/react": {
31
39
  "types": "./dist/adapters/react.d.ts",
40
+ "import": "./dist/adapters/react.js",
32
41
  "default": "./dist/adapters/react.js"
33
42
  },
34
43
  "./adapters/angular": {
35
44
  "types": "./dist/adapters/angular.d.ts",
45
+ "import": "./dist/adapters/angular.js",
36
46
  "default": "./dist/adapters/angular.js"
37
47
  },
38
48
  "./adapters/dom": {
39
49
  "types": "./dist/adapters/dom.d.ts",
50
+ "import": "./dist/adapters/dom.js",
40
51
  "default": "./dist/adapters/dom.js"
41
52
  },
42
53
  "./adapters/element": {
43
54
  "types": "./dist/adapters/element.d.ts",
55
+ "import": "./dist/adapters/element.js",
44
56
  "default": "./dist/adapters/element.js"
45
57
  },
46
58
  "./env": {
@@ -77,11 +89,11 @@
77
89
  }
78
90
  },
79
91
  "optionalDependencies": {
80
- "tslaws-compiler-darwin-arm64": "0.10.0",
81
- "tslaws-compiler-darwin-x64": "0.10.0",
82
- "tslaws-compiler-linux-arm64-gnu": "0.10.0",
83
- "tslaws-compiler-linux-x64-gnu": "0.10.0",
84
- "tslaws-compiler-win32-x64-msvc": "0.10.0"
92
+ "tslaws-compiler-darwin-arm64": "1.0.0",
93
+ "tslaws-compiler-darwin-x64": "1.0.0",
94
+ "tslaws-compiler-linux-arm64-gnu": "1.0.0",
95
+ "tslaws-compiler-linux-x64-gnu": "1.0.0",
96
+ "tslaws-compiler-win32-x64-msvc": "1.0.0"
85
97
  },
86
98
  "publishConfig": {
87
99
  "access": "public"