pqc-scan 0.1.1 → 0.1.2
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/dist/cli.js +23 -1
- package/dist/core/parallel.js +3 -3
- package/dist/core/scan.js +14 -8
- package/dist/core/walk.js +33 -7
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { existsSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { existsSync, readFileSync, statSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { parseArgs } from 'node:util';
|
|
4
4
|
import { dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
5
5
|
import { loadRules } from "./core/rules.js";
|
|
@@ -32,6 +32,28 @@ for (const [option, example] of [['cache', '~/.cache/pqc-scan.json'], ['sarif',
|
|
|
32
32
|
process.exit(2);
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
|
+
// An output option may only replace a file that an earlier pqc-scan run wrote (a baseline, SARIF, CBOM or cache), so a
|
|
36
|
+
// mistyped path such as --sarif src/index.ts cannot overwrite someone's source code.
|
|
37
|
+
function isEarlierOutput(file) {
|
|
38
|
+
try {
|
|
39
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
40
|
+
return parsed.baselineVersion === 1 || parsed.runs?.[0]?.tool?.driver?.name === 'pqc-scan' || parsed.metadata?.tools?.components?.[0]?.name === 'pqc-scan' || (typeof parsed.body === 'string' && parsed.body.includes('"files"'));
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const option of ['cache', 'sarif', 'cbom', 'write-baseline']) {
|
|
47
|
+
const file = values[option];
|
|
48
|
+
if (typeof file === 'string' && !existsSync(dirname(resolve(file)))) {
|
|
49
|
+
console.error(`pqc-scan: --${option} ${file}: the folder ${dirname(resolve(file))} does not exist. Create it first.`);
|
|
50
|
+
process.exit(2);
|
|
51
|
+
}
|
|
52
|
+
if (typeof file === 'string' && existsSync(resolve(file)) && !isEarlierOutput(resolve(file))) {
|
|
53
|
+
console.error(`pqc-scan: --${option} ${file} would overwrite a file that pqc-scan did not write. Choose a new file name, or delete that file first.`);
|
|
54
|
+
process.exit(2);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
35
57
|
const maxFileSize = values['max-file-size'] === undefined ? undefined : Number(values['max-file-size']);
|
|
36
58
|
if (maxFileSize !== undefined && !(maxFileSize > 0)) {
|
|
37
59
|
console.error('pqc-scan: --max-file-size needs a size in MB greater than 0, for example --max-file-size 50');
|
package/dist/core/parallel.js
CHANGED
|
@@ -9,7 +9,7 @@ import { languages } from "../languages/index.js";
|
|
|
9
9
|
* code to repay starting them. Results are put back in the same order, so the output is identical to `scan`.
|
|
10
10
|
*/
|
|
11
11
|
export async function scanInParallel(target, rules, options = {}) {
|
|
12
|
-
const { root, files } = planScan(target, languages);
|
|
12
|
+
const { root, files, unreadable } = planScan(target, languages);
|
|
13
13
|
const findings = new Array(files.length);
|
|
14
14
|
const { cache } = options;
|
|
15
15
|
const maxBytes = options.maxFileBytes ?? DEFAULT_MAX_FILE_BYTES;
|
|
@@ -35,7 +35,7 @@ export async function scanInParallel(target, rules, options = {}) {
|
|
|
35
35
|
if (cache)
|
|
36
36
|
for (const index of pending)
|
|
37
37
|
cache.set(keys[index], findings[index]);
|
|
38
|
-
return finishScan(root, files, findings, rules);
|
|
38
|
+
return finishScan(root, files, findings, rules, undefined, unreadable);
|
|
39
39
|
}
|
|
40
40
|
const pool = new Pool(threads, rules);
|
|
41
41
|
try {
|
|
@@ -48,7 +48,7 @@ export async function scanInParallel(target, rules, options = {}) {
|
|
|
48
48
|
const sites = new Map();
|
|
49
49
|
await pool.run(wrapperCallerFiles(files, findings, rules), sizes, (index) => ({ kind: 'calls', index, path: files[index].path, name: files[index].name, language: files[index].adapter.id, maxBytes }), (reply) => { if ('sites' in reply)
|
|
50
50
|
sites.set(files[reply.index].path, reply.sites); });
|
|
51
|
-
return finishScan(root, files, findings, rules, sites);
|
|
51
|
+
return finishScan(root, files, findings, rules, sites, unreadable);
|
|
52
52
|
}
|
|
53
53
|
finally {
|
|
54
54
|
await pool.close();
|
package/dist/core/scan.js
CHANGED
|
@@ -13,10 +13,12 @@ export async function loadLanguages(adapters = languages) {
|
|
|
13
13
|
export function planScan(target, adapters = languages) {
|
|
14
14
|
const root = statSync(target).isDirectory() ? target : undefined;
|
|
15
15
|
const byExtension = new Map(adapters.flatMap((adapter) => adapter.extensions.map((extension) => [extension, adapter])));
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
const skipped = [];
|
|
17
|
+
// Reported file names use `/` on every system, so reports and baselines are the same on Windows.
|
|
18
|
+
const nameOf = (path) => (root ? relative(root, path) : basename(path)).split(sep).join('/');
|
|
19
|
+
const files = sourceFiles(target, new Set(byExtension.keys()), skipped)
|
|
20
|
+
.map((path) => ({ path, name: nameOf(path), adapter: byExtension.get(extname(path)) }));
|
|
21
|
+
return { root, files, unreadable: skipped.map((path) => unreadableFinding(nameOf(path) || '.')) };
|
|
20
22
|
}
|
|
21
23
|
/** One rules array per language for the whole scan, so adapters can build their lookup tables once and reuse them. */
|
|
22
24
|
export function rulesByAdapter(rules, adapters = languages) {
|
|
@@ -32,6 +34,10 @@ export function tooLargeFinding(name, bytes, maxBytes) {
|
|
|
32
34
|
const mb = (size) => `${Number((size / 1024 / 1024).toFixed(1))} MB`;
|
|
33
35
|
return finding(name, 1, 'file-too-large', 'unknown', null, 'unresolved', `Not scanned: the file is ${mb(bytes)}, over the ${mb(maxBytes)} limit. Check it by hand, or scan it with a higher --max-file-size.`);
|
|
34
36
|
}
|
|
37
|
+
/** The finding for a file or folder that could not be read: reported, not skipped, so it cannot hide anything. */
|
|
38
|
+
export function unreadableFinding(name) {
|
|
39
|
+
return finding(name, 1, 'path-unreadable', 'unknown', null, 'unresolved', 'Not scanned: pqc-scan could not read this file or folder (no permission). Check it by hand, or scan it with access.');
|
|
40
|
+
}
|
|
35
41
|
/** The findings in one file. */
|
|
36
42
|
export function scanFile(file, rulesFor, maxBytes = DEFAULT_MAX_FILE_BYTES) {
|
|
37
43
|
const bytes = statSync(file.path).size;
|
|
@@ -40,13 +46,13 @@ export function scanFile(file, rulesFor, maxBytes = DEFAULT_MAX_FILE_BYTES) {
|
|
|
40
46
|
return file.adapter.scanSource(readFileSync(file.path, 'utf8'), file.name, rulesFor.get(file.adapter));
|
|
41
47
|
}
|
|
42
48
|
/** Adds the cross-file findings to the per-file findings (in plan order) and returns them all. */
|
|
43
|
-
export function finishScan(root, files, findings, rules, prefetched) {
|
|
49
|
+
export function finishScan(root, files, findings, rules, prefetched, unreadable = []) {
|
|
44
50
|
const scanned = files.map((file, index) => ({ ...file, findings: findings[index], text: '' }));
|
|
45
51
|
if (root)
|
|
46
52
|
addWrapperFindings(scanned);
|
|
47
53
|
addCallsToWrappers(scanned, rules, prefetched);
|
|
48
54
|
// The wrapper marks are only for the step above; they are not part of a finding as reported.
|
|
49
|
-
return scanned.flatMap((item) => item.findings.map(({ wrapper: _wrapper, ...finding }) => finding));
|
|
55
|
+
return [...scanned.flatMap((item) => item.findings.map(({ wrapper: _wrapper, ...finding }) => finding)), ...unreadable];
|
|
50
56
|
}
|
|
51
57
|
/**
|
|
52
58
|
* Follows functions whose crypto algorithm is one of their parameters to where they are called (see wrappers.ts), in
|
|
@@ -134,9 +140,9 @@ function addCallsToWrappers(files, rules, prefetched) {
|
|
|
134
140
|
}
|
|
135
141
|
/** Scan a file or folder with every registered language, giving each adapter only the rules for its own language. */
|
|
136
142
|
export function scan(target, rules, adapters = languages) {
|
|
137
|
-
const { root, files } = planScan(target, adapters);
|
|
143
|
+
const { root, files, unreadable } = planScan(target, adapters);
|
|
138
144
|
const rulesFor = rulesByAdapter(rules, adapters);
|
|
139
|
-
return finishScan(root, files, files.map((file) => scanFile(file, rulesFor)), rules);
|
|
145
|
+
return finishScan(root, files, files.map((file) => scanFile(file, rulesFor)), rules, undefined, unreadable);
|
|
140
146
|
}
|
|
141
147
|
// A module whose algorithm comes from a parameter (or that lets a crypto reference escape) is a wrapper: its importers
|
|
142
148
|
// may be choosing the algorithm, and that flow is not tracked across files.
|
package/dist/core/walk.js
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
|
-
import { existsSync, readdirSync, statSync } from 'node:fs';
|
|
1
|
+
import { accessSync, constants, existsSync, readdirSync, statSync } from 'node:fs';
|
|
2
2
|
import { extname, join } from 'node:path';
|
|
3
3
|
const skipped = new Set(['.git', 'dist', 'node_modules']);
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
return
|
|
4
|
+
const readable = (path) => {
|
|
5
|
+
try {
|
|
6
|
+
accessSync(path, constants.R_OK);
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Source files under `target` (a file or a folder) whose extension is in `extensions`. A source file or folder that
|
|
15
|
+
* cannot be read is added to `unreadable` instead, so the scan can report it rather than stop.
|
|
16
|
+
*/
|
|
17
|
+
export function sourceFiles(target, extensions, unreadable = []) {
|
|
18
|
+
if (!statSync(target).isDirectory()) {
|
|
19
|
+
if (!extensions.has(extname(target)))
|
|
20
|
+
return [];
|
|
21
|
+
if (readable(target))
|
|
22
|
+
return [target];
|
|
23
|
+
unreadable.push(target);
|
|
24
|
+
return [];
|
|
25
|
+
}
|
|
8
26
|
const found = [];
|
|
9
27
|
const walk = (directory) => {
|
|
10
|
-
|
|
28
|
+
let entries;
|
|
29
|
+
try {
|
|
30
|
+
entries = readdirSync(directory, { withFileTypes: true });
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
unreadable.push(directory);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
for (const entry of entries) {
|
|
11
37
|
if (skipped.has(entry.name))
|
|
12
38
|
continue;
|
|
13
39
|
// Cargo's build output (generated code and copies of packaged crates), and Go's copies of dependencies made by
|
|
@@ -20,7 +46,7 @@ export function sourceFiles(target, extensions) {
|
|
|
20
46
|
if (entry.isDirectory())
|
|
21
47
|
walk(path);
|
|
22
48
|
else if (entry.isFile() && extensions.has(extname(entry.name)))
|
|
23
|
-
found.push(path);
|
|
49
|
+
(readable(path) ? found : unreadable).push(path);
|
|
24
50
|
}
|
|
25
51
|
};
|
|
26
52
|
walk(target);
|
package/package.json
CHANGED