@kaynooo/cli-env 0.8.0 → 0.9.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.
@@ -0,0 +1,181 @@
1
+ import type { Piece } from '@kaynooo/cli-kit/shell'
2
+ import { parseCommand } from '@kaynooo/cli-kit/shell'
3
+ import { EXCLUDE_OPTIONS } from './guard-paths'
4
+
5
+ const ENV_NAME_IN_PATTERN = /(?:^|[^\w-])\.env(?![\w-])/
6
+ const SHORT_FLAGS = /^-[a-z]+$/i
7
+
8
+ export const GREPS = new Set(['grep', 'egrep', 'fgrep'])
9
+ export const GREP_VALUE_OPTIONS = new Set(['-e', '-f', '-m', '-A', '-B', '-C', '-d', '-D', '--regexp', '--file', '--max-count', '--include', '--exclude', '--exclude-dir', '--exclude-from', '--context', '--after-context', '--before-context', '--label', '--binary-files', '--directories', '--devices'])
10
+ export const RG_VALUE_OPTIONS = new Set(['-e', '-f', '-g', '-t', '-T', '-m', '-A', '-B', '-C', '-j', '-d', '-r', '-M', '-E', '--replace', '--max-columns', '--encoding', '--regexp', '--file', '--glob', '--iglob', '--type', '--type-not', '--max-count', '--context', '--after-context', '--before-context', '--threads', '--max-depth', '--max-filesize'])
11
+ const PATTERN_OPTIONS = new Set(['-e', '-f', '--regexp', '--file'])
12
+ // rg's -L follows links, it does not list the files without a match
13
+ const GREP_NAMES_ONLY = new Set('lLcq')
14
+ const RG_NAMES_ONLY = new Set('lcq')
15
+ const NAMES_ONLY_LONG = new Set(['--files-with-matches', '--files-without-match', '--count', '--count-matches', '--quiet', '--silent'])
16
+ const FIND_ACTIONS = new Set(['-exec', '-execdir', '-ok', '-okdir', '-delete', '-fprint', '-fprint0', '-fprintf', '-fls', '-files0-from'])
17
+ const HIDDEN_FLAGS = /^(?:-u{2,}|--hidden|-\.)$/i
18
+ const HAS_R = /r/i
19
+ // A directory listing everything under it: the current one, its parents, home, the root.
20
+ const WHOLE_TREE_OPERAND = /^(?:(?:\.\.?\/?)+|~\/?|\$HOME\/?|\/)$/
21
+ // An option leaving env files out of a search: grep's `--exclude=.env*`, ripgrep's `-g '!.env*'`.
22
+ const EXCLUDES_ENV = /^(?:--exclude(?:-dir)?=.*env|(?:--glob=|--iglob=|-g)?!.*env)/
23
+
24
+ // Whether a piece is a grep-family search, which the refusal then advises on.
25
+ export function isSearchCommand(piece: Piece): boolean {
26
+ const { program } = parseCommand(piece.command)
27
+
28
+ return GREPS.has(program) || program === 'rg' || program === 'ag'
29
+ }
30
+
31
+ // Whether the grep family lists file names or counts matches only (`grep -l`, `-L`, `-c`, `-q`), which prints no line of a file.
32
+ export function printsNamesOnly(program: string, args: readonly string[]): boolean {
33
+ const isGrep = GREPS.has(program)
34
+
35
+ if (!isGrep && program !== 'rg')
36
+ return false
37
+
38
+ const valueOptions = isGrep ? GREP_VALUE_OPTIONS : RG_VALUE_OPTIONS
39
+ const valueLetters = valueLettersOf(valueOptions)
40
+ const namesOnly = isGrep ? GREP_NAMES_ONLY : RG_NAMES_ONLY
41
+ // the word after an option taking a value is that value, never a flag: `grep -e -l .env` prints lines
42
+ let isValue = false
43
+
44
+ for (const arg of args) {
45
+ if (isValue) {
46
+ isValue = false
47
+
48
+ continue
49
+ }
50
+
51
+ if (arg === '--')
52
+ return false
53
+
54
+ if (NAMES_ONLY_LONG.has(arg))
55
+ return true
56
+
57
+ if (valueOptions.has(arg)) {
58
+ isValue = true
59
+
60
+ continue
61
+ }
62
+
63
+ if (!SHORT_FLAGS.test(arg))
64
+ continue
65
+
66
+ // a cluster ends at the first option taking a value, which swallows the rest of it or else the next word
67
+ for (const [i, letter] of [...arg.slice(1)].entries()) {
68
+ if (valueLetters.has(letter)) {
69
+ isValue = i === arg.length - 2
70
+
71
+ break
72
+ }
73
+
74
+ if (namesOnly.has(letter))
75
+ return true
76
+ }
77
+ }
78
+
79
+ return false
80
+ }
81
+
82
+ // `find` only printing the names it matches, with no action that runs a command or writes a file.
83
+ export function listsFiles(program: string, args: readonly string[]): boolean {
84
+ return program === 'find' && !args.some(arg => FIND_ACTIONS.has(arg))
85
+ }
86
+
87
+ // The letters of the short options taking a value.
88
+ function valueLettersOf(valueOptions: ReadonlySet<string>): Set<string> {
89
+ return new Set([...valueOptions].filter(option => option.length === 2 && !option.startsWith('--')).map(option => option.slice(1)))
90
+ }
91
+
92
+ // The index in `args` of the pattern a search program is given, undefined when `-e` or `-f` gives it, even glued to a cluster as in `-ne KEY`: it is a regex, never a file.
93
+ export function patternIndex(args: readonly string[], valueOptions: ReadonlySet<string>): number | undefined {
94
+ const valueLetters = valueLettersOf(valueOptions)
95
+
96
+ for (let i = 0; i < args.length; i++) {
97
+ const arg = args[i] ?? ''
98
+
99
+ if (arg.startsWith('--regexp=') || arg.startsWith('--file='))
100
+ return undefined
101
+
102
+ if (!arg.startsWith('-') || arg === '-')
103
+ return i
104
+
105
+ if (arg.startsWith('--')) {
106
+ if (PATTERN_OPTIONS.has(arg))
107
+ return undefined
108
+
109
+ if (valueOptions.has(arg))
110
+ i++
111
+
112
+ continue
113
+ }
114
+
115
+ // a cluster of short options ends at the first one taking a value, glued after it or the next word
116
+ const at = [...arg.slice(1)].findIndex(letter => valueLetters.has(letter))
117
+ const letter = arg[at + 1]
118
+
119
+ if (at === -1)
120
+ continue
121
+
122
+ if (letter === 'e' || letter === 'f')
123
+ return undefined
124
+
125
+ if (at === arg.length - 2)
126
+ i++
127
+ }
128
+
129
+ return undefined
130
+ }
131
+
132
+ // The operands of a search program: its words less the options, their values and the pattern, which `-e` or `-f` may have already given.
133
+ function operandsOf(args: readonly string[], valueOptions: ReadonlySet<string>): string[] {
134
+ const operands: string[] = []
135
+ let hasPattern = false
136
+
137
+ for (let i = 0; i < args.length; i++) {
138
+ const arg = args[i] ?? ''
139
+
140
+ if (PATTERN_OPTIONS.has(arg) || arg.startsWith('--regexp=') || arg.startsWith('--file='))
141
+ hasPattern = true
142
+
143
+ if (valueOptions.has(arg))
144
+ i++
145
+ else if (!arg.startsWith('-'))
146
+ operands.push(arg)
147
+ }
148
+
149
+ return hasPattern ? operands : operands.slice(1)
150
+ }
151
+
152
+ // Whether a search over a whole tree would open an env file: a recursive grep, or ripgrep and ag told to look at hidden files, which they skip by default.
153
+ export function searchesEnv(program: string, args: readonly string[]): boolean {
154
+ const isGrep = GREPS.has(program)
155
+
156
+ if ((!isGrep && program !== 'rg' && program !== 'ag') || printsNamesOnly(program, args))
157
+ return false
158
+
159
+ const isRecursive = args.some(arg => (SHORT_FLAGS.test(arg) && HAS_R.test(arg)) || arg === '--recursive' || arg === '--dereference-recursive')
160
+ const isHidden = args.some(arg => HIDDEN_FLAGS.test(arg) || (program === 'ag' && (arg === '-u' || arg === '--unrestricted')))
161
+ const isLimited = args.some((arg, i) => arg.startsWith('--include') || EXCLUDES_ENV.test(arg) || (EXCLUDE_OPTIONS.has(args[i - 1] ?? '') && arg.includes('env')))
162
+
163
+ if (isLimited || (isGrep ? !isRecursive : !isHidden))
164
+ return false
165
+
166
+ const operands = operandsOf(args, isGrep ? GREP_VALUE_OPTIONS : RG_VALUE_OPTIONS)
167
+
168
+ return operands.length === 0 || operands.some(operand => WHOLE_TREE_OPERAND.test(operand))
169
+ }
170
+
171
+ // Whether the piece is a search whose pattern, its escapes dropped, names an env file (`grep '^\.env'`): fed a listing, what it prints are env file names.
172
+ export function searchesEnvNames(piece: Piece): boolean {
173
+ const { program, args } = parseCommand(piece.command)
174
+
175
+ if (!GREPS.has(program) && program !== 'rg')
176
+ return false
177
+
178
+ const at = patternIndex(args, GREPS.has(program) ? GREP_VALUE_OPTIONS : RG_VALUE_OPTIONS)
179
+
180
+ return at !== undefined && ENV_NAME_IN_PATTERN.test((args[at] ?? '').replaceAll('\\', ''))
181
+ }
package/src/guard.ts ADDED
@@ -0,0 +1,51 @@
1
+ import type { Piece } from '@kaynooo/cli-kit/shell'
2
+ import { parseCommand, splitCommand, substitutions, tokenize } from '@kaynooo/cli-kit/shell'
3
+ import { dumpsContainerEnv, dumpsEnvironment, printsTrackedEnv } from './guard-dumps'
4
+ import { readsEnv, variablesOf } from './guard-names'
5
+ import { loopReadsListedEnv, runnerReadsListedEnv } from './guard-pipes'
6
+ import { searchesEnv } from './guard-search'
7
+
8
+ // Code printing the whole environment Bun filled from the .env files.
9
+ const ENV_DUMP = /(?:console\.\w+|JSON\.stringify|Object\.(?:entries|keys|values)|print)\(\s*process\.env\s*[,)]/
10
+
11
+ export const USE_EV = '.env files hold secrets that must never reach the transcript, so they are not read, edited or printed directly. Use `ev` instead: `ev list`, `ev get KEY` (masked), `ev grep <regex>` for the keys matching it, `ev show <rev>:<file>` for one at a git revision, `printf %s "$value" | ev set KEY [file]` (`--empty` for an empty value), `ev unset KEY`, `ev cp KEY --from .env --to .env.local`, `ev mv KEY --from .env --to .env.local`, `ev diff a b`, `ev missing`; and pipe output that may carry a secret through `redact`, as `docker compose exec <svc> printenv | redact` or `docker compose config | redact`. If `ev` is missing: `bun install -g @kaynooo/cli-env`.'
12
+
13
+ export const USE_EV_SEARCH = 'To search them, `ev grep <regex>` prints the matching keys, masked. `grep -l`, `-L` and `-c` (file names and counts) are allowed, and a tree-wide search can run as `rg <regex>`, which skips gitignored files, or `grep -r --exclude=\'.env*\' <regex> .`.'
14
+
15
+ // The pieces of a command that would read, write or print an env file or the whole environment.
16
+ export function touchingPieces(command: string, options: { hasTrackedEnv?: boolean } = {}): Piece[] {
17
+ const pieces = splitCommand(command)
18
+
19
+ if (ENV_DUMP.test(command) || substitutions(command).some(each => touchesEnv(each, options)))
20
+ return pieces
21
+
22
+ const variables = variablesOf(command)
23
+
24
+ return pieces.filter((piece, index) => {
25
+ const { program, args } = parseCommand(piece.command)
26
+
27
+ if (readsEnv(piece, variables) || searchesEnv(program, args) || runnerReadsListedEnv(pieces, index, variables) || loopReadsListedEnv(pieces, index, variables))
28
+ return true
29
+
30
+ const isDump = dumpsContainerEnv(program, args) || dumpsEnvironment(program, args, tokenize(piece.command)) || (options.hasTrackedEnv === true && printsTrackedEnv(program, args))
31
+
32
+ if (!isDump)
33
+ return false
34
+
35
+ // a redact further down its pipeline masks what the command prints
36
+ for (let at = index; pieces[at]?.separator === '|'; at++) {
37
+ if (parseCommand(pieces[at + 1]?.command ?? '').program === 'redact')
38
+ return false
39
+ }
40
+
41
+ return true
42
+ })
43
+ }
44
+
45
+ // 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, and a container's environment piped through `redact` is masked.
46
+ export function touchesEnv(command: string, options: { hasTrackedEnv?: boolean } = {}): boolean {
47
+ return touchingPieces(command, options).length > 0
48
+ }
49
+
50
+ export { isEnvFile, reachesEnvFile } from './guard-paths'
51
+ export { isSearchCommand } from './guard-search'
package/src/mask.ts ADDED
@@ -0,0 +1,264 @@
1
+ import { isSecretName } from '@kaynooo/cli-kit'
2
+
3
+ export interface Match {
4
+ start: number
5
+ end: number
6
+ kind: string
7
+ // What replaces the span: a hint of the secret, or `***` for a URL password.
8
+ mask: string
9
+ }
10
+
11
+ // Secret values taken from .env files: the regex finds them whole in a line, `keys` says which key each belongs to.
12
+ export interface KnownSecrets {
13
+ pattern: RegExp | undefined
14
+ keys: Map<string, string>
15
+ }
16
+
17
+ // A value shorter than this is not masked when found loose in output, too many false hits.
18
+ export const MIN_KNOWN_LENGTH = 4
19
+
20
+ export const KEY_MASK = '*** private key redacted ***'
21
+ // A value that is a setting rather than a secret, even under a secret-looking key.
22
+ const PLAIN_VALUE = /^(?:true|false|null|none|nil|yes|no|on|off|undefined)$/i
23
+ const PLACEHOLDER = /^[$%{<(*/]/
24
+ const URL_PASSWORD = /(?<![a-z0-9+.-])[a-z][a-z0-9+.-]{0,31}:\/\/[^\s:/@]*:([^\s'"]{1,1024})@/dgi
25
+ const PORT = /^\d{1,5}$/
26
+ const ESCAPE = /[.*+?^${}()|[\]\\]/g
27
+
28
+ interface TokenFormat {
29
+ kind: string
30
+ pattern: RegExp
31
+ // Skips a candidate that is a placeholder or a plain word.
32
+ skip?: (value: string) => boolean
33
+ }
34
+
35
+ // A secret value starting like this is a reference or a placeholder, not the secret.
36
+ function isPlaceholder(value: string): boolean {
37
+ return PLACEHOLDER.test(value) || PLAIN_VALUE.test(value)
38
+ }
39
+
40
+ // A pattern with a group masks that group only, else the whole match.
41
+ const TOKEN_FORMATS: TokenFormat[] = [
42
+ { kind: 'GitHub token', pattern: /\b(?:gh[pousr]_|github_pat_)\w{20,}/dg },
43
+ { kind: 'GitLab token', pattern: /\bglpat-[\w-]{20,}/dg },
44
+ { kind: 'API key', pattern: /\bsk-[\w-]{20,}/dg },
45
+ { kind: 'Slack token', pattern: /\bx(?:ox[abprs]|app)-[\w-]{10,}/dg },
46
+ { kind: 'JWT', pattern: /\beyJ[\w-]+\.[\w-]+\.[\w-]+/dg },
47
+ { kind: 'AWS access key', pattern: /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/dg },
48
+ { kind: 'npm token', pattern: /\bnpm_[A-Za-z0-9]{30,}/dg },
49
+ { kind: 'Stripe key', pattern: /\b[sr]k_(?:live|test)_[A-Za-z0-9]{10,}/dg },
50
+ { kind: 'Google API key', pattern: /\bAIza[\w-]{35}/dg },
51
+ { kind: 'SendGrid key', pattern: /\bSG\.[\w-]{16,}\.[\w-]{16,}/dg },
52
+ { kind: 'Authorization header', pattern: /\bAuthorization["']?\s*[:=]\s*["']?(?:Bearer|Basic|Token|Digest)\s+([^\s"',;]{8,})/dgi, skip: isPlaceholder },
53
+ { kind: 'Bearer token', pattern: /\bBearer\s+((?=[\w.~+/=-]*\d)[\w.~+/=-]{20,})/dg },
54
+ { kind: 'password', pattern: /(?<![\w-])(?:[\w-]{0,30}[_-])?(?:password|passwd|secret|api[_-]?key)(?:[_-][\w-]{0,30})?=([^\s"'&;,]{4,})/dgi, skip: isPlaceholder },
55
+ { kind: 'password', pattern: /(?<![\w-])["']?(?:[\w-]{0,30}[_-])?(?:password|passwd|secret|api[_-]?key)(?:[_-][\w-]{0,30})?["']?\s*[:=]\s*(["'])((?:\\.|(?!\1)[^\\\n]){4,})\1/dgi, skip: isPlaceholder },
56
+ ]
57
+
58
+ // 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.
59
+ export const PEM_BEGIN = /-----BEGIN [A-Z ]*PRIVATE KEY(?: BLOCK)?-----/
60
+ export const PEM_END = /-----END [A-Z ]*PRIVATE KEY(?: BLOCK)?-----/
61
+ const PEM_BODY = /(?<=-----BEGIN [A-Z ]*PRIVATE KEY(?: BLOCK)?-----)(?:(?!-----END [A-Z ]*PRIVATE KEY(?: BLOCK)?-----)[\s\S])+/dg
62
+ const PEM_BEGIN_ALL = /-----BEGIN [A-Z ]*PRIVATE KEY(?: BLOCK)?-----/g
63
+
64
+ // A URL split into authority, path and query, each with its offsets (flag d).
65
+ const URL_PARTS = /(?<![a-z0-9+.-])[a-z][a-z0-9+.-]{0,31}:\/\/([^\s/?#'"<>`]*)([^\s?#'"<>`]*)(?:\?([^\s#'"<>`]*))?/dgi
66
+ const PATH_SEGMENT = /[^/]+/g
67
+ const SECRET_PARAM = /^(?:token|[a-z]+_token|key|api[_-]?key|access[_-]?key|secret|[a-z]+_secret|password|passwd|pwd|sig|signature|auth|authorization)$/i
68
+
69
+ // Webhook and ping URLs whose last path segment is the secret, whatever the key holding the URL is named.
70
+ const WEBHOOK_FORMATS: [kind: string, pattern: RegExp][] = [
71
+ ['Discord webhook', /(?:^|\.)discord(?:app)?\.com\/api\/(?:v\d+\/)?webhooks\/\d+\/([\w-]+)/di],
72
+ ['Slack webhook', /^hooks\.slack\.com\/(?:services|workflows)\/(?:[\w-]+\/)*([\w-]+)/di],
73
+ ['Uptime Kuma push URL', /\/api\/push\/([\w-]+)/di],
74
+ ['healthchecks.io ping URL', /^hc-ping\.com\/([\w-]+)/di],
75
+ ]
76
+
77
+ // A path segment that looks like a token: at least 20 characters of [A-Za-z0-9_-] mixing lowercase, uppercase and digits, or at least 32 alphanumerics mixing letters and digits (hex, base32, dashless UUIDs). Plain slugs, dates and words never do.
78
+ const OPAQUE_SEGMENT = /^(?:(?=[\w-]{20,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\w-]+|(?=[A-Za-z\d]{32,}$)(?=.*[A-Za-z])(?=.*\d).+)$/
79
+
80
+ export function isSecretKey(key: string): boolean {
81
+ return isSecretName(key)
82
+ }
83
+
84
+ // A few leading characters and the length, so two values can be told apart without leaking either.
85
+ export function hint(value: string): string {
86
+ const shown = Math.min(3, Math.floor(value.length / 4))
87
+
88
+ return `${value.slice(0, shown)}***(${value.length})`
89
+ }
90
+
91
+ // The forms a value takes in output: as is, percent-encoded and escaped inside a JSON string.
92
+ function forms(value: string): string[] {
93
+ return [...new Set([value, encodeURIComponent(value), JSON.stringify(value).slice(1, -1)])]
94
+ }
95
+
96
+ export function knownSecrets(pairs: Iterable<readonly [key: string, value: string]>): KnownSecrets {
97
+ const keys = new Map<string, string>()
98
+
99
+ for (const [key, value] of pairs) {
100
+ if (value.length < MIN_KNOWN_LENGTH || PLAIN_VALUE.test(value))
101
+ continue
102
+
103
+ for (const form of forms(value)) {
104
+ if (!keys.has(form))
105
+ keys.set(form, key)
106
+ }
107
+ }
108
+
109
+ if (keys.size === 0)
110
+ return { pattern: undefined, keys }
111
+
112
+ const alternatives = [...keys.keys()].sort((a, b) => b.length - a.length).map(value => value.replace(ESCAPE, '\\$&'))
113
+
114
+ return { pattern: new RegExp(`(?<!\\w)(?:${alternatives.join('|')})(?!\\w)`, 'g'), keys }
115
+ }
116
+
117
+ // The password span of each URL in the text. A password may hold `@`, the last one before the host closing it, and `/`, unless only a port precedes the `/` (`host:3000/@scope/pkg`).
118
+ function passwordSpans(text: string): [start: number, end: number][] {
119
+ const spans: [number, number][] = []
120
+
121
+ for (const url of text.matchAll(URL_PASSWORD)) {
122
+ const [start, end] = url.indices?.[1] ?? []
123
+ const password = url[1] ?? ''
124
+ const slash = password.indexOf('/')
125
+
126
+ if (start !== undefined && end !== undefined && !(slash !== -1 && PORT.test(password.slice(0, slash))))
127
+ spans.push([start, end])
128
+ }
129
+
130
+ return spans
131
+ }
132
+
133
+ export function urlPasswords(text: string): string[] {
134
+ return passwordSpans(text).map(([start, end]) => text.slice(start, end))
135
+ }
136
+
137
+ // The tokens a URL carries outside its password: the secret of a known webhook format, an opaque path segment and the value of a token-like query parameter.
138
+ function urlTokens(line: string): Match[] {
139
+ const found: Match[] = []
140
+ const add = (kind: string, start: number, value: string): void => {
141
+ found.push({ start, end: start + value.length, kind, mask: hint(value) })
142
+ }
143
+
144
+ for (const url of line.matchAll(URL_PARTS)) {
145
+ const [authority, path = '', query] = [url[1] ?? '', url[2] ?? '', url[3]]
146
+ const authorityStart = url.indices?.[1]?.[0]
147
+ const pathStart = url.indices?.[2]?.[0]
148
+ const queryStart = url.indices?.[3]?.[0]
149
+
150
+ if (authorityStart === undefined || pathStart === undefined)
151
+ continue
152
+
153
+ const hostAt = authority.lastIndexOf('@') + 1
154
+ const target = authority.slice(hostAt) + path
155
+
156
+ for (const [kind, pattern] of WEBHOOK_FORMATS) {
157
+ const [start, end] = pattern.exec(target)?.indices?.[1] ?? []
158
+
159
+ if (start !== undefined && end !== undefined)
160
+ add(kind, authorityStart + hostAt + start, target.slice(start, end))
161
+ }
162
+
163
+ for (const segment of path.matchAll(PATH_SEGMENT)) {
164
+ if (OPAQUE_SEGMENT.test(segment[0]))
165
+ add('URL token', pathStart + segment.index, segment[0])
166
+ }
167
+
168
+ if (query === undefined || queryStart === undefined)
169
+ continue
170
+
171
+ let offset = 0
172
+
173
+ // Split rather than matched with a regex, which rescans the rest of a long parameter from every character.
174
+ for (const param of query.split('&')) {
175
+ const equals = param.indexOf('=')
176
+
177
+ if (equals > 0 && equals < param.length - 1 && SECRET_PARAM.test(param.slice(0, equals)))
178
+ add('URL token', queryStart + offset + equals + 1, param.slice(equals + 1))
179
+
180
+ offset += param.length + 1
181
+ }
182
+ }
183
+
184
+ return found.filter(match => match.end > match.start)
185
+ }
186
+
187
+ // Every secret in one line, longest first where spans overlap.
188
+ export function findSecrets(line: string, known?: KnownSecrets): Match[] {
189
+ const found: Match[] = []
190
+
191
+ for (const [start, end] of passwordSpans(line))
192
+ found.push({ start, end, kind: 'URL password', mask: '***' })
193
+
194
+ for (const body of line.matchAll(PEM_BODY)) {
195
+ const [start, end] = body.indices?.[0] ?? []
196
+
197
+ if (start !== undefined && end !== undefined)
198
+ found.push({ start, end, kind: 'private key', mask: KEY_MASK })
199
+ }
200
+
201
+ for (const { kind, pattern, skip } of TOKEN_FORMATS) {
202
+ for (const token of line.matchAll(pattern)) {
203
+ const [start, end] = token.indices?.at(-1) ?? []
204
+
205
+ if (start === undefined || end === undefined)
206
+ continue
207
+
208
+ const value = line.slice(start, end)
209
+
210
+ if (!skip?.(value))
211
+ found.push({ start, end, kind, mask: hint(value) })
212
+ }
213
+ }
214
+
215
+ found.push(...urlTokens(line))
216
+
217
+ if (known?.pattern !== undefined) {
218
+ for (const value of line.matchAll(known.pattern))
219
+ found.push({ start: value.index, end: value.index + value[0].length, kind: `value of ${known.keys.get(value[0])}`, mask: hint(value[0]) })
220
+ }
221
+
222
+ found.sort((a, b) => a.start - b.start || b.end - a.end)
223
+
224
+ const kept: Match[] = []
225
+
226
+ for (const match of found) {
227
+ if (match.start >= (kept.at(-1)?.end ?? 0))
228
+ kept.push(match)
229
+ }
230
+
231
+ return kept
232
+ }
233
+
234
+ // When the line opens a private key block it does not close (the last BEGIN marker has no END marker after it), the key text that follows the marker on the line, else undefined.
235
+ export function openKeyBody(line: string): string | undefined {
236
+ const last = [...line.matchAll(PEM_BEGIN_ALL)].at(-1)
237
+
238
+ if (last === undefined)
239
+ return undefined
240
+
241
+ const after = line.slice(last.index + last[0].length)
242
+
243
+ return PEM_END.test(after) ? undefined : after.trim()
244
+ }
245
+
246
+ export function maskLine(line: string, known?: KnownSecrets): string {
247
+ let out = ''
248
+ let at = 0
249
+
250
+ for (const match of findSecrets(line, known)) {
251
+ out += line.slice(at, match.start) + match.mask
252
+ at = match.end
253
+ }
254
+
255
+ return out + line.slice(at)
256
+ }
257
+
258
+ // A value shown by ev: wholly masked under a secret-looking key or when it holds a private key, else only the secrets inside it.
259
+ export function maskValue(key: string, value: string, known?: KnownSecrets): string {
260
+ if (value === '')
261
+ return value
262
+
263
+ return isSecretName(key) || PEM_BEGIN.test(value) ? hint(value) : maskLine(value, known)
264
+ }