@appystore/secret-shield-cli 0.0.0-stage → 0.1.3
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/README.md +24 -2
- package/dist/bin.js +24 -0
- package/dist/chunk-IAKEM2UT.js +356 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/package.json +56 -3
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/README.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @appystore/secret-shield-cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Command-line scanner for [SecretShield](https://www.npmjs.com/package/@appystore/secret-shield):
|
|
4
|
+
detect and redact secrets **locally** before sharing code with an LLM or committing it.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npx @appystore/secret-shield-cli scan . # human-readable output
|
|
8
|
+
npx @appystore/secret-shield-cli scan . --format sarif -o secretshield.sarif
|
|
9
|
+
npx @appystore/secret-shield-cli redact src/config.ts # print a redacted copy
|
|
10
|
+
pbpaste | npx @appystore/secret-shield-cli redact - # redact a prompt from stdin
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
| Exit code | Meaning |
|
|
14
|
+
| --------- | ---------------------------------------------------- |
|
|
15
|
+
| `0` | No findings at or above `--fail-on` (default: `low`) |
|
|
16
|
+
| `1` | Findings |
|
|
17
|
+
| `2` | Usage or runtime error (nothing is written) |
|
|
18
|
+
|
|
19
|
+
- Nothing is sent over the network; no telemetry.
|
|
20
|
+
- Reports never contain secret values, masked values or secret lengths.
|
|
21
|
+
- `redact` never modifies the input file and refuses to overwrite existing files without `--force`.
|
|
22
|
+
- Paths listed as **not scanned** (ignored, binary, too large, symlinks…) were not analysed and must
|
|
23
|
+
not be considered safe.
|
|
24
|
+
|
|
25
|
+
Run `secretshield --help` for every option. License: Apache-2.0.
|
package/dist/bin.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
run
|
|
4
|
+
} from "./chunk-IAKEM2UT.js";
|
|
5
|
+
|
|
6
|
+
// src/bin.ts
|
|
7
|
+
async function readStdin(maxLength) {
|
|
8
|
+
const chunks = [];
|
|
9
|
+
let length = 0;
|
|
10
|
+
process.stdin.setEncoding("utf8");
|
|
11
|
+
for await (const chunk of process.stdin) {
|
|
12
|
+
length += chunk.length;
|
|
13
|
+
if (length > maxLength) throw new Error("stdin too large");
|
|
14
|
+
chunks.push(chunk);
|
|
15
|
+
}
|
|
16
|
+
return chunks.join("");
|
|
17
|
+
}
|
|
18
|
+
var code = await run(process.argv.slice(2), {
|
|
19
|
+
stdout: (text) => process.stdout.write(text),
|
|
20
|
+
stderr: (text) => process.stderr.write(text),
|
|
21
|
+
readStdin,
|
|
22
|
+
cwd: process.cwd()
|
|
23
|
+
});
|
|
24
|
+
process.exitCode = code;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { lstat, open, realpath, writeFile } from "fs/promises";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import { parseArgs } from "util";
|
|
5
|
+
import {
|
|
6
|
+
SecretShield,
|
|
7
|
+
SecretShieldError,
|
|
8
|
+
SEVERITIES,
|
|
9
|
+
VERSION
|
|
10
|
+
} from "@appystore/secret-shield";
|
|
11
|
+
var EXIT_OK = 0;
|
|
12
|
+
var EXIT_FINDINGS = 1;
|
|
13
|
+
var EXIT_ERROR = 2;
|
|
14
|
+
var MAX_RULES_FILE_SIZE = 1024 * 1024;
|
|
15
|
+
var MAX_STDIN_LENGTH = 5e6;
|
|
16
|
+
var LANGUAGES = [
|
|
17
|
+
"javascript",
|
|
18
|
+
"typescript",
|
|
19
|
+
"json",
|
|
20
|
+
"yaml",
|
|
21
|
+
"dotenv",
|
|
22
|
+
"text"
|
|
23
|
+
];
|
|
24
|
+
var HELP = `SecretShield ${VERSION} \u2014 detect and redact secrets locally.
|
|
25
|
+
|
|
26
|
+
Usage:
|
|
27
|
+
secretshield scan [paths...] [options] Scan files and directories (default: .)
|
|
28
|
+
secretshield redact <file|-> [options] Print a redacted copy of a file or of stdin
|
|
29
|
+
secretshield rules [options] List detection rules
|
|
30
|
+
secretshield --help | --version
|
|
31
|
+
|
|
32
|
+
Scan options:
|
|
33
|
+
--format <text|json|markdown|sarif> Output format (default: text)
|
|
34
|
+
--output <file> Write the report to a file instead of stdout
|
|
35
|
+
--fail-on <low|medium|high|critical|none>
|
|
36
|
+
Exit with code 1 when a finding has at least this
|
|
37
|
+
severity (default: low)
|
|
38
|
+
--min-confidence <0-100> Ignore findings below this confidence (default: 40)
|
|
39
|
+
--ignore <pattern> Extra ignore pattern (repeatable)
|
|
40
|
+
--no-ignore-file Do not read .secretshieldignore
|
|
41
|
+
--max-file-size <bytes> Skip larger files (default: 2097152)
|
|
42
|
+
|
|
43
|
+
Redact options:
|
|
44
|
+
--strategy <partial|full|token> Masking strategy (default: partial)
|
|
45
|
+
--language <lang> javascript, typescript, json, yaml, dotenv, text
|
|
46
|
+
--output <file> Write to a new file (never the input file)
|
|
47
|
+
--force Allow overwriting an existing output file
|
|
48
|
+
|
|
49
|
+
Rule options (all commands):
|
|
50
|
+
--rules <file.json> Load custom rules (JSON array, RE2 patterns)
|
|
51
|
+
--disable-rule <id> Disable a rule (repeatable)
|
|
52
|
+
|
|
53
|
+
Exit codes: 0 = no findings, 1 = findings, 2 = error.
|
|
54
|
+
Everything runs locally; nothing is sent over the network.
|
|
55
|
+
Reports never contain secret values. Paths listed as "not scanned" were not analysed.
|
|
56
|
+
`;
|
|
57
|
+
var UsageError = class extends Error {
|
|
58
|
+
};
|
|
59
|
+
function fail(message) {
|
|
60
|
+
throw new UsageError(message);
|
|
61
|
+
}
|
|
62
|
+
function parseInteger(value, name, min, max) {
|
|
63
|
+
if (value === void 0) return void 0;
|
|
64
|
+
if (!/^\d+$/.test(value)) fail(`--${name} must be an integer.`);
|
|
65
|
+
const parsed = Number(value);
|
|
66
|
+
if (parsed < min || parsed > max) fail(`--${name} must be between ${min} and ${max}.`);
|
|
67
|
+
return parsed;
|
|
68
|
+
}
|
|
69
|
+
function oneOf(value, allowed, name) {
|
|
70
|
+
if (value === void 0) return void 0;
|
|
71
|
+
if (!allowed.includes(value))
|
|
72
|
+
fail(`--${name} must be one of: ${allowed.join(", ")}.`);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
async function loadRules(file, cwd) {
|
|
76
|
+
if (file === void 0) return [];
|
|
77
|
+
const absolute = path.resolve(cwd, file);
|
|
78
|
+
let raw;
|
|
79
|
+
let handle;
|
|
80
|
+
try {
|
|
81
|
+
handle = await open(absolute, "r");
|
|
82
|
+
const stats = await handle.stat();
|
|
83
|
+
if (!stats.isFile() || stats.size > MAX_RULES_FILE_SIZE)
|
|
84
|
+
fail("The rules file must be a regular file under 1 MiB.");
|
|
85
|
+
raw = await handle.readFile({ encoding: "utf8" });
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error instanceof UsageError) throw error;
|
|
88
|
+
fail("The rules file could not be read.");
|
|
89
|
+
} finally {
|
|
90
|
+
await handle?.close().catch(() => void 0);
|
|
91
|
+
}
|
|
92
|
+
let parsed;
|
|
93
|
+
try {
|
|
94
|
+
parsed = JSON.parse(raw);
|
|
95
|
+
} catch {
|
|
96
|
+
fail("The rules file is not valid JSON.");
|
|
97
|
+
}
|
|
98
|
+
if (!Array.isArray(parsed)) fail("The rules file must contain a JSON array of rule definitions.");
|
|
99
|
+
return parsed;
|
|
100
|
+
}
|
|
101
|
+
function displayPath(cwd, absolute) {
|
|
102
|
+
const relative = path.relative(cwd, absolute);
|
|
103
|
+
const shown = relative === "" ? "." : relative.startsWith("..") ? absolute : relative;
|
|
104
|
+
return shown.split(path.sep).join("/");
|
|
105
|
+
}
|
|
106
|
+
function joinDisplay(base, relative) {
|
|
107
|
+
return base === "." ? relative : `${base}/${relative}`;
|
|
108
|
+
}
|
|
109
|
+
function formatText(results, scanned) {
|
|
110
|
+
const lines = [];
|
|
111
|
+
const found = results.filter((r) => r.status === "scanned" && r.findings.length > 0);
|
|
112
|
+
const skipped = results.filter((r) => r.status === "not-scanned");
|
|
113
|
+
for (const result of found) {
|
|
114
|
+
for (const f of result.findings) {
|
|
115
|
+
lines.push(
|
|
116
|
+
`${result.file}:${f.line}:${f.column} ${f.severity.toUpperCase().padEnd(8)} ${f.category} (${f.ruleId}, confidence ${f.confidence}%)`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const total = found.reduce((n, r) => n + r.findings.length, 0);
|
|
121
|
+
lines.push(
|
|
122
|
+
"",
|
|
123
|
+
total === 0 ? `No secrets detected in ${scanned} scanned file(s).` : `${total} possible secret(s) in ${found.length} of ${scanned} scanned file(s).`
|
|
124
|
+
);
|
|
125
|
+
if (skipped.length > 0) {
|
|
126
|
+
lines.push(`${skipped.length} path(s) not scanned (not analysed, not considered safe):`);
|
|
127
|
+
for (const entry of skipped.slice(0, 50)) lines.push(` - ${entry.file} (${entry.reason})`);
|
|
128
|
+
if (skipped.length > 50)
|
|
129
|
+
lines.push(` \u2026 and ${skipped.length - 50} more (use --format json for the full list)`);
|
|
130
|
+
}
|
|
131
|
+
lines.push("Values are never printed. Rotate any secret that was already shared.");
|
|
132
|
+
return lines.join("\n") + "\n";
|
|
133
|
+
}
|
|
134
|
+
var EMPTY_SUMMARY = { total: 0, critical: 0, high: 0, medium: 0, low: 0 };
|
|
135
|
+
async function runScan(shield, positionals, values, io) {
|
|
136
|
+
const format = oneOf(
|
|
137
|
+
values["format"],
|
|
138
|
+
["text", "json", "markdown", "sarif"],
|
|
139
|
+
"format"
|
|
140
|
+
) ?? "text";
|
|
141
|
+
const failOn = oneOf(values["fail-on"], [...SEVERITIES, "none"], "fail-on") ?? "low";
|
|
142
|
+
const maxFileSize = parseInteger(
|
|
143
|
+
values["max-file-size"],
|
|
144
|
+
"max-file-size",
|
|
145
|
+
1,
|
|
146
|
+
50 * 1024 * 1024
|
|
147
|
+
);
|
|
148
|
+
const targets = positionals.length > 0 ? positionals : ["."];
|
|
149
|
+
const results = [];
|
|
150
|
+
let scannedCount = 0;
|
|
151
|
+
for (const target of targets) {
|
|
152
|
+
const absolute = path.resolve(io.cwd, target);
|
|
153
|
+
const shown = displayPath(io.cwd, absolute);
|
|
154
|
+
let stats;
|
|
155
|
+
try {
|
|
156
|
+
stats = await lstat(absolute);
|
|
157
|
+
} catch {
|
|
158
|
+
results.push({
|
|
159
|
+
file: shown,
|
|
160
|
+
status: "not-scanned",
|
|
161
|
+
reason: "unreadable",
|
|
162
|
+
findings: [],
|
|
163
|
+
summary: { ...EMPTY_SUMMARY }
|
|
164
|
+
});
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (stats.isDirectory()) {
|
|
168
|
+
const dir = await shield.scanDirectory(absolute, {
|
|
169
|
+
useIgnoreFile: values["ignore-file"] !== false,
|
|
170
|
+
ignore: values["ignore"] ?? [],
|
|
171
|
+
...maxFileSize !== void 0 ? { maxFileSize } : {}
|
|
172
|
+
});
|
|
173
|
+
scannedCount += dir.scannedFiles;
|
|
174
|
+
for (const file of dir.files)
|
|
175
|
+
results.push({
|
|
176
|
+
...file,
|
|
177
|
+
file: joinDisplay(shown, file.file),
|
|
178
|
+
findings: file.findings.map((f) => ({ ...f, file: joinDisplay(shown, file.file) }))
|
|
179
|
+
});
|
|
180
|
+
for (const entry of dir.notScanned) {
|
|
181
|
+
results.push({
|
|
182
|
+
file: joinDisplay(shown, entry.file),
|
|
183
|
+
status: "not-scanned",
|
|
184
|
+
reason: entry.reason,
|
|
185
|
+
findings: [],
|
|
186
|
+
summary: { ...EMPTY_SUMMARY }
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
const file = await shield.scanFile(
|
|
191
|
+
absolute,
|
|
192
|
+
maxFileSize !== void 0 ? { maxFileSize } : {}
|
|
193
|
+
);
|
|
194
|
+
if (file.status === "scanned") scannedCount++;
|
|
195
|
+
results.push({
|
|
196
|
+
...file,
|
|
197
|
+
file: shown,
|
|
198
|
+
findings: file.findings.map((f) => ({ ...f, file: shown }))
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const output = format === "text" ? formatText(results, scannedCount) : shield.generateReport(results, format);
|
|
203
|
+
const outputFile = values["output"];
|
|
204
|
+
if (outputFile !== void 0) {
|
|
205
|
+
await writeFile(path.resolve(io.cwd, outputFile), output, { encoding: "utf8", mode: 420 });
|
|
206
|
+
} else {
|
|
207
|
+
io.stdout(output.endsWith("\n") ? output : output + "\n");
|
|
208
|
+
}
|
|
209
|
+
if (failOn === "none") return EXIT_OK;
|
|
210
|
+
const threshold = SEVERITIES.indexOf(failOn);
|
|
211
|
+
const failing = results.some(
|
|
212
|
+
(r) => r.findings.some((f) => SEVERITIES.indexOf(f.severity) >= threshold)
|
|
213
|
+
);
|
|
214
|
+
return failing ? EXIT_FINDINGS : EXIT_OK;
|
|
215
|
+
}
|
|
216
|
+
async function runRedact(shield, positionals, values, io) {
|
|
217
|
+
if (positionals.length !== 1) fail("redact expects exactly one file path, or - for stdin.");
|
|
218
|
+
const target = positionals[0];
|
|
219
|
+
const language = oneOf(values["language"], LANGUAGES, "language");
|
|
220
|
+
const outputFile = values["output"];
|
|
221
|
+
let text;
|
|
222
|
+
let findings;
|
|
223
|
+
if (target === "-") {
|
|
224
|
+
const input = await io.readStdin(MAX_STDIN_LENGTH);
|
|
225
|
+
const result = shield.redact(input, language ? { language } : { language: "text" });
|
|
226
|
+
text = result.text;
|
|
227
|
+
findings = result.findings.length;
|
|
228
|
+
} else {
|
|
229
|
+
const absolute = path.resolve(io.cwd, target);
|
|
230
|
+
const result = await shield.redactFile(absolute, language ? { language } : {});
|
|
231
|
+
if (result.status !== "scanned" || result.text === void 0) {
|
|
232
|
+
fail(`The file was not scanned (${result.reason ?? "unknown reason"}).`);
|
|
233
|
+
}
|
|
234
|
+
if (outputFile !== void 0) {
|
|
235
|
+
const out = path.resolve(io.cwd, outputFile);
|
|
236
|
+
const sameFile = await realpath(out).then(
|
|
237
|
+
(p) => realpath(absolute).then((q) => p === q),
|
|
238
|
+
() => false
|
|
239
|
+
);
|
|
240
|
+
if (sameFile) fail("--output must not be the input file: originals are never overwritten.");
|
|
241
|
+
}
|
|
242
|
+
text = result.text;
|
|
243
|
+
findings = result.findings.length;
|
|
244
|
+
}
|
|
245
|
+
if (outputFile !== void 0) {
|
|
246
|
+
try {
|
|
247
|
+
await writeFile(path.resolve(io.cwd, outputFile), text, {
|
|
248
|
+
encoding: "utf8",
|
|
249
|
+
flag: values["force"] ? "w" : "wx"
|
|
250
|
+
});
|
|
251
|
+
} catch {
|
|
252
|
+
fail(
|
|
253
|
+
"The output file could not be written (it may already exist; use --force to overwrite)."
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
io.stderr(`${findings} secret(s) masked. Redacted copy written.
|
|
257
|
+
`);
|
|
258
|
+
} else {
|
|
259
|
+
io.stdout(text);
|
|
260
|
+
io.stderr(`${findings} secret(s) masked.
|
|
261
|
+
`);
|
|
262
|
+
}
|
|
263
|
+
return EXIT_OK;
|
|
264
|
+
}
|
|
265
|
+
function runRules(shield, io) {
|
|
266
|
+
const rows = shield.listRules().map(
|
|
267
|
+
(r) => `${r.enabled ? "on " : "off"} ${r.source.padEnd(7)} ${r.id.padEnd(34)} ${r.severity.padEnd(8)} ${r.category}`
|
|
268
|
+
);
|
|
269
|
+
io.stdout(
|
|
270
|
+
["ON SOURCE ID SEVERITY CATEGORY", ...rows].join("\n") + "\n"
|
|
271
|
+
);
|
|
272
|
+
return EXIT_OK;
|
|
273
|
+
}
|
|
274
|
+
async function run(argv, io) {
|
|
275
|
+
try {
|
|
276
|
+
let parsed;
|
|
277
|
+
try {
|
|
278
|
+
parsed = parseArgs({
|
|
279
|
+
args: argv,
|
|
280
|
+
allowPositionals: true,
|
|
281
|
+
allowNegative: true,
|
|
282
|
+
strict: true,
|
|
283
|
+
options: {
|
|
284
|
+
help: { type: "boolean", short: "h" },
|
|
285
|
+
version: { type: "boolean", short: "v" },
|
|
286
|
+
format: { type: "string", short: "f" },
|
|
287
|
+
output: { type: "string", short: "o" },
|
|
288
|
+
"fail-on": { type: "string" },
|
|
289
|
+
"min-confidence": { type: "string" },
|
|
290
|
+
ignore: { type: "string", multiple: true },
|
|
291
|
+
"ignore-file": { type: "boolean", default: true },
|
|
292
|
+
"max-file-size": { type: "string" },
|
|
293
|
+
strategy: { type: "string" },
|
|
294
|
+
language: { type: "string" },
|
|
295
|
+
force: { type: "boolean" },
|
|
296
|
+
rules: { type: "string" },
|
|
297
|
+
"disable-rule": { type: "string", multiple: true }
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
} catch {
|
|
301
|
+
fail("Invalid arguments. Run `secretshield --help` for usage.");
|
|
302
|
+
}
|
|
303
|
+
const { values, positionals } = parsed;
|
|
304
|
+
if (values.version) {
|
|
305
|
+
io.stdout(`${VERSION}
|
|
306
|
+
`);
|
|
307
|
+
return EXIT_OK;
|
|
308
|
+
}
|
|
309
|
+
const [command, ...rest] = positionals;
|
|
310
|
+
if (values.help || command === void 0 || command === "help") {
|
|
311
|
+
io.stdout(HELP);
|
|
312
|
+
return command === void 0 && !values.help ? EXIT_ERROR : EXIT_OK;
|
|
313
|
+
}
|
|
314
|
+
const options = {
|
|
315
|
+
detection: {
|
|
316
|
+
minConfidence: parseInteger(values["min-confidence"], "min-confidence", 0, 100) ?? 40
|
|
317
|
+
},
|
|
318
|
+
masking: {
|
|
319
|
+
strategy: oneOf(values.strategy, ["partial", "full", "token"], "strategy") ?? "partial"
|
|
320
|
+
},
|
|
321
|
+
rules: {
|
|
322
|
+
custom: await loadRules(values.rules, io.cwd),
|
|
323
|
+
disabled: values["disable-rule"] ?? []
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
const shield = new SecretShield(options);
|
|
327
|
+
switch (command) {
|
|
328
|
+
case "scan":
|
|
329
|
+
return await runScan(shield, rest, values, io);
|
|
330
|
+
case "redact":
|
|
331
|
+
return await runRedact(shield, rest, values, io);
|
|
332
|
+
case "rules":
|
|
333
|
+
return runRules(shield, io);
|
|
334
|
+
default:
|
|
335
|
+
fail("Unknown command. Run `secretshield --help` for usage.");
|
|
336
|
+
}
|
|
337
|
+
} catch (error) {
|
|
338
|
+
if (error instanceof UsageError) {
|
|
339
|
+
io.stderr(`Error: ${error.message}
|
|
340
|
+
`);
|
|
341
|
+
} else if (error instanceof SecretShieldError) {
|
|
342
|
+
io.stderr(`Error (${error.code}): ${error.message}
|
|
343
|
+
`);
|
|
344
|
+
} else {
|
|
345
|
+
io.stderr("Error: unexpected failure. No content was written.\n");
|
|
346
|
+
}
|
|
347
|
+
return EXIT_ERROR;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
export {
|
|
352
|
+
EXIT_OK,
|
|
353
|
+
EXIT_FINDINGS,
|
|
354
|
+
EXIT_ERROR,
|
|
355
|
+
run
|
|
356
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface CliIO {
|
|
2
|
+
stdout: (text: string) => void;
|
|
3
|
+
stderr: (text: string) => void;
|
|
4
|
+
readStdin: (maxLength: number) => Promise<string>;
|
|
5
|
+
cwd: string;
|
|
6
|
+
}
|
|
7
|
+
declare const EXIT_OK = 0;
|
|
8
|
+
declare const EXIT_FINDINGS = 1;
|
|
9
|
+
declare const EXIT_ERROR = 2;
|
|
10
|
+
/** Runs the CLI and returns the process exit code. Never prints stack traces or analysed content. */
|
|
11
|
+
declare function run(argv: string[], io: CliIO): Promise<number>;
|
|
12
|
+
|
|
13
|
+
export { EXIT_ERROR, EXIT_FINDINGS, EXIT_OK, run };
|
|
14
|
+
export type { CliIO };
|
package/dist/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appystore/secret-shield-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Command-line scanner for SecretShield: detect and redact secrets locally before sharing code with an LLM.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/IT-Skill31/secretshield.git",
|
|
9
|
+
"directory": "packages/cli"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/IT-Skill31/secretshield#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/IT-Skill31/secretshield/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"secrets",
|
|
18
|
+
"scanner",
|
|
19
|
+
"cli",
|
|
20
|
+
"redaction",
|
|
21
|
+
"security",
|
|
22
|
+
"sarif"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22.12.0"
|
|
26
|
+
},
|
|
27
|
+
"bin": {
|
|
28
|
+
"secretshield": "./dist/bin.js"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"README.md",
|
|
39
|
+
"LICENSE"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"provenance": true
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@appystore/secret-shield": "0.1.3"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/node": "22.20.7",
|
|
50
|
+
"tsup": "9.0.0",
|
|
51
|
+
"typescript": "6.0.3",
|
|
52
|
+
"vitest": "5.1.2"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
57
|
+
"test": "vitest run"
|
|
58
|
+
}
|
|
6
59
|
}
|