tslaws 0.9.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 (123) hide show
  1. package/README.md +8 -2
  2. package/bin/tslaws.js +8 -2
  3. package/dist/browser.d.ts +1 -1
  4. package/dist/browser.js +36 -7
  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 +215 -28
  14. package/dist/cli.d.ts +4 -0
  15. package/dist/cli.js +324 -41
  16. package/dist/compile.d.ts +17 -1
  17. package/dist/compile.js +244 -25
  18. package/dist/coverage.d.ts +55 -3
  19. package/dist/coverage.js +231 -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 +70 -0
  54. package/dist/projects.js +457 -0
  55. package/dist/promote.d.ts +56 -2
  56. package/dist/promote.js +429 -13
  57. package/dist/property.js +13 -2
  58. package/dist/propertyWorker.js +11 -1
  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 +32 -0
  63. package/dist/runtimeEnv.js +146 -0
  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/env.d.ts +17 -0
  122. package/hooks/claude-hook.mjs +2 -0
  123. package/package.json +22 -6
@@ -0,0 +1,32 @@
1
+ /** The global that `import.meta.env` is rewritten to, in compiled laws, bundles and natively loaded TypeScript. */
2
+ export declare const importMetaEnvGlobal = "__tslawsImportMetaEnv";
3
+ /**
4
+ * What esbuild's `define` should make of `import.meta` in a module of `file` that loads as CommonJS, where `import.meta`
5
+ * does not exist: `env` is the runner's global, `url`, `filename` and `dirname` name the file, as they do in an ES module.
6
+ */
7
+ export declare function importMetaDefines(file: string): Record<string, string>;
8
+ /** The value of `import.meta.env` in every place a law runs. */
9
+ export type ImportMetaEnv = Record<string, string | boolean>;
10
+ /** The `env` setting of tslaws.json: a name to a string, or to a boolean for `DEV`, `PROD` and `SSR` (and only those). */
11
+ export declare function parseEnvConfig(raw: unknown): Record<string, string | boolean>;
12
+ /**
13
+ * The environment of a law: every `process.env` entry in Node, but in a page only those that start with `VITE_` (a page
14
+ * runs project and dependency code that could send what it reads, so credentials in the process stay out), then `MODE`
15
+ * (`test`), `DEV` (true), `PROD` (false), `SSR` (true in Node, false in a page) and `BASE_URL` (`/`), then the `env` of
16
+ * tslaws.json, which overrides all of it. `MODE` and `BASE_URL` keep the value `process.env` gives them.
17
+ */
18
+ export declare function importMetaEnvOf(configured: Record<string, string | boolean> | undefined, surface: 'node' | 'browser', processEnv?: NodeJS.ProcessEnv): ImportMetaEnv;
19
+ /** Sets `import.meta.env` for everything loaded afterwards in this thread. */
20
+ export declare function setImportMetaEnv(env: ImportMetaEnv): void;
21
+ /**
22
+ * Teaches Node's own loading of TypeScript two things a bundler does: a relative import without an extension
23
+ * (`export * from './database'`) finds the `.ts` file, and `import.meta.env` reads the environment of the laws. Both
24
+ * apply to project TypeScript only, never to anything under `node_modules`. A third hook resolves a package whose
25
+ * `exports` offer only an `import` condition when a law (compiled to CommonJS) requires it, as an ES import would; the
26
+ * original error stands when that fails too. Needs `module.registerHooks`
27
+ * (Node 22.15, 23.5 or later); on an older Node the hooks are skipped and such imports fail as they did before.
28
+ * Returns whether the hooks are in place. Registers once per thread.
29
+ */
30
+ export declare function installLoaderHooks(): boolean;
31
+ /** Everything a law needs before its module loads: the hooks and the environment. */
32
+ export declare function prepareRuntime(env: ImportMetaEnv): void;
@@ -0,0 +1,146 @@
1
+ // What laws need at run time to load project code written for a bundler: `import.meta.env`, and relative imports without
2
+ // an extension in TypeScript files that Node loads itself. Nothing here loads the TypeScript compiler, so a property worker
3
+ // can import it.
4
+ import nodeModule from 'node:module';
5
+ import { dirname } from 'node:path';
6
+ import { fileURLToPath, pathToFileURL } from 'node:url';
7
+ /** The global that `import.meta.env` is rewritten to, in compiled laws, bundles and natively loaded TypeScript. */
8
+ export const importMetaEnvGlobal = '__tslawsImportMetaEnv';
9
+ /**
10
+ * What esbuild's `define` should make of `import.meta` in a module of `file` that loads as CommonJS, where `import.meta`
11
+ * does not exist: `env` is the runner's global, `url`, `filename` and `dirname` name the file, as they do in an ES module.
12
+ */
13
+ export function importMetaDefines(file) {
14
+ const meta = { url: pathToFileURL(file).href, filename: file, dirname: dirname(file) };
15
+ return {
16
+ 'import.meta': JSON.stringify(meta),
17
+ 'import.meta.url': JSON.stringify(meta.url),
18
+ 'import.meta.filename': JSON.stringify(meta.filename),
19
+ 'import.meta.dirname': JSON.stringify(meta.dirname),
20
+ 'import.meta.env': `globalThis.${importMetaEnvGlobal}`,
21
+ };
22
+ }
23
+ /** The keys that are booleans, as in Vite; every other name is a string. */
24
+ const booleanKeys = new Set(['DEV', 'PROD', 'SSR']);
25
+ /** The `env` setting of tslaws.json: a name to a string, or to a boolean for `DEV`, `PROD` and `SSR` (and only those). */
26
+ export function parseEnvConfig(raw) {
27
+ if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
28
+ throw new Error(`tslaws.json: "env" must be an object of names to strings, got ${JSON.stringify(raw)}`);
29
+ for (const [key, value] of Object.entries(raw)) {
30
+ const want = booleanKeys.has(key) ? 'boolean' : 'string';
31
+ if (typeof value !== want)
32
+ throw new Error(`tslaws.json: "env.${key}" must be a ${want}, got ${JSON.stringify(value)}`);
33
+ }
34
+ return raw;
35
+ }
36
+ /** The prefix of the `process.env` entries a page may see: Vite's convention for values that are safe to ship. */
37
+ const publicPrefix = 'VITE_';
38
+ /**
39
+ * The environment of a law: every `process.env` entry in Node, but in a page only those that start with `VITE_` (a page
40
+ * runs project and dependency code that could send what it reads, so credentials in the process stay out), then `MODE`
41
+ * (`test`), `DEV` (true), `PROD` (false), `SSR` (true in Node, false in a page) and `BASE_URL` (`/`), then the `env` of
42
+ * tslaws.json, which overrides all of it. `MODE` and `BASE_URL` keep the value `process.env` gives them.
43
+ */
44
+ export function importMetaEnvOf(configured, surface, processEnv = process.env) {
45
+ const env = {};
46
+ for (const [key, value] of Object.entries(processEnv))
47
+ if (value !== undefined && (surface === 'node' || key.startsWith(publicPrefix)))
48
+ env[key] = value;
49
+ return {
50
+ ...env,
51
+ MODE: processEnv.MODE ?? 'test',
52
+ DEV: true,
53
+ PROD: false,
54
+ SSR: surface === 'node',
55
+ BASE_URL: processEnv.BASE_URL ?? '/',
56
+ ...configured,
57
+ };
58
+ }
59
+ /** Sets `import.meta.env` for everything loaded afterwards in this thread. */
60
+ export function setImportMetaEnv(env) {
61
+ globalThis[importMetaEnvGlobal] = env;
62
+ }
63
+ const sourceFile = /\.[cm]?tsx?$/;
64
+ const unresolved = ['.ts', '.tsx', '.mts', '.js', '/index.ts', '/index.tsx'];
65
+ const javascriptEnding = /\.[cm]?jsx?$/;
66
+ const slash = (path) => path.replaceAll(String.fromCharCode(92), '/');
67
+ /** Whether `url` is project TypeScript: a TypeScript file that is not inside a dependency. */
68
+ function isProjectSource(url) {
69
+ if (url === undefined || !url.startsWith('file:'))
70
+ return false;
71
+ const path = slash(fileURLToPath(url));
72
+ return sourceFile.test(path) && !path.includes('/node_modules/');
73
+ }
74
+ let installed;
75
+ /**
76
+ * Teaches Node's own loading of TypeScript two things a bundler does: a relative import without an extension
77
+ * (`export * from './database'`) finds the `.ts` file, and `import.meta.env` reads the environment of the laws. Both
78
+ * apply to project TypeScript only, never to anything under `node_modules`. A third hook resolves a package whose
79
+ * `exports` offer only an `import` condition when a law (compiled to CommonJS) requires it, as an ES import would; the
80
+ * original error stands when that fails too. Needs `module.registerHooks`
81
+ * (Node 22.15, 23.5 or later); on an older Node the hooks are skipped and such imports fail as they did before.
82
+ * Returns whether the hooks are in place. Registers once per thread.
83
+ */
84
+ export function installLoaderHooks() {
85
+ if (installed !== undefined)
86
+ return installed;
87
+ installed = false;
88
+ const { registerHooks } = nodeModule;
89
+ if (typeof registerHooks !== 'function')
90
+ return installed;
91
+ registerHooks({
92
+ resolve(specifier, context, nextResolve) {
93
+ try {
94
+ return nextResolve(specifier, context);
95
+ }
96
+ catch (error) {
97
+ const { conditions } = context;
98
+ if (error.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' &&
99
+ conditions.includes('require') &&
100
+ !conditions.includes('import')) {
101
+ try {
102
+ return nextResolve(specifier, {
103
+ ...context,
104
+ conditions: conditions.map((condition) => (condition === 'require' ? 'import' : condition)),
105
+ });
106
+ }
107
+ catch {
108
+ throw error;
109
+ }
110
+ }
111
+ if (!specifier.startsWith('.') || !isProjectSource(context.parentURL))
112
+ throw error;
113
+ const stem = specifier.replace(javascriptEnding, '');
114
+ const candidates = unresolved.map((ending) => `${specifier}${ending}`);
115
+ // `./a.js` names a `.ts` file in TypeScript, as `./a` does for a bundler.
116
+ if (stem !== specifier)
117
+ candidates.push(`${stem}.ts`, `${stem}.tsx`, `${stem}.mts`);
118
+ for (const candidate of candidates) {
119
+ try {
120
+ return nextResolve(candidate, context);
121
+ }
122
+ catch {
123
+ // The next candidate.
124
+ }
125
+ }
126
+ throw error;
127
+ }
128
+ },
129
+ load(url, context, nextLoad) {
130
+ const loaded = nextLoad(url, context);
131
+ if (loaded.source === undefined || loaded.source === null || !isProjectSource(url))
132
+ return loaded;
133
+ const text = typeof loaded.source === 'string' ? loaded.source : new TextDecoder().decode(loaded.source);
134
+ if (!text.includes('import.meta.env'))
135
+ return loaded;
136
+ return { ...loaded, source: text.replace(/\bimport\.meta\.env\b/g, `globalThis.${importMetaEnvGlobal}`) };
137
+ },
138
+ });
139
+ installed = true;
140
+ return installed;
141
+ }
142
+ /** Everything a law needs before its module loads: the hooks and the environment. */
143
+ export function prepareRuntime(env) {
144
+ installLoaderHooks();
145
+ setImportMetaEnv(env);
146
+ }
@@ -0,0 +1,98 @@
1
+ import type { Prepared } from './lawHost.ts';
2
+ import type { PropertyOutcome } from './propertyRun.ts';
3
+ import { type GuardConfig } from './sandboxGuard.ts';
4
+ import type { Plan } from './plan.ts';
5
+ import type { CheckOptions, ManifestLaw } from './types.ts';
6
+ /** The permission flag this Node has, or undefined when it has none (before Node 20). */
7
+ export declare function permissionFlag(version?: string): string | undefined;
8
+ /** Whether the module guard can cover `import` as well as `require`: synchronous loader hooks came in 22.15 and 23.5. */
9
+ export declare function hasLoaderHooks(version?: string): boolean;
10
+ /**
11
+ * Whether laws run in the sandbox for these options. Fails closed: a Node that cannot enforce the permission model is an
12
+ * error, not a quiet fallback, and `sandbox: false` (`--no-sandbox`) is the explicit way out and says what it gives up.
13
+ */
14
+ export declare function useSandbox(options: Pick<CheckOptions, 'sandbox'>, version?: string): boolean;
15
+ /** Says once that end-to-end laws are running outside the sandbox because the person allowed it. */
16
+ export declare function warnE2e(): void;
17
+ /** The `--allow-fs-read` paths: the project, its `node_modules`, the tslaws package, and the trees workspace links lead to. */
18
+ export declare function readableBy(cwd: string, extra?: string[]): string[];
19
+ /** The flags that make a process a sandbox, for a project at `cwd`. */
20
+ export declare function sandboxFlags(cwd: string, extra?: string[]): string[];
21
+ /**
22
+ * The files a law may load besides the build: the entries (what was compiled) and everything the seals of the laws cover,
23
+ * which is what the lock digests (see seal.ts): each laws file, and every module it imports, transitively, including files
24
+ * outside the project and workspace sources. A path computed at run time is not among them, so the guard refuses it.
25
+ */
26
+ export declare function reviewedFiles(cwd: string, entries: string[], laws: ManifestLaw[]): string[];
27
+ /**
28
+ * The files of the reviewed set outside the project that the law process may read, by name. Only these are granted: the
29
+ * packages of the workspace (a `workspaces` field of the root `package.json`), the `paths`, `baseUrl` and `references`
30
+ * targets of the tsconfig, and the directories the person lists under `sandboxReads` in tslaws.json (the declared
31
+ * dependencies already sit in the `node_modules` trees). A dotfile, the home directory and a file system root are refused,
32
+ * as is any other file; each grant and each refusal is printed once.
33
+ */
34
+ export declare function grantOutside(cwd: string, outside: string[]): {
35
+ granted: string[];
36
+ refused: string[];
37
+ };
38
+ /**
39
+ * The packages the code may load: those the manifests declare (the project's `package.json` with its dev dependencies,
40
+ * and tslaws's own) and, from each one's manifest, what it declares in turn. Anything else in a `node_modules` was put
41
+ * there by something else, and is not trusted. A package that is a link to a directory outside `node_modules` (a workspace
42
+ * package) is source: its tree is trusted as a tree. The project itself is not a package; its files are reviewed one by one.
43
+ */
44
+ export declare function trustedPackages(cwd: string): {
45
+ modules: string[];
46
+ linked: string[];
47
+ };
48
+ /** Everything the process needs to know about the reviewed set, built once per check after the build. */
49
+ export interface HostConfig {
50
+ guard: GuardConfig;
51
+ transforms: Record<string, Prepared>;
52
+ /** Files outside the project root the process must be allowed to read. */
53
+ outside: string[];
54
+ /** Tells two configurations apart. */
55
+ key: string;
56
+ }
57
+ /**
58
+ * The reviewed set of a project after a build: the build's output, the files its laws reach, the packages, and the
59
+ * transformed text of what needs transforming. `outDir` is where the build wrote the output of `cwd`.
60
+ */
61
+ export declare function hostConfig(cwd: string, entries: string[], outDir: string, laws: ManifestLaw[]): HostConfig;
62
+ /** A law could not be loaded: the compiled module is missing, does not export the law, or loads something unreviewed. */
63
+ export declare class LawLoadError extends Error {
64
+ }
65
+ /**
66
+ * A property outcome as plain data, or an error. The process runs the law, so its answer is the law's claim and nothing
67
+ * more: each field is rebuilt from what is checked here, anything else is dropped, and a counterexample that the corpus
68
+ * could not read back is not kept.
69
+ */
70
+ export declare function readOutcome(value: unknown): PropertyOutcome;
71
+ /** The law processes alive now, across every project. */
72
+ export declare function liveSandboxProcesses(): number;
73
+ /** Ends the idle law processes of a project and of any project below it, for a scratch copy about to be deleted. */
74
+ export declare function releaseSandbox(cwd: string): void;
75
+ /** Starts `n` law processes ahead of the first law, so they load while the code compiles. */
76
+ export declare function warmSandbox(cwd: string, n: number): void;
77
+ /** What a sandboxed run of a law came to: the outcome and whether the law is async (the prover cannot model an await). */
78
+ export interface SandboxedRun {
79
+ outcome: PropertyOutcome;
80
+ async: boolean;
81
+ }
82
+ /** Runs laws and replays inputs in sandboxed processes for one project, under one reviewed set. */
83
+ export declare class Sandbox {
84
+ private readonly pool;
85
+ private readonly config;
86
+ private readonly cwd;
87
+ constructor(cwd: string, config: HostConfig);
88
+ /** Sends one request to a process of its own, and gives up on it after `timeoutMs`. */
89
+ private ask;
90
+ private askOnce;
91
+ /** The property test of `law`, `'timeout'` when it does not finish; throws `LawLoadError` when it cannot be loaded. */
92
+ run(outDir: string, law: ManifestLaw, options: CheckOptions, plans: Plan[]): Promise<SandboxedRun | 'timeout'>;
93
+ /**
94
+ * Whether the law holds on `inputs` (a model the prover found), as the prover's replay needs it. A law that hangs or
95
+ * cannot be loaded counts as holding: the model is then not confirmed, and nothing is reported as refuted.
96
+ */
97
+ holds(outDir: string, law: ManifestLaw, inputs: unknown[], timeoutMs: number): Promise<boolean>;
98
+ }