@kaynooo/cli-env 0.15.1 → 0.15.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/README.md +4 -3
- package/dist/ev.js +16452 -1566
- package/package.json +7 -2
- package/src/commands/compare.ts +26 -16
- package/src/commands/edit.ts +70 -18
- package/src/commands/read.ts +26 -20
- package/src/dotenv.ts +72 -22
- package/src/files.ts +47 -16
- package/src/fix.ts +3 -18
- package/src/guard-dumps.ts +69 -7
- package/src/guard.ts +6 -4
- package/src/mask.ts +36 -8
- package/src/structured-write.ts +383 -0
- package/src/structured.ts +68 -0
package/src/files.ts
CHANGED
|
@@ -2,10 +2,11 @@ import type { Dirent } from 'node:fs'
|
|
|
2
2
|
import type { Dotenv } from './dotenv'
|
|
3
3
|
import type { KnownSecrets } from './mask'
|
|
4
4
|
import { chmod, chown, readdir, realpath, rename, rm, stat, writeFile } from 'node:fs/promises'
|
|
5
|
-
import { basename, dirname, extname, join } from 'node:path'
|
|
5
|
+
import { basename, dirname, extname, join, resolve } from 'node:path'
|
|
6
6
|
import { fail, ignoredFolders, isSecretName } from '@kaynooo/cli-kit'
|
|
7
7
|
import { entries, parseDotenv, serializeDotenv } from './dotenv'
|
|
8
8
|
import { knownSecrets, urlPasswords } from './mask'
|
|
9
|
+
import { parseStructured, structuredFormat } from './structured'
|
|
9
10
|
|
|
10
11
|
// A dotenv file's name (`.env`, `.env.local`, `prod.env`, `.envrc`), and the templates among them that hold no values.
|
|
11
12
|
export const ENV_FILE = /^(?:\.env(?:\..+)?|.+\.env|\.envrc)$/
|
|
@@ -50,22 +51,32 @@ export async function envFiles(folder = process.cwd()): Promise<string[]> {
|
|
|
50
51
|
}
|
|
51
52
|
|
|
52
53
|
// Formats ev does not read, told apart by their extension.
|
|
53
|
-
const OTHER_FORMATS: Record<string, string> = { '.
|
|
54
|
+
const OTHER_FORMATS: Record<string, string> = { '.ini': 'INI', '.xml': 'XML' }
|
|
54
55
|
const BLANK_OR_COMMENT = /^\s*(?:#.*)?$/
|
|
55
56
|
|
|
56
57
|
// `redact` is the command masking that file's text, `ev redact < <path>` for a file on disk.
|
|
57
58
|
function notDotenv(why: string, redact: string): never {
|
|
58
|
-
fail(`${why}, and ev reads dotenv
|
|
59
|
+
fail(`${why}, and ev reads dotenv (KEY=value lines), JSON, YAML and TOML files`, 2, `\`${redact}\` masks the secrets of any text`)
|
|
59
60
|
}
|
|
60
61
|
|
|
61
|
-
// A file named as
|
|
62
|
-
export function
|
|
62
|
+
// A file named as a format ev cannot read is refused before ev reads it.
|
|
63
|
+
export function assertReadableName(path: string, redact = `ev redact < ${path}`): void {
|
|
63
64
|
const format = OTHER_FORMATS[extname(path).toLowerCase()]
|
|
64
65
|
|
|
65
66
|
if (format !== undefined)
|
|
66
67
|
notDotenv(`${path} is ${format}`, redact)
|
|
67
68
|
}
|
|
68
69
|
|
|
70
|
+
// Past set and unset, ev writes dotenv files only, so any other format is refused before ev reads a file to write it.
|
|
71
|
+
export function assertDotenvName(path: string, redact = `ev redact < ${path}`): void {
|
|
72
|
+
assertReadableName(path, redact)
|
|
73
|
+
|
|
74
|
+
const format = structuredFormat(path)
|
|
75
|
+
|
|
76
|
+
if (format !== undefined)
|
|
77
|
+
fail(`${path} is ${format}, which ev set and unset edit while the other commands that write take dotenv files (KEY=value lines)`)
|
|
78
|
+
}
|
|
79
|
+
|
|
69
80
|
// A file named on the command line is refused when it holds lines but no assignment, rather than read as empty.
|
|
70
81
|
export function assertDotenvText(path: string, dotenv: Dotenv, redact = `ev redact < ${path}`): void {
|
|
71
82
|
if (dotenv.lines.every(line => line.key === undefined) && !dotenv.lines.every(line => BLANK_OR_COMMENT.test(line.raw)))
|
|
@@ -90,6 +101,19 @@ export async function readEnv(path: string, { scanned = false } = {}): Promise<D
|
|
|
90
101
|
return dotenv
|
|
91
102
|
}
|
|
92
103
|
|
|
104
|
+
// The keys of a file named on the command line, or of one of the folder's own .env* files (`scanned`): a JSON, YAML or TOML one by dotted paths.
|
|
105
|
+
export async function readEntries(path: string, { scanned = false } = {}): Promise<Map<string, string>> {
|
|
106
|
+
if (scanned || structuredFormat(path) === undefined)
|
|
107
|
+
return entries(await readEnv(path, { scanned }))
|
|
108
|
+
|
|
109
|
+
const file = Bun.file(path)
|
|
110
|
+
|
|
111
|
+
if (!await file.exists())
|
|
112
|
+
fail(`${path} does not exist`)
|
|
113
|
+
|
|
114
|
+
return parseStructured(path, await file.text())
|
|
115
|
+
}
|
|
116
|
+
|
|
93
117
|
// Writes through a temp file in the same folder renamed over the target, so a crash or a full disk never leaves a half-written file. The target keeps its mode, owner and group and a new file is 0600.
|
|
94
118
|
export async function writeAtomic(path: string, text: string): Promise<void> {
|
|
95
119
|
let target = path
|
|
@@ -145,8 +169,11 @@ export async function withLocks<T>(paths: string[], fn: () => Promise<T>): Promi
|
|
|
145
169
|
const held: string[] = []
|
|
146
170
|
|
|
147
171
|
try {
|
|
148
|
-
|
|
149
|
-
|
|
172
|
+
// One file spelled two ways, or through a symlink, takes its lock once.
|
|
173
|
+
const locks = new Map(await Promise.all(paths.map(async path => [await lockFor(path), path] as const)))
|
|
174
|
+
|
|
175
|
+
for (const [lock, path] of [...locks].sort(([a], [b]) => a < b ? -1 : 1))
|
|
176
|
+
held.push(await acquire(lock, path))
|
|
150
177
|
|
|
151
178
|
return await fn()
|
|
152
179
|
}
|
|
@@ -187,8 +214,9 @@ async function reap(lock: string): Promise<void> {
|
|
|
187
214
|
}
|
|
188
215
|
}
|
|
189
216
|
|
|
190
|
-
|
|
191
|
-
|
|
217
|
+
// The lock file of a path, beside the file it resolves to.
|
|
218
|
+
async function lockFor(path: string): Promise<string> {
|
|
219
|
+
let real = resolve(path)
|
|
192
220
|
|
|
193
221
|
try {
|
|
194
222
|
real = await realpath(path)
|
|
@@ -197,7 +225,10 @@ async function acquire(path: string): Promise<string> {
|
|
|
197
225
|
// a missing file is locked by its own name
|
|
198
226
|
}
|
|
199
227
|
|
|
200
|
-
|
|
228
|
+
return join(dirname(real), `.${basename(real)}.lock`)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function acquire(lock: string, path: string): Promise<string> {
|
|
201
232
|
const deadline = Date.now() + LOCK_WAIT_MS
|
|
202
233
|
|
|
203
234
|
while (true) {
|
|
@@ -220,11 +251,11 @@ async function acquire(path: string): Promise<string> {
|
|
|
220
251
|
}
|
|
221
252
|
}
|
|
222
253
|
|
|
223
|
-
// The secrets of a
|
|
224
|
-
export function secretPairs(
|
|
254
|
+
// The secrets of a file's keys: values of secret-looking keys and the password of any URL value.
|
|
255
|
+
export function secretPairs(keys: Map<string, string>): [string, string][] {
|
|
225
256
|
const pairs: [string, string][] = []
|
|
226
257
|
|
|
227
|
-
for (const [key, value] of
|
|
258
|
+
for (const [key, value] of keys) {
|
|
228
259
|
if (isSecretName(key))
|
|
229
260
|
pairs.push([key, value])
|
|
230
261
|
else
|
|
@@ -258,13 +289,13 @@ async function envFilesBelow(folder: string, depth = 0): Promise<string[]> {
|
|
|
258
289
|
return found
|
|
259
290
|
}
|
|
260
291
|
|
|
261
|
-
// The secrets of the folder's .env* files and of those in the folders below it, plus any `extra`
|
|
262
|
-
export async function loadKnown(folder = process.cwd(), extra:
|
|
292
|
+
// The secrets of the folder's .env* files and of those in the folders below it, plus the keys of any `extra` file, ready to be searched for in output.
|
|
293
|
+
export async function loadKnown(folder = process.cwd(), extra: Map<string, string>[] = []): Promise<KnownSecrets> {
|
|
263
294
|
const pairs = extra.flatMap(secretPairs)
|
|
264
295
|
|
|
265
296
|
for (const path of await envFilesBelow(folder)) {
|
|
266
297
|
try {
|
|
267
|
-
pairs.push(...secretPairs(parseDotenv(await Bun.file(path).text())))
|
|
298
|
+
pairs.push(...secretPairs(entries(parseDotenv(await Bun.file(path).text()))))
|
|
268
299
|
}
|
|
269
300
|
catch {
|
|
270
301
|
// a file that cannot be read has no secret to learn
|
package/src/fix.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { chmod, chown, lstat, rm, stat } from 'node:fs/promises'
|
|
|
2
2
|
import { dirname, join } from 'node:path'
|
|
3
3
|
import { compose } from '@kaynooo/cli-kit'
|
|
4
4
|
import { isInlineSecret } from './audit'
|
|
5
|
-
import { entries,
|
|
5
|
+
import { entries, formatAssignment, parseDotenv, serializeDotenv, setLine, unquotable } from './dotenv'
|
|
6
6
|
import { writeAtomic } from './files'
|
|
7
7
|
|
|
8
8
|
export interface Left {
|
|
@@ -29,7 +29,6 @@ const LIST_ENTRY = /^(\s*-[ \t]*)(["']?)([A-Z0-9_]+)=(.*)$/
|
|
|
29
29
|
const TRAILING = /^\s*(?:#.*)?$/
|
|
30
30
|
const PLAIN_COMMENT = /\s+#.*$/
|
|
31
31
|
const NOT_PLAIN = /^[|>&*![{]/
|
|
32
|
-
const ENV_PLAIN = /^[^\s#"'\\$`]*$/
|
|
33
32
|
|
|
34
33
|
// The inside of a YAML quoted scalar starting after its opening quote: double quotes take only \\ and \" escapes here.
|
|
35
34
|
function quoted(text: string, quote: string): { value: string, after: string } | undefined {
|
|
@@ -100,20 +99,6 @@ export function composeEntry(line: string): Entry | undefined {
|
|
|
100
99
|
return { key, value: scalar.value, swap: ref => `${dash}${quote}${key}=${ref}${quote}${scalar.after}` }
|
|
101
100
|
}
|
|
102
101
|
|
|
103
|
-
// The .env line holding a value compose and Bun both read literally: plain when nothing needs quoting, single-quoted otherwise, double-quoted with each `$` escaped when it holds one since Bun expands it even in single quotes.
|
|
104
|
-
export function envLine(key: string, value: string): string | undefined {
|
|
105
|
-
if (ENV_PLAIN.test(value))
|
|
106
|
-
return `${key}=${value}`
|
|
107
|
-
|
|
108
|
-
if (LINE_BREAK.test(value))
|
|
109
|
-
return undefined
|
|
110
|
-
|
|
111
|
-
if (value.includes('$'))
|
|
112
|
-
return `${key}="${escapeDouble(value)}"`
|
|
113
|
-
|
|
114
|
-
return value.includes('\'') ? undefined : `${key}='${value}'`
|
|
115
|
-
}
|
|
116
|
-
|
|
117
102
|
// A link, dangling or not: writing through it would change a file elsewhere.
|
|
118
103
|
async function isLink(path: string): Promise<boolean> {
|
|
119
104
|
try {
|
|
@@ -201,10 +186,10 @@ export async function fixCompose(path: string, group = false): Promise<ComposeFi
|
|
|
201
186
|
const existing = known.get(key)
|
|
202
187
|
|
|
203
188
|
if (existing === undefined) {
|
|
204
|
-
const raw =
|
|
189
|
+
const raw = formatAssignment(key, value)
|
|
205
190
|
|
|
206
191
|
if (raw === undefined) {
|
|
207
|
-
fix.left.push({ key, why:
|
|
192
|
+
fix.left.push({ key, why: unquotable(value) })
|
|
208
193
|
|
|
209
194
|
return line
|
|
210
195
|
}
|
package/src/guard-dumps.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { basename } from 'node:path'
|
|
1
2
|
import { splitCommand, tokenize } from '@kaynooo/cli-kit/shell'
|
|
2
3
|
|
|
3
4
|
const OPENER = /^[({]+/
|
|
@@ -5,7 +6,7 @@ const DUMP_FLAGS = /^-[a-z]*[px]/
|
|
|
5
6
|
// Git commands whose output carries the content of tracked files.
|
|
6
7
|
const GIT_PATCHES = new Set(['diff', 'log', 'show', 'whatchanged', 'format-patch', 'grep'])
|
|
7
8
|
const GIT_NO_PATCH = new Set(['--stat', '--numstat', '--shortstat', '--name-only', '--name-status', '--summary', '-s', '--no-patch', '--check', '--quiet'])
|
|
8
|
-
const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path'])
|
|
9
|
+
const GIT_VALUE_OPTIONS = new Set(['-C', '-c', '--git-dir', '--work-tree', '--namespace', '--exec-path', '--config-env', '--attr-source'])
|
|
9
10
|
const GIT_PATCH_FLAGS = new Set(['-p', '-u', '--patch', '-c', '--cc', '--patch-with-stat', '--patch-with-raw'])
|
|
10
11
|
|
|
11
12
|
export const GIT_EXCLUDE = /^:(?:!|\^|\(exclude\))/
|
|
@@ -18,6 +19,8 @@ const SHELL_COMMAND_FLAG = /^-[abd-z]*c[a-z]*$/
|
|
|
18
19
|
// Options taking a value, before a docker or compose subcommand, and before exec's container.
|
|
19
20
|
const DOCKER_VALUE_OPTIONS = new Set(['-f', '--file', '-p', '--project-name', '--profile', '--env-file', '--project-directory', '--ansi', '--progress', '--parallel', '-H', '--host', '-c', '--context', '--config', '-l', '--log-level'])
|
|
20
21
|
const EXEC_VALUE_OPTIONS = new Set(['-e', '--env', '--env-file', '-u', '--user', '-w', '--workdir', '--index', '--detach-keys'])
|
|
22
|
+
// Options taking a value before the image of `docker run` or the service of `compose run`.
|
|
23
|
+
const RUN_VALUE_OPTIONS = new Set([...EXEC_VALUE_OPTIONS, '-a', '--attach', '--add-host', '--annotation', '--blkio-weight', '--cap-add', '--cap-drop', '--cgroup-parent', '--cgroupns', '--cidfile', '-c', '--cpu-shares', '--cpus', '--cpuset-cpus', '--cpuset-mems', '--cpu-period', '--cpu-quota', '--device', '--dns', '--dns-option', '--dns-search', '--domainname', '--entrypoint', '--env-from-file', '--expose', '--gpus', '--group-add', '--health-cmd', '--health-interval', '--health-retries', '--health-start-period', '--health-timeout', '-h', '--hostname', '--ip', '--ip6', '--ipc', '--isolation', '-l', '--label', '--label-file', '--link', '--log-driver', '--log-opt', '--mac-address', '-m', '--memory', '--memory-reservation', '--memory-swap', '--mount', '--name', '--network', '--net', '--network-alias', '--pid', '--pids-limit', '--platform', '-p', '--publish', '--pull', '--restart', '--runtime', '--security-opt', '--shm-size', '--stop-signal', '--stop-timeout', '--storage-opt', '--sysctl', '--tmpfs', '--ulimit', '--userns', '--uts', '-v', '--volume', '--volumes-from'])
|
|
21
24
|
// `compose config` options printing names, never values.
|
|
22
25
|
const CONFIG_LISTS = new Set(['--services', '--volumes', '--profiles', '--images', '--networks', '--hash', '-q', '--quiet'])
|
|
23
26
|
const FORMAT_OPTIONS = new Set(['-f', '--format'])
|
|
@@ -38,17 +41,30 @@ function firstOperand(words: readonly string[], valueOptions: ReadonlySet<string
|
|
|
38
41
|
return words.length
|
|
39
42
|
}
|
|
40
43
|
|
|
44
|
+
// Whether a program, by path or by name, prints the whole environment.
|
|
45
|
+
function isEnvPrinter(program: string): boolean {
|
|
46
|
+
return ENV_PRINTERS.has(basename(program))
|
|
47
|
+
}
|
|
48
|
+
|
|
41
49
|
// The command a container runs prints its environment: `env` or `printenv` as the program, or as one run by `sh -c`.
|
|
42
50
|
function printsEnv([program = '', ...args]: readonly string[]): boolean {
|
|
43
51
|
const flags = args.findIndex(arg => !arg.startsWith('-') || SHELL_COMMAND_FLAG.test(arg))
|
|
44
52
|
|
|
45
|
-
if (SHELLS.has(program) && SHELL_COMMAND_FLAG.test(args[flags] ?? ''))
|
|
46
|
-
return splitCommand(args[flags + 1] ?? '').some(piece =>
|
|
53
|
+
if (SHELLS.has(basename(program)) && SHELL_COMMAND_FLAG.test(args[flags] ?? ''))
|
|
54
|
+
return splitCommand(args[flags + 1] ?? '').some(piece => isEnvPrinter(tokenize(piece.command)[0] ?? ''))
|
|
55
|
+
|
|
56
|
+
return isEnvPrinter(program)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// `docker run` and `compose run` print the environment when what runs is a dump: the entrypoint given, followed by the words after the image or service, or those words alone.
|
|
60
|
+
function runPrintsEnv(after: readonly string[]): boolean {
|
|
61
|
+
const entrypoint = after.find((_, i) => after[i - 1] === '--entrypoint') ?? after.find(word => word.startsWith('--entrypoint='))?.slice('--entrypoint='.length)
|
|
62
|
+
const command = after.slice(firstOperand(after, RUN_VALUE_OPTIONS) + 1)
|
|
47
63
|
|
|
48
|
-
return
|
|
64
|
+
return printsEnv(entrypoint === undefined || entrypoint === '' ? command : [entrypoint, ...command])
|
|
49
65
|
}
|
|
50
66
|
|
|
51
|
-
// A docker call printing a container's environment: `env` or `printenv` run through exec, `compose config` and `inspect`, which spell it out.
|
|
67
|
+
// A docker call printing a container's environment: `env` or `printenv` run through exec or run, `compose config` and `inspect`, which spell it out.
|
|
52
68
|
export function dumpsContainerEnv(program: string, args: string[]): boolean {
|
|
53
69
|
if (!DOCKER.has(program))
|
|
54
70
|
return false
|
|
@@ -60,11 +76,15 @@ export function dumpsContainerEnv(program: string, args: string[]): boolean {
|
|
|
60
76
|
if (program === 'docker' && isCompose)
|
|
61
77
|
rest = afterOptions(rest.slice(1))
|
|
62
78
|
|
|
63
|
-
|
|
79
|
+
// `docker container exec` and `docker container run` are exec and run.
|
|
80
|
+
const [subcommand = '', ...after] = !isCompose && rest[0] === 'container' && ['exec', 'run'].includes(rest[1] ?? '') ? rest.slice(1) : rest
|
|
64
81
|
|
|
65
82
|
if (subcommand === 'exec')
|
|
66
83
|
return printsEnv(after.slice(firstOperand(after, EXEC_VALUE_OPTIONS) + 1))
|
|
67
84
|
|
|
85
|
+
if (subcommand === 'run')
|
|
86
|
+
return runPrintsEnv(after)
|
|
87
|
+
|
|
68
88
|
if (isCompose)
|
|
69
89
|
return subcommand === 'config' && !after.some(word => CONFIG_LISTS.has(word))
|
|
70
90
|
|
|
@@ -89,7 +109,7 @@ export function dumpsEnvironment(program: string, args: readonly string[], token
|
|
|
89
109
|
if (program === 'set')
|
|
90
110
|
return args.length === 0
|
|
91
111
|
|
|
92
|
-
return program === '' && tokens.some(token => token.replace(OPENER, '') === 'env')
|
|
112
|
+
return program === '' && tokens.some(token => basename(token.replace(OPENER, '')) === 'env')
|
|
93
113
|
}
|
|
94
114
|
|
|
95
115
|
// Whether a git command prints the content of a tracked env file: a patch or a search with no pathspec leaving env files out.
|
|
@@ -112,3 +132,45 @@ export function printsTrackedEnv(program: string, args: readonly string[]): bool
|
|
|
112
132
|
|
|
113
133
|
return !rest.some(arg => GIT_NO_PATCH.has(arg))
|
|
114
134
|
}
|
|
135
|
+
|
|
136
|
+
// A redirection word: the fd it moves (none for stdout, `&` for both), the operator, and its target when written in the same word.
|
|
137
|
+
const REDIRECT = /^(\d*|&)(>>|>&|>|<)(.*)$/
|
|
138
|
+
// A redirection operator alone, its target being the next word.
|
|
139
|
+
const BARE_REDIRECT = /^(?:\d*|&)(?:>>|>&|>|<)$/
|
|
140
|
+
const FD = /^\d$/
|
|
141
|
+
|
|
142
|
+
// The words less the redirections and their targets, so `printenv > out.txt` names no variable.
|
|
143
|
+
export function withoutRedirects(words: readonly string[]): string[] {
|
|
144
|
+
return words.filter((word, at) => !REDIRECT.test(word) && !BARE_REDIRECT.test(words[at - 1] ?? ''))
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Where a redirection sends its fd: an fd copied from the current targets, a closed one (`>&-`) counting as /dev/null.
|
|
148
|
+
function resolveTarget(operator: string, to: string, target: Readonly<Record<string, string>>): string {
|
|
149
|
+
if (operator === '>&' && FD.test(to))
|
|
150
|
+
return target[to] ?? 'other'
|
|
151
|
+
|
|
152
|
+
return to === '-' ? '/dev/null' : to
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Whether the words send the piece's stdout to /dev/null (or close it), followed through `2>&1` and `1>&2` in the order bash applies them.
|
|
156
|
+
export function discardsStdout(words: readonly string[]): boolean {
|
|
157
|
+
const target: Record<string, string> = { 1: 'tty', 2: 'tty' }
|
|
158
|
+
|
|
159
|
+
for (let at = 0; at < words.length; at++) {
|
|
160
|
+
const match = REDIRECT.exec(words[at] ?? '')
|
|
161
|
+
|
|
162
|
+
if (match === null || match[2] === '<')
|
|
163
|
+
continue
|
|
164
|
+
|
|
165
|
+
const [, fd = '', operator, written] = match
|
|
166
|
+
const to = written === '' ? (words[++at] ?? '') : written as string
|
|
167
|
+
const resolved = resolveTarget(operator as string, to, target)
|
|
168
|
+
|
|
169
|
+
if (fd === '&' || (operator === '>&' && fd === '' && !FD.test(to)))
|
|
170
|
+
target[1] = target[2] = resolved
|
|
171
|
+
else
|
|
172
|
+
target[fd === '' ? 1 : fd] = resolved
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return target[1] === '/dev/null'
|
|
176
|
+
}
|
package/src/guard.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Piece } from '@kaynooo/cli-kit/shell'
|
|
2
2
|
import { parseCommand, splitCommand, substitutions, tokenize } from '@kaynooo/cli-kit/shell'
|
|
3
|
-
import { dumpsContainerEnv, dumpsEnvironment, printsTrackedEnv } from './guard-dumps'
|
|
3
|
+
import { discardsStdout, dumpsContainerEnv, dumpsEnvironment, printsTrackedEnv, withoutRedirects } from './guard-dumps'
|
|
4
4
|
import { readsEnv, variablesOf } from './guard-names'
|
|
5
5
|
import { loopReadsListedEnv, runnerReadsListedEnv } from './guard-pipes'
|
|
6
6
|
import { searchesEnv } from './guard-search'
|
|
@@ -27,9 +27,11 @@ export function touchingPieces(command: string, options: { hasTrackedEnv?: boole
|
|
|
27
27
|
if (readsEnv(piece, variables) || searchesEnv(program, args) || runnerReadsListedEnv(pieces, index, variables) || loopReadsListedEnv(pieces, index, variables))
|
|
28
28
|
return true
|
|
29
29
|
|
|
30
|
-
const
|
|
30
|
+
const operands = withoutRedirects(args)
|
|
31
|
+
const isDump = dumpsContainerEnv(program, operands) || dumpsEnvironment(program, operands, tokenize(piece.command)) || (options.hasTrackedEnv === true && printsTrackedEnv(program, operands))
|
|
31
32
|
|
|
32
|
-
|
|
33
|
+
// a dump whose stdout goes to /dev/null prints nothing, as a parse gate like `docker compose config >/dev/null`
|
|
34
|
+
if (!isDump || discardsStdout(tokenize(piece.command)))
|
|
33
35
|
return false
|
|
34
36
|
|
|
35
37
|
// an `ev redact` further down its pipeline masks what the command prints
|
|
@@ -44,7 +46,7 @@ export function touchingPieces(command: string, options: { hasTrackedEnv?: boole
|
|
|
44
46
|
})
|
|
45
47
|
}
|
|
46
48
|
|
|
47
|
-
// True when the command would read, write or print an env file or the whole environment; an env file named only inside a quoted message or a heredoc body is text,
|
|
49
|
+
// True when the command would read, write or print an env file or the whole environment; an env file named only inside a quoted message or a heredoc body is text, a container's environment piped through `ev redact` is masked, and one sent to /dev/null is never printed.
|
|
48
50
|
export function touchesEnv(command: string, options: { hasTrackedEnv?: boolean } = {}): boolean {
|
|
49
51
|
return touchingPieces(command, options).length > 0
|
|
50
52
|
}
|
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. `before` is the line text ahead of it
|
|
32
|
-
skip?: (value: string, before: string) => boolean
|
|
31
|
+
// Skips a candidate that is a placeholder or a plain word. `before` is the line text ahead of it, `name` the key a pattern names `name`.
|
|
32
|
+
skip?: (value: string, before: string, name?: string) => boolean
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// A secret value starting like this is a reference or a placeholder, not the secret.
|
|
@@ -57,7 +57,31 @@ function isWeak(value: string, before: string): boolean {
|
|
|
57
57
|
|
|
58
58
|
// A key name holding one of the words: `AWS_SECRET_ACCESS_KEY`, `X-Api-Key`, `access_token`.
|
|
59
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')
|
|
60
|
+
return new RegExp(`(?<![\\w-])["']?(?<name>(?:[\\w-]{0,30}[_-])?(?:${names})(?:[_-][\\w-]{0,30})?)["']?${KEY_VALUE_GAP}${value}`, 'dgi')
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Any key name, which `isSecretName` then judges, so `PGPASSWORD`, `MYSQL_PWD` and `clientSecret` count as `DB_PASSWORD` does.
|
|
64
|
+
const ANY_NAME = '(?<![\\w.-])["\']?(?<name>[a-z_][\\w.-]{0,60})["\']?'
|
|
65
|
+
// Names a value under is a secret even when short and without a digit, as under `password`.
|
|
66
|
+
const PASSWORD_NAME = new RegExp(`${STRONG_NAME}|pass|pwd`, 'i')
|
|
67
|
+
// Name endings of a setting about a secret rather than the secret (`TOKEN_TTL`, `PASSWORD_FILE`).
|
|
68
|
+
const SETTING_SUFFIX = /[_.-](?:TTL|FILE|PATH|DIR|NAME|URL|TIMEOUT|EXPIRY|EXPIRES|LIFETIME|LENGTH|ENABLED|ALGORITHM|HEADER|PREFIX|COUNT|MAX|MIN)$/i
|
|
69
|
+
|
|
70
|
+
const NUMBER = /^\d+$/
|
|
71
|
+
// Names of a count rather than of the thing counted: `max_tokens`, `num_sessions`, `tokens`.
|
|
72
|
+
const COUNT_NAME = /(?:^|[_.-])(?:max|min|num)[_.-]|^tokens$/i
|
|
73
|
+
|
|
74
|
+
// A value under a setting's name, or a number under a count's (`max_tokens=4096`), is no secret.
|
|
75
|
+
function isSetting(value: string, name: string): boolean {
|
|
76
|
+
return SETTING_SUFFIX.test(name) || (COUNT_NAME.test(name) && NUMBER.test(value))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// The skip of a key the name alone does not settle: a private key is masked by its own markers.
|
|
80
|
+
function isPlainNamed(value: string, before: string, name = ''): boolean {
|
|
81
|
+
if (!isSecretName(name) || isSetting(value, name) || value.length < 4 || value.includes('-----BEGIN '))
|
|
82
|
+
return true
|
|
83
|
+
|
|
84
|
+
return PASSWORD_NAME.test(name) ? isProse(value, before) : isWeak(value, before)
|
|
61
85
|
}
|
|
62
86
|
|
|
63
87
|
// A pattern with a group masks that group only, else the whole match.
|
|
@@ -80,10 +104,14 @@ const TOKEN_FORMATS: TokenFormat[] = [
|
|
|
80
104
|
{ kind: 'SendGrid key', pattern: /\bSG\.[\w-]{16,}\.[\w-]{16,}/dg },
|
|
81
105
|
{ kind: 'Authorization header', pattern: /\bAuthorization["']?\s*[:=]\s*["']?(?:Bearer|Basic|Token|Digest)\s+([^\s"',;]{8,})/dgi, skip: isPlaceholder },
|
|
82
106
|
{ kind: 'Bearer token', pattern: /\bBearer\s+((?=[\w.~+/=-]*\d)[\w.~+/=-]{20,})/dg },
|
|
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, '(["\'])((?:\\\\.|(?!\\
|
|
86
|
-
{ kind: 'password', pattern: keyed(WEAK_NAME, '(["\'])((?:\\\\.|(?!\\
|
|
107
|
+
{ kind: 'password', pattern: keyed(STRONG_NAME, '([^\\s"\'&;,]{4,})'), skip: (value, before, name = '') => isSetting(value, name) || isProse(value, before) },
|
|
108
|
+
{ kind: 'password', pattern: keyed(WEAK_NAME, '([^\\s"\'&;,]{4,})'), skip: (value, before, name = '') => isSetting(value, name) || isWeak(value, before) },
|
|
109
|
+
{ kind: 'password', pattern: keyed(STRONG_NAME, '(["\'])((?:\\\\.|(?!\\2)[^\\\\\\n]){4,})\\2'), skip: (value, _, name = '') => isSetting(value, name) || isPlaceholder(value) },
|
|
110
|
+
{ kind: 'password', pattern: keyed(WEAK_NAME, '(["\'])((?:\\\\.|(?!\\2)[^\\\\\\n]){4,})\\2'), skip: (value, before, name = '') => isSetting(value, name) || isWeak(value, before) },
|
|
111
|
+
{ kind: 'password', pattern: new RegExp(`${ANY_NAME}${KEY_VALUE_GAP}([^\\s"'&;,]{4,})`, 'dgi'), skip: isPlainNamed },
|
|
112
|
+
{ kind: 'password', pattern: new RegExp(`${ANY_NAME}${KEY_VALUE_GAP}(["'])((?:\\\\.|(?!\\2)[^\\\\\\n]){4,})\\2`, 'dgi'), skip: isPlainNamed },
|
|
113
|
+
// A variable as printenv or env prints it, whose value runs to the end of the line, spaces included.
|
|
114
|
+
{ kind: 'password', pattern: /^\s*(?:export\s+)?(?<name>[A-Za-z_]\w{0,60})=(?!["'])(\S(?:.*\S)?)/dg, skip: isPlainNamed },
|
|
87
115
|
]
|
|
88
116
|
|
|
89
117
|
// 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.
|
|
@@ -238,7 +266,7 @@ export function findSecrets(line: string, known?: KnownSecrets): Match[] {
|
|
|
238
266
|
|
|
239
267
|
const value = line.slice(start, end)
|
|
240
268
|
|
|
241
|
-
if (!skip?.(value, line.slice(0, start)))
|
|
269
|
+
if (!skip?.(value, line.slice(0, start), token.groups?.name))
|
|
242
270
|
found.push({ start, end, kind, mask: hint(value) })
|
|
243
271
|
}
|
|
244
272
|
}
|