pqc-scan 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +23 -0
- package/README.md +62 -2
- package/dist/cli.js +89 -0
- package/dist/core/cache.js +123 -0
- package/dist/core/finding.js +6 -0
- package/dist/core/language.js +1 -0
- package/dist/core/match.js +30 -0
- package/dist/core/matcher.js +49 -0
- package/dist/core/parallel.js +92 -0
- package/dist/core/rules.js +54 -0
- package/dist/core/scan-worker.js +21 -0
- package/dist/core/scan.js +165 -0
- package/dist/core/syntax.js +61 -0
- package/dist/core/tree-sitter.js +22 -0
- package/dist/core/types.js +2 -0
- package/dist/core/walk.js +28 -0
- package/dist/core/wrappers.js +259 -0
- package/dist/languages/c/adapter.js +165 -0
- package/dist/languages/c/api.js +3 -0
- package/dist/languages/cpp/adapter.js +216 -0
- package/dist/languages/go/adapter.js +246 -0
- package/dist/languages/index.js +8 -0
- package/dist/languages/javascript/adapter.js +325 -0
- package/dist/languages/python/adapter.js +150 -0
- package/dist/languages/rust/adapter.js +276 -0
- package/dist/output/baseline.js +51 -0
- package/dist/output/cbom.js +145 -0
- package/dist/output/json.js +9 -0
- package/dist/output/sarif.js +73 -0
- package/dist/output/text.js +48 -0
- package/grammars/LICENSE-tree-sitter-c +21 -0
- package/grammars/LICENSE-tree-sitter-cpp +21 -0
- package/grammars/LICENSE-tree-sitter-go +21 -0
- package/grammars/LICENSE-tree-sitter-python +21 -0
- package/grammars/LICENSE-tree-sitter-rust +21 -0
- package/grammars/tree-sitter-c.wasm +0 -0
- package/grammars/tree-sitter-cpp.wasm +0 -0
- package/grammars/tree-sitter-go.wasm +0 -0
- package/grammars/tree-sitter-python.wasm +0 -0
- package/grammars/tree-sitter-rust.wasm +0 -0
- package/package.json +64 -4
- package/rules/c/README.md +54 -0
- package/rules/c/botan.yaml +166 -0
- package/rules/c/gnutls.yaml +130 -0
- package/rules/c/libsodium.yaml +87 -0
- package/rules/c/mbedtls.yaml +214 -0
- package/rules/c/openssl.yaml +350 -0
- package/rules/c/psa-crypto.yaml +116 -0
- package/rules/c/wolfssl.yaml +247 -0
- package/rules/cpp/README.md +62 -0
- package/rules/cpp/botan.yaml +251 -0
- package/rules/cpp/cryptopp.yaml +264 -0
- package/rules/go/README.md +64 -0
- package/rules/go/circl.yaml +137 -0
- package/rules/go/jwt.yaml +120 -0
- package/rules/go/stdlib.yaml +391 -0
- package/rules/go/x-crypto.yaml +239 -0
- package/rules/javascript/README.md +35 -0
- package/rules/javascript/jsonwebtoken.yaml +42 -0
- package/rules/javascript/jws.yaml +66 -0
- package/rules/javascript/node-crypto.yaml +198 -0
- package/rules/javascript/webcrypto-polyfills.yaml +3 -0
- package/rules/python/README.md +17 -0
- package/rules/python/cryptography.yaml +39 -0
- package/rules/python/ecdsa.yaml +14 -0
- package/rules/python/paramiko.yaml +23 -0
- package/rules/python/pycryptodome.yaml +20 -0
- package/rules/python/pyjwt.yaml +13 -0
- package/rules/python/pynacl.yaml +12 -0
- package/rules/python/stdlib.yaml +45 -0
- package/rules/rust/README.md +63 -0
- package/rules/rust/jsonwebtoken.yaml +50 -0
- package/rules/rust/openssl.yaml +468 -0
- package/rules/rust/random.yaml +11 -0
- package/rules/rust/ring.yaml +348 -0
- package/rules/rust/rustcrypto.yaml +499 -0
- package/rules/rust/rustls.yaml +74 -0
- package/schema/cyclonedx/README.md +5 -0
- package/schema/cyclonedx/cryptography-defs.json +4228 -0
- package/schema/report-v1.schema.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
pqc-scan
|
|
2
|
+
Copyright 2026 Giddywiddit
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
4
|
+
|
|
5
|
+
This product includes the following third-party files, each under its own licence:
|
|
6
|
+
|
|
7
|
+
- tree-sitter grammars for C, C++, Rust and Go (the .wasm files in grammars/, copied from the npm packages
|
|
8
|
+
tree-sitter-c 0.24.1, tree-sitter-cpp 0.23.4, tree-sitter-rust 0.24.0 and tree-sitter-go 0.25.0).
|
|
9
|
+
MIT License; copyright Max Brunsfeld (C, C++, Go) and Maxim Sokolov (Rust). The full licence texts are in
|
|
10
|
+
grammars/LICENSE-tree-sitter-*.
|
|
11
|
+
|
|
12
|
+
- schema/cyclonedx/cryptography-defs.json, the CycloneDX cryptography registry from the CycloneDX specification
|
|
13
|
+
1.7.2 (https://github.com/CycloneDX/specification), unchanged. Apache License, Version 2.0.
|
|
14
|
+
|
|
15
|
+
The source repository also contains, for tests only and not in the npm package:
|
|
16
|
+
|
|
17
|
+
- test/schemas/bom-1.7.schema.json, spdx.schema.json, jsf-0.82.schema.json and cryptography-defs.schema.json, from
|
|
18
|
+
the CycloneDX specification 1.7.2, unchanged. Apache License, Version 2.0.
|
|
19
|
+
- test/schemas/sarif-schema-2.1.0.json, the SARIF 2.1.0 JSON Schema (errata 01), © OASIS Open, unchanged, from
|
|
20
|
+
https://docs.oasis-open.org/sarif/sarif/v2.1.0/errata01/os/schemas/sarif-schema-2.1.0.json.
|
|
21
|
+
|
|
22
|
+
pqc-scan installs, and does not include, these packages: TypeScript (Apache License 2.0), web-tree-sitter (MIT)
|
|
23
|
+
and yaml (ISC).
|
package/README.md
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pqc-scan
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```
|
|
4
|
+
/\_/\
|
|
5
|
+
( 0.1 ) [ pqc-scan • Post-Quantum Cryptography Scanner ]
|
|
6
|
+
> ^ < Scans your JavaScript, TypeScript, C, C++, Rust, Go and Python
|
|
7
|
+
---------------------------------------------------------
|
|
8
|
+
Flags the cryptography a quantum computer could break
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
pqc-scan reads your source code, lists where it uses cryptography, and flags each use a quantum computer could break.
|
|
12
|
+
|
|
13
|
+
A cryptanalytically relevant quantum computer (CRQC), one able to attack real cryptographic systems, would break RSA and elliptic-curve cryptography, which protect most signatures and encrypted connections today. None exists yet; a 2025 estimate is that one with fewer than a million noisy qubits could break 2048-bit RSA in under a week ([Gidney](https://arxiv.org/abs/2505.15917)). Two reasons to act before then, both set out in [NIST IR 8547](https://csrc.nist.gov/pubs/ir/8547/final):
|
|
14
|
+
|
|
15
|
+
- **Harvest now, decrypt later:** encrypted data recorded today can be decrypted once a CRQC exists.
|
|
16
|
+
- **Mosca's theorem:** if data must stay secret for X years, moving to new cryptography takes Y years, and a CRQC is Z years away, then when X + Y > Z that data will be exposed. The move has to start before that point.
|
|
17
|
+
|
|
18
|
+
NIST's transition plan begins phasing out quantum-vulnerable public-key algorithms after 2030, with broader disallowance milestones by 2035. The exact date depends on the algorithm and security strength. Replacing them starts with knowing where they are.
|
|
19
|
+
|
|
20
|
+
## Try it
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npx pqc-scan .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Needs Node.js 22 or newer. It reads your code without running, changing or uploading it, and needs no build.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
RESISTANT index.ts:3 SHA-256
|
|
30
|
+
VULNERABLE keys.js:3 RSA
|
|
31
|
+
A quantum computer could work out the private key from the public key (using Shor's algorithm). Plan to replace this key type.
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Each finding is one of:
|
|
35
|
+
|
|
36
|
+
| Label | Meaning |
|
|
37
|
+
|---|---|
|
|
38
|
+
| Vulnerable | RSA, DSA, elliptic curves or Diffie-Hellman. Plan to replace it. |
|
|
39
|
+
| Observed-resistant | The algorithm on this line is believed to withstand quantum computers, such as SHA-256, AES-256 or ML-KEM. This does not prove that the whole program is safe. |
|
|
40
|
+
| Review | Known exactly, but acceptable only by judgement, such as AES-128, SHA-1 or MD5. |
|
|
41
|
+
| Unresolved | The algorithm is chosen in a way the code does not show. Check that line by hand. |
|
|
42
|
+
|
|
43
|
+
## Output and CI
|
|
44
|
+
|
|
45
|
+
| Option | Writes |
|
|
46
|
+
|---|---|
|
|
47
|
+
| `--json` | The findings as JSON ([report schema](schema/report-v1.schema.json)). |
|
|
48
|
+
| `--sarif <file>` | SARIF for GitHub code scanning. |
|
|
49
|
+
| `--cbom <file>` | A CycloneDX 1.7 cryptography bill of materials. |
|
|
50
|
+
| `--write-baseline <file>`, `--baseline <file>` | Accepts the current findings, so CI fails only on new ones. |
|
|
51
|
+
|
|
52
|
+
The exit status is 1 when anything is vulnerable, needs review or is unresolved. There is a [GitHub Action](docs/github-action.md).
|
|
53
|
+
|
|
54
|
+
## Languages
|
|
55
|
+
|
|
56
|
+
| Language | Libraries |
|
|
57
|
+
|---|---|
|
|
58
|
+
| [JavaScript / TypeScript](rules/javascript/README.md) | Node.js crypto, WebCrypto, jsonwebtoken, jws |
|
|
59
|
+
| [C](rules/c/README.md) | OpenSSL, libsodium, Mbed TLS, PSA Crypto, wolfSSL, GnuTLS, Botan |
|
|
60
|
+
| [C++](rules/cpp/README.md) | Crypto++, Botan, and the C libraries |
|
|
61
|
+
| [Rust](rules/rust/README.md) | RustCrypto, ring, aws-lc-rs, openssl, rustls, jsonwebtoken |
|
|
62
|
+
| [Go](rules/go/README.md) | Standard library, golang.org/x/crypto, CIRCL, golang-jwt |
|
|
63
|
+
| [Python](rules/python/README.md) | cryptography, PyJWT, PyCryptodome, ecdsa, hashlib, Paramiko, PyNaCl |
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { existsSync, statSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
5
|
+
import { loadRules } from "./core/rules.js";
|
|
6
|
+
import { ResultCache } from "./core/cache.js";
|
|
7
|
+
import { scanInParallel } from "./core/parallel.js";
|
|
8
|
+
import { loadLanguages } from "./core/scan.js";
|
|
9
|
+
import { fingerprints, readBaseline, toBaseline } from "./output/baseline.js";
|
|
10
|
+
import { toCbom } from "./output/cbom.js";
|
|
11
|
+
import { toReport } from "./output/json.js";
|
|
12
|
+
import { toSarif } from "./output/sarif.js";
|
|
13
|
+
import { MASCOT_BANNER, printTable } from "./output/text.js";
|
|
14
|
+
const { values, positionals } = parseArgs({
|
|
15
|
+
allowPositionals: true,
|
|
16
|
+
strict: false,
|
|
17
|
+
options: { json: { type: 'boolean' }, quiet: { type: 'boolean', short: 'q' }, 'no-banner': { type: 'boolean' }, cache: { type: 'string' }, 'max-file-size': { type: 'string' }, sarif: { type: 'string' }, cbom: { type: 'string' }, baseline: { type: 'string' }, 'write-baseline': { type: 'string' } },
|
|
18
|
+
});
|
|
19
|
+
const json = values.json === true;
|
|
20
|
+
const quiet = values.quiet === true || values['no-banner'] === true;
|
|
21
|
+
const target = positionals[0];
|
|
22
|
+
const showBanner = !json && !quiet;
|
|
23
|
+
if (!target) {
|
|
24
|
+
if (showBanner)
|
|
25
|
+
console.log(MASCOT_BANNER);
|
|
26
|
+
console.error('Usage: pqc-scan <path> [--json] [--quiet] [--sarif <file>] [--cbom <file>] [--baseline <file>] [--write-baseline <file>] [--cache <file>] [--max-file-size <MB>]');
|
|
27
|
+
process.exit(2);
|
|
28
|
+
}
|
|
29
|
+
for (const [option, example] of [['cache', '~/.cache/pqc-scan.json'], ['sarif', 'pqc-scan.sarif'], ['cbom', 'cbom.json'], ['baseline', 'pqc-scan-baseline.json'], ['write-baseline', 'pqc-scan-baseline.json']]) {
|
|
30
|
+
if (values[option] !== undefined && typeof values[option] !== 'string') {
|
|
31
|
+
console.error(`pqc-scan: --${option} needs a file name, for example --${option} ${example}`);
|
|
32
|
+
process.exit(2);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const maxFileSize = values['max-file-size'] === undefined ? undefined : Number(values['max-file-size']);
|
|
36
|
+
if (maxFileSize !== undefined && !(maxFileSize > 0)) {
|
|
37
|
+
console.error('pqc-scan: --max-file-size needs a size in MB greater than 0, for example --max-file-size 50');
|
|
38
|
+
process.exit(2);
|
|
39
|
+
}
|
|
40
|
+
const path = resolve(target);
|
|
41
|
+
if (!existsSync(path)) {
|
|
42
|
+
console.error(`pqc-scan: path not found: ${path}`);
|
|
43
|
+
process.exit(2);
|
|
44
|
+
}
|
|
45
|
+
// A cache inside the folder being scanned could come from the code itself (a commit in a pull request), and a planted
|
|
46
|
+
// cache can hide findings. It is allowed there only when signed with a key (PQC_SCAN_CACHE_KEY) the code cannot know.
|
|
47
|
+
const cacheKey = process.env.PQC_SCAN_CACHE_KEY;
|
|
48
|
+
const cachePath = typeof values.cache === 'string' ? resolve(values.cache) : undefined;
|
|
49
|
+
if (cachePath && !cacheKey) {
|
|
50
|
+
const folder = statSync(path).isDirectory() ? path : dirname(path);
|
|
51
|
+
const inside = relative(folder, cachePath);
|
|
52
|
+
if (!inside.startsWith('..') && !isAbsolute(inside)) {
|
|
53
|
+
console.error(`pqc-scan: the cache file is inside the folder being scanned (${cachePath}). A cache there could be changed by the code being scanned to hide findings. Keep it outside, for example --cache ~/.cache/pqc-scan.json, or set PQC_SCAN_CACHE_KEY to sign it.`);
|
|
54
|
+
process.exit(2);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (showBanner)
|
|
58
|
+
console.log(MASCOT_BANNER);
|
|
59
|
+
await loadLanguages();
|
|
60
|
+
const cache = cachePath ? new ResultCache(cachePath, cacheKey) : undefined;
|
|
61
|
+
if (cache?.warning)
|
|
62
|
+
console.error(`pqc-scan: ${cache.warning}`);
|
|
63
|
+
let baseline;
|
|
64
|
+
try {
|
|
65
|
+
baseline = typeof values.baseline === 'string' ? readBaseline(resolve(values.baseline)) : undefined;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
console.error(`pqc-scan: ${error instanceof Error ? error.message : String(error)}`);
|
|
69
|
+
process.exit(2);
|
|
70
|
+
}
|
|
71
|
+
const findings = await scanInParallel(path, loadRules(), { cache, maxFileBytes: maxFileSize === undefined ? undefined : maxFileSize * 1024 * 1024 });
|
|
72
|
+
cache?.save();
|
|
73
|
+
const prints = baseline || values.sarif || values['write-baseline'] ? fingerprints(findings, path) : [];
|
|
74
|
+
if (typeof values['write-baseline'] === 'string')
|
|
75
|
+
writeFileSync(resolve(values['write-baseline']), `${JSON.stringify(toBaseline(findings, prints), null, 2)}\n`);
|
|
76
|
+
if (typeof values.sarif === 'string')
|
|
77
|
+
writeFileSync(resolve(values.sarif), JSON.stringify(toSarif(findings, prints, baseline)));
|
|
78
|
+
// The CBOM is the full inventory: every finding, whatever the baseline says.
|
|
79
|
+
if (typeof values.cbom === 'string')
|
|
80
|
+
writeFileSync(resolve(values.cbom), `${JSON.stringify(toCbom(findings, path), null, 2)}\n`);
|
|
81
|
+
// With a baseline, the findings it lists are left out of the output and do not change the exit status.
|
|
82
|
+
const shown = baseline ? findings.filter((_, index) => !baseline.has(prints[index])) : findings;
|
|
83
|
+
const known = baseline ? findings.length - shown.length : undefined;
|
|
84
|
+
if (json)
|
|
85
|
+
console.log(JSON.stringify(toReport(shown, known), null, 2));
|
|
86
|
+
else
|
|
87
|
+
printTable(shown, known);
|
|
88
|
+
// exitCode, not exit(): exiting at once would cut off output still being written to a pipe.
|
|
89
|
+
process.exitCode = shown.some((item) => item.status !== 'observed-resistant') ? 1 : 0;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { createHash, createHmac, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { existsSync, readFileSync, readdirSync, renameSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
const projectRoot = fileURLToPath(new URL('../../', import.meta.url));
|
|
7
|
+
/** The scanner's own code: `src` when working on it, `dist` in the published package. */
|
|
8
|
+
const codeFolder = fileURLToPath(new URL('../', import.meta.url)).slice(projectRoot.length).replace(/[\\/]$/, '');
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
/**
|
|
11
|
+
* The installed version of a package, found from the package's own entry point, since some packages (web-tree-sitter)
|
|
12
|
+
* do not let their package.json be loaded by name. Undefined when the package is not installed.
|
|
13
|
+
*/
|
|
14
|
+
export function installedVersion(name) {
|
|
15
|
+
let folder;
|
|
16
|
+
try {
|
|
17
|
+
folder = dirname(require.resolve(name));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
for (; folder !== dirname(folder); folder = dirname(folder)) {
|
|
23
|
+
const file = join(folder, 'package.json');
|
|
24
|
+
if (!existsSync(file))
|
|
25
|
+
continue;
|
|
26
|
+
const manifest = JSON.parse(readFileSync(file, 'utf8'));
|
|
27
|
+
if (manifest.name === name)
|
|
28
|
+
return manifest.version;
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A fingerprint of everything that decides what a scan finds: the scanner's own code, its rules, the grammars and the
|
|
34
|
+
* exact versions of the packages it reads code with. When any of them changes, every cached result is thrown away.
|
|
35
|
+
*/
|
|
36
|
+
function toolVersion() {
|
|
37
|
+
const hash = createHash('sha256');
|
|
38
|
+
const add = (directory) => {
|
|
39
|
+
for (const entry of readdirSync(join(projectRoot, directory), { withFileTypes: true }).sort((left, right) => left.name.localeCompare(right.name))) {
|
|
40
|
+
const path = join(directory, entry.name);
|
|
41
|
+
if (entry.isDirectory())
|
|
42
|
+
add(path);
|
|
43
|
+
else
|
|
44
|
+
hash.update(path).update(readFileSync(join(projectRoot, path)));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
add(codeFolder);
|
|
48
|
+
add('rules');
|
|
49
|
+
if (existsSync(join(projectRoot, 'grammars')))
|
|
50
|
+
add('grammars');
|
|
51
|
+
hash.update(readFileSync(join(projectRoot, 'package.json')));
|
|
52
|
+
// The grammar packages are not installed with the published package, which carries their files in grammars/.
|
|
53
|
+
for (const name of ['typescript', 'web-tree-sitter', 'yaml', 'tree-sitter-c', 'tree-sitter-cpp', 'tree-sitter-rust', 'tree-sitter-go']) {
|
|
54
|
+
hash.update(`${name}@${installedVersion(name) ?? 'not installed'}\n`);
|
|
55
|
+
}
|
|
56
|
+
return hash.digest('hex');
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Per-file findings saved between runs, keyed by a SHA-256 fingerprint of the file's name and contents. Only the
|
|
60
|
+
* files of the latest scan are kept, so the cache does not grow without limit.
|
|
61
|
+
*
|
|
62
|
+
* Whoever can write the cache file can change what a scan reports, so a cache is only as trustworthy as the place it
|
|
63
|
+
* is kept. With a key, the cache is signed (HMAC-SHA256) and a cache that was changed, or signed with another key, is
|
|
64
|
+
* thrown away: then only someone who has the key can write a cache this scanner will use.
|
|
65
|
+
*/
|
|
66
|
+
export class ResultCache {
|
|
67
|
+
version = toolVersion();
|
|
68
|
+
path;
|
|
69
|
+
key;
|
|
70
|
+
previous;
|
|
71
|
+
current = {};
|
|
72
|
+
/** Why an existing cache file was not used, if it was not. */
|
|
73
|
+
warning;
|
|
74
|
+
constructor(path, key) {
|
|
75
|
+
this.path = path;
|
|
76
|
+
this.key = key || undefined;
|
|
77
|
+
let stored;
|
|
78
|
+
let warning;
|
|
79
|
+
try {
|
|
80
|
+
const saved = existsSync(path) ? JSON.parse(readFileSync(path, 'utf8')) : undefined;
|
|
81
|
+
if (saved && this.key && (typeof saved.mac !== 'string' || !this.signatureMatches(saved.body, saved.mac))) {
|
|
82
|
+
warning = `the cache ${path} is not signed with this PQC_SCAN_CACHE_KEY, so it was not used.`;
|
|
83
|
+
}
|
|
84
|
+
else if (saved) {
|
|
85
|
+
stored = JSON.parse(saved.body);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
stored = undefined; // An unreadable cache is ignored and rewritten.
|
|
90
|
+
}
|
|
91
|
+
this.warning = warning;
|
|
92
|
+
this.previous = stored?.version === this.version ? stored.files : {};
|
|
93
|
+
}
|
|
94
|
+
sign(body) {
|
|
95
|
+
return createHmac('sha256', this.key).update(body).digest('hex');
|
|
96
|
+
}
|
|
97
|
+
signatureMatches(body, mac) {
|
|
98
|
+
if (typeof body !== 'string')
|
|
99
|
+
return false;
|
|
100
|
+
const expected = Buffer.from(this.sign(body), 'hex');
|
|
101
|
+
const given = Buffer.from(mac, 'hex');
|
|
102
|
+
return given.length === expected.length && timingSafeEqual(given, expected);
|
|
103
|
+
}
|
|
104
|
+
static key(name, text) {
|
|
105
|
+
return createHash('sha256').update(name).update('\0').update(text).digest('hex');
|
|
106
|
+
}
|
|
107
|
+
get(key) {
|
|
108
|
+
const found = Object.hasOwn(this.previous, key) ? this.previous[key] : undefined;
|
|
109
|
+
if (found)
|
|
110
|
+
this.current[key] = found;
|
|
111
|
+
return found;
|
|
112
|
+
}
|
|
113
|
+
set(key, findings) {
|
|
114
|
+
this.current[key] = findings;
|
|
115
|
+
}
|
|
116
|
+
/** Writes to a temporary file first, so an interrupted run never leaves a half-written cache. */
|
|
117
|
+
save() {
|
|
118
|
+
const temporary = `${this.path}.${process.pid}.tmp`;
|
|
119
|
+
const body = JSON.stringify({ version: this.version, files: this.current });
|
|
120
|
+
writeFileSync(temporary, JSON.stringify({ body, ...(this.key ? { mac: this.sign(body) } : {}) }));
|
|
121
|
+
renameSync(temporary, this.path);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Shared markers: the cross-file check in core recognises a "decided by the caller" finding by these. */
|
|
2
|
+
export const UNRESOLVED_ALGORITHM_NOTE = 'The algorithm comes from a value the tool cannot read here, such as a variable or a setting; check this line by hand.';
|
|
3
|
+
export const ESCAPING_REFERENCE_RULE_ID = 'crypto-reference-escapes';
|
|
4
|
+
export function finding(file, line, ruleId, category, algorithm, status, note, trace = []) {
|
|
5
|
+
return { file, line, ruleId, category, algorithm, status, note, trace };
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { UNRESOLVED_ALGORITHM_NOTE, finding } from "./finding.js";
|
|
2
|
+
/**
|
|
3
|
+
* Turns one call that matched `rule` into findings. Shared by every language so the meaning of a rule is identical
|
|
4
|
+
* everywhere. `resolve` is called only for rules that take the algorithm from an argument.
|
|
5
|
+
*/
|
|
6
|
+
export function ruleFindings(rule, file, line, trace, resolve) {
|
|
7
|
+
// `ignore` rules operate on an existing key; the algorithm is reported where the key is created or imported.
|
|
8
|
+
if (rule.ignore)
|
|
9
|
+
return [];
|
|
10
|
+
if (rule.algorithm && rule.status)
|
|
11
|
+
return [finding(file, line, rule.id, rule.category, rule.algorithm, rule.status, rule.note, trace)];
|
|
12
|
+
if (!rule.algorithmFrom)
|
|
13
|
+
return [finding(file, line, rule.id, rule.category, null, 'unresolved', rule.note, trace)];
|
|
14
|
+
const { names, qualifiers } = resolve();
|
|
15
|
+
if (!names)
|
|
16
|
+
return [finding(file, line, rule.id, rule.category, null, 'unresolved', UNRESOLVED_ALGORITHM_NOTE, trace)];
|
|
17
|
+
const values = rule.values ?? {};
|
|
18
|
+
return names.map((value) => {
|
|
19
|
+
const match = lookup(values, value, qualifiers);
|
|
20
|
+
return match
|
|
21
|
+
? finding(file, line, rule.id, rule.category, match.algorithm, match.status, match.note, trace)
|
|
22
|
+
: finding(file, line, rule.id, rule.category, null, 'unresolved', `Algorithm "${value}" has no rule.`, trace);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** Finds a value in a rule table: qualified forms first (`aes-gcm-256`), then exact, then lower case. */
|
|
26
|
+
export function lookup(values, value, qualifiers = []) {
|
|
27
|
+
const base = value.toLowerCase();
|
|
28
|
+
const key = [...qualifiers.map((item) => `${base}-${item.toLowerCase()}`), value, base].find((candidate) => Object.hasOwn(values, candidate));
|
|
29
|
+
return key === undefined ? undefined : values[key];
|
|
30
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ruleFindings } from "./match.js";
|
|
2
|
+
export function indexRules(groups) {
|
|
3
|
+
const index = { functions: new Map(), names: new Map(), mirrors: new Map() };
|
|
4
|
+
for (const group of groups) {
|
|
5
|
+
for (const rule of group.rules) {
|
|
6
|
+
for (const name of rule.functions)
|
|
7
|
+
if (!index.functions.has(name))
|
|
8
|
+
index.functions.set(name, rule);
|
|
9
|
+
for (const name of rule.names ?? [])
|
|
10
|
+
if (!index.names.has(name))
|
|
11
|
+
index.names.set(name, rule);
|
|
12
|
+
}
|
|
13
|
+
for (const [from, to] of Object.entries(group.mirrors ?? {}))
|
|
14
|
+
index.mirrors.set(from, to);
|
|
15
|
+
}
|
|
16
|
+
return index;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Collects one file's findings. A name used twice on one line (`std::unique_ptr<SHA256> p(new SHA256)`) is one use;
|
|
20
|
+
* every call is its own finding. The lines reported are remembered so the parse-error check does not repeat them.
|
|
21
|
+
*/
|
|
22
|
+
export class LineReporter {
|
|
23
|
+
seen = new Set();
|
|
24
|
+
file;
|
|
25
|
+
findings;
|
|
26
|
+
reportedLines;
|
|
27
|
+
// Plain fields, not constructor parameter properties: worker threads load this file with Node's type stripping,
|
|
28
|
+
// which does not support them.
|
|
29
|
+
constructor(file, findings = [], reportedLines = new Set()) {
|
|
30
|
+
this.file = file;
|
|
31
|
+
this.findings = findings;
|
|
32
|
+
this.reportedLines = reportedLines;
|
|
33
|
+
}
|
|
34
|
+
/** A use of a name: reported once per rule and line. */
|
|
35
|
+
name(rule, line, note) {
|
|
36
|
+
if (this.seen.has(`${line} ${rule.id}`))
|
|
37
|
+
return;
|
|
38
|
+
this.add(rule, line, ruleFindings(rule, this.file, line, [{ note }], () => ({ names: null, qualifiers: [] })));
|
|
39
|
+
}
|
|
40
|
+
/** A call, whose findings the caller has worked out from its arguments. */
|
|
41
|
+
call(rule, line, findings) {
|
|
42
|
+
this.add(rule, line, findings);
|
|
43
|
+
}
|
|
44
|
+
add(rule, line, findings) {
|
|
45
|
+
this.seen.add(`${line} ${rule.id}`);
|
|
46
|
+
this.reportedLines.add(line);
|
|
47
|
+
this.findings.push(...findings);
|
|
48
|
+
}
|
|
49
|
+
}
|