@kaynooo/cli-env 0.13.1 → 0.13.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/dist/ev.js +207 -115
- package/dist/redact.js +157 -55
- package/package.json +2 -2
- package/src/commands/audit.ts +5 -3
- package/src/commands/compare.ts +7 -6
- package/src/commands/edit.ts +13 -12
- package/src/commands/pins.ts +5 -3
- package/src/commands/read.ts +13 -12
- package/src/commands/redact.ts +5 -2
- package/src/dotenv.ts +12 -1
- package/src/mask.ts +37 -6
package/src/commands/read.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import type { Dotenv } from '../dotenv'
|
|
2
3
|
import type { KnownSecrets } from '../mask'
|
|
3
4
|
import { fail, jsonOption, parse, printJson, role, table } from '@kaynooo/cli-kit'
|
|
@@ -10,17 +11,17 @@ const listUsage = `ev list [file]
|
|
|
10
11
|
|
|
11
12
|
Prints every key of the folder's .env* files, or of one file, with secret values masked: a few leading characters and the length under a key named like TOKEN, SECRET, PASSWORD, PASS, PWD, KEY, PRIVATE, DSN, AUTH, SALT or COOKIE, any value equal to a secret of the folder's .env* files, and the password and tokens of any URL.
|
|
12
13
|
|
|
13
|
-
--json prints [{file, key, value, secret}], value masked and secret true when a value was
|
|
14
|
+
--json prints [{file, key, value, secret}], value masked and secret true when a value was.`
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
const LIST_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'there is no .env* file'], [2, 'a usage error']]
|
|
16
17
|
|
|
17
18
|
const getUsage = `ev get KEY [file]
|
|
18
19
|
|
|
19
20
|
Prints the masked value of one key, from every .env* file that sets it or from one file.
|
|
20
21
|
|
|
21
|
-
--json prints [{file, key, value, secret}] for each file that sets it
|
|
22
|
+
--json prints [{file, key, value, secret}] for each file that sets it.`
|
|
22
23
|
|
|
23
|
-
|
|
24
|
+
const GET_EXITS: ExitRow[] = [[0, 'the key is set'], [1, 'it is not set anywhere'], [2, 'a usage error']]
|
|
24
25
|
|
|
25
26
|
function display(key: string, value: string, known: KnownSecrets): string {
|
|
26
27
|
return maskValue(key, value, known).replaceAll('\n', '\\n').replaceAll('\r', '\\r')
|
|
@@ -69,7 +70,7 @@ async function readAll(names: string[]): Promise<{ files: [name: string, file: D
|
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
export async function list(args: string[]): Promise<number> {
|
|
72
|
-
const parsed = parse(args, listUsage, jsonFlag)
|
|
73
|
+
const parsed = parse(args, listUsage, jsonFlag, { exits: LIST_EXITS })
|
|
73
74
|
|
|
74
75
|
const [file] = parsed.positionals
|
|
75
76
|
const names = file === undefined ? await envFiles() : [file]
|
|
@@ -95,7 +96,7 @@ export async function list(args: string[]): Promise<number> {
|
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
export async function get(args: string[]): Promise<number> {
|
|
98
|
-
const parsed = parse(args, getUsage, jsonFlag)
|
|
99
|
+
const parsed = parse(args, getUsage, jsonFlag, { exits: GET_EXITS })
|
|
99
100
|
|
|
100
101
|
const [key, file] = parsed.positionals
|
|
101
102
|
|
|
@@ -126,22 +127,22 @@ const grepUsage = `ev grep [-i] <regex> [file...]
|
|
|
126
127
|
|
|
127
128
|
Prints the keys matching the regular expression, from the folder's .env* files or the files given, with their values masked as ev list does. The regex is matched against the key only, never the value: \`ev grep '^DATABASE_'\` instead of \`grep ^DATABASE_ .env\`.
|
|
128
129
|
|
|
129
|
-
--json prints [{file, key, value, secret}]
|
|
130
|
+
--json prints [{file, key, value, secret}].`
|
|
130
131
|
|
|
131
|
-
|
|
132
|
+
const GREP_EXITS: ExitRow[] = [[0, 'a key matches'], [1, 'none does'], [2, 'a usage error']]
|
|
132
133
|
|
|
133
134
|
const showUsage = `ev show <rev>:<file>
|
|
134
135
|
|
|
135
136
|
Prints every key of an env file as a git revision holds it, values masked as ev list does: \`ev show HEAD~1:api/.env\` instead of \`git show HEAD~1:api/.env\`.
|
|
136
137
|
|
|
137
|
-
--json prints [{file, key, value, secret}]
|
|
138
|
+
--json prints [{file, key, value, secret}].`
|
|
138
139
|
|
|
139
|
-
|
|
140
|
+
const SHOW_EXITS: ExitRow[] = [[0, 'it printed them'], [1, 'git cannot show the file'], [2, 'a usage error']]
|
|
140
141
|
|
|
141
142
|
const REVISION = /^([^:]+):(.+)$/
|
|
142
143
|
|
|
143
144
|
export async function grep(args: string[]): Promise<number> {
|
|
144
|
-
const parsed = parse(args, grepUsage, { 'ignore-case': { type: 'boolean', short: 'i', description: 'match the key whatever its case' }, ...jsonFlag })
|
|
145
|
+
const parsed = parse(args, grepUsage, { 'ignore-case': { type: 'boolean', short: 'i', description: 'match the key whatever its case' }, ...jsonFlag }, { exits: GREP_EXITS })
|
|
145
146
|
|
|
146
147
|
const [source, ...given] = parsed.positionals
|
|
147
148
|
|
|
@@ -178,7 +179,7 @@ export async function grep(args: string[]): Promise<number> {
|
|
|
178
179
|
}
|
|
179
180
|
|
|
180
181
|
export async function show(args: string[]): Promise<number> {
|
|
181
|
-
const parsed = parse(args, showUsage, jsonFlag)
|
|
182
|
+
const parsed = parse(args, showUsage, jsonFlag, { exits: SHOW_EXITS })
|
|
182
183
|
|
|
183
184
|
const [target] = parsed.positionals
|
|
184
185
|
const [, revision, path] = REVISION.exec(target ?? '') ?? []
|
package/src/commands/redact.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExitRow } from '@kaynooo/cli-kit'
|
|
1
2
|
import process from 'node:process'
|
|
2
3
|
import { parse, readInput } from '@kaynooo/cli-kit'
|
|
3
4
|
import { loadKnown } from '../files'
|
|
@@ -6,7 +7,9 @@ import { check, Redactor } from '../redactor'
|
|
|
6
7
|
const usage = `redact [--check]
|
|
7
8
|
|
|
8
9
|
Copies stdin to stdout line by line with secrets masked: known token formats (GitHub, GitLab, Slack, AWS, npm, Stripe, Google and SendGrid keys, sk-, JWTs, Authorization headers, password= and secret= pairs), private key blocks, the password and the tokens of any URL and the values of secret-looking keys in this folder's .env* files.
|
|
9
|
-
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check
|
|
10
|
+
--check prints nothing of the input and lists the file and line of each secret found, exiting 1 when there is any: \`git diff --cached | redact --check\`.`
|
|
11
|
+
|
|
12
|
+
const EXITS: ExitRow[] = [[0, 'it copied the input or found no secret'], [1, '--check finds one'], [2, 'a usage error']]
|
|
10
13
|
|
|
11
14
|
const options = {
|
|
12
15
|
'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
|
|
@@ -14,7 +17,7 @@ const options = {
|
|
|
14
17
|
} as const
|
|
15
18
|
|
|
16
19
|
export async function redact(args: string[]): Promise<number> {
|
|
17
|
-
const parsed = parse(args, usage, options, { replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
|
|
20
|
+
const parsed = parse(args, usage, options, { exits: EXITS, replaces: 'printing a command\'s secrets into a log or a transcript', examples: [['docker compose config | redact', 'the resolved compose file, secrets masked'], ['git diff --cached | redact --check', 'refuse a commit carrying a secret']] })
|
|
18
21
|
|
|
19
22
|
const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
|
|
20
23
|
|
package/src/dotenv.ts
CHANGED
|
@@ -3,6 +3,8 @@ export interface DotenvLine {
|
|
|
3
3
|
raw: string
|
|
4
4
|
key?: string
|
|
5
5
|
value?: string
|
|
6
|
+
// The value opens a quote that no line closes before the next assignment: it is read as the rest of its own line, as Bun does.
|
|
7
|
+
unterminated?: boolean
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export interface Dotenv {
|
|
@@ -83,13 +85,22 @@ export function parseDotenv(text: string): Dotenv {
|
|
|
83
85
|
let raw = line
|
|
84
86
|
let end = closingQuote(body, quote)
|
|
85
87
|
|
|
86
|
-
|
|
88
|
+
const first = index
|
|
89
|
+
|
|
90
|
+
while (end === -1 && index + 1 < physical.length && !ASSIGNMENT.test(physical[index + 1] ?? '')) {
|
|
87
91
|
index++
|
|
88
92
|
body += `\n${(physical[index] ?? '').replace(CARRIAGE_RETURN, '')}`
|
|
89
93
|
raw += `\n${physical[index] ?? ''}`
|
|
90
94
|
end = closingQuote(body, quote)
|
|
91
95
|
}
|
|
92
96
|
|
|
97
|
+
if (end === -1 && index + 1 < physical.length) {
|
|
98
|
+
index = first
|
|
99
|
+
lines.push({ raw: line, key, value: unescapeDollar(start.replace(INLINE_COMMENT, '').trim()), unterminated: true })
|
|
100
|
+
|
|
101
|
+
continue
|
|
102
|
+
}
|
|
103
|
+
|
|
93
104
|
const inner = end === -1 ? body : body.slice(0, end)
|
|
94
105
|
|
|
95
106
|
lines.push({ raw, key, value: quote === '"' ? unescapeDouble(inner) : unescapeDollar(inner) })
|
package/src/mask.ts
CHANGED
|
@@ -28,8 +28,8 @@ const ESCAPE = /[.*+?^${}()|[\]\\]/g
|
|
|
28
28
|
interface TokenFormat {
|
|
29
29
|
kind: string
|
|
30
30
|
pattern: RegExp
|
|
31
|
-
// Skips a candidate that is a placeholder or a plain word.
|
|
32
|
-
skip?: (value: string) => boolean
|
|
31
|
+
// Skips a candidate that is a placeholder or a plain word. `before` is the line text ahead of it.
|
|
32
|
+
skip?: (value: string, before: string) => boolean
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// A secret value starting like this is a reference or a placeholder, not the secret.
|
|
@@ -37,10 +37,39 @@ function isPlaceholder(value: string): boolean {
|
|
|
37
37
|
return PLACEHOLDER.test(value) || PLAIN_VALUE.test(value)
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
// Names that are secrets whatever they are followed by, and names that also open prose (`token: expired`, `auth: failed`).
|
|
41
|
+
const STRONG_NAME = 'password|passwd|secret|api[_-]?key|access[_-]?key|private[_-]?key|account[_-]?key|shared[_-]?access[_-]?key'
|
|
42
|
+
const WEAK_NAME = 'token|credentials?|auth'
|
|
43
|
+
const PLAIN_WORD = /^[a-z]+$/i
|
|
44
|
+
const AFTER_COLON = /:\s*$/
|
|
45
|
+
const DIGIT = /\d/
|
|
46
|
+
const KEY_VALUE_GAP = '\\s*[:=]\\s*'
|
|
47
|
+
|
|
48
|
+
// A strong name before a colon, so likely prose, holding only a word is left alone.
|
|
49
|
+
function isProse(value: string, before: string): boolean {
|
|
50
|
+
return isPlaceholder(value) || (AFTER_COLON.test(before) && PLAIN_WORD.test(value))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// A value under a weak name is a secret only when it holds a digit or is long, which a word of prose never does.
|
|
54
|
+
function isWeak(value: string, before: string): boolean {
|
|
55
|
+
return isProse(value, before) || !(DIGIT.test(value) || value.length >= 16)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// A key name holding one of the words: `AWS_SECRET_ACCESS_KEY`, `X-Api-Key`, `access_token`.
|
|
59
|
+
function keyed(names: string, value: string): RegExp {
|
|
60
|
+
return new RegExp(`(?<![\\w-])["']?(?:[\\w-]{0,30}[_-])?(?:${names})(?:[_-][\\w-]{0,30})?["']?${KEY_VALUE_GAP}${value}`, 'dgi')
|
|
61
|
+
}
|
|
62
|
+
|
|
40
63
|
// A pattern with a group masks that group only, else the whole match.
|
|
41
64
|
const TOKEN_FORMATS: TokenFormat[] = [
|
|
42
65
|
{ kind: 'GitHub token', pattern: /\b(?:gh[pousr]_|github_pat_)\w{20,}/dg },
|
|
43
|
-
{ kind: 'GitLab token', pattern: /\
|
|
66
|
+
{ kind: 'GitLab token', pattern: /\bgl(?:pat|rt|dt|ptt)-[\w-]{20,}/dg },
|
|
67
|
+
{ kind: 'Hugging Face token', pattern: /\bhf_[A-Za-z]{30,}/dg },
|
|
68
|
+
{ kind: 'webhook secret', pattern: /\bwhsec_\w{20,}/dg },
|
|
69
|
+
{ kind: 'DigitalOcean token', pattern: /\bdop_v1_\w{20,}/dg },
|
|
70
|
+
{ kind: 'Shopify token', pattern: /\bshp(?:at|ca|pa|ss)_\w{20,}/dg },
|
|
71
|
+
{ kind: 'password', pattern: /\bmysql(?:dump|admin)?\s+(?:\S+\s+)*?-p([^\s"'|;&-]\S{3,})/dg },
|
|
72
|
+
{ kind: 'password', pattern: /\bsshpass\s+(?:-[a-oq-z]\s+)*-p\s*([^\s"'|;&]{4,})/dg },
|
|
44
73
|
{ kind: 'API key', pattern: /\bsk-[\w-]{20,}/dg },
|
|
45
74
|
{ kind: 'Slack token', pattern: /\bx(?:ox[abprs]|app)-[\w-]{10,}/dg },
|
|
46
75
|
{ kind: 'JWT', pattern: /\beyJ[\w-]+\.[\w-]+\.[\w-]+/dg },
|
|
@@ -51,8 +80,10 @@ const TOKEN_FORMATS: TokenFormat[] = [
|
|
|
51
80
|
{ kind: 'SendGrid key', pattern: /\bSG\.[\w-]{16,}\.[\w-]{16,}/dg },
|
|
52
81
|
{ kind: 'Authorization header', pattern: /\bAuthorization["']?\s*[:=]\s*["']?(?:Bearer|Basic|Token|Digest)\s+([^\s"',;]{8,})/dgi, skip: isPlaceholder },
|
|
53
82
|
{ kind: 'Bearer token', pattern: /\bBearer\s+((?=[\w.~+/=-]*\d)[\w.~+/=-]{20,})/dg },
|
|
54
|
-
{ kind: 'password', pattern:
|
|
55
|
-
{ kind: 'password', pattern:
|
|
83
|
+
{ kind: 'password', pattern: keyed(STRONG_NAME, '([^\\s"\'&;,]{4,})'), skip: isProse },
|
|
84
|
+
{ kind: 'password', pattern: keyed(WEAK_NAME, '([^\\s"\'&;,]{4,})'), skip: isWeak },
|
|
85
|
+
{ kind: 'password', pattern: keyed(STRONG_NAME, '(["\'])((?:\\\\.|(?!\\1)[^\\\\\\n]){4,})\\1'), skip: isPlaceholder },
|
|
86
|
+
{ kind: 'password', pattern: keyed(WEAK_NAME, '(["\'])((?:\\\\.|(?!\\1)[^\\\\\\n]){4,})\\1'), skip: isWeak },
|
|
56
87
|
]
|
|
57
88
|
|
|
58
89
|
// A private key block (PEM, PGP). Its body is masked from just after the BEGIN marker to the END marker or the end of the line, so one line may hold the whole key escaped as JSON.
|
|
@@ -207,7 +238,7 @@ export function findSecrets(line: string, known?: KnownSecrets): Match[] {
|
|
|
207
238
|
|
|
208
239
|
const value = line.slice(start, end)
|
|
209
240
|
|
|
210
|
-
if (!skip?.(value))
|
|
241
|
+
if (!skip?.(value, line.slice(0, start)))
|
|
211
242
|
found.push({ start, end, kind, mask: hint(value) })
|
|
212
243
|
}
|
|
213
244
|
}
|