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.
- package/README.md +7 -2
- package/bin/tslaws.js +8 -2
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +25 -5
- package/dist/bundled.js +2 -2
- package/dist/bundlerPlugins.d.ts +2 -0
- package/dist/bundlerPlugins.js +7 -0
- package/dist/certificates.d.ts +139 -0
- package/dist/certificates.js +433 -0
- package/dist/chc.d.ts +85 -0
- package/dist/chc.js +298 -0
- package/dist/check.d.ts +13 -1
- package/dist/check.js +211 -27
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +175 -34
- package/dist/compile.d.ts +15 -5
- package/dist/compile.js +198 -69
- package/dist/coverage.d.ts +38 -1
- package/dist/coverage.js +94 -12
- package/dist/e2eApi.js +3 -1
- package/dist/generators.js +12 -2
- package/dist/github.d.ts +49 -5
- package/dist/github.js +107 -24
- package/dist/githubCli.d.ts +28 -0
- package/dist/githubCli.js +1039 -211
- package/dist/hints.js +2 -1
- package/dist/init.js +42 -0
- package/dist/isolate.d.ts +28 -0
- package/dist/isolate.js +262 -0
- package/dist/isolatedCheck.d.ts +1 -0
- package/dist/isolatedCheck.js +16 -0
- package/dist/lawHost.d.ts +50 -0
- package/dist/lawHost.js +199 -0
- package/dist/lawRuntime.d.ts +39 -0
- package/dist/lawRuntime.js +242 -0
- package/dist/lock.d.ts +49 -9
- package/dist/lock.js +132 -21
- package/dist/mcp.js +28 -7
- package/dist/mutate.d.ts +4 -0
- package/dist/mutate.js +7 -1
- package/dist/plan.d.ts +1 -0
- package/dist/plan.js +4 -0
- package/dist/pluginHooks.d.ts +16 -0
- package/dist/pluginHooks.js +48 -0
- package/dist/pluginHost.d.ts +5 -0
- package/dist/pluginHost.js +43 -0
- package/dist/pluginWorker.d.ts +1 -0
- package/dist/pluginWorker.js +40 -0
- package/dist/plural.d.ts +2 -0
- package/dist/plural.js +4 -0
- package/dist/pool.d.ts +80 -7
- package/dist/pool.js +222 -11
- package/dist/projects.d.ts +43 -1
- package/dist/projects.js +283 -11
- package/dist/promote.d.ts +56 -2
- package/dist/promote.js +429 -13
- package/dist/property.js +5 -1
- package/dist/propertyWorker.js +7 -0
- package/dist/proposals.d.ts +52 -4
- package/dist/proposals.js +484 -21
- package/dist/reporters.js +5 -1
- package/dist/runtimeEnv.d.ts +13 -5
- package/dist/runtimeEnv.js +48 -10
- package/dist/sandbox.d.ts +98 -0
- package/dist/sandbox.js +673 -0
- package/dist/sandboxGuard.d.ts +56 -0
- package/dist/sandboxGuard.js +249 -0
- package/dist/seal.d.ts +9 -0
- package/dist/seal.js +426 -0
- package/dist/security/cli.d.ts +6 -0
- package/dist/security/cli.js +39 -0
- package/dist/security/cwe.d.ts +14 -0
- package/dist/security/cwe.js +40 -0
- package/dist/security/deps/cache.d.ts +28 -0
- package/dist/security/deps/cache.js +107 -0
- package/dist/security/deps/config.d.ts +16 -0
- package/dist/security/deps/config.js +43 -0
- package/dist/security/deps/imports.d.ts +4 -0
- package/dist/security/deps/imports.js +33 -0
- package/dist/security/deps/lockfiles.d.ts +23 -0
- package/dist/security/deps/lockfiles.js +153 -0
- package/dist/security/deps/osv.d.ts +66 -0
- package/dist/security/deps/osv.js +137 -0
- package/dist/security/deps/run.d.ts +54 -0
- package/dist/security/deps/run.js +135 -0
- package/dist/security/deps/severity.d.ts +26 -0
- package/dist/security/deps/severity.js +78 -0
- package/dist/security/generators.d.ts +9 -0
- package/dist/security/generators.js +36 -0
- package/dist/security/index.d.ts +27 -0
- package/dist/security/index.js +18 -0
- package/dist/security/integers.d.ts +31 -0
- package/dist/security/integers.js +105 -0
- package/dist/security/law.d.ts +54 -0
- package/dist/security/law.js +54 -0
- package/dist/security/pathTraversal.d.ts +29 -0
- package/dist/security/pathTraversal.js +146 -0
- package/dist/security/prototypePollution.d.ts +30 -0
- package/dist/security/prototypePollution.js +142 -0
- package/dist/security/redirect.d.ts +21 -0
- package/dist/security/redirect.js +154 -0
- package/dist/security/redos.d.ts +21 -0
- package/dist/security/redos.js +93 -0
- package/dist/security/validation.d.ts +25 -0
- package/dist/security/validation.js +89 -0
- package/dist/security/xss.d.ts +21 -0
- package/dist/security/xss.js +90 -0
- package/dist/settings.d.ts +20 -0
- package/dist/settings.js +32 -0
- package/dist/smt.d.ts +29 -1
- package/dist/smt.js +239 -107
- package/dist/smtWorker.d.ts +1 -0
- package/dist/smtWorker.js +26 -0
- package/dist/solvers.d.ts +9 -0
- package/dist/solvers.js +11 -5
- package/dist/symbolic.d.ts +54 -1
- package/dist/symbolic.js +239 -13
- package/dist/tsconfigResolve.d.ts +28 -0
- package/dist/tsconfigResolve.js +141 -0
- package/dist/types.d.ts +28 -0
- package/hooks/claude-hook.mjs +2 -0
- package/package.json +18 -6
package/dist/seal.js
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// The seal of a law: what it depends on besides its own text, so the lock notices a law weakened without touching it.
|
|
2
|
+
//
|
|
3
|
+
// A law is only as strong as the helpers and generators it calls and the settings it runs under. The seal digests:
|
|
4
|
+
// - the top-level declarations of its file that the law, or the code that runs when the file loads, references,
|
|
5
|
+
// transitively (helpers, generators, types);
|
|
6
|
+
// - the code of its file that runs when it loads: statements that declare nothing, and declarations whose initializer
|
|
7
|
+
// may run code (`const x = setUp()`);
|
|
8
|
+
// - every module its file imports, whole and transitively: an import runs when the file loads whether or not a law
|
|
9
|
+
// names what it exports, and a type decides the parameters a proof ranges over. Project files, files outside the
|
|
10
|
+
// project (a workspace package, a shared directory) and declaration files with their runtime sibling are digested;
|
|
11
|
+
// a package from a registry is recorded by name and version; a specifier that resolves to nothing is recorded as it
|
|
12
|
+
// is written, so it cannot go missing unseen;
|
|
13
|
+
// - the effective tslaws.json (see `sealedSettings`), the files its `bundlerPlugins` name and the tsconfig it resolves
|
|
14
|
+
// with.
|
|
15
|
+
// The other laws of the file are not in it, so editing one law, or a helper only it uses, tampers that law alone. Code is
|
|
16
|
+
// compared as tokens, so reformatting and comments change nothing but a string, a number or an operator does.
|
|
17
|
+
//
|
|
18
|
+
// What it cannot see: a `require(name)` or `import(name)` whose argument is not a string literal, a file the code reads at
|
|
19
|
+
// run time (`readFileSync`), the contents of an installed registry package beyond its version, and command line flags.
|
|
20
|
+
import { createHash } from 'node:crypto';
|
|
21
|
+
import { existsSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
22
|
+
import { builtinModules } from 'node:module';
|
|
23
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
24
|
+
import ts from 'typescript';
|
|
25
|
+
import { readConfig } from "./coverage.js";
|
|
26
|
+
import { moduleSpecifiers } from "./lawKinds.js";
|
|
27
|
+
import { identifiersIn, parseLawFile } from "./promote.js";
|
|
28
|
+
import { sealedSettings } from "./settings.js";
|
|
29
|
+
import { readResolution, resolutionCompilerOptions } from "./tsconfigResolve.js";
|
|
30
|
+
const digest = (data) => createHash('sha256').update(data).digest('hex');
|
|
31
|
+
/** The code as its tokens joined by one space: no comments or layout, every literal exact. */
|
|
32
|
+
function tokensOf(text, fileName) {
|
|
33
|
+
const source = ts.createSourceFile(fileName, text, ts.ScriptTarget.Latest, true);
|
|
34
|
+
const tokens = [];
|
|
35
|
+
const visit = (node) => {
|
|
36
|
+
if (node.kind >= ts.SyntaxKind.FirstJSDocNode && node.kind <= ts.SyntaxKind.LastJSDocNode)
|
|
37
|
+
return;
|
|
38
|
+
const children = node.getChildren(source);
|
|
39
|
+
if (children.length > 0)
|
|
40
|
+
children.forEach(visit);
|
|
41
|
+
else if (node.kind !== ts.SyntaxKind.EndOfFileToken)
|
|
42
|
+
tokens.push(node.getText(source));
|
|
43
|
+
};
|
|
44
|
+
visit(source);
|
|
45
|
+
return tokens.join(' ');
|
|
46
|
+
}
|
|
47
|
+
const scripts = /\.(?:[cm]?[jt]sx?)$/;
|
|
48
|
+
/** The line endings of a checkout must not matter, so text is read with `\n`. */
|
|
49
|
+
const readText = (path) => readFileSync(path, 'utf8').replaceAll('\r\n', '\n');
|
|
50
|
+
/** A file's digest: the tokens of a script, the text of any other text file, the bytes of a binary one. */
|
|
51
|
+
function digestOf(path) {
|
|
52
|
+
if (scripts.test(path))
|
|
53
|
+
return digest(tokensOf(readText(path), path));
|
|
54
|
+
const bytes = readFileSync(path);
|
|
55
|
+
return digest(bytes.includes(0) ? bytes : bytes.toString('utf8').replaceAll('\r\n', '\n'));
|
|
56
|
+
}
|
|
57
|
+
const builtins = new Set(builtinModules);
|
|
58
|
+
const underNodeModules = /(?:^|[\\/])node_modules[\\/]/;
|
|
59
|
+
const slash = (path) => path.replaceAll('\\', '/');
|
|
60
|
+
const realOf = (path) => {
|
|
61
|
+
try {
|
|
62
|
+
return realpathSync(path);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return path;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/** The `name` and `version` of the closest package.json above `file` that has both, stopping at a node_modules directory. */
|
|
69
|
+
function packageOfFile(file) {
|
|
70
|
+
for (let dir = dirname(file);; dir = dirname(dir)) {
|
|
71
|
+
const manifest = join(dir, 'package.json');
|
|
72
|
+
if (existsSync(manifest)) {
|
|
73
|
+
try {
|
|
74
|
+
const json = JSON.parse(readFileSync(manifest, 'utf8'));
|
|
75
|
+
if (typeof json.name === 'string' && typeof json.version === 'string')
|
|
76
|
+
return { name: json.name, version: json.version };
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// An unreadable manifest names no package.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (dirname(dir) === dir || /(?:^|[\\/])node_modules$/.test(dir))
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** The package a bare specifier names: `lodash/fp` -> `lodash`, `@scope/pkg/x` -> `@scope/pkg`. */
|
|
87
|
+
const packageNameOf = (specifier) => specifier
|
|
88
|
+
.split('/')
|
|
89
|
+
.slice(0, specifier.startsWith('@') ? 2 : 1)
|
|
90
|
+
.join('/');
|
|
91
|
+
/** The sibling of a declaration file that holds the code, when there is one: `x.d.ts` -> `x.js`, `.d.mts` -> `.mjs`. */
|
|
92
|
+
function runtimeSibling(file) {
|
|
93
|
+
const match = /^(.*)\.d\.([cm]?)ts$/.exec(file);
|
|
94
|
+
if (match === null)
|
|
95
|
+
return undefined;
|
|
96
|
+
const sibling = `${match[1]}.${match[2]}js`;
|
|
97
|
+
return existsSync(sibling) ? sibling : undefined;
|
|
98
|
+
}
|
|
99
|
+
/** Resolves the specifiers of the files a law reaches, with the project's own `paths`. */
|
|
100
|
+
class Linker {
|
|
101
|
+
options;
|
|
102
|
+
constructor(cwd) {
|
|
103
|
+
this.options = {
|
|
104
|
+
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
105
|
+
module: ts.ModuleKind.ESNext,
|
|
106
|
+
target: ts.ScriptTarget.ES2022,
|
|
107
|
+
allowJs: true,
|
|
108
|
+
resolveJsonModule: true,
|
|
109
|
+
...resolutionCompilerOptions(readResolution(cwd)?.options),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/** The registry package a bare specifier names, found the way node finds it: in the closest node_modules above `from`. */
|
|
113
|
+
installed(specifier, from) {
|
|
114
|
+
const name = packageNameOf(specifier);
|
|
115
|
+
for (let dir = dirname(from);; dir = dirname(dir)) {
|
|
116
|
+
const manifest = join(dir, 'node_modules', name, 'package.json');
|
|
117
|
+
if (existsSync(manifest)) {
|
|
118
|
+
const real = realOf(manifest);
|
|
119
|
+
// A link to a directory outside node_modules (a workspace package) is source: its files are digested.
|
|
120
|
+
if (!underNodeModules.test(real))
|
|
121
|
+
return 'linked';
|
|
122
|
+
return packageOfFile(real) ?? undefined;
|
|
123
|
+
}
|
|
124
|
+
if (dirname(dir) === dir)
|
|
125
|
+
return undefined;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
link(specifier, from) {
|
|
129
|
+
if (specifier.startsWith('node:'))
|
|
130
|
+
return { files: [], externals: [`builtin ${specifier}`] };
|
|
131
|
+
// A bare name that is also a builtin is the builtin only when nothing else answers to it.
|
|
132
|
+
const builtin = builtins.has(specifier);
|
|
133
|
+
const files = [];
|
|
134
|
+
const externals = [];
|
|
135
|
+
const bare = !/^\.{1,2}(?:[\\/]|$)/.test(specifier) && !isAbsolute(specifier);
|
|
136
|
+
const installed = bare ? this.installed(specifier, from) : undefined;
|
|
137
|
+
if (installed !== undefined && installed !== 'linked')
|
|
138
|
+
externals.push(registry(installed, specifier));
|
|
139
|
+
const found = ts.resolveModuleName(specifier, from, this.options, ts.sys).resolvedModule;
|
|
140
|
+
const typesOnly = builtin && found !== undefined && /[\/]node_modules[\/]@types[\/]/.test(realOf(found.resolvedFileName));
|
|
141
|
+
if (found !== undefined && existsSync(found.resolvedFileName) && !typesOnly) {
|
|
142
|
+
const real = realOf(found.resolvedFileName);
|
|
143
|
+
if (underNodeModules.test(real)) {
|
|
144
|
+
// A registry package reached through `paths` or as types only (`@types/x`): its name and version.
|
|
145
|
+
const owner = packageOfFile(real);
|
|
146
|
+
externals.push(owner === undefined ? `unresolved ${specifier}` : registry(owner, specifier));
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const owner = bare ? packageOfFile(real) : undefined;
|
|
150
|
+
// The tool itself is whatever version runs it; the lock's own format says which semantics it records.
|
|
151
|
+
if (owner?.name === 'tslaws' && (specifier === 'tslaws' || specifier.startsWith('tslaws/')))
|
|
152
|
+
externals.push('package tslaws');
|
|
153
|
+
else {
|
|
154
|
+
files.push(real);
|
|
155
|
+
const sibling = runtimeSibling(real);
|
|
156
|
+
if (sibling !== undefined)
|
|
157
|
+
files.push(realOf(sibling));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (builtin && (installed === undefined || installed === 'linked') && files.length === 0) {
|
|
162
|
+
externals.push(`builtin ${specifier}`);
|
|
163
|
+
}
|
|
164
|
+
else if (installed === undefined || installed === 'linked') {
|
|
165
|
+
// An asset TypeScript does not resolve (css, svg, txt) is digested when the specifier names a file.
|
|
166
|
+
const asset = bare ? undefined : resolve(dirname(from), specifier);
|
|
167
|
+
if (asset !== undefined && existsSync(asset) && statSync(asset).isFile())
|
|
168
|
+
files.push(realOf(asset));
|
|
169
|
+
else
|
|
170
|
+
externals.push(`unresolved ${specifier}`);
|
|
171
|
+
}
|
|
172
|
+
return { files, externals };
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const registry = (owner, specifier) => `package ${owner.name}@${owner.version}${packageNameOf(specifier) === owner.name ? '' : ` for ${specifier}`}`;
|
|
176
|
+
/** Whether the code of a top-level declaration may run something when its file loads, as opposed to defining a value. */
|
|
177
|
+
function runsOnLoad(text) {
|
|
178
|
+
const source = ts.createSourceFile('declaration.ts', text, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
179
|
+
const plainName = (name) => name === undefined || !ts.isComputedPropertyName(name);
|
|
180
|
+
const inert = (node) => {
|
|
181
|
+
if (node === undefined)
|
|
182
|
+
return true;
|
|
183
|
+
if (ts.isIdentifier(node) ||
|
|
184
|
+
ts.isStringLiteralLike(node) ||
|
|
185
|
+
ts.isNumericLiteral(node) ||
|
|
186
|
+
ts.isBigIntLiteral(node) ||
|
|
187
|
+
ts.isRegularExpressionLiteral(node) ||
|
|
188
|
+
ts.isArrowFunction(node) ||
|
|
189
|
+
ts.isFunctionExpression(node) ||
|
|
190
|
+
node.kind === ts.SyntaxKind.TrueKeyword ||
|
|
191
|
+
node.kind === ts.SyntaxKind.FalseKeyword ||
|
|
192
|
+
node.kind === ts.SyntaxKind.NullKeyword ||
|
|
193
|
+
node.kind === ts.SyntaxKind.ThisKeyword)
|
|
194
|
+
return true;
|
|
195
|
+
if (ts.isClassExpression(node))
|
|
196
|
+
return classInert(node);
|
|
197
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isNonNullExpression(node))
|
|
198
|
+
return inert(node.expression);
|
|
199
|
+
if (ts.isSatisfiesExpression(node) || ts.isTypeAssertionExpression(node))
|
|
200
|
+
return inert(node.expression);
|
|
201
|
+
if (ts.isArrayLiteralExpression(node))
|
|
202
|
+
return node.elements.every((e) => !ts.isSpreadElement(e) && inert(e));
|
|
203
|
+
if (ts.isObjectLiteralExpression(node))
|
|
204
|
+
return node.properties.every((p) => {
|
|
205
|
+
if (ts.isShorthandPropertyAssignment(p))
|
|
206
|
+
return true;
|
|
207
|
+
return ts.isPropertyAssignment(p) && plainName(p.name) && inert(p.initializer);
|
|
208
|
+
});
|
|
209
|
+
return false;
|
|
210
|
+
};
|
|
211
|
+
const classInert = (node) => !ts.canHaveDecorators(node) || (ts.getDecorators(node) ?? []).length === 0
|
|
212
|
+
? node.heritageClauses === undefined &&
|
|
213
|
+
node.members.every((member) => {
|
|
214
|
+
if (ts.isClassStaticBlockDeclaration(member))
|
|
215
|
+
return false;
|
|
216
|
+
if (!plainName(member.name))
|
|
217
|
+
return false;
|
|
218
|
+
if (ts.canHaveDecorators(member) && (ts.getDecorators(member) ?? []).length > 0)
|
|
219
|
+
return false;
|
|
220
|
+
const isStatic = ts.getCombinedModifierFlags(member) & ts.ModifierFlags.Static;
|
|
221
|
+
return !(ts.isPropertyDeclaration(member) && isStatic !== 0 && !inert(member.initializer));
|
|
222
|
+
})
|
|
223
|
+
: false;
|
|
224
|
+
return source.statements.some((statement) => {
|
|
225
|
+
if (ts.isFunctionDeclaration(statement) ||
|
|
226
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
227
|
+
ts.isTypeAliasDeclaration(statement))
|
|
228
|
+
return false;
|
|
229
|
+
if (ts.isClassDeclaration(statement))
|
|
230
|
+
return !classInert(statement);
|
|
231
|
+
if (ts.isEnumDeclaration(statement))
|
|
232
|
+
return !statement.members.every((m) => inert(m.initializer));
|
|
233
|
+
if (ts.isVariableStatement(statement))
|
|
234
|
+
return !statement.declarationList.declarations.every((d) => ts.isIdentifier(d.name) && inert(d.initializer));
|
|
235
|
+
return true;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* The code of a laws file that runs when it loads: statements that declare nothing and are not imports or laws, and
|
|
240
|
+
* declarations whose initializer may run code.
|
|
241
|
+
*/
|
|
242
|
+
function loadCode(rest, declarations) {
|
|
243
|
+
const source = ts.createSourceFile('laws.ts', rest, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
|
|
244
|
+
const declared = new Set(declarations.map((d) => d.end));
|
|
245
|
+
const code = source.statements
|
|
246
|
+
.filter((s) => !declared.has(s.end) && !ts.isEmptyStatement(s))
|
|
247
|
+
.map((s) => ({ text: s.getText(source), tokens: tokensOf(s.getText(source), 'laws.ts') }));
|
|
248
|
+
for (const declaration of declarations)
|
|
249
|
+
if (runsOnLoad(declaration.text))
|
|
250
|
+
code.push({ text: declaration.text, tokens: tokensOf(declaration.text, 'laws.ts') });
|
|
251
|
+
return code;
|
|
252
|
+
}
|
|
253
|
+
/** Reads laws files and the modules behind them once, and seals the laws of a project. */
|
|
254
|
+
class Sealer {
|
|
255
|
+
cwd;
|
|
256
|
+
linker;
|
|
257
|
+
facts = new Map();
|
|
258
|
+
/** The digest of each module and what it imports. */
|
|
259
|
+
modules = new Map();
|
|
260
|
+
settings;
|
|
261
|
+
constructor(cwd) {
|
|
262
|
+
this.cwd = realOf(resolve(cwd));
|
|
263
|
+
this.linker = new Linker(this.cwd);
|
|
264
|
+
const config = readConfig(cwd);
|
|
265
|
+
const plugins = this.reach((config.bundlerPlugins ?? []).map((path) => realOf(resolve(this.cwd, path))), 'plugin');
|
|
266
|
+
const resolution = readResolution(cwd);
|
|
267
|
+
const tsconfig = resolution === undefined
|
|
268
|
+
? null
|
|
269
|
+
: {
|
|
270
|
+
file: slash(relative(this.cwd, realOf(resolution.file))),
|
|
271
|
+
digest: digestOf(resolution.file),
|
|
272
|
+
options: portable(resolution.options, this.cwd),
|
|
273
|
+
};
|
|
274
|
+
this.settings = JSON.stringify(sorted({ config: sealedSettings(config), plugins, tsconfig }));
|
|
275
|
+
}
|
|
276
|
+
factsOf(file) {
|
|
277
|
+
if (!this.facts.has(file)) {
|
|
278
|
+
let facts;
|
|
279
|
+
try {
|
|
280
|
+
const parsed = parseLawFile(readText(resolve(this.cwd, file)));
|
|
281
|
+
const code = loadCode(parsed.rest, parsed.declarations);
|
|
282
|
+
facts = {
|
|
283
|
+
imports: parsed.imports.flatMap((statement) => moduleSpecifiers(statement).map((s) => s.value)),
|
|
284
|
+
declarations: parsed.declarations,
|
|
285
|
+
loadCode: code,
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
catch {
|
|
289
|
+
// An unreadable file seals nothing but its settings; the compiler reports the file itself.
|
|
290
|
+
}
|
|
291
|
+
this.facts.set(file, facts);
|
|
292
|
+
}
|
|
293
|
+
return this.facts.get(file);
|
|
294
|
+
}
|
|
295
|
+
moduleOf(path) {
|
|
296
|
+
if (!this.modules.has(path)) {
|
|
297
|
+
this.modules.set(path, undefined);
|
|
298
|
+
let found;
|
|
299
|
+
try {
|
|
300
|
+
found = {
|
|
301
|
+
digest: digestOf(path),
|
|
302
|
+
links: scripts.test(path)
|
|
303
|
+
? moduleSpecifiers(readText(path), path).map((s) => this.linker.link(s.value, path))
|
|
304
|
+
: [],
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
catch {
|
|
308
|
+
return undefined;
|
|
309
|
+
}
|
|
310
|
+
this.modules.set(path, found);
|
|
311
|
+
}
|
|
312
|
+
return this.modules.get(path);
|
|
313
|
+
}
|
|
314
|
+
/** The files in `start` and every module they reach, with each one's digest, and what is recorded as written. */
|
|
315
|
+
reach(start, what = 'module') {
|
|
316
|
+
const modules = {};
|
|
317
|
+
const externals = new Set();
|
|
318
|
+
const seen = new Set();
|
|
319
|
+
const queue = [...start];
|
|
320
|
+
for (let path = queue.pop(); path !== undefined; path = queue.pop()) {
|
|
321
|
+
if (seen.has(path))
|
|
322
|
+
continue;
|
|
323
|
+
seen.add(path);
|
|
324
|
+
const found = this.moduleOf(path);
|
|
325
|
+
if (found === undefined) {
|
|
326
|
+
externals.add(`unreadable ${what} ${slash(relative(this.cwd, path))}`);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
modules[slash(relative(this.cwd, path))] = found.digest;
|
|
330
|
+
for (const link of found.links) {
|
|
331
|
+
queue.push(...link.files);
|
|
332
|
+
for (const external of link.externals)
|
|
333
|
+
externals.add(external);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return { modules, externals: [...externals].sort() };
|
|
337
|
+
}
|
|
338
|
+
seal(law) {
|
|
339
|
+
return this.sealed(law).seal;
|
|
340
|
+
}
|
|
341
|
+
/** The seal of a law and the files it covers: the law's own file and every module its seal reaches, as absolute paths. */
|
|
342
|
+
sealed(law) {
|
|
343
|
+
const facts = this.factsOf(law.file);
|
|
344
|
+
const base = resolve(this.cwd, law.file);
|
|
345
|
+
const needed = new Set();
|
|
346
|
+
const byName = new Map();
|
|
347
|
+
for (const declaration of facts?.declarations ?? [])
|
|
348
|
+
for (const name of declaration.names)
|
|
349
|
+
byName.set(name, [...(byName.get(name) ?? []), declaration]);
|
|
350
|
+
// What runs when the file loads references declarations too, so they are reached from there as well as from the law.
|
|
351
|
+
const identifiers = identifiersIn(law.text);
|
|
352
|
+
for (const code of facts?.loadCode ?? [])
|
|
353
|
+
for (const name of identifiersIn(code.text))
|
|
354
|
+
identifiers.add(name);
|
|
355
|
+
const queue = [...identifiers];
|
|
356
|
+
for (let name = queue.pop(); name !== undefined; name = queue.pop())
|
|
357
|
+
for (const declaration of byName.get(name) ?? []) {
|
|
358
|
+
if (needed.has(declaration))
|
|
359
|
+
continue;
|
|
360
|
+
needed.add(declaration);
|
|
361
|
+
for (const reference of declaration.references) {
|
|
362
|
+
identifiers.add(reference);
|
|
363
|
+
queue.push(reference);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
const helpers = (facts?.declarations ?? []).filter((d) => needed.has(d)).map((d) => tokensOf(d.text, 'laws.ts'));
|
|
367
|
+
const load = (facts?.loadCode ?? []).map((c) => c.tokens);
|
|
368
|
+
// Every import of the file runs when it loads, and `require()`, `import()` and re-exports inside what the seal covers
|
|
369
|
+
// load a module too.
|
|
370
|
+
const specifiers = new Set(facts?.imports ?? []);
|
|
371
|
+
for (const text of [law.text, ...helpers, ...load])
|
|
372
|
+
for (const s of moduleSpecifiers(text))
|
|
373
|
+
specifiers.add(s.value);
|
|
374
|
+
const linked = [...specifiers].sort().map((specifier) => this.linker.link(specifier, base));
|
|
375
|
+
const reached = this.reach(linked.flatMap((l) => l.files));
|
|
376
|
+
const externals = [...new Set([...linked.flatMap((l) => l.externals), ...reached.externals])].sort();
|
|
377
|
+
const seal = digest(JSON.stringify({
|
|
378
|
+
settings: this.settings,
|
|
379
|
+
helpers,
|
|
380
|
+
loadCode: load,
|
|
381
|
+
modules: sorted(reached.modules),
|
|
382
|
+
externals,
|
|
383
|
+
}));
|
|
384
|
+
return { seal, files: [base, ...Object.keys(reached.modules).map((module) => resolve(this.cwd, module))] };
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/** `value` with the keys of every object in sorted order, so equal data stringifies equally. */
|
|
388
|
+
function sorted(value) {
|
|
389
|
+
if (Array.isArray(value))
|
|
390
|
+
return value.map(sorted);
|
|
391
|
+
if (typeof value !== 'object' || value === null)
|
|
392
|
+
return value;
|
|
393
|
+
return Object.fromEntries(Object.entries(value)
|
|
394
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
|
|
395
|
+
.map(([key, entry]) => [key, sorted(entry)]));
|
|
396
|
+
}
|
|
397
|
+
/** `value` with every absolute path in it written relative to `base`, so a seal does not depend on where a project is checked out. */
|
|
398
|
+
function portable(value, base) {
|
|
399
|
+
if (typeof value === 'string')
|
|
400
|
+
return isAbsolute(value)
|
|
401
|
+
? `${slash(relative(base, value))}${value.endsWith('/') || value.endsWith('\\') ? '/' : ''}`
|
|
402
|
+
: value;
|
|
403
|
+
if (Array.isArray(value))
|
|
404
|
+
return value.map((entry) => portable(entry, base));
|
|
405
|
+
if (typeof value !== 'object' || value === null)
|
|
406
|
+
return value;
|
|
407
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, portable(entry, base)]));
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* The files the seals of `laws` cover: each law's file and every module it reaches, absolute and real. This is the set a
|
|
411
|
+
* sandboxed law process may load besides the build's output (see sandboxGuard.ts), so the code that runs is the code the
|
|
412
|
+
* lock digests, no more.
|
|
413
|
+
*/
|
|
414
|
+
export function sealedFiles(cwd, laws) {
|
|
415
|
+
if (laws.length === 0)
|
|
416
|
+
return [];
|
|
417
|
+
const sealer = new Sealer(cwd);
|
|
418
|
+
return [...new Set(laws.flatMap((law) => sealer.sealed(law).files))];
|
|
419
|
+
}
|
|
420
|
+
/** The laws with their `seal` set. */
|
|
421
|
+
export function sealLaws(cwd, laws) {
|
|
422
|
+
if (laws.length === 0)
|
|
423
|
+
return laws;
|
|
424
|
+
const sealer = new Sealer(cwd);
|
|
425
|
+
return laws.map((law) => ({ ...law, seal: sealer.seal(law) }));
|
|
426
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { FetchLike } from './deps/osv.ts';
|
|
2
|
+
export declare const securityUsage = "tslaws security deps [--offline] [--fail-on low|moderate|high|critical] [--no-cache] [--json] check the npm packages the covered code imports against OSV (sends package names and versions only; exit 1 when an advisory is at or above security.failOn, default high)";
|
|
3
|
+
/** Exit status: 0 passed, 1 an advisory at or above the threshold, 2 the check could not run. */
|
|
4
|
+
export declare function securityCommand(cwd: string, args: string[], flags: Map<string, string | true>, write: (text: string) => void, fetch?: FetchLike,
|
|
5
|
+
/** Project-relative paths of projects nested in this one: each checks its own imports and config. */
|
|
6
|
+
skip?: string[]): Promise<number>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// `tslaws security deps [--offline] [--fail-on LEVEL] [--no-cache] [--json]`
|
|
2
|
+
import { readConfig } from "../coverage.js";
|
|
3
|
+
import { parseSecurityConfig } from "./deps/config.js";
|
|
4
|
+
import { checkDeps, renderDeps } from "./deps/run.js";
|
|
5
|
+
import { parseSeverity } from "./deps/severity.js";
|
|
6
|
+
export const securityUsage = `tslaws security deps [--offline] [--fail-on low|moderate|high|critical] [--no-cache] [--json] check the npm packages the covered code imports against OSV (sends package names and versions only; exit 1 when an advisory is at or above security.failOn, default high)`;
|
|
7
|
+
/** Exit status: 0 passed, 1 an advisory at or above the threshold, 2 the check could not run. */
|
|
8
|
+
export async function securityCommand(cwd, args, flags, write, fetch,
|
|
9
|
+
/** Project-relative paths of projects nested in this one: each checks its own imports and config. */
|
|
10
|
+
skip = []) {
|
|
11
|
+
if (args[0] !== 'deps') {
|
|
12
|
+
write(`${securityUsage}\n`);
|
|
13
|
+
return 2;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const config = readConfig(cwd);
|
|
17
|
+
const security = parseSecurityConfig(config.security);
|
|
18
|
+
const flag = flags.get('fail-on');
|
|
19
|
+
const failOn = flag === undefined ? undefined : parseSeverity(flag);
|
|
20
|
+
if (flag !== undefined && (failOn === undefined || failOn === 'unknown'))
|
|
21
|
+
throw new Error(`--fail-on must be low, moderate, high or critical, got ${String(flag)}`);
|
|
22
|
+
const report = await checkDeps({
|
|
23
|
+
cwd,
|
|
24
|
+
cover: config.cover,
|
|
25
|
+
config: security,
|
|
26
|
+
failOn: failOn,
|
|
27
|
+
offline: flags.has('offline') ? true : undefined,
|
|
28
|
+
noCache: flags.has('no-cache'),
|
|
29
|
+
skip,
|
|
30
|
+
fetch,
|
|
31
|
+
});
|
|
32
|
+
write(flags.has('json') ? `${JSON.stringify(report, null, 2)}\n` : renderDeps(report));
|
|
33
|
+
return report.failed ? 1 : 0;
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
write(`security deps: ${error instanceof Error ? error.message : String(error)}\nUse --offline to check from the cache (in your user cache directory) without the network.\n`);
|
|
37
|
+
return 2;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** A Common Weakness Enumeration id, as in `CWE-22`. */
|
|
2
|
+
export type CweId = `CWE-${number}`;
|
|
3
|
+
/** The weaknesses the pack covers, with the short name the reports use. */
|
|
4
|
+
export declare const cweCatalog: Readonly<Record<string, string>>;
|
|
5
|
+
/** `CWE-400/CWE-1333` for two ids, `CWE-22` for one. */
|
|
6
|
+
export declare function cweLabel(ids: readonly CweId[]): string;
|
|
7
|
+
/** What a failed security law throws: the message is `[CWE-22] what went wrong`. */
|
|
8
|
+
export declare class CweViolation extends Error {
|
|
9
|
+
readonly cwe: readonly CweId[];
|
|
10
|
+
readonly detail: string;
|
|
11
|
+
constructor(cwe: readonly CweId[], detail: string);
|
|
12
|
+
}
|
|
13
|
+
/** A string for a message: JSON, so control characters, quotes and a null byte are visible. Long text is cut. */
|
|
14
|
+
export declare function show(value: unknown): string;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// CWE tags for the security law pack. A builder's law fails by throwing a CweViolation whose message starts with
|
|
2
|
+
// the tag, so every reporter that prints a law's message (text, JUnit, GitHub annotations) shows the CWE.
|
|
3
|
+
/** The weaknesses the pack covers, with the short name the reports use. */
|
|
4
|
+
export const cweCatalog = {
|
|
5
|
+
'CWE-20': 'Improper input validation',
|
|
6
|
+
'CWE-22': 'Path traversal',
|
|
7
|
+
'CWE-79': 'Cross-site scripting',
|
|
8
|
+
'CWE-190': 'Integer overflow or wraparound',
|
|
9
|
+
'CWE-400': 'Uncontrolled resource consumption',
|
|
10
|
+
'CWE-601': 'Open redirect',
|
|
11
|
+
'CWE-681': 'Incorrect conversion between numeric types',
|
|
12
|
+
'CWE-1321': 'Prototype pollution',
|
|
13
|
+
'CWE-1333': 'Inefficient regular expression complexity',
|
|
14
|
+
};
|
|
15
|
+
/** `CWE-400/CWE-1333` for two ids, `CWE-22` for one. */
|
|
16
|
+
export function cweLabel(ids) {
|
|
17
|
+
return ids.join('/');
|
|
18
|
+
}
|
|
19
|
+
/** What a failed security law throws: the message is `[CWE-22] what went wrong`. */
|
|
20
|
+
export class CweViolation extends Error {
|
|
21
|
+
cwe;
|
|
22
|
+
detail;
|
|
23
|
+
constructor(cwe, detail) {
|
|
24
|
+
super(`[${cweLabel(cwe)}] ${detail}`);
|
|
25
|
+
this.name = 'CweViolation';
|
|
26
|
+
this.cwe = cwe;
|
|
27
|
+
this.detail = detail;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** A string for a message: JSON, so control characters, quotes and a null byte are visible. Long text is cut. */
|
|
31
|
+
export function show(value) {
|
|
32
|
+
let text;
|
|
33
|
+
try {
|
|
34
|
+
text = typeof value === 'string' ? JSON.stringify(value) : (JSON.stringify(value) ?? String(value));
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
text = String(value);
|
|
38
|
+
}
|
|
39
|
+
return text.length > 120 ? `${text.slice(0, 117)}...` : text;
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Vuln } from './osv.ts';
|
|
2
|
+
export interface OsvCache {
|
|
3
|
+
/** The ids for a package, when known and (online) fresh enough; `stale: true` answers from old entries too (offline). */
|
|
4
|
+
ids(key: string, options: {
|
|
5
|
+
now: number;
|
|
6
|
+
ttlMs: number;
|
|
7
|
+
stale: boolean;
|
|
8
|
+
}): string[] | undefined;
|
|
9
|
+
advisory(id: string, options: {
|
|
10
|
+
now: number;
|
|
11
|
+
ttlMs: number;
|
|
12
|
+
stale: boolean;
|
|
13
|
+
}): Vuln | undefined;
|
|
14
|
+
setIds(key: string, ids: string[], now: number): void;
|
|
15
|
+
setAdvisory(id: string, vuln: Vuln, now: number): void;
|
|
16
|
+
save(): void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The per-user cache directory: `%LOCALAPPDATA% slaws` on Windows, else `$XDG_CACHE_HOME/tslaws` or `~/.cache/tslaws`.
|
|
20
|
+
* A variable that is empty or not an absolute path is ignored, as the XDG spec says: a relative one would put the cache
|
|
21
|
+
* inside whichever directory the run starts in, which can be the repository.
|
|
22
|
+
*/
|
|
23
|
+
export declare function cacheDir(env?: NodeJS.ProcessEnv, platform?: NodeJS.Platform, home?: string): string;
|
|
24
|
+
export declare const cachePath: (env?: NodeJS.ProcessEnv) => string;
|
|
25
|
+
/** Reads the cache file (an empty cache when it is missing or unreadable). */
|
|
26
|
+
export declare function openCache(env?: NodeJS.ProcessEnv): OsvCache;
|
|
27
|
+
/** A cache that keeps nothing. */
|
|
28
|
+
export declare const noCache: OsvCache;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// The OSV answers kept in the user's cache directory (`tslaws/osv-cache.json`), so a rerun or an offline run does not
|
|
2
|
+
// need the network. The file is never read from the project: a cache a repository commits could hide known
|
|
3
|
+
// vulnerabilities from a run that trusts it.
|
|
4
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { homedir } from 'node:os';
|
|
6
|
+
import { dirname, join, posix, win32 } from 'node:path';
|
|
7
|
+
/**
|
|
8
|
+
* The per-user cache directory: `%LOCALAPPDATA% slaws` on Windows, else `$XDG_CACHE_HOME/tslaws` or `~/.cache/tslaws`.
|
|
9
|
+
* A variable that is empty or not an absolute path is ignored, as the XDG spec says: a relative one would put the cache
|
|
10
|
+
* inside whichever directory the run starts in, which can be the repository.
|
|
11
|
+
*/
|
|
12
|
+
export function cacheDir(env = process.env, platform = process.platform, home = homedir()) {
|
|
13
|
+
const isAbsolute = platform === 'win32' ? win32.isAbsolute : posix.isAbsolute;
|
|
14
|
+
const absolute = (value) => value !== undefined && isAbsolute(value) ? value : undefined;
|
|
15
|
+
if (platform === 'win32')
|
|
16
|
+
return join(absolute(env.LOCALAPPDATA) ?? join(home, 'AppData', 'Local'), 'tslaws');
|
|
17
|
+
return join(absolute(env.XDG_CACHE_HOME) ?? join(home, '.cache'), 'tslaws');
|
|
18
|
+
}
|
|
19
|
+
export const cachePath = (env = process.env) => join(cacheDir(env), 'osv-cache.json');
|
|
20
|
+
/** An entry dated further ahead than this is not believed: clocks drift a little, a planted file is not that close. */
|
|
21
|
+
const skewMs = 5 * 60_000;
|
|
22
|
+
const fresh = (at, o) => {
|
|
23
|
+
// Offline there is no newer answer to prefer, so any age is accepted; online a date in the future never is.
|
|
24
|
+
if (o.stale)
|
|
25
|
+
return true;
|
|
26
|
+
return at <= o.now + skewMs && o.now - at <= o.ttlMs;
|
|
27
|
+
};
|
|
28
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
29
|
+
const isStrings = (value) => Array.isArray(value) && value.every((item) => typeof item === 'string');
|
|
30
|
+
const isTime = (value) => typeof value === 'number' && Number.isFinite(value);
|
|
31
|
+
const optional = (value, ok) => value === undefined || ok(value);
|
|
32
|
+
const isString = (value) => typeof value === 'string';
|
|
33
|
+
const isArrayOf = (value, ok) => Array.isArray(value) && value.every(ok);
|
|
34
|
+
/** An OSV range event: `introduced`, `fixed`, `last_affected` or `limit`, each a version string. */
|
|
35
|
+
const isEvent = (value) => isObject(value) && Object.values(value).every(isString);
|
|
36
|
+
const isRange = (value) => isObject(value) && optional(value.type, isString) && optional(value.events, (v) => isArrayOf(v, isEvent));
|
|
37
|
+
const isAffected = (value) => isObject(value) &&
|
|
38
|
+
optional(value.package, (v) => isObject(v) && optional(v.name, isString) && optional(v.ecosystem, isString)) &&
|
|
39
|
+
optional(value.ranges, (v) => isArrayOf(v, isRange)) &&
|
|
40
|
+
optional(value.ecosystem_specific, isObject);
|
|
41
|
+
const isScore = (value) => isObject(value) && optional(value.type, isString) && optional(value.score, isString);
|
|
42
|
+
/** Whether `value` has the shape `trimVuln` writes, all the way down, and is the advisory `id`. */
|
|
43
|
+
function isVuln(value, id) {
|
|
44
|
+
if (!isObject(value) || value.id !== id)
|
|
45
|
+
return false;
|
|
46
|
+
return (optional(value.aliases, isStrings) &&
|
|
47
|
+
optional(value.summary, isString) &&
|
|
48
|
+
optional(value.severity, (v) => isArrayOf(v, isScore)) &&
|
|
49
|
+
optional(value.database_specific, isObject) &&
|
|
50
|
+
optional(value.affected, (v) => isArrayOf(v, isAffected)));
|
|
51
|
+
}
|
|
52
|
+
/** Objects without a prototype, so a key such as `__proto__` in a planted file is only a key. */
|
|
53
|
+
const empty = () => ({ version: 1, packages: Object.create(null), advisories: Object.create(null) });
|
|
54
|
+
/** The entries of a cache file that are well formed; the rest are dropped, so they are asked of OSV again. */
|
|
55
|
+
function validated(read) {
|
|
56
|
+
const out = empty();
|
|
57
|
+
if (!isObject(read) || read.version !== 1)
|
|
58
|
+
return out;
|
|
59
|
+
if (isObject(read.packages))
|
|
60
|
+
for (const [key, entry] of Object.entries(read.packages))
|
|
61
|
+
if (isObject(entry) && isStrings(entry.ids) && isTime(entry.at))
|
|
62
|
+
out.packages[key] = { ids: entry.ids, at: entry.at };
|
|
63
|
+
if (isObject(read.advisories))
|
|
64
|
+
for (const [id, entry] of Object.entries(read.advisories))
|
|
65
|
+
if (isObject(entry) && isTime(entry.at) && isVuln(entry.vuln, id))
|
|
66
|
+
out.advisories[id] = { vuln: entry.vuln, at: entry.at };
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Reads the cache file (an empty cache when it is missing or unreadable). */
|
|
70
|
+
export function openCache(env = process.env) {
|
|
71
|
+
const path = cachePath(env);
|
|
72
|
+
let stored = empty();
|
|
73
|
+
try {
|
|
74
|
+
stored = validated(JSON.parse(readFileSync(path, 'utf8')));
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// Nothing cached yet.
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
ids: (key, o) => {
|
|
81
|
+
const entry = Object.hasOwn(stored.packages, key) ? stored.packages[key] : undefined;
|
|
82
|
+
return entry !== undefined && fresh(entry.at, o) ? entry.ids : undefined;
|
|
83
|
+
},
|
|
84
|
+
advisory: (id, o) => {
|
|
85
|
+
const entry = Object.hasOwn(stored.advisories, id) ? stored.advisories[id] : undefined;
|
|
86
|
+
return entry !== undefined && fresh(entry.at, o) ? entry.vuln : undefined;
|
|
87
|
+
},
|
|
88
|
+
setIds: (key, ids, now) => {
|
|
89
|
+
stored.packages[key] = { ids, at: now };
|
|
90
|
+
},
|
|
91
|
+
setAdvisory: (id, vuln, now) => {
|
|
92
|
+
stored.advisories[id] = { vuln, at: now };
|
|
93
|
+
},
|
|
94
|
+
save: () => {
|
|
95
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
96
|
+
writeFileSync(path, `${JSON.stringify(stored)}\n`);
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/** A cache that keeps nothing. */
|
|
101
|
+
export const noCache = {
|
|
102
|
+
ids: () => undefined,
|
|
103
|
+
advisory: () => undefined,
|
|
104
|
+
setIds: () => undefined,
|
|
105
|
+
setAdvisory: () => undefined,
|
|
106
|
+
save: () => undefined,
|
|
107
|
+
};
|