@kaynooo/cli-env 0.8.1 → 0.9.1

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/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
+ }
package/src/pins.ts ADDED
@@ -0,0 +1,268 @@
1
+ import type { Dirent } from 'node:fs'
2
+ import { readdir, readFile } from 'node:fs/promises'
3
+ import { join, relative } from 'node:path'
4
+ import { fail } from '@kaynooo/cli-kit'
5
+ import { parseDotenv } from './dotenv'
6
+
7
+ export interface PinFinding {
8
+ // A test file that renders a page or starts the app, relative to the app folder.
9
+ file: string
10
+ unpinned: string[]
11
+ }
12
+
13
+ export interface AppPins {
14
+ app: string
15
+ schema: string
16
+ // The optional variables of the schema whose name says they reach outside.
17
+ reaching: string[]
18
+ // What no test env entry, preload or test file pins.
19
+ unpinned: string[]
20
+ // The rendering test files leaving some of the reaching variables to the developer's env.
21
+ files: PinFinding[]
22
+ // The local preload that drops what `.env` sets and `.env.test` does not, which pins every variable.
23
+ preload?: string
24
+ }
25
+
26
+ const OPTIONAL = /^\s*([A-Z][A-Z0-9_]*)\s*:\s*s\.Optional\(/gm
27
+ // A URL, key, token, secret, password, DSN or non-bare host, or the file holding one; the app's own public address only links back to itself.
28
+ const REACHING = /(?:URL|URI|TOKEN|KEY|SECRET|PASSWORD|DSN|_HOST)(?:_FILE)?$/
29
+ const SELF = /(?:^|_)(?:PUBLIC|SITE|BASE|APP)_URL$/
30
+ const RENDERS = /\b(?:testSite|proxyKeyTests)\s*\(/
31
+ // A test file that renders pages reaching none of the variables says so with this marker and a reason.
32
+ const NOT_NEEDED = /\/\/\s*ev pins:\s*none reached\b/
33
+ const OVERRIDE = /\boverrideEnv\s*\(/g
34
+ const KEY = /(?:^|[{,])\s*([A-Z_]\w*)\s*:/gi
35
+ const TEST_FILE = /\.test\.[cm]?[jt]sx?$/
36
+ const ENV_TEST = /^\.env\.test(?:\.local)?$/
37
+ const TEST_ROOTS = ['tests', 'test', '__tests__', 'e2e', 'src']
38
+ const SKIPPED = new Set(['node_modules', '.git', '.kay', 'dist', 'vendor'])
39
+ const SCHEMA_CANDIDATES = ['src/env.ts', 'env.ts', 'src/env/index.ts']
40
+ const OPENERS = new Set(['(', '[', '{'])
41
+ const CLOSERS = new Set([')', ']', '}'])
42
+ const QUOTES = new Set(['\'', '"', '`'])
43
+
44
+ async function exists(path: string): Promise<boolean> {
45
+ return Bun.file(path).exists()
46
+ }
47
+
48
+ async function walk(folder: string, keep: (name: string) => boolean): Promise<string[]> {
49
+ const found: string[] = []
50
+ let entries: Dirent[] = []
51
+
52
+ try {
53
+ entries = await readdir(folder, { withFileTypes: true })
54
+ }
55
+ catch {
56
+ return found
57
+ }
58
+
59
+ for (const entry of entries) {
60
+ if (SKIPPED.has(entry.name))
61
+ continue
62
+
63
+ const path = join(folder, entry.name)
64
+
65
+ if (entry.isDirectory())
66
+ found.push(...await walk(path, keep))
67
+ else if (entry.isFile() && keep(entry.name))
68
+ found.push(path)
69
+ }
70
+
71
+ return found.sort()
72
+ }
73
+
74
+ // The test files under the folders tests live in, which leaves an app's data folders alone.
75
+ async function testFiles(folder: string): Promise<string[]> {
76
+ const found: string[] = []
77
+
78
+ for (const root of TEST_ROOTS) {
79
+ found.push(...await walk(join(folder, root), name => TEST_FILE.test(name)))
80
+ }
81
+
82
+ return found
83
+ }
84
+
85
+ // The text between the parenthesis opening at `open` and its match, skipping string literals.
86
+ function balanced(text: string, open: number): string {
87
+ let depth = 0
88
+ let quote = ''
89
+
90
+ for (let index = open; index < text.length; index++) {
91
+ const char = text[index] ?? ''
92
+
93
+ if (quote !== '') {
94
+ if (char === '\\')
95
+ index++
96
+ else if (char === quote)
97
+ quote = ''
98
+ }
99
+ else if (QUOTES.has(char)) {
100
+ quote = char
101
+ }
102
+ else if (OPENERS.has(char)) {
103
+ depth++
104
+ }
105
+ else if (CLOSERS.has(char) && --depth === 0) {
106
+ return text.slice(open + 1, index)
107
+ }
108
+ }
109
+
110
+ return text.slice(open + 1)
111
+ }
112
+
113
+ // What is written at the top level of a text: nested brackets and string literals are blanked, so only the keys of the object itself remain.
114
+ function topLevel(text: string): string {
115
+ let depth = 0
116
+ let quote = ''
117
+ let out = ''
118
+
119
+ for (let index = 0; index < text.length; index++) {
120
+ const char = text[index] ?? ''
121
+
122
+ if (quote !== '') {
123
+ if (char === '\\')
124
+ index++
125
+ else if (char === quote)
126
+ quote = ''
127
+ }
128
+ else if (QUOTES.has(char)) {
129
+ quote = char
130
+ }
131
+ else if (OPENERS.has(char)) {
132
+ out += depth === 0 ? char : ''
133
+ depth++
134
+ }
135
+ else if (CLOSERS.has(char)) {
136
+ depth--
137
+ out += depth === 0 ? char : ''
138
+ }
139
+ else if (depth <= 1) {
140
+ out += char
141
+ }
142
+ }
143
+
144
+ return out
145
+ }
146
+
147
+ // The variable names a test file gives another value through `overrideEnv(env, { NAME: ... })`; values are never looked at.
148
+ export function overriddenNames(source: string): Set<string> {
149
+ const names = new Set<string>()
150
+
151
+ for (const match of source.matchAll(OVERRIDE)) {
152
+ const args = topLevel(balanced(source, match.index + match[0].length - 1))
153
+ const object = args.slice(args.indexOf(',') + 1)
154
+
155
+ for (const key of object.matchAll(KEY))
156
+ names.add(key[1] ?? '')
157
+ }
158
+
159
+ return names
160
+ }
161
+
162
+ // The optional variables of a `defineEnv` schema, by name.
163
+ export function optionalNames(source: string): string[] {
164
+ const start = source.indexOf('defineEnv(')
165
+
166
+ return start === -1 ? [] : [...source.slice(start).matchAll(OPTIONAL)].map(match => match[1] ?? '')
167
+ }
168
+
169
+ export function reachesOutside(name: string): boolean {
170
+ return REACHING.test(name) && !SELF.test(name)
171
+ }
172
+
173
+ async function schemaFile(folder: string): Promise<string | undefined> {
174
+ for (const candidate of SCHEMA_CANDIDATES) {
175
+ const path = join(folder, candidate)
176
+
177
+ if (await exists(path) && (await readFile(path, 'utf8')).includes('defineEnv('))
178
+ return path
179
+ }
180
+
181
+ return undefined
182
+ }
183
+
184
+ // The keys `.env.test` and `.env.test.local` set, even to an empty value; the values are dropped as parsed.
185
+ async function testEnvKeys(folder: string): Promise<Set<string>> {
186
+ const keys = new Set<string>()
187
+
188
+ for (const entry of await readdir(folder, { withFileTypes: true })) {
189
+ if (!entry.isFile() || !ENV_TEST.test(entry.name))
190
+ continue
191
+
192
+ for (const line of parseDotenv(await readFile(join(folder, entry.name), 'utf8')).lines) {
193
+ if (line.key !== undefined)
194
+ keys.add(line.key)
195
+ }
196
+ }
197
+
198
+ return keys
199
+ }
200
+
201
+ // A local test preload reading `.env` and writing process.env drops what the files set that `.env.test` does not, so no developer value reaches a test.
202
+ async function pinningPreload(folder: string): Promise<string | undefined> {
203
+ const path = join(folder, 'bunfig.toml')
204
+
205
+ if (!await exists(path))
206
+ return undefined
207
+
208
+ let config: { preload?: unknown, test?: { preload?: unknown } }
209
+
210
+ try {
211
+ config = Bun.TOML.parse(await readFile(path, 'utf8'))
212
+ }
213
+ catch {
214
+ fail(`${path} is not valid TOML`)
215
+ }
216
+
217
+ const preloads = [config.test?.preload, config.preload].flat().filter((entry): entry is string => typeof entry === 'string' && entry.startsWith('.'))
218
+
219
+ for (const preload of preloads) {
220
+ const file = join(folder, preload)
221
+
222
+ if (!await exists(file))
223
+ continue
224
+
225
+ const source = await readFile(file, 'utf8')
226
+
227
+ if (source.includes('.env') && source.includes('process.env'))
228
+ return preload
229
+ }
230
+
231
+ return undefined
232
+ }
233
+
234
+ // Reads one app folder, or undefined when it declares no env schema.
235
+ export async function checkApp(folder: string, except: ReadonlySet<string> = new Set()): Promise<AppPins | undefined> {
236
+ const schema = await schemaFile(folder)
237
+
238
+ if (schema === undefined)
239
+ return undefined
240
+
241
+ const reaching = optionalNames(await readFile(schema, 'utf8')).filter(name => reachesOutside(name) && !except.has(name))
242
+ const preload = await pinningPreload(folder)
243
+ const app: AppPins = { app: folder, schema: relative(folder, schema), reaching, unpinned: [], files: [], ...preload !== undefined && { preload } }
244
+
245
+ if (preload !== undefined)
246
+ return app
247
+
248
+ const fromEnv = await testEnvKeys(folder)
249
+ const open = reaching.filter(name => !fromEnv.has(name))
250
+ const anywhere = new Set<string>()
251
+
252
+ for (const path of await testFiles(folder)) {
253
+ const source = await readFile(path, 'utf8')
254
+ const pinned = overriddenNames(source)
255
+
256
+ for (const name of pinned)
257
+ anywhere.add(name)
258
+
259
+ const unpinned = open.filter(name => !pinned.has(name))
260
+
261
+ if (RENDERS.test(source) && !NOT_NEEDED.test(source) && unpinned.length > 0)
262
+ app.files.push({ file: relative(folder, path), unpinned })
263
+ }
264
+
265
+ app.unpinned = open.filter(name => !anywhere.has(name))
266
+
267
+ return app
268
+ }
package/src/redact.ts ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env -S bun --no-env-file
2
+
3
+ import process from 'node:process'
4
+ import { parse, readInput, runMain } from '@kaynooo/cli-kit'
5
+ import { loadKnown } from './files'
6
+ import { check, Redactor } from './redactor'
7
+
8
+ const usage = `redact [--check]
9
+
10
+ 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.
11
+ --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\`.\n\nExits 0 when it copied the input or found no secret, 1 when --check finds one, 2 on a usage error.`
12
+
13
+ const options = {
14
+ 'check': { type: 'boolean', description: 'list where secrets are instead of masking them, exit 1 when found' },
15
+ 'no-env': { type: 'boolean', description: 'do not search for the values of the .env* files' },
16
+ } as const
17
+
18
+ async function run(args: string[]): Promise<number> {
19
+ 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
+
21
+ const known = parsed.values['no-env'] === true ? undefined : await loadKnown()
22
+
23
+ if (parsed.values.check === true) {
24
+ const findings = check(await readInput('-'), known)
25
+
26
+ for (const { file, line, kind } of findings)
27
+ console.log(`${file}:${line}: ${kind}`)
28
+
29
+ return findings.length === 0 ? 0 : 1
30
+ }
31
+
32
+ const redactor = new Redactor(known)
33
+ const decoder = new TextDecoder()
34
+
35
+ for await (const chunk of Bun.stdin.stream())
36
+ process.stdout.write(redactor.push(decoder.decode(chunk, { stream: true })))
37
+
38
+ process.stdout.write(redactor.push(decoder.decode()) + redactor.end())
39
+
40
+ return 0
41
+ }
42
+
43
+ process.exitCode = await runMain({ name: 'redact', run })