@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/README.md +6 -4
- package/dist/ev.js +200 -76
- package/dist/redact.js +91 -28
- package/package.json +7 -4
- package/src/audit.ts +167 -0
- package/src/commands/audit.ts +135 -0
- package/src/commands/compare.ts +77 -0
- package/src/commands/edit.ts +181 -0
- package/src/commands/pins.ts +98 -0
- package/src/commands/read.ts +193 -0
- package/src/dotenv.ts +8 -4
- package/src/ev.ts +31 -0
- package/src/files.ts +242 -0
- package/src/fix.ts +294 -0
- package/src/guard-dumps.ts +114 -0
- package/src/guard-names.ts +107 -0
- package/src/guard-paths.ts +80 -0
- package/src/guard-pipes.ts +76 -0
- package/src/guard-search.ts +181 -0
- package/src/guard.ts +51 -0
- package/src/mask.ts +264 -0
- package/src/pins.ts +268 -0
- package/src/redact.ts +43 -0
- package/src/redactor.ts +166 -0
package/src/redactor.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import type { KnownSecrets } from './mask'
|
|
2
|
+
import { findSecrets, KEY_MASK, maskLine, openKeyBody, PEM_END } from './mask'
|
|
3
|
+
|
|
4
|
+
export interface Finding {
|
|
5
|
+
file: string
|
|
6
|
+
line: number
|
|
7
|
+
kind: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const DIFF_START = /^(?:diff --git |@@ )/
|
|
11
|
+
const NEW_FILE = /^\+\+\+ (?:b\/)?(.+)$/
|
|
12
|
+
const HUNK = /^@@ -\d+(?:,\d+)? \+(\d+)/
|
|
13
|
+
|
|
14
|
+
// A line longer than this with no line break is cut, keeping its last OVERLAP characters to be read with what follows.
|
|
15
|
+
const MAX_PENDING = 1 << 18
|
|
16
|
+
const OVERLAP = 1 << 13
|
|
17
|
+
|
|
18
|
+
// Masks text pushed in chunks of any size: only whole lines are emitted, so a secret split across two chunks is seen whole, and the lines of a private key block are replaced by one marker.
|
|
19
|
+
export class Redactor {
|
|
20
|
+
private pending = ''
|
|
21
|
+
private inKey = false
|
|
22
|
+
private hidden = false
|
|
23
|
+
|
|
24
|
+
constructor(private readonly known?: KnownSecrets) {}
|
|
25
|
+
|
|
26
|
+
push(chunk: string): string {
|
|
27
|
+
const end = chunk.lastIndexOf('\n')
|
|
28
|
+
|
|
29
|
+
this.pending += chunk
|
|
30
|
+
|
|
31
|
+
if (end === -1)
|
|
32
|
+
return this.pending.length > MAX_PENDING ? this.cut() : ''
|
|
33
|
+
|
|
34
|
+
const split = this.pending.length - chunk.length + end + 1
|
|
35
|
+
const complete = this.pending.slice(0, split)
|
|
36
|
+
|
|
37
|
+
this.pending = this.pending.slice(split)
|
|
38
|
+
|
|
39
|
+
return complete.split('\n').slice(0, -1).map(line => this.line(line)).filter(line => line !== undefined).map(line => `${line}\n`).join('')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Emits a line that never ends, up to the last space before the overlap when there is one so a secret is not cut in two.
|
|
43
|
+
private cut(): string {
|
|
44
|
+
const limit = this.pending.length - OVERLAP
|
|
45
|
+
const space = this.pending.lastIndexOf(' ', limit)
|
|
46
|
+
const at = space > 0 ? space + 1 : limit
|
|
47
|
+
const head = this.pending.slice(0, at)
|
|
48
|
+
|
|
49
|
+
this.pending = this.pending.slice(at)
|
|
50
|
+
|
|
51
|
+
return this.line(head) ?? ''
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
end(): string {
|
|
55
|
+
const rest = this.pending
|
|
56
|
+
|
|
57
|
+
this.pending = ''
|
|
58
|
+
|
|
59
|
+
return rest === '' ? '' : this.line(rest) ?? ''
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
private line(line: string): string | undefined {
|
|
63
|
+
if (this.inKey) {
|
|
64
|
+
const end = PEM_END.exec(line)
|
|
65
|
+
|
|
66
|
+
if (end === null) {
|
|
67
|
+
if (this.hidden)
|
|
68
|
+
return undefined
|
|
69
|
+
|
|
70
|
+
this.hidden = true
|
|
71
|
+
|
|
72
|
+
return KEY_MASK
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.inKey = false
|
|
76
|
+
|
|
77
|
+
// The text before the END marker is the last of the key, the text after it is read as any line.
|
|
78
|
+
return (end.index > 0 && !this.hidden ? KEY_MASK : '') + this.line(line.slice(end.index))
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const body = openKeyBody(line)
|
|
82
|
+
|
|
83
|
+
if (body !== undefined) {
|
|
84
|
+
this.inKey = true
|
|
85
|
+
this.hidden = body !== ''
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return maskLine(line, this.known)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Lists the secrets in text without printing any. A unified diff is read on its added lines, with the new file's name and line numbers.
|
|
93
|
+
export function check(text: string, known?: KnownSecrets): Finding[] {
|
|
94
|
+
const findings: Finding[] = []
|
|
95
|
+
const isDiff = text.split('\n').some(line => DIFF_START.test(line))
|
|
96
|
+
let file = 'stdin'
|
|
97
|
+
let number = 0
|
|
98
|
+
let inKey = false
|
|
99
|
+
|
|
100
|
+
for (const [index, raw] of text.split('\n').entries()) {
|
|
101
|
+
let line = raw
|
|
102
|
+
|
|
103
|
+
if (isDiff) {
|
|
104
|
+
const hunk = HUNK.exec(raw)
|
|
105
|
+
|
|
106
|
+
if (hunk !== null) {
|
|
107
|
+
number = Number(hunk[1]) - 1
|
|
108
|
+
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const named = NEW_FILE.exec(raw)
|
|
113
|
+
|
|
114
|
+
if (named !== null) {
|
|
115
|
+
file = named[1] ?? file
|
|
116
|
+
|
|
117
|
+
continue
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (raw.startsWith('diff --git ')) {
|
|
121
|
+
file = '(unnamed)'
|
|
122
|
+
|
|
123
|
+
continue
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (raw.startsWith('-') || raw.startsWith('\\'))
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
number++
|
|
130
|
+
|
|
131
|
+
if (!raw.startsWith('+'))
|
|
132
|
+
continue
|
|
133
|
+
|
|
134
|
+
line = raw.slice(1)
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
number = index + 1
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (inKey) {
|
|
141
|
+
const end = PEM_END.exec(line)
|
|
142
|
+
|
|
143
|
+
inKey = end === null
|
|
144
|
+
|
|
145
|
+
if (end === null)
|
|
146
|
+
continue
|
|
147
|
+
|
|
148
|
+
line = line.slice(end.index)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const found = findSecrets(line, known).map(match => match.kind)
|
|
152
|
+
const body = openKeyBody(line)
|
|
153
|
+
|
|
154
|
+
if (body !== undefined) {
|
|
155
|
+
inKey = true
|
|
156
|
+
|
|
157
|
+
if (body === '')
|
|
158
|
+
found.push('private key')
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
for (const kind of found)
|
|
162
|
+
findings.push({ file, line: number, kind })
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return findings
|
|
166
|
+
}
|