@mainwave/cli 0.11.0 → 0.12.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.
- package/README.md +168 -13
- package/dist/adapters/claude-code.js +3 -3
- package/dist/adapters/claude-desktop.js +35 -1
- package/dist/adapters/cursor.js +7 -2
- package/dist/adapters/kiro.js +13 -8
- package/dist/commands.js +217 -0
- package/dist/core/apply.js +3 -3
- package/dist/core/auth-commands.js +2 -1
- package/dist/core/codecs/kiro-hooks-json.js +1 -1
- package/dist/core/doctor.js +71 -8
- package/dist/core/entry-script.js +2 -3
- package/dist/core/format.js +0 -1
- package/dist/core/hook-copies.js +157 -0
- package/dist/core/install-hooks.js +144 -15
- package/dist/core/interview.js +8 -2
- package/dist/core/markers.js +86 -22
- package/dist/core/redaction.js +179 -0
- package/dist/core/skills.js +3 -11
- package/dist/core/status.js +21 -1
- package/dist/core/store.js +254 -4
- package/dist/core/wizard.js +91 -6
- package/dist/main.js +163 -162
- package/hooks/kiro-otlp.mjs +10 -1
- package/hooks/mainwave-hook.mjs +11 -1
- package/hooks/marker-core.mjs +56 -2
- package/hooks/matchers.json +1 -1
- package/hooks/redaction-data.mjs +201 -0
- package/hooks/redaction-json.mjs +215 -0
- package/hooks/redaction-pattern.mjs +503 -0
- package/hooks/redaction.d.mts +84 -0
- package/hooks/redaction.mjs +701 -0
- package/package.json +3 -1
- package/skills/mainwave-instrument/SKILL.md +24 -6
- package/skills/mainwave-instrument/reference/frameworks.md +4 -2
- package/skills/mainwave-instrument/reference/troubleshooting.md +4 -0
- package/skills/mainwave-instrument-node/SKILL.md +83 -38
- package/skills/mainwave-instrument-node/reference/api.md +16 -16
- package/skills/mainwave-instrument-node/reference/frameworks.md +42 -27
- package/skills/mainwave-instrument-node/reference/troubleshooting.md +11 -7
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
// The redaction pattern grammar, the one the Mainwave SDKs enforce: a
|
|
2
|
+
// tokenizer and parser for the allowlisted syntax, the static checks
|
|
3
|
+
// (zero-width, cost limits, case folding), and the translation to a JavaScript
|
|
4
|
+
// regular expression source. A rule's text never reaches `RegExp`: the engine
|
|
5
|
+
// only sees the source this module writes, so a construct the grammar does not
|
|
6
|
+
// list cannot slip through, and the places where regex engines disagree (`$`,
|
|
7
|
+
// `.`, `\s`, case folding) cannot show.
|
|
8
|
+
//
|
|
9
|
+
// The translated source runs with the `u` flag and never the `i` flag:
|
|
10
|
+
// `$` -> `(?![^])` plain `$` skips matches before astral text in V8
|
|
11
|
+
// `.` -> an explicit class no `\n`, `\r`, U+2028, U+2029
|
|
12
|
+
// `\s` -> `[\t\n\v\f\r ]` JS `\s` is Unicode
|
|
13
|
+
// case_insensitive each ASCII letter written as both cases; with
|
|
14
|
+
// `u` and `i`, JS folds U+017F and U+212A
|
|
15
|
+
// escaped punctuation, every non-alphanumeric literal -> `\u{HEX}`
|
|
16
|
+
//
|
|
17
|
+
// Pure: no I/O. Runtime-dependency-free like every hook module.
|
|
18
|
+
|
|
19
|
+
/** A pattern outside the grammar; the message is the reason. */
|
|
20
|
+
export class PatternError extends Error {
|
|
21
|
+
name = 'PatternError'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const MAX_REPEAT = 65535
|
|
25
|
+
/** The longest pattern, in code points. */
|
|
26
|
+
const MAX_PATTERN_LENGTH = 4096
|
|
27
|
+
/** The deepest group nesting; checked before the parser recurses. */
|
|
28
|
+
const MAX_GROUP_DEPTH = 32
|
|
29
|
+
|
|
30
|
+
const code = (char) => char.codePointAt(0)
|
|
31
|
+
// `\ ^ $ . | ? * + ( ) [ ] { }`: literal characters only when escaped.
|
|
32
|
+
const SYNTAX = new Set([...'\\^$.|?*+()[]{}'].map(code))
|
|
33
|
+
// Escapable as themselves: the syntax set plus `/` and `-`.
|
|
34
|
+
const PUNCT_ESCAPES = new Set([...SYNTAX, code('/'), code('-')])
|
|
35
|
+
const CLASS_ESCAPES = new Set(['d', 'D', 'w', 'W', 's', 'S'])
|
|
36
|
+
const CONTROL_ESCAPES = { t: 9, n: 10, r: 13, f: 12, v: 11 }
|
|
37
|
+
const CI_ASCII_ONLY =
|
|
38
|
+
'case_insensitive needs an ASCII-only pattern (case folding differs outside ASCII)'
|
|
39
|
+
const BRACE = 'a { must be a complete quantifier, or written \\{'
|
|
40
|
+
|
|
41
|
+
const isHexDigit = (cp) =>
|
|
42
|
+
cp !== undefined &&
|
|
43
|
+
((cp >= 0x30 && cp <= 0x39) || (cp >= 0x41 && cp <= 0x46) || (cp >= 0x61 && cp <= 0x66))
|
|
44
|
+
const isDigit = (cp) => cp !== undefined && cp >= 0x30 && cp <= 0x39
|
|
45
|
+
const isAsciiLetter = (cp) => (cp >= 0x41 && cp <= 0x5a) || (cp >= 0x61 && cp <= 0x7a)
|
|
46
|
+
|
|
47
|
+
const fail = (reason) => {
|
|
48
|
+
throw new PatternError(reason)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const rejectedEscape = (char) => {
|
|
52
|
+
if (isDigit(code(char))) return `\\${char}: backreferences and digit escapes are not supported`
|
|
53
|
+
if ('AZzG'.includes(char)) return `\\${char}: not supported (^ and $ are the anchors)`
|
|
54
|
+
if ('pP'.includes(char)) return `\\${char}: Unicode properties are not supported`
|
|
55
|
+
if (char === 'u' || char === 'U')
|
|
56
|
+
return `\\${char}: not supported (write the character, or \\xHH)`
|
|
57
|
+
return `\\${char}: this escape is not in the pattern grammar`
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// ─── Static lengths ──────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
// Nodes: { kind: 'lit', cp } | { kind: 'dot' } | { kind: 'esc', cls } |
|
|
63
|
+
// { kind: 'assert', which: '^' | '$' | 'b' | 'B' } |
|
|
64
|
+
// { kind: 'class', negate, items } (items: { kind: 'cp', cp } |
|
|
65
|
+
// { kind: 'range', lo, hi } | { kind: 'esc', cls }) |
|
|
66
|
+
// { kind: 'group', alts } | { kind: 'quant', node, min, max, lazy }.
|
|
67
|
+
|
|
68
|
+
/** The shortest text a node can match, counting ^ $ \b \B as 0. */
|
|
69
|
+
const minLength = (node) => {
|
|
70
|
+
switch (node.kind) {
|
|
71
|
+
case 'assert':
|
|
72
|
+
return 0
|
|
73
|
+
case 'group':
|
|
74
|
+
return node.alts.reduce((least, alt) => Math.min(least, seqMin(alt)), Infinity)
|
|
75
|
+
case 'quant':
|
|
76
|
+
return node.min === 0 ? 0 : node.min * minLength(node.node)
|
|
77
|
+
default:
|
|
78
|
+
return 1
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const seqMin = (nodes) => nodes.reduce((total, node) => total + minLength(node), 0)
|
|
82
|
+
|
|
83
|
+
/** The longest text a node can match; Infinity when unbounded. */
|
|
84
|
+
const maxLength = (node) => {
|
|
85
|
+
switch (node.kind) {
|
|
86
|
+
case 'assert':
|
|
87
|
+
return 0
|
|
88
|
+
case 'group':
|
|
89
|
+
return node.alts.reduce((most, alt) => Math.max(most, seqMax(alt)), 0)
|
|
90
|
+
case 'quant': {
|
|
91
|
+
const inner = maxLength(node.node)
|
|
92
|
+
return inner === 0 || node.max === 0 ? 0 : node.max * inner
|
|
93
|
+
}
|
|
94
|
+
default:
|
|
95
|
+
return 1
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const seqMax = (nodes) => nodes.reduce((total, node) => total + maxLength(node), 0)
|
|
99
|
+
|
|
100
|
+
// ─── Parse ───────────────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/** Parses code points `cps` into alternatives of node sequences, or throws
|
|
103
|
+
* PatternError. Groups nest at most MAX_GROUP_DEPTH deep, checked before
|
|
104
|
+
* recursing, so no stack limit decides the result. */
|
|
105
|
+
const parse = (cps, caseInsensitive) => {
|
|
106
|
+
let pos = 0
|
|
107
|
+
let depth = 0
|
|
108
|
+
// Whether the class member just read was a literal or \xHH (a valid range end).
|
|
109
|
+
let plainEnd = false
|
|
110
|
+
const peek = (offset = 0) => cps[pos + offset]
|
|
111
|
+
|
|
112
|
+
const literal = (cp) => {
|
|
113
|
+
if (caseInsensitive && cp >= 0x80) fail(CI_ASCII_ONLY)
|
|
114
|
+
return { kind: 'lit', cp }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// The escapes shared by a class and the pattern: `\xHH` and punctuation.
|
|
118
|
+
// `pos` is on the character after the backslash.
|
|
119
|
+
const hexOrPunct = (char, cp) => {
|
|
120
|
+
if (char === 'x') {
|
|
121
|
+
const hi = peek(1)
|
|
122
|
+
const lo = peek(2)
|
|
123
|
+
if (!isHexDigit(hi) || !isHexDigit(lo)) fail('\\x needs exactly two hex digits')
|
|
124
|
+
const value = parseInt(String.fromCodePoint(hi, lo), 16)
|
|
125
|
+
if (caseInsensitive && value >= 0x80) fail(CI_ASCII_ONLY)
|
|
126
|
+
pos += 3
|
|
127
|
+
return value
|
|
128
|
+
}
|
|
129
|
+
if (PUNCT_ESCAPES.has(cp)) {
|
|
130
|
+
pos++
|
|
131
|
+
return cp
|
|
132
|
+
}
|
|
133
|
+
return undefined
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const escape = () => {
|
|
137
|
+
pos++ // backslash
|
|
138
|
+
const cp = peek()
|
|
139
|
+
if (cp === undefined) return fail('a trailing backslash')
|
|
140
|
+
const char = String.fromCodePoint(cp)
|
|
141
|
+
if (CLASS_ESCAPES.has(char)) {
|
|
142
|
+
pos++
|
|
143
|
+
return { kind: 'esc', cls: char }
|
|
144
|
+
}
|
|
145
|
+
if (char === 'b' || char === 'B') {
|
|
146
|
+
pos++
|
|
147
|
+
return { kind: 'assert', which: char }
|
|
148
|
+
}
|
|
149
|
+
if (Object.hasOwn(CONTROL_ESCAPES, char)) {
|
|
150
|
+
pos++
|
|
151
|
+
return { kind: 'lit', cp: CONTROL_ESCAPES[char] }
|
|
152
|
+
}
|
|
153
|
+
const value = hexOrPunct(char, cp)
|
|
154
|
+
if (value !== undefined) return { kind: 'lit', cp: value }
|
|
155
|
+
return fail(rejectedEscape(char))
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// `&&`, `--` and `~~` are set operators in Rust's regex.
|
|
159
|
+
const setOperator = (cp) => {
|
|
160
|
+
if ((cp === code('&') || cp === code('-') || cp === code('~')) && peek(1) === cp) {
|
|
161
|
+
fail(`${String.fromCodePoint(cp, cp)} inside a character class is not supported`)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const classMember = () => {
|
|
166
|
+
const cp = peek()
|
|
167
|
+
if (cp !== code('\\')) {
|
|
168
|
+
pos++
|
|
169
|
+
if (caseInsensitive && cp >= 0x80) fail(CI_ASCII_ONLY)
|
|
170
|
+
plainEnd = true
|
|
171
|
+
return { kind: 'cp', cp }
|
|
172
|
+
}
|
|
173
|
+
pos++
|
|
174
|
+
const escaped = peek()
|
|
175
|
+
if (escaped === undefined) return fail('a trailing backslash')
|
|
176
|
+
const char = String.fromCodePoint(escaped)
|
|
177
|
+
if (CLASS_ESCAPES.has(char)) {
|
|
178
|
+
pos++
|
|
179
|
+
plainEnd = false
|
|
180
|
+
return { kind: 'esc', cls: char }
|
|
181
|
+
}
|
|
182
|
+
if (Object.hasOwn(CONTROL_ESCAPES, char)) {
|
|
183
|
+
pos++
|
|
184
|
+
plainEnd = false
|
|
185
|
+
return { kind: 'cp', cp: CONTROL_ESCAPES[char] }
|
|
186
|
+
}
|
|
187
|
+
if (char === 'x') {
|
|
188
|
+
plainEnd = true
|
|
189
|
+
return { kind: 'cp', cp: hexOrPunct(char, escaped) }
|
|
190
|
+
}
|
|
191
|
+
if (PUNCT_ESCAPES.has(escaped)) {
|
|
192
|
+
pos++
|
|
193
|
+
plainEnd = false
|
|
194
|
+
return { kind: 'cp', cp: escaped }
|
|
195
|
+
}
|
|
196
|
+
if (char === 'b' || char === 'B') return fail(`\\${char} inside a character class`)
|
|
197
|
+
return fail(rejectedEscape(char))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// `[...]` or `[^...]`.
|
|
201
|
+
const charClass = () => {
|
|
202
|
+
pos++ // [
|
|
203
|
+
let negate = false
|
|
204
|
+
if (peek() === code('^')) {
|
|
205
|
+
negate = true
|
|
206
|
+
pos++
|
|
207
|
+
}
|
|
208
|
+
if (peek() === code(']')) fail('a character class cannot start with ] (write \\])')
|
|
209
|
+
const items = []
|
|
210
|
+
let first = true
|
|
211
|
+
for (;;) {
|
|
212
|
+
const cp = peek()
|
|
213
|
+
if (cp === undefined) return fail('a character class is not closed')
|
|
214
|
+
if (cp === code(']')) {
|
|
215
|
+
pos++
|
|
216
|
+
break
|
|
217
|
+
}
|
|
218
|
+
if (cp === code('[')) return fail('an unescaped [ inside a character class (write \\[)')
|
|
219
|
+
if (cp === code('-') && !(first || peek(1) === code(']'))) {
|
|
220
|
+
return fail('a - inside a character class must form a range (write \\- for a literal)')
|
|
221
|
+
}
|
|
222
|
+
setOperator(cp)
|
|
223
|
+
const member = classMember()
|
|
224
|
+
first = false
|
|
225
|
+
const dash = peek() === code('-') && peek(1) !== code(']') && peek(1) !== undefined
|
|
226
|
+
if (!dash) {
|
|
227
|
+
items.push(member)
|
|
228
|
+
continue
|
|
229
|
+
}
|
|
230
|
+
// A range: both ends are literals or \xHH.
|
|
231
|
+
if (member.kind !== 'cp' || !plainEnd) fail('a range end must be a literal or \\xHH')
|
|
232
|
+
const low = member.cp
|
|
233
|
+
pos++ // -
|
|
234
|
+
const next = peek()
|
|
235
|
+
if (next === code('-')) return fail('-- inside a character class is not supported')
|
|
236
|
+
if (next === code('[') || next === code(']')) {
|
|
237
|
+
return fail(`a range end of ${String.fromCodePoint(next)} (write it escaped)`)
|
|
238
|
+
}
|
|
239
|
+
setOperator(next)
|
|
240
|
+
const end = classMember()
|
|
241
|
+
if (end.kind !== 'cp' || !plainEnd) fail('a range end must be a literal or \\xHH')
|
|
242
|
+
if (low > end.cp) fail('a reversed range')
|
|
243
|
+
items.push({ kind: 'range', lo: low, hi: end.cp })
|
|
244
|
+
}
|
|
245
|
+
return { kind: 'class', negate, items }
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// `{n}`, `{n,}` or `{n,m}`; anything else is a stray brace.
|
|
249
|
+
const braces = () => {
|
|
250
|
+
pos++ // {
|
|
251
|
+
const number = () => {
|
|
252
|
+
const start = pos
|
|
253
|
+
while (isDigit(peek())) pos++
|
|
254
|
+
if (pos === start) return undefined
|
|
255
|
+
const value = Number(String.fromCodePoint(...cps.slice(start, pos)))
|
|
256
|
+
if (!(value <= MAX_REPEAT)) return fail(`a repeat count above ${MAX_REPEAT}`)
|
|
257
|
+
return value
|
|
258
|
+
}
|
|
259
|
+
const min = number()
|
|
260
|
+
if (min === undefined) return fail(BRACE)
|
|
261
|
+
let max = min
|
|
262
|
+
if (peek() === code(',')) {
|
|
263
|
+
pos++
|
|
264
|
+
max = number() ?? Infinity
|
|
265
|
+
}
|
|
266
|
+
if (peek() !== code('}')) return fail(BRACE)
|
|
267
|
+
pos++
|
|
268
|
+
if (min > max) fail('a quantifier with its minimum above its maximum')
|
|
269
|
+
return [min, max]
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// A quantifier after `atom`, if one follows.
|
|
273
|
+
const quantified = (atom) => {
|
|
274
|
+
const cp = peek()
|
|
275
|
+
if (cp === undefined) return atom
|
|
276
|
+
let min
|
|
277
|
+
let max
|
|
278
|
+
if (cp === code('*')) {
|
|
279
|
+
;[min, max] = [0, Infinity]
|
|
280
|
+
pos++
|
|
281
|
+
} else if (cp === code('+')) {
|
|
282
|
+
;[min, max] = [1, Infinity]
|
|
283
|
+
pos++
|
|
284
|
+
} else if (cp === code('?')) {
|
|
285
|
+
;[min, max] = [0, 1]
|
|
286
|
+
pos++
|
|
287
|
+
} else if (cp === code('{')) {
|
|
288
|
+
;[min, max] = braces()
|
|
289
|
+
} else {
|
|
290
|
+
return atom
|
|
291
|
+
}
|
|
292
|
+
if (atom.kind === 'assert') fail('a quantifier on ^, $, \\b or \\B')
|
|
293
|
+
if (atom.kind === 'group' && minLength(atom) === 0) {
|
|
294
|
+
fail('a quantifier on a group that can match zero-width')
|
|
295
|
+
}
|
|
296
|
+
let lazy = false
|
|
297
|
+
if (peek() === code('?')) {
|
|
298
|
+
lazy = true
|
|
299
|
+
pos++
|
|
300
|
+
}
|
|
301
|
+
const next = peek()
|
|
302
|
+
if (next === code('+')) fail('possessive quantifiers are not supported')
|
|
303
|
+
if (next === code('*') || next === code('?') || next === code('{')) {
|
|
304
|
+
fail('a quantifier directly after a quantifier')
|
|
305
|
+
}
|
|
306
|
+
return { kind: 'quant', node: atom, min, max, lazy }
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const group = () => {
|
|
310
|
+
pos++ // (
|
|
311
|
+
if (peek() === code('?')) {
|
|
312
|
+
if (peek(1) !== code(':')) {
|
|
313
|
+
fail('only (?: is supported: no lookaround, flags, named or atomic groups, or comments')
|
|
314
|
+
}
|
|
315
|
+
pos += 2
|
|
316
|
+
}
|
|
317
|
+
if (depth >= MAX_GROUP_DEPTH) fail(`groups nest at most ${MAX_GROUP_DEPTH} deep`)
|
|
318
|
+
depth++
|
|
319
|
+
const alts = alternatives(true)
|
|
320
|
+
depth--
|
|
321
|
+
pos++ // )
|
|
322
|
+
return { kind: 'group', alts }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const atom = () => {
|
|
326
|
+
const cp = peek()
|
|
327
|
+
if (cp === code('(')) return group()
|
|
328
|
+
if (cp === code('[')) return charClass()
|
|
329
|
+
if (cp === code('\\')) return escape()
|
|
330
|
+
pos++
|
|
331
|
+
if (cp === code('.')) return { kind: 'dot' }
|
|
332
|
+
if (cp === code('^')) return { kind: 'assert', which: '^' }
|
|
333
|
+
if (cp === code('$')) return { kind: 'assert', which: '$' }
|
|
334
|
+
if (cp === code('*') || cp === code('+') || cp === code('?')) {
|
|
335
|
+
return fail('a quantifier has nothing to repeat')
|
|
336
|
+
}
|
|
337
|
+
if (cp === code('{')) return fail(BRACE)
|
|
338
|
+
if (cp === code('}')) return fail('an unescaped } (write \\})')
|
|
339
|
+
if (cp === code(']')) return fail('an unescaped ] (write \\])')
|
|
340
|
+
return literal(cp)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const sequence = () => {
|
|
344
|
+
const nodes = []
|
|
345
|
+
for (;;) {
|
|
346
|
+
const cp = peek()
|
|
347
|
+
if (cp === undefined || cp === code('|') || cp === code(')')) return nodes
|
|
348
|
+
nodes.push(quantified(atom()))
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Alternatives up to the closing `)` of a group (not consumed) or the end.
|
|
353
|
+
const alternatives = (inGroup) => {
|
|
354
|
+
const alts = [sequence()]
|
|
355
|
+
while (peek() === code('|')) {
|
|
356
|
+
pos++
|
|
357
|
+
alts.push(sequence())
|
|
358
|
+
}
|
|
359
|
+
if (inGroup && peek() !== code(')')) fail('a group is not closed')
|
|
360
|
+
return alts
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const alts = alternatives(false)
|
|
364
|
+
if (pos < cps.length) fail('unbalanced )')
|
|
365
|
+
return alts
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// ─── Cost limits ─────────────────────────────────────────────────────────
|
|
369
|
+
|
|
370
|
+
// The limits that reject the classic exponential forms of nested quantifiers
|
|
371
|
+
// (length and group depth are checked while parsing):
|
|
372
|
+
// - a quantifier inside a quantified group whose maximum is above 1 must be
|
|
373
|
+
// bounded;
|
|
374
|
+
// - an unbounded quantifier over a group needs an operand of one fixed
|
|
375
|
+
// length: its minimum length equals its maximum, both finite. Over a single
|
|
376
|
+
// character, class or escape it is always fine;
|
|
377
|
+
// - along every chain of nested quantifiers the product of the bounded
|
|
378
|
+
// maximums is at most 65535, an unbounded maximum counting as 1.
|
|
379
|
+
// They reject known bad shapes; they do not bound every pattern's cost.
|
|
380
|
+
const checkCost = (nodes, product, repeated) => {
|
|
381
|
+
for (const node of nodes) {
|
|
382
|
+
if (node.kind === 'group') {
|
|
383
|
+
for (const alt of node.alts) checkCost(alt, product, repeated)
|
|
384
|
+
} else if (node.kind === 'quant') {
|
|
385
|
+
const unbounded = node.max === Infinity
|
|
386
|
+
if (unbounded && repeated) {
|
|
387
|
+
fail('an unbounded quantifier inside a repeated group must be bounded')
|
|
388
|
+
}
|
|
389
|
+
if (unbounded && node.node.kind === 'group') {
|
|
390
|
+
const low = minLength(node.node)
|
|
391
|
+
const high = maxLength(node.node)
|
|
392
|
+
if (low !== high || high === Infinity) {
|
|
393
|
+
fail('an unbounded quantifier over a group needs an operand of one fixed length')
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const next = product * (unbounded ? 1 : node.max)
|
|
397
|
+
if (next > MAX_REPEAT) fail(`nested repeats multiply to more than ${MAX_REPEAT}`)
|
|
398
|
+
if (node.node.kind === 'group') checkCost([node.node], next, repeated || node.max > 1)
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// ─── Translate ───────────────────────────────────────────────────────────
|
|
404
|
+
|
|
405
|
+
const hex = (cp) => `\\u{${cp.toString(16)}}`
|
|
406
|
+
|
|
407
|
+
/** One code point as a source fragment valid inside and outside a class under
|
|
408
|
+
* `u`: alphanumerics as they are, everything else as `\u{HEX}`. */
|
|
409
|
+
const lit = (cp) =>
|
|
410
|
+
(cp >= 0x30 && cp <= 0x39) || isAsciiLetter(cp) ? String.fromCodePoint(cp) : hex(cp)
|
|
411
|
+
|
|
412
|
+
const ESC_OUTSIDE = {
|
|
413
|
+
d: '\\d',
|
|
414
|
+
D: '\\D',
|
|
415
|
+
w: '\\w',
|
|
416
|
+
W: '\\W',
|
|
417
|
+
s: '[\\t\\n\\v\\f\\r\\u{20}]',
|
|
418
|
+
S: '[^\\t\\n\\v\\f\\r\\u{20}]',
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// Inside a class a member cannot be negated: the complements are ranges.
|
|
422
|
+
const ESC_INSIDE = {
|
|
423
|
+
d: '\\d',
|
|
424
|
+
D: '\\u{0}-\\u{2f}\\u{3a}-\\u{10ffff}',
|
|
425
|
+
w: '\\w',
|
|
426
|
+
W: '\\u{0}-\\u{2f}\\u{3a}-\\u{40}\\u{5b}-\\u{5e}\\u{60}\\u{7b}-\\u{10ffff}',
|
|
427
|
+
s: '\\t\\n\\v\\f\\r\\u{20}',
|
|
428
|
+
S: '\\u{0}-\\u{8}\\u{e}-\\u{1f}\\u{21}-\\u{10ffff}',
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const otherCase = (cp) => (cp >= 0x61 ? cp - 32 : cp + 32)
|
|
432
|
+
|
|
433
|
+
const literalSource = (cp, ci) =>
|
|
434
|
+
ci && isAsciiLetter(cp) ? `[${lit(cp)}${lit(otherCase(cp))}]` : lit(cp)
|
|
435
|
+
|
|
436
|
+
const classSource = (node, ci) => {
|
|
437
|
+
let out = ''
|
|
438
|
+
for (const item of node.items) {
|
|
439
|
+
if (item.kind === 'esc') {
|
|
440
|
+
out += ESC_INSIDE[item.cls]
|
|
441
|
+
} else if (item.kind === 'cp') {
|
|
442
|
+
out += lit(item.cp)
|
|
443
|
+
if (ci && isAsciiLetter(item.cp)) out += lit(otherCase(item.cp))
|
|
444
|
+
} else {
|
|
445
|
+
out += `${lit(item.lo)}-${lit(item.hi)}`
|
|
446
|
+
if (ci) {
|
|
447
|
+
// The case-flipped halves of the range that overlap the letters, so a
|
|
448
|
+
// negated class excludes both cases.
|
|
449
|
+
const [lowLo, lowHi] = [Math.max(item.lo, 0x61), Math.min(item.hi, 0x7a)]
|
|
450
|
+
if (lowLo <= lowHi) out += `${lit(lowLo - 32)}-${lit(lowHi - 32)}`
|
|
451
|
+
const [upLo, upHi] = [Math.max(item.lo, 0x41), Math.min(item.hi, 0x5a)]
|
|
452
|
+
if (upLo <= upHi) out += `${lit(upLo + 32)}-${lit(upHi + 32)}`
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return `[${node.negate ? '^' : ''}${out}]`
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const countSource = ({ min, max }) => {
|
|
460
|
+
if (max === Infinity) return min === 0 ? '*' : min === 1 ? '+' : `{${min},}`
|
|
461
|
+
if (min === 0 && max === 1) return '?'
|
|
462
|
+
return min === max ? `{${min}}` : `{${min},${max}}`
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
const altsSource = (alts, ci) =>
|
|
466
|
+
alts.map((alt) => alt.map((node) => nodeSource(node, ci)).join('')).join('|')
|
|
467
|
+
|
|
468
|
+
const nodeSource = (node, ci) => {
|
|
469
|
+
switch (node.kind) {
|
|
470
|
+
case 'lit':
|
|
471
|
+
return literalSource(node.cp, ci)
|
|
472
|
+
case 'dot':
|
|
473
|
+
return '[^\\u{a}\\u{d}\\u{2028}-\\u{2029}]'
|
|
474
|
+
case 'esc':
|
|
475
|
+
return ESC_OUTSIDE[node.cls]
|
|
476
|
+
case 'assert':
|
|
477
|
+
return node.which === '$' ? '(?![^])' : node.which === '^' ? '^' : `\\${node.which}`
|
|
478
|
+
case 'class':
|
|
479
|
+
return classSource(node, ci)
|
|
480
|
+
case 'group':
|
|
481
|
+
return `(?:${altsSource(node.alts, ci)})`
|
|
482
|
+
case 'quant':
|
|
483
|
+
return `${nodeSource(node.node, ci)}${countSource(node)}${node.lazy ? '?' : ''}`
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Validates `pattern` against the grammar and returns the JavaScript source
|
|
488
|
+
* (for the `u` flag, without `i`) that matches exactly what the grammar says.
|
|
489
|
+
* Throws PatternError with the reason. */
|
|
490
|
+
export const translatePattern = (pattern, caseInsensitive) => {
|
|
491
|
+
// Code units are never fewer than code points, so this cuts a huge pattern
|
|
492
|
+
// before it is split.
|
|
493
|
+
const tooLong = `a pattern is at most ${MAX_PATTERN_LENGTH} characters`
|
|
494
|
+
if (pattern.length > MAX_PATTERN_LENGTH * 2) fail(tooLong)
|
|
495
|
+
const cps = Array.from(pattern, code)
|
|
496
|
+
if (cps.length > MAX_PATTERN_LENGTH) fail(tooLong)
|
|
497
|
+
const alts = parse(cps, caseInsensitive)
|
|
498
|
+
for (const alt of alts) checkCost(alt, 1, false)
|
|
499
|
+
if (minLength({ kind: 'group', alts }) === 0) {
|
|
500
|
+
fail('the pattern can match zero-width (an empty match is never allowed)')
|
|
501
|
+
}
|
|
502
|
+
return altsSource(alts, caseInsensitive)
|
|
503
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Type declarations for redaction.mjs — the hooks are runtime-dep-free .mjs and
|
|
2
|
+
// aren't compiled by tsc, but the CLI's TypeScript (setup, status, doctor)
|
|
3
|
+
// reads the same rules, so NodeNext needs a .d.mts sibling to type it.
|
|
4
|
+
|
|
5
|
+
export class RedactionConfigError extends Error {}
|
|
6
|
+
|
|
7
|
+
export type RedactionPack = 'secrets' | 'pii.contact' | 'pii.us_gov_id' | 'pii.payment_card'
|
|
8
|
+
export type RedactionSummary = 'info' | 'silent'
|
|
9
|
+
|
|
10
|
+
/** A pattern string, or a named rule. The token reads `[REDACTED:<name>]`;
|
|
11
|
+
* without a name, `[REDACTED]`. */
|
|
12
|
+
export type RedactionRule =
|
|
13
|
+
string | { readonly name?: string; readonly pattern: string; readonly case_insensitive?: boolean }
|
|
14
|
+
|
|
15
|
+
/** The rules file's shape. `compileRedaction` takes `unknown` and checks it. */
|
|
16
|
+
export interface RedactionRules {
|
|
17
|
+
readonly packs?: readonly RedactionPack[]
|
|
18
|
+
readonly blocked_values?: readonly RedactionRule[]
|
|
19
|
+
readonly allowed_values?: readonly RedactionRule[]
|
|
20
|
+
readonly blocked_key_patterns?: readonly RedactionRule[]
|
|
21
|
+
readonly ignored_keys?: readonly string[]
|
|
22
|
+
readonly ignored_key_patterns?: readonly string[]
|
|
23
|
+
readonly summary?: RedactionSummary
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Compiled rules. Only `compileRedaction` / `readRedactionFile` make one. */
|
|
27
|
+
export interface Redactor {
|
|
28
|
+
/** The enabled packs, in the order they run. */
|
|
29
|
+
readonly packs: readonly RedactionPack[]
|
|
30
|
+
readonly summary: RedactionSummary
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const REDACTION_COUNT_KEY: string
|
|
34
|
+
/** Present in the text of a `redaction.mjs` that applies `redaction.org`. */
|
|
35
|
+
export const ORG_PACKS_SUPPORT: string
|
|
36
|
+
export const REDACTION_PACKS: readonly RedactionPack[]
|
|
37
|
+
|
|
38
|
+
export const compileRedaction: (rules: unknown) => Redactor
|
|
39
|
+
export const readRedactionFile: (path: string, options?: { withSecrets?: boolean }) => Redactor
|
|
40
|
+
export const redactOtlpTraces: <T>(redactor: Redactor, payload: T) => T
|
|
41
|
+
|
|
42
|
+
/** What the hooks run. `on` with `source: 'default'` has no rules file
|
|
43
|
+
* recorded; with `source: 'file'` the recorded file's rules (plus `secrets`)
|
|
44
|
+
* run after the organization's packs. `org` is the organization's packs
|
|
45
|
+
* (recorded by `install-hooks` from managed settings) this hook applies in a
|
|
46
|
+
* first pass of their own, empty when there are none, in which case the
|
|
47
|
+
* developer's rules run alone, as before organizations could set packs.
|
|
48
|
+
* `secrets` is whether the default is on. `off` means `redaction.secrets` is `false` and
|
|
49
|
+
* neither a rules file nor organization packs are recorded. `org: true` on an
|
|
50
|
+
* error: the organization's packs are what cannot be loaded. */
|
|
51
|
+
export type HookRedaction =
|
|
52
|
+
| { readonly state: 'off' }
|
|
53
|
+
| {
|
|
54
|
+
readonly state: 'on'
|
|
55
|
+
readonly source: 'default'
|
|
56
|
+
readonly org: readonly RedactionPack[]
|
|
57
|
+
readonly secrets: boolean
|
|
58
|
+
readonly redactor: Redactor
|
|
59
|
+
}
|
|
60
|
+
| {
|
|
61
|
+
readonly state: 'on'
|
|
62
|
+
readonly source: 'file'
|
|
63
|
+
readonly file: string
|
|
64
|
+
readonly org: readonly RedactionPack[]
|
|
65
|
+
readonly secrets: boolean
|
|
66
|
+
readonly redactor: Redactor
|
|
67
|
+
}
|
|
68
|
+
| {
|
|
69
|
+
readonly state: 'error'
|
|
70
|
+
readonly file?: string
|
|
71
|
+
readonly org?: true
|
|
72
|
+
readonly reason: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** `configDir`: the Claude config dir the hook runs for; without it, every
|
|
76
|
+
* recorded dir's organization packs apply. */
|
|
77
|
+
export const loadHookRedaction: (options?: {
|
|
78
|
+
settingsPath?: string
|
|
79
|
+
configDir?: string
|
|
80
|
+
}) => HookRedaction
|
|
81
|
+
|
|
82
|
+
/** Every recorded config dir's organization packs, as written; `{}` when none
|
|
83
|
+
* can be read. */
|
|
84
|
+
export const recordedOrgPacks: (options?: { settingsPath?: string }) => Record<string, string[]>
|