cbom-tools 0.1.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/LICENSE +198 -0
- package/README.md +99 -0
- package/algo-metadata.json +134 -0
- package/cbom-diff.js +353 -0
- package/cbom-scan.cjs +172 -0
- package/package.json +18 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work, or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communications that are conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (or agreed to in writing in
|
|
156
|
+
any event) shall any Contributor be liable to You for damages,
|
|
157
|
+
including any direct, indirect, special, incidental, or consequential
|
|
158
|
+
damages of any character arising as a result of this License or
|
|
159
|
+
out of the use or inability to use the Work (including but not limited
|
|
160
|
+
to damages for loss of goodwill, work stoppage, computer failure or
|
|
161
|
+
malfunction, or any and all other commercial damages or losses), even
|
|
162
|
+
if such Contributor has been advised of the possibility of such damages.
|
|
163
|
+
|
|
164
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
165
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
166
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
167
|
+
or other liability obligations and/or rights consistent with this
|
|
168
|
+
License. However, in accepting such obligations, You may act only
|
|
169
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
170
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
171
|
+
defend, and hold each Contributor harmless for any liability
|
|
172
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
173
|
+
of your accepting any such warranty or additional liability.
|
|
174
|
+
|
|
175
|
+
END OF TERMS AND CONDITIONS
|
|
176
|
+
|
|
177
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
178
|
+
|
|
179
|
+
To apply the Apache License to your work, attach the following
|
|
180
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
181
|
+
replaced with your own identifying information. (Don't include
|
|
182
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
183
|
+
comment syntax for the file format. We also recommend that a
|
|
184
|
+
file or class name and bibliography
|
|
185
|
+
|
|
186
|
+
Copyright [yyyy] [name of copyright owner]
|
|
187
|
+
|
|
188
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
189
|
+
you may not use this file except in compliance with the License.
|
|
190
|
+
You may obtain a copy of the License at
|
|
191
|
+
|
|
192
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
193
|
+
|
|
194
|
+
Unless required by applicable law or agreed to in writing, software
|
|
195
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
196
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
197
|
+
See the License for the specific language governing permissions and
|
|
198
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# cbom-tools
|
|
2
|
+
|
|
3
|
+
**Cryptographic Bill of Materials — scan, diff, report.**
|
|
4
|
+
|
|
5
|
+
Scan a directory tree for cryptographic assets, emit a
|
|
6
|
+
[CycloneDX 1.6](https://cyclonedx.org/) CBOM, and diff two CBOMs to
|
|
7
|
+
detect algorithm additions, removals, and metadata changes.
|
|
8
|
+
|
|
9
|
+
Zero external dependencies. Pure Node.js.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm install -g cbom-tools
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### 1. Scan a project
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
cbom-scan --dir ./my-project --out cbom.json
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Scans:
|
|
26
|
+
- `package.json` dependencies (declarative rule table)
|
|
27
|
+
- Source files (`.js/.cjs/.mjs/.ts/.tsx/.jsx`) via regex patterns
|
|
28
|
+
|
|
29
|
+
Output: a CycloneDX 1.6 JSON with `cryptoProperties` (asset type,
|
|
30
|
+
primitive, quantum security level) and `evidence.occurrences` (file
|
|
31
|
+
paths + line numbers, capped at 50 per algorithm, deduplicated).
|
|
32
|
+
|
|
33
|
+
### 2. Diff two CBOMs
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
cbom-diff cbom-before.json cbom-after.json
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Exit codes:
|
|
40
|
+
- `0` — no changes
|
|
41
|
+
- `1` — changes detected
|
|
42
|
+
- `2` — file not found or parse error
|
|
43
|
+
|
|
44
|
+
## Example (real output)
|
|
45
|
+
|
|
46
|
+
Scanning a repository before and after a PQC migration:
|
|
47
|
+
|
|
48
|
+
```
|
|
49
|
+
$ cbom-scan --dir ./repo-before --out before.json
|
|
50
|
+
cbom-scan: wrote 11 algorithms to before.json
|
|
51
|
+
|
|
52
|
+
$ cbom-scan --dir ./repo-after --out after.json
|
|
53
|
+
cbom-scan: wrote 13 algorithms to after.json
|
|
54
|
+
|
|
55
|
+
$ cbom-diff before.json after.json
|
|
56
|
+
## CBOM Diff
|
|
57
|
+
|
|
58
|
+
Removed: none
|
|
59
|
+
Added: Keccak-256, SHA3-512
|
|
60
|
+
|
|
61
|
+
Exit code: 1
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Detected algorithms
|
|
65
|
+
|
|
66
|
+
Rule table covers 13 algorithms:
|
|
67
|
+
|
|
68
|
+
| Primitive | Algorithms |
|
|
69
|
+
|---|---|
|
|
70
|
+
| KEM | ML-KEM-768 (and aliases) |
|
|
71
|
+
| Signature | ML-DSA-65, SLH-DSA, SM2 |
|
|
72
|
+
| Hash | SHA-256, SHA3-256, SHA3-512, Keccak-256, SM3 |
|
|
73
|
+
| Symmetric | SM4 |
|
|
74
|
+
| Other | (extensible via source rules) |
|
|
75
|
+
|
|
76
|
+
Detection input:
|
|
77
|
+
- Package names (`@noble/post-quantum`, `sm-crypto`, etc.)
|
|
78
|
+
- API call patterns (`crypto.createHash('sha3-256')`, etc.)
|
|
79
|
+
|
|
80
|
+
## What this tool does NOT do
|
|
81
|
+
|
|
82
|
+
- ❌ Does NOT implement cryptographic algorithms
|
|
83
|
+
- ❌ Does NOT provide cryptographic operations (no keygen, encrypt, sign)
|
|
84
|
+
- ❌ Does NOT read or verify keys, certificates, or signatures
|
|
85
|
+
- ❌ Does NOT constitute a compliance certificate or audit
|
|
86
|
+
|
|
87
|
+
All cryptographic information is derived from CycloneDX SBOM
|
|
88
|
+
specification and public algorithm parameters. Static pattern matching
|
|
89
|
+
only — no cryptographic computation is performed.
|
|
90
|
+
|
|
91
|
+
## Standards
|
|
92
|
+
|
|
93
|
+
Implements the [CycloneDX 1.6](https://cyclonedx.org/specification/overview/)
|
|
94
|
+
CBOM specification, maintained by [OWASP](https://owasp.org/) under
|
|
95
|
+
Apache-2.0.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
Apache-2.0
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
{
|
|
2
|
+
"ML-KEM": {
|
|
3
|
+
"assetType": "algorithm",
|
|
4
|
+
"algorithmProperties": {
|
|
5
|
+
"primitive": "KEM"
|
|
6
|
+
},
|
|
7
|
+
"implementationPlatform": "javascript",
|
|
8
|
+
"certificationLevel": "known-answer-tested",
|
|
9
|
+
"quantumSecurity": {
|
|
10
|
+
"level": "safe"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"NTT": {
|
|
14
|
+
"assetType": "algorithm",
|
|
15
|
+
"algorithmProperties": {
|
|
16
|
+
"primitive": "MATH"
|
|
17
|
+
},
|
|
18
|
+
"implementationPlatform": "javascript",
|
|
19
|
+
"certificationLevel": "known-answer-tested",
|
|
20
|
+
"quantumSecurity": {
|
|
21
|
+
"level": "safe"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"SHA-256": {
|
|
25
|
+
"assetType": "algorithm",
|
|
26
|
+
"algorithmProperties": {
|
|
27
|
+
"primitive": "HASH"
|
|
28
|
+
},
|
|
29
|
+
"implementationPlatform": "javascript",
|
|
30
|
+
"certificationLevel": "known-answer-tested",
|
|
31
|
+
"quantumSecurity": {
|
|
32
|
+
"level": "weak"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"SM2": {
|
|
36
|
+
"assetType": "algorithm",
|
|
37
|
+
"algorithmProperties": {
|
|
38
|
+
"primitive": "SIGN"
|
|
39
|
+
},
|
|
40
|
+
"implementationPlatform": "javascript",
|
|
41
|
+
"certificationLevel": "known-answer-tested",
|
|
42
|
+
"quantumSecurity": {
|
|
43
|
+
"level": "vulnerable"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"P-256/ECDH": {
|
|
47
|
+
"assetType": "algorithm",
|
|
48
|
+
"algorithmProperties": {
|
|
49
|
+
"primitive": "ECC"
|
|
50
|
+
},
|
|
51
|
+
"implementationPlatform": "javascript",
|
|
52
|
+
"certificationLevel": "known-answer-tested",
|
|
53
|
+
"quantumSecurity": {
|
|
54
|
+
"level": "vulnerable"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
"Double-Ratchet": {
|
|
58
|
+
"assetType": "algorithm",
|
|
59
|
+
"algorithmProperties": {
|
|
60
|
+
"primitive": "PROTO"
|
|
61
|
+
},
|
|
62
|
+
"implementationPlatform": "javascript",
|
|
63
|
+
"certificationLevel": "known-answer-tested",
|
|
64
|
+
"quantumSecurity": {
|
|
65
|
+
"level": "weak"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
"SM3": {
|
|
69
|
+
"assetType": "algorithm",
|
|
70
|
+
"algorithmProperties": {
|
|
71
|
+
"primitive": "HASH"
|
|
72
|
+
},
|
|
73
|
+
"implementationPlatform": "javascript",
|
|
74
|
+
"certificationLevel": "known-answer-tested",
|
|
75
|
+
"quantumSecurity": {
|
|
76
|
+
"level": "weak"
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"ML-DSA/fml-dsa": {
|
|
80
|
+
"assetType": "algorithm",
|
|
81
|
+
"algorithmProperties": {
|
|
82
|
+
"primitive": "SIGN"
|
|
83
|
+
},
|
|
84
|
+
"implementationPlatform": "javascript",
|
|
85
|
+
"certificationLevel": "known-answer-tested",
|
|
86
|
+
"quantumSecurity": {
|
|
87
|
+
"level": "safe"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
"SM4": {
|
|
91
|
+
"assetType": "algorithm",
|
|
92
|
+
"algorithmProperties": {
|
|
93
|
+
"primitive": "SYM"
|
|
94
|
+
},
|
|
95
|
+
"implementationPlatform": "javascript",
|
|
96
|
+
"certificationLevel": "known-answer-tested",
|
|
97
|
+
"quantumSecurity": {
|
|
98
|
+
"level": "weak"
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
"AES": {
|
|
102
|
+
"assetType": "algorithm",
|
|
103
|
+
"algorithmProperties": {
|
|
104
|
+
"primitive": "SYM"
|
|
105
|
+
},
|
|
106
|
+
"implementationPlatform": "javascript",
|
|
107
|
+
"certificationLevel": "known-answer-tested",
|
|
108
|
+
"quantumSecurity": {
|
|
109
|
+
"level": "safe"
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
"SLH-DSA": {
|
|
113
|
+
"assetType": "algorithm",
|
|
114
|
+
"algorithmProperties": {
|
|
115
|
+
"primitive": "SIGN"
|
|
116
|
+
},
|
|
117
|
+
"implementationPlatform": "javascript",
|
|
118
|
+
"certificationLevel": "known-answer-tested",
|
|
119
|
+
"quantumSecurity": {
|
|
120
|
+
"level": "safe"
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
"TLA+": {
|
|
124
|
+
"assetType": "algorithm",
|
|
125
|
+
"algorithmProperties": {
|
|
126
|
+
"primitive": "PROTO"
|
|
127
|
+
},
|
|
128
|
+
"implementationPlatform": "javascript",
|
|
129
|
+
"certificationLevel": "known-answer-tested",
|
|
130
|
+
"quantumSecurity": {
|
|
131
|
+
"level": "safe"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
package/cbom-diff.js
ADDED
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Copyright (c) 2026 刘天赫
|
|
4
|
+
/**
|
|
5
|
+
* CBOM Diff — 加密材料清单变更检测器
|
|
6
|
+
*
|
|
7
|
+
* 用法:
|
|
8
|
+
* node tools/cbom-diff.js # diff HEAD~1 vs HEAD
|
|
9
|
+
* node tools/cbom-diff.js <old-hash> <new-hash> # diff 两个 commit
|
|
10
|
+
* node tools/cbom-diff.js --json # 输出 JSON(CI 友好)
|
|
11
|
+
* node tools/cbom-diff.js --markdown # 输出 Markdown(人类可读,默认)
|
|
12
|
+
*
|
|
13
|
+
* CI 集成:
|
|
14
|
+
* 每次 push 后运行,检测算法新增/删减/属性变更
|
|
15
|
+
*
|
|
16
|
+
* 退出码:
|
|
17
|
+
* 0 — 无变更
|
|
18
|
+
* 1 — 有信息性变更(算法属性升级等)
|
|
19
|
+
* 2 — 有警告性变更(算法删除、新经典算法引用等)
|
|
20
|
+
* 3 — 有严重变更(量子脆弱算法被引入)
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const { execSync } = require('child_process');
|
|
24
|
+
const fs = require('fs');
|
|
25
|
+
const path = require('path');
|
|
26
|
+
|
|
27
|
+
// ════════════════════════════
|
|
28
|
+
// 命令行解析
|
|
29
|
+
// ════════════════════════════
|
|
30
|
+
const args = process.argv.slice(2);
|
|
31
|
+
let oldHash = null, newHash = null;
|
|
32
|
+
let format = 'markdown'; // 'markdown' | 'json'
|
|
33
|
+
let gitMode = false;
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < args.length; i++) {
|
|
36
|
+
const a = args[i];
|
|
37
|
+
if (a === '--json') { format = 'json'; }
|
|
38
|
+
else if (a === '--markdown') { format = 'markdown'; }
|
|
39
|
+
else if (a === '--git') { gitMode = true; }
|
|
40
|
+
else if (!oldHash) { oldHash = a; }
|
|
41
|
+
else { newHash = a; }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!oldHash) oldHash = 'HEAD~1';
|
|
45
|
+
if (!newHash) newHash = 'HEAD';
|
|
46
|
+
|
|
47
|
+
// ════════════════════════════
|
|
48
|
+
// CBOM 加载
|
|
49
|
+
// ════════════════════════════
|
|
50
|
+
function loadCBOM(hash) {
|
|
51
|
+
// 1. File path mode: .json extension → read file directly
|
|
52
|
+
if (hash && hash.toLowerCase().endsWith('.json')) {
|
|
53
|
+
const filePath = path.resolve(hash);
|
|
54
|
+
if (!fs.existsSync(filePath)) {
|
|
55
|
+
console.error(`✕ cbom-diff: file not found: ${filePath}`);
|
|
56
|
+
process.exit(2);
|
|
57
|
+
}
|
|
58
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
|
|
59
|
+
}
|
|
60
|
+
// 2. Git mode
|
|
61
|
+
if (gitMode) {
|
|
62
|
+
try {
|
|
63
|
+
const raw = execSync(`git show ${hash}:tools/cbom-cyclonedx.json`, {
|
|
64
|
+
encoding: 'utf-8',
|
|
65
|
+
maxBuffer: 10 * 1024 * 1024,
|
|
66
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
67
|
+
});
|
|
68
|
+
return JSON.parse(raw);
|
|
69
|
+
} catch (e) {
|
|
70
|
+
console.error(`✕ Cannot load CBOM from git ${hash}: ${e.message}`);
|
|
71
|
+
process.exit(4);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
// 3. Default: load from local file
|
|
75
|
+
try {
|
|
76
|
+
const local = path.join(__dirname, 'cbom-cyclonedx.json');
|
|
77
|
+
return JSON.parse(fs.readFileSync(local, 'utf-8'));
|
|
78
|
+
} catch (e) {
|
|
79
|
+
console.error(`✕ Cannot load CBOM from local file: ${e.message}`);
|
|
80
|
+
process.exit(4);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const oldCBOM = loadCBOM(oldHash);
|
|
85
|
+
const newCBOM = loadCBOM(newHash);
|
|
86
|
+
|
|
87
|
+
// ════════════════════════════
|
|
88
|
+
// 比较逻辑
|
|
89
|
+
// ════════════════════════════
|
|
90
|
+
const OLD = oldCBOM ? oldCBOM.version || 0 : 0;
|
|
91
|
+
const NEW = newCBOM ? newCBOM.version || 0 : 0;
|
|
92
|
+
|
|
93
|
+
const diff = {
|
|
94
|
+
meta: {
|
|
95
|
+
oldHash: oldHash,
|
|
96
|
+
newHash: newHash,
|
|
97
|
+
oldVersion: OLD,
|
|
98
|
+
newVersion: NEW,
|
|
99
|
+
date: new Date().toISOString(),
|
|
100
|
+
},
|
|
101
|
+
version: NEW > OLD ? 'bumped' : (NEW < OLD ? 'downgraded' : 'same'),
|
|
102
|
+
components: { added: [], removed: [], changed: [], unchanged: 0 },
|
|
103
|
+
dependencies: { added: [], removed: [] },
|
|
104
|
+
severity: 'none', // 'none' | 'info' | 'warning' | 'critical'
|
|
105
|
+
exitCode: 0,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// --- 组件比较 ---
|
|
109
|
+
if (oldCBOM && newCBOM) {
|
|
110
|
+
const oldMap = new Map();
|
|
111
|
+
oldCBOM.components.forEach(c => oldMap.set(c['bom-ref'] || c.name, c));
|
|
112
|
+
const newMap = new Map();
|
|
113
|
+
newCBOM.components.forEach(c => newMap.set(c['bom-ref'] || c.name, c));
|
|
114
|
+
|
|
115
|
+
// Added
|
|
116
|
+
newMap.forEach((c, ref) => {
|
|
117
|
+
if (!oldMap.has(ref)) {
|
|
118
|
+
const crypto = c.cryptoProperties || {};
|
|
119
|
+
const assetType = crypto.assetType || c.type;
|
|
120
|
+
diff.components.added.push({
|
|
121
|
+
ref,
|
|
122
|
+
name: c.name,
|
|
123
|
+
type: assetType,
|
|
124
|
+
risk: classifyRisk(assetType),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Removed
|
|
130
|
+
oldMap.forEach((c, ref) => {
|
|
131
|
+
if (!newMap.has(ref)) {
|
|
132
|
+
diff.components.removed.push({
|
|
133
|
+
ref,
|
|
134
|
+
name: c.name,
|
|
135
|
+
type: (c.cryptoProperties || {}).assetType || c.type,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Changed
|
|
141
|
+
newMap.forEach((nc, ref) => {
|
|
142
|
+
const oc = oldMap.get(ref);
|
|
143
|
+
if (!oc) return;
|
|
144
|
+
const changes = compareProperties(oc, nc);
|
|
145
|
+
if (changes.length > 0) {
|
|
146
|
+
diff.components.changed.push({ ref, name: nc.name, changes });
|
|
147
|
+
} else {
|
|
148
|
+
diff.components.unchanged++;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
} else if (!oldCBOM && newCBOM) {
|
|
152
|
+
diff.components.added = newCBOM.components.map(c => ({
|
|
153
|
+
ref: c['bom-ref'],
|
|
154
|
+
name: c.name,
|
|
155
|
+
type: (c.cryptoProperties || {}).assetType || c.type,
|
|
156
|
+
risk: 'info',
|
|
157
|
+
}));
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// --- 依赖关系比较 ---
|
|
161
|
+
if (oldCBOM && newCBOM) {
|
|
162
|
+
const oldDepMap = new Map();
|
|
163
|
+
;(oldCBOM.dependencies || []).forEach(d => oldDepMap.set(d.ref, (d.dependsOn || []).sort().join(',')));
|
|
164
|
+
|
|
165
|
+
const newDepMap = new Map();
|
|
166
|
+
;(newCBOM.dependencies || []).forEach(d => newDepMap.set(d.ref, (d.dependsOn || []).sort().join(',')));
|
|
167
|
+
|
|
168
|
+
// Added deps
|
|
169
|
+
newDepMap.forEach((deps, ref) => {
|
|
170
|
+
if (!oldDepMap.has(ref)) {
|
|
171
|
+
diff.dependencies.added.push({ ref, dependsOn: deps });
|
|
172
|
+
} else if (oldDepMap.get(ref) !== deps) {
|
|
173
|
+
diff.dependencies.added.push({ ref, dependsOn: deps, previous: oldDepMap.get(ref) });
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Removed deps
|
|
178
|
+
oldDepMap.forEach((deps, ref) => {
|
|
179
|
+
if (!newDepMap.has(ref)) {
|
|
180
|
+
diff.dependencies.removed.push({ ref, dependsOn: deps });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// --- 严重性判定 ---
|
|
186
|
+
const hasCriticalAdd = diff.components.added.some(c => c.risk === 'critical');
|
|
187
|
+
const hasWarningAdd = diff.components.added.some(c => c.risk === 'warning');
|
|
188
|
+
const hasRemoval = diff.components.removed.length > 0;
|
|
189
|
+
const hasChanges = diff.components.changed.length > 0;
|
|
190
|
+
const hasAdded = diff.components.added.length > 0;
|
|
191
|
+
|
|
192
|
+
if (hasCriticalAdd) {
|
|
193
|
+
diff.severity = 'critical';
|
|
194
|
+
diff.exitCode = 3;
|
|
195
|
+
} else if (hasRemoval || hasWarningAdd) {
|
|
196
|
+
diff.severity = 'warning';
|
|
197
|
+
diff.exitCode = 2;
|
|
198
|
+
} else if (hasAdded || hasChanges) {
|
|
199
|
+
diff.severity = 'info';
|
|
200
|
+
diff.exitCode = 1;
|
|
201
|
+
} else {
|
|
202
|
+
diff.severity = 'none';
|
|
203
|
+
diff.exitCode = 0;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ════════════════════════════
|
|
207
|
+
// 算法风险分类
|
|
208
|
+
// ════════════════════════════
|
|
209
|
+
function classifyRisk(assetType) {
|
|
210
|
+
const type = (assetType || '').toLowerCase();
|
|
211
|
+
// PQC algorithms: safe additions
|
|
212
|
+
if (/kem|sign|signature|post.quantum|pqc|mlkem|mldsa|slh/i.test(type)) return 'info';
|
|
213
|
+
// Primitive/verification: neutral
|
|
214
|
+
if (/ntt|sha|hash|primitive|verification|protocol/i.test(type)) return 'info';
|
|
215
|
+
// Classic ECC: warning
|
|
216
|
+
if (/ecc|ecdh|p-256|sm2|curve/i.test(type)) return 'warning';
|
|
217
|
+
// Classic symmetric: info (still safe with large keys)
|
|
218
|
+
if (/sym|sm4|aes|gcm/i.test(type)) return 'info';
|
|
219
|
+
// Unknown: treat as warning
|
|
220
|
+
return 'warning';
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ════════════════════════════
|
|
224
|
+
// 组件属性对比
|
|
225
|
+
// ════════════════════════════
|
|
226
|
+
function compareProperties(oldC, newC) {
|
|
227
|
+
const changes = [];
|
|
228
|
+
const oldCp = oldC.cryptoProperties || {};
|
|
229
|
+
const newCp = newC.cryptoProperties || {};
|
|
230
|
+
|
|
231
|
+
// Check assetType change
|
|
232
|
+
if (oldCp.assetType !== newCp.assetType) {
|
|
233
|
+
changes.push({ field: 'assetType', old: oldCp.assetType, new: newCp.assetType });
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Check algorithm level
|
|
237
|
+
const oldAlg = (oldCp.algorithmProperties || {});
|
|
238
|
+
const newAlg = (newCp.algorithmProperties || {});
|
|
239
|
+
if (oldAlg.classicalSecurityLevel !== newAlg.classicalSecurityLevel) {
|
|
240
|
+
changes.push({ field: 'classicalSecurityLevel', old: oldAlg.classicalSecurityLevel, new: newAlg.classicalSecurityLevel });
|
|
241
|
+
}
|
|
242
|
+
if (oldAlg.nistQuantumSecurityLevel !== newAlg.nistQuantumSecurityLevel) {
|
|
243
|
+
changes.push({ field: 'nistQuantumSecurityLevel', old: oldAlg.nistQuantumSecurityLevel, new: newAlg.nistQuantumSecurityLevel });
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Check evidence
|
|
247
|
+
const oldEvidence = oldC.evidence || {};
|
|
248
|
+
const newEvidence = newC.evidence || {};
|
|
249
|
+
if (oldEvidence.count !== newEvidence.count) {
|
|
250
|
+
changes.push({ field: 'evidence.count', old: oldEvidence.count, new: newEvidence.count });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// version bump
|
|
254
|
+
if (oldC.version !== newC.version && oldC.version != null && newC.version != null) {
|
|
255
|
+
changes.push({ field: 'version', old: oldC.version, new: newC.version });
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return changes;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// ════════════════════════════
|
|
262
|
+
// 输出
|
|
263
|
+
// ════════════════════════════
|
|
264
|
+
if (format === 'json') {
|
|
265
|
+
console.log(JSON.stringify(diff, null, 2));
|
|
266
|
+
} else {
|
|
267
|
+
// Markdown
|
|
268
|
+
const lines = [];
|
|
269
|
+
lines.push(`## CBOM Diff \`${oldHash.substring(0,8)} → ${newHash.substring(0,8)}\``);
|
|
270
|
+
lines.push('');
|
|
271
|
+
lines.push(`| 属性 | 值 |`);
|
|
272
|
+
lines.push(`|------|-----|`);
|
|
273
|
+
lines.push(`| 旧版本 | ${OLD} |`);
|
|
274
|
+
lines.push(`| 新版本 | ${NEW} |`);
|
|
275
|
+
lines.push(`| 版本 | ${diff.version} |`);
|
|
276
|
+
lines.push(`| 严重性 | **${diff.severity}** (exit=${diff.exitCode}) |`);
|
|
277
|
+
lines.push('');
|
|
278
|
+
|
|
279
|
+
// Added components
|
|
280
|
+
if (diff.components.added.length > 0) {
|
|
281
|
+
lines.push('### ➕ 新增算法');
|
|
282
|
+
lines.push('');
|
|
283
|
+
lines.push('| 名称 | 类型 | 风险 |');
|
|
284
|
+
lines.push('|------|------|------|');
|
|
285
|
+
diff.components.added.forEach(c => {
|
|
286
|
+
const riskEmoji = c.risk === 'critical' ? '🔴' : c.risk === 'warning' ? '🟡' : '🟢';
|
|
287
|
+
lines.push(`| ${c.name} | ${c.type} | ${riskEmoji} ${c.risk} |`);
|
|
288
|
+
});
|
|
289
|
+
lines.push('');
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Removed
|
|
293
|
+
if (diff.components.removed.length > 0) {
|
|
294
|
+
lines.push('### ➖ 删除算法');
|
|
295
|
+
lines.push('');
|
|
296
|
+
lines.push('| 名称 | 类型 |');
|
|
297
|
+
lines.push('|------|------|');
|
|
298
|
+
diff.components.removed.forEach(c => {
|
|
299
|
+
lines.push(`| ${c.name} | ${c.type} |`);
|
|
300
|
+
});
|
|
301
|
+
lines.push('');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Changed
|
|
305
|
+
if (diff.components.changed.length > 0) {
|
|
306
|
+
lines.push(`### 🔄 属性变更 (${diff.components.changed.length} 项)`);
|
|
307
|
+
lines.push('');
|
|
308
|
+
diff.components.changed.forEach(c => {
|
|
309
|
+
lines.push(`**${c.name}**`);
|
|
310
|
+
c.changes.forEach(ch => {
|
|
311
|
+
lines.push(`- \`${ch.field}\`: \`${ch.old}\` → \`${ch.new}\``);
|
|
312
|
+
});
|
|
313
|
+
lines.push('');
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Dependencies
|
|
318
|
+
if (diff.dependencies.added.length > 0 || diff.dependencies.removed.length > 0) {
|
|
319
|
+
lines.push('### 🔗 依赖关系变更');
|
|
320
|
+
lines.push('');
|
|
321
|
+
if (diff.dependencies.added.length > 0) {
|
|
322
|
+
diff.dependencies.added.forEach(d => {
|
|
323
|
+
lines.push(`- ➕ \`${d.ref}\` → ${d.dependsOn || '(none)'}`);
|
|
324
|
+
if (d.previous) lines.push(` (先前: ${d.previous})`);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
if (diff.dependencies.removed.length > 0) {
|
|
328
|
+
diff.dependencies.removed.forEach(d => {
|
|
329
|
+
lines.push(`- ➖ \`${d.ref}\` ← ${d.dependsOn || '(none)'}`);
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
lines.push('');
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Summary
|
|
336
|
+
if (diff.severity === 'none') {
|
|
337
|
+
lines.push('### ✅ 无变更');
|
|
338
|
+
lines.push(`> 前后 CBOM 完全一致,${diff.components.unchanged} 算法无变化。`);
|
|
339
|
+
} else if (diff.severity === 'info') {
|
|
340
|
+
lines.push(`### ℹ️ 信息性变更`);
|
|
341
|
+
lines.push(`> ${diff.components.unchanged} 算法保持原样。(${diff.meta.date})`);
|
|
342
|
+
} else if (diff.severity === 'warning') {
|
|
343
|
+
lines.push('### ⚠️ 警告');
|
|
344
|
+
lines.push('> 检测到值得关注的变更,请确认是否符合预期。');
|
|
345
|
+
} else {
|
|
346
|
+
lines.push('### 🔴 严重');
|
|
347
|
+
lines.push('> 检测到量子脆弱算法被引入,**请立即审查**。');
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
console.log(lines.join('\n'));
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
process.exit(diff.exitCode);
|
package/cbom-scan.cjs
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
// Copyright (c) 2026 刘天赫
|
|
4
|
+
//
|
|
5
|
+
// cbom-scan.cjs — scan a directory tree for cryptographic assets,
|
|
6
|
+
// emit CycloneDX 1.6 CBOM.
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
|
|
13
|
+
const ALGO_META = JSON.parse(fs.readFileSync(path.join(__dirname, 'algo-metadata.json'), 'utf-8'));
|
|
14
|
+
|
|
15
|
+
const NAME_ALIASES = {
|
|
16
|
+
'ML-KEM-768': 'ML-KEM',
|
|
17
|
+
'ML-KEM-512': 'ML-KEM',
|
|
18
|
+
'ML-KEM-1024': 'ML-KEM',
|
|
19
|
+
'ML-DSA': 'ML-DSA/fml-dsa',
|
|
20
|
+
'ML-DSA-44': 'ML-DSA/fml-dsa',
|
|
21
|
+
'ML-DSA-65': 'ML-DSA/fml-dsa',
|
|
22
|
+
'ML-DSA-87': 'ML-DSA/fml-dsa',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function lookupMeta(name) {
|
|
26
|
+
if (ALGO_META[name]) return ALGO_META[name];
|
|
27
|
+
const alias = NAME_ALIASES[name];
|
|
28
|
+
if (alias && ALGO_META[alias]) return ALGO_META[alias];
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const DEFAULT_RULES = {
|
|
33
|
+
packages: {
|
|
34
|
+
'@noble/post-quantum': ['ML-KEM-768', 'ML-DSA-65', 'SLH-DSA'],
|
|
35
|
+
'@noble/hashes': ['SHA3-256', 'SHA3-512', 'Keccak-256'],
|
|
36
|
+
'sm-crypto': ['SM2', 'SM3', 'SM4'],
|
|
37
|
+
'sm2-crypto': ['SM2'],
|
|
38
|
+
jsbn: ['RSA'],
|
|
39
|
+
'@fibemate/sm2-crypto': ['SM2'],
|
|
40
|
+
'@fibemate/keccak': ['Keccak-256', 'SHA3-256'],
|
|
41
|
+
},
|
|
42
|
+
sourcePatterns: [
|
|
43
|
+
{ re: /require\(['"]sm-crypto['"]\)/g, algs: ['SM2', 'SM3', 'SM4'] },
|
|
44
|
+
{ re: /crypto\.createHash\(['"]sha3-(\d+)['"]\)/g, algs: ['SHA3-$1'] },
|
|
45
|
+
{ re: /crypto\.createHash\(['"]sha-?256['"]\)/g, algs: ['SHA-256'] },
|
|
46
|
+
{ re: /crypto\.createHash\(['"]sha-?512['"]\)/g, algs: ['SHA-512'] },
|
|
47
|
+
{ re: /noble.*post-?quantum/gi, algs: ['ML-KEM', 'ML-DSA'] },
|
|
48
|
+
{ re: /\bml[-_]?kem\b/gi, algs: ['ML-KEM'] },
|
|
49
|
+
{ re: /\bml[-_]?dsa\b/gi, algs: ['ML-DSA'] },
|
|
50
|
+
{ re: /\bslh[-_]?dsa\b/gi, algs: ['SLH-DSA'] },
|
|
51
|
+
{ re: /\bkeccak\b/gi, algs: ['Keccak'] },
|
|
52
|
+
{ re: /\bsm2\b/gi, algs: ['SM2'] },
|
|
53
|
+
{ re: /\bsm3\b/gi, algs: ['SM3'] },
|
|
54
|
+
{ re: /\bsm4\b/gi, algs: ['SM4'] },
|
|
55
|
+
],
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', 'build', 'coverage', '.git', '.cache']);
|
|
59
|
+
const SCAN_EXT = /\.(cjs|mjs|js|ts|tsx|jsx)$/;
|
|
60
|
+
const MAX_OCC_PER_ALGO = 50;
|
|
61
|
+
|
|
62
|
+
function loadPackageDeps(dir) {
|
|
63
|
+
const pkgPath = path.join(dir, 'package.json');
|
|
64
|
+
if (!fs.existsSync(pkgPath)) return {};
|
|
65
|
+
try {
|
|
66
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
67
|
+
return { ...(pkg.dependencies || {}), ...(pkg.devDependencies || {}) };
|
|
68
|
+
} catch {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function matchDeps(deps, rules) {
|
|
74
|
+
const found = new Set();
|
|
75
|
+
for (const name of Object.keys(deps)) {
|
|
76
|
+
if (rules.packages[name]) rules.packages[name].forEach(a => found.add(a));
|
|
77
|
+
}
|
|
78
|
+
return found;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function scanSource(root, rules) {
|
|
82
|
+
const found = new Map();
|
|
83
|
+
|
|
84
|
+
function addOcc(name, file, line) {
|
|
85
|
+
let arr = found.get(name);
|
|
86
|
+
if (!arr) { arr = []; found.set(name, arr); }
|
|
87
|
+
if (arr.length >= MAX_OCC_PER_ALGO) return;
|
|
88
|
+
const loc = path.relative(root, file).replace(/\\/g, '/');
|
|
89
|
+
if (arr.some(o => o.location === loc && o.line === line)) return;
|
|
90
|
+
arr.push({ location: loc, line });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function walk(d) {
|
|
94
|
+
let entries;
|
|
95
|
+
try { entries = fs.readdirSync(d, { withFileTypes: true }); }
|
|
96
|
+
catch { return; }
|
|
97
|
+
for (const entry of entries) {
|
|
98
|
+
if (entry.name.startsWith('.') || SKIP_DIRS.has(entry.name)) continue;
|
|
99
|
+
const full = path.join(d, entry.name);
|
|
100
|
+
if (entry.isDirectory()) walk(full);
|
|
101
|
+
else if (SCAN_EXT.test(entry.name)) {
|
|
102
|
+
let content;
|
|
103
|
+
try { content = fs.readFileSync(full, 'utf-8'); }
|
|
104
|
+
catch { continue; }
|
|
105
|
+
for (const { re, algs } of rules.sourcePatterns) {
|
|
106
|
+
re.lastIndex = 0;
|
|
107
|
+
let m;
|
|
108
|
+
while ((m = re.exec(content)) !== null) {
|
|
109
|
+
const line = content.slice(0, m.index).split('\n').length;
|
|
110
|
+
for (const a of algs) {
|
|
111
|
+
const name = a.replace(/\$(\d+)/g, (_, i) => m[i]);
|
|
112
|
+
addOcc(name, full, line);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
walk(root);
|
|
120
|
+
return found;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function toCycloneDX(algorithms) {
|
|
124
|
+
return {
|
|
125
|
+
bomFormat: 'CycloneDX',
|
|
126
|
+
specVersion: '1.6',
|
|
127
|
+
version: 1,
|
|
128
|
+
metadata: {
|
|
129
|
+
timestamp: new Date().toISOString(),
|
|
130
|
+
tools: [{ name: 'cbom-scan', version: '0.1.0', vendor: 'FIBEMATE' }],
|
|
131
|
+
},
|
|
132
|
+
components: [...algorithms.keys()].sort().map(name => {
|
|
133
|
+
const meta = lookupMeta(name);
|
|
134
|
+
const comp = {
|
|
135
|
+
type: 'cryptographic-asset',
|
|
136
|
+
name,
|
|
137
|
+
'bom-ref': `crypto:${name}`,
|
|
138
|
+
};
|
|
139
|
+
if (meta) comp.cryptoProperties = meta;
|
|
140
|
+
const occ = algorithms.get(name);
|
|
141
|
+
if (occ && occ.length) comp.evidence = { occurrences: occ };
|
|
142
|
+
return comp;
|
|
143
|
+
}),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function main() {
|
|
148
|
+
const args = process.argv.slice(2);
|
|
149
|
+
const dir = args.includes('--dir') ? args[args.indexOf('--dir') + 1] : process.cwd();
|
|
150
|
+
const outPath = args.includes('--out') ? args[args.indexOf('--out') + 1] : null;
|
|
151
|
+
|
|
152
|
+
const deps = loadPackageDeps(dir);
|
|
153
|
+
const fromDeps = matchDeps(deps, DEFAULT_RULES);
|
|
154
|
+
const fromSource = scanSource(dir, DEFAULT_RULES);
|
|
155
|
+
|
|
156
|
+
// Merge: union of both. Source occurrences override empty array from deps.
|
|
157
|
+
const merged = new Map();
|
|
158
|
+
for (const name of fromDeps) merged.set(name, []);
|
|
159
|
+
for (const [name, occ] of fromSource) merged.set(name, occ);
|
|
160
|
+
|
|
161
|
+
const cbom = toCycloneDX(merged);
|
|
162
|
+
const output = JSON.stringify(cbom, null, 2);
|
|
163
|
+
if (outPath) {
|
|
164
|
+
fs.writeFileSync(outPath, output, 'utf-8');
|
|
165
|
+
console.error(`cbom-scan: wrote ${merged.size} algorithms to ${outPath}`);
|
|
166
|
+
} else {
|
|
167
|
+
console.log(output);
|
|
168
|
+
}
|
|
169
|
+
process.exit(0);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
main();
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cbom-tools",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Crypto Bill of Materials — scan, diff, report (CycloneDX 1.6 CBOM)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "刘天赫 <research@fibemate.net>",
|
|
7
|
+
"bin": {
|
|
8
|
+
"cbom-scan": "./cbom-scan.cjs",
|
|
9
|
+
"cbom-diff": "./cbom-diff.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"cbom-scan.cjs",
|
|
13
|
+
"cbom-diff.js",
|
|
14
|
+
"algo-metadata.json",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
]
|
|
18
|
+
}
|