wide-cli 0.5.1611261726 → 0.7.211126
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 +16 -1
- package/data/osv-bundle.json +1 -1
- package/data/rules.json +1 -1
- package/dist/agent/index.d.ts +25 -3
- package/dist/agent/index.d.ts.map +1 -1
- package/dist/agent/index.js +117 -18
- package/dist/agent/index.js.map +1 -1
- package/dist/completion.js +5 -5
- package/dist/completion.js.map +1 -1
- package/dist/config/index.d.ts +4 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +9 -2
- package/dist/config/index.js.map +1 -1
- package/dist/fix.d.ts +25 -0
- package/dist/fix.d.ts.map +1 -0
- package/dist/fix.js +197 -0
- package/dist/fix.js.map +1 -0
- package/dist/hook.d.ts +31 -0
- package/dist/hook.d.ts.map +1 -0
- package/dist/hook.js +152 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.js +169 -6
- package/dist/index.js.map +1 -1
- package/dist/providers/antigravity.d.ts +46 -0
- package/dist/providers/antigravity.d.ts.map +1 -0
- package/dist/providers/antigravity.js +496 -0
- package/dist/providers/antigravity.js.map +1 -0
- package/dist/providers/claude-code.d.ts +60 -0
- package/dist/providers/claude-code.d.ts.map +1 -0
- package/dist/providers/claude-code.js +973 -0
- package/dist/providers/claude-code.js.map +1 -0
- package/dist/providers/codex.d.ts +52 -0
- package/dist/providers/codex.d.ts.map +1 -0
- package/dist/providers/codex.js +742 -0
- package/dist/providers/codex.js.map +1 -0
- package/dist/providers/index.d.ts.map +1 -1
- package/dist/providers/index.js +33 -1
- package/dist/providers/index.js.map +1 -1
- package/dist/repl/commands.d.ts.map +1 -1
- package/dist/repl/commands.js +53 -3
- package/dist/repl/commands.js.map +1 -1
- package/dist/repl/index.d.ts +3 -0
- package/dist/repl/index.d.ts.map +1 -1
- package/dist/repl/index.js +11 -6
- package/dist/repl/index.js.map +1 -1
- package/dist/repl/setup.d.ts.map +1 -1
- package/dist/repl/setup.js +16 -4
- package/dist/repl/setup.js.map +1 -1
- package/dist/security/crawler.d.ts +90 -0
- package/dist/security/crawler.d.ts.map +1 -0
- package/dist/security/crawler.js +622 -0
- package/dist/security/crawler.js.map +1 -0
- package/dist/security/fuzzer.d.ts +1 -0
- package/dist/security/fuzzer.d.ts.map +1 -1
- package/dist/security/fuzzer.js +25 -1
- package/dist/security/fuzzer.js.map +1 -1
- package/dist/security/inspect/evasion-decode.d.ts +41 -0
- package/dist/security/inspect/evasion-decode.d.ts.map +1 -0
- package/dist/security/inspect/evasion-decode.js +316 -0
- package/dist/security/inspect/evasion-decode.js.map +1 -0
- package/dist/security/inspect/http-conformance.d.ts +51 -0
- package/dist/security/inspect/http-conformance.d.ts.map +1 -0
- package/dist/security/inspect/http-conformance.js +389 -0
- package/dist/security/inspect/http-conformance.js.map +1 -0
- package/dist/security/inspect/lexical-sqli.d.ts +7 -0
- package/dist/security/inspect/lexical-sqli.d.ts.map +1 -0
- package/dist/security/inspect/lexical-sqli.js +986 -0
- package/dist/security/inspect/lexical-sqli.js.map +1 -0
- package/dist/security/inspect/lexical-xss.d.ts +10 -0
- package/dist/security/inspect/lexical-xss.d.ts.map +1 -0
- package/dist/security/inspect/lexical-xss.js +654 -0
- package/dist/security/inspect/lexical-xss.js.map +1 -0
- package/dist/security/inspect/libinjection-tables.d.ts +6 -0
- package/dist/security/inspect/libinjection-tables.d.ts.map +1 -0
- package/dist/security/inspect/libinjection-tables.js +6 -0
- package/dist/security/inspect/libinjection-tables.js.map +1 -0
- package/dist/security/inspect/safe-pattern.d.ts +62 -0
- package/dist/security/inspect/safe-pattern.d.ts.map +1 -0
- package/dist/security/inspect/safe-pattern.js +640 -0
- package/dist/security/inspect/safe-pattern.js.map +1 -0
- package/dist/security/inspect/score-policy.d.ts +82 -0
- package/dist/security/inspect/score-policy.d.ts.map +1 -0
- package/dist/security/inspect/score-policy.js +241 -0
- package/dist/security/inspect/score-policy.js.map +1 -0
- package/dist/security/inspect.d.ts +7 -0
- package/dist/security/inspect.d.ts.map +1 -0
- package/dist/security/inspect.js +7 -0
- package/dist/security/inspect.js.map +1 -0
- package/dist/security/loader.d.ts.map +1 -1
- package/dist/security/loader.js +12 -2
- package/dist/security/loader.js.map +1 -1
- package/dist/security/payloads.d.ts.map +1 -1
- package/dist/security/payloads.js +19 -0
- package/dist/security/payloads.js.map +1 -1
- package/dist/security/scanner.d.ts +2 -0
- package/dist/security/scanner.d.ts.map +1 -1
- package/dist/security/scanner.js +31 -26
- package/dist/security/scanner.js.map +1 -1
- package/dist/security/secrets.d.ts +2 -2
- package/dist/security/secrets.d.ts.map +1 -1
- package/dist/security/secrets.js +3 -43
- package/dist/security/secrets.js.map +1 -1
- package/dist/security/taint.d.ts +2 -13
- package/dist/security/taint.d.ts.map +1 -1
- package/dist/security/taint.js +1 -516
- package/dist/security/taint.js.map +1 -1
- package/dist/tools/article.d.ts +18 -0
- package/dist/tools/article.d.ts.map +1 -0
- package/dist/tools/article.js +193 -0
- package/dist/tools/article.js.map +1 -0
- package/dist/tools/browser.d.ts +8 -2
- package/dist/tools/browser.d.ts.map +1 -1
- package/dist/tools/browser.js +76 -10
- package/dist/tools/browser.js.map +1 -1
- package/dist/tools/docx.d.ts +11 -0
- package/dist/tools/docx.d.ts.map +1 -0
- package/dist/tools/docx.js +149 -0
- package/dist/tools/docx.js.map +1 -0
- package/dist/tools/fs.d.ts +2 -0
- package/dist/tools/fs.d.ts.map +1 -1
- package/dist/tools/fs.js +96 -7
- package/dist/tools/fs.js.map +1 -1
- package/dist/tools/http.d.ts.map +1 -1
- package/dist/tools/http.js +125 -48
- package/dist/tools/http.js.map +1 -1
- package/dist/tools/index.d.ts +21 -2
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +137 -14
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/resilience.d.ts +78 -0
- package/dist/tools/resilience.d.ts.map +1 -0
- package/dist/tools/resilience.js +211 -0
- package/dist/tools/resilience.js.map +1 -0
- package/dist/tools/security.d.ts +2 -0
- package/dist/tools/security.d.ts.map +1 -1
- package/dist/tools/security.js +79 -1
- package/dist/tools/security.js.map +1 -1
- package/dist/tools/types.d.ts +4 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/ui/banner.d.ts.map +1 -1
- package/dist/ui/banner.js +2 -2
- package/dist/ui/banner.js.map +1 -1
- package/dist/vendor/wide-engine/index.d.ts +4 -0
- package/dist/vendor/wide-engine/index.d.ts.map +1 -0
- package/dist/vendor/wide-engine/index.js +3 -0
- package/dist/vendor/wide-engine/index.js.map +1 -0
- package/dist/vendor/wide-engine/secrets.d.ts +4 -0
- package/dist/vendor/wide-engine/secrets.d.ts.map +1 -0
- package/dist/vendor/wide-engine/secrets.js +48 -0
- package/dist/vendor/wide-engine/secrets.js.map +1 -0
- package/dist/vendor/wide-engine/taint.d.ts +5 -0
- package/dist/vendor/wide-engine/taint.d.ts.map +1 -0
- package/dist/vendor/wide-engine/taint.js +533 -0
- package/dist/vendor/wide-engine/taint.js.map +1 -0
- package/dist/vendor/wide-engine/types.d.ts +30 -0
- package/dist/vendor/wide-engine/types.d.ts.map +1 -0
- package/dist/vendor/wide-engine/types.js +2 -0
- package/dist/vendor/wide-engine/types.js.map +1 -0
- package/package.json +5 -3
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
const MAX_PATTERN_BYTES = 4096;
|
|
2
|
+
const MAX_INSTS = 2048;
|
|
3
|
+
function makeEmptyClass() {
|
|
4
|
+
return { bits: new Uint32Array(8) };
|
|
5
|
+
}
|
|
6
|
+
function setBit(cls, byte, caseInsensitive = false) {
|
|
7
|
+
const b = byte & 0xff;
|
|
8
|
+
cls.bits[b >>> 5] |= 1 << (b & 31);
|
|
9
|
+
if (caseInsensitive) {
|
|
10
|
+
if (b >= 0x41 && b <= 0x5a) {
|
|
11
|
+
const low = b | 0x20;
|
|
12
|
+
cls.bits[low >>> 5] |= 1 << (low & 31);
|
|
13
|
+
}
|
|
14
|
+
else if (b >= 0x61 && b <= 0x7a) {
|
|
15
|
+
const up = b & ~0x20;
|
|
16
|
+
cls.bits[up >>> 5] |= 1 << (up & 31);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function hasBit(cls, byte) {
|
|
21
|
+
const b = byte & 0xff;
|
|
22
|
+
return (cls.bits[b >>> 5] & (1 << (b & 31))) !== 0;
|
|
23
|
+
}
|
|
24
|
+
function invertClass(cls) {
|
|
25
|
+
for (let i = 0; i < 8; i += 1)
|
|
26
|
+
cls.bits[i] = (~cls.bits[i]) >>> 0;
|
|
27
|
+
}
|
|
28
|
+
function addShorthand(cls, esc) {
|
|
29
|
+
if (esc === "d" || esc === "D") {
|
|
30
|
+
const neg = esc === "D";
|
|
31
|
+
for (let c = 0; c < 256; c += 1) {
|
|
32
|
+
const hit = c >= 0x30 && c <= 0x39;
|
|
33
|
+
if (hit !== neg)
|
|
34
|
+
setBit(cls, c);
|
|
35
|
+
}
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
if (esc === "w" || esc === "W") {
|
|
39
|
+
const neg = esc === "W";
|
|
40
|
+
for (let c = 0; c < 256; c += 1) {
|
|
41
|
+
const hit = (c >= 0x30 && c <= 0x39) || (c >= 0x41 && c <= 0x5a) || (c >= 0x61 && c <= 0x7a) || c === 0x5f;
|
|
42
|
+
if (hit !== neg)
|
|
43
|
+
setBit(cls, c);
|
|
44
|
+
}
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
if (esc === "s" || esc === "S") {
|
|
48
|
+
const neg = esc === "S";
|
|
49
|
+
for (let c = 0; c < 256; c += 1) {
|
|
50
|
+
const hit = c === 0x20 || c === 0x09 || c === 0x0a || c === 0x0d || c === 0x0b || c === 0x0c;
|
|
51
|
+
if (hit !== neg)
|
|
52
|
+
setBit(cls, c);
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function failPattern(code, msg) {
|
|
59
|
+
const err = new Error(`${code}: ${msg}`);
|
|
60
|
+
err.code = code;
|
|
61
|
+
throw err;
|
|
62
|
+
}
|
|
63
|
+
function parseEscapeChar(pat, pos) {
|
|
64
|
+
if (pos >= pat.length)
|
|
65
|
+
failPattern("InvalidEscape", "trailing backslash");
|
|
66
|
+
const c = pat[pos];
|
|
67
|
+
if (c >= "1" && c <= "9") {
|
|
68
|
+
failPattern("BackreferenceUnsupported", `backreference \\${c} rejected to prevent exponential backtracking`);
|
|
69
|
+
}
|
|
70
|
+
if ("dDwWsS".includes(c))
|
|
71
|
+
return { ch: -1, nextPos: pos + 1, shorthand: c };
|
|
72
|
+
if (c === "n")
|
|
73
|
+
return { ch: 0x0a, nextPos: pos + 1 };
|
|
74
|
+
if (c === "r")
|
|
75
|
+
return { ch: 0x0d, nextPos: pos + 1 };
|
|
76
|
+
if (c === "t")
|
|
77
|
+
return { ch: 0x09, nextPos: pos + 1 };
|
|
78
|
+
if (c === "0")
|
|
79
|
+
return { ch: 0, nextPos: pos + 1 };
|
|
80
|
+
if (c === "x" && pos + 2 < pat.length && /^[0-9a-fA-F]{2}$/.test(pat.slice(pos + 1, pos + 3))) {
|
|
81
|
+
return { ch: Number.parseInt(pat.slice(pos + 1, pos + 3), 16), nextPos: pos + 3 };
|
|
82
|
+
}
|
|
83
|
+
return { ch: c.charCodeAt(0) & 0xff, nextPos: pos + 1 };
|
|
84
|
+
}
|
|
85
|
+
class RegexCompiler {
|
|
86
|
+
src;
|
|
87
|
+
caseInsensitive;
|
|
88
|
+
pos = 0;
|
|
89
|
+
insts = [];
|
|
90
|
+
classes = [];
|
|
91
|
+
constructor(src, caseInsensitive) {
|
|
92
|
+
this.src = src;
|
|
93
|
+
this.caseInsensitive = caseInsensitive;
|
|
94
|
+
}
|
|
95
|
+
addClass(cls) {
|
|
96
|
+
const idx = this.classes.length;
|
|
97
|
+
this.classes.push(cls);
|
|
98
|
+
return idx;
|
|
99
|
+
}
|
|
100
|
+
emit(inst) {
|
|
101
|
+
if (this.insts.length >= MAX_INSTS)
|
|
102
|
+
failPattern("ProgramTooLarge", "compiled pattern exceeds instruction limit");
|
|
103
|
+
const idx = this.insts.length;
|
|
104
|
+
this.insts.push(inst);
|
|
105
|
+
return idx;
|
|
106
|
+
}
|
|
107
|
+
cloneSlice(slice, offset) {
|
|
108
|
+
return slice.map((inst) => {
|
|
109
|
+
if (inst.op === "split")
|
|
110
|
+
return { op: "split", a: inst.a + offset, b: inst.b + offset };
|
|
111
|
+
if (inst.op === "jmp")
|
|
112
|
+
return { op: "jmp", target: inst.target + offset };
|
|
113
|
+
return { ...inst };
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
parseAlternation() {
|
|
117
|
+
const branchStarts = [];
|
|
118
|
+
const jmpsToEnd = [];
|
|
119
|
+
while (true) {
|
|
120
|
+
const splitIdx = this.emit({ op: "split", a: 0, b: 0 });
|
|
121
|
+
branchStarts.push(splitIdx);
|
|
122
|
+
const bodyStart = this.insts.length;
|
|
123
|
+
this.parseConcat();
|
|
124
|
+
if (this.pos < this.src.length && this.src[this.pos] === "|") {
|
|
125
|
+
this.pos += 1;
|
|
126
|
+
const jmpIdx = this.emit({ op: "jmp", target: 0 });
|
|
127
|
+
jmpsToEnd.push(jmpIdx);
|
|
128
|
+
const sp = this.insts[splitIdx];
|
|
129
|
+
if (sp.op === "split") {
|
|
130
|
+
sp.a = bodyStart;
|
|
131
|
+
sp.b = this.insts.length;
|
|
132
|
+
}
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (branchStarts.length === 1) {
|
|
136
|
+
const body = this.insts.slice(bodyStart);
|
|
137
|
+
this.insts.length = splitIdx;
|
|
138
|
+
const shifted = this.cloneSlice(body, -1);
|
|
139
|
+
for (const inst of shifted)
|
|
140
|
+
this.emit(inst);
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
const sp = this.insts[splitIdx];
|
|
144
|
+
if (sp.op === "split") {
|
|
145
|
+
this.insts[splitIdx] = { op: "jmp", target: bodyStart };
|
|
146
|
+
}
|
|
147
|
+
const end = this.insts.length;
|
|
148
|
+
for (const j of jmpsToEnd) {
|
|
149
|
+
const jmpInst = this.insts[j];
|
|
150
|
+
if (jmpInst.op === "jmp")
|
|
151
|
+
jmpInst.target = end;
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
parseConcat() {
|
|
157
|
+
while (this.pos < this.src.length) {
|
|
158
|
+
const ch = this.src[this.pos];
|
|
159
|
+
if (ch === ")" || ch === "|")
|
|
160
|
+
break;
|
|
161
|
+
this.parseQuantifiedAtom();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
parseQuantifiedAtom() {
|
|
165
|
+
const atomStart = this.insts.length;
|
|
166
|
+
this.parseAtom();
|
|
167
|
+
const atomEnd = this.insts.length;
|
|
168
|
+
if (atomEnd === atomStart) {
|
|
169
|
+
if (this.pos < this.src.length && "*+?{".includes(this.src[this.pos])) {
|
|
170
|
+
failPattern("NothingToRepeat", `quantifier at ${this.pos} has no atom`);
|
|
171
|
+
}
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (this.pos >= this.src.length)
|
|
175
|
+
return;
|
|
176
|
+
const q = this.src[this.pos];
|
|
177
|
+
let min = 1;
|
|
178
|
+
let max = 1;
|
|
179
|
+
if (q === "*") {
|
|
180
|
+
min = 0;
|
|
181
|
+
max = null;
|
|
182
|
+
this.pos += 1;
|
|
183
|
+
}
|
|
184
|
+
else if (q === "+") {
|
|
185
|
+
min = 1;
|
|
186
|
+
max = null;
|
|
187
|
+
this.pos += 1;
|
|
188
|
+
}
|
|
189
|
+
else if (q === "?") {
|
|
190
|
+
min = 0;
|
|
191
|
+
max = 1;
|
|
192
|
+
this.pos += 1;
|
|
193
|
+
}
|
|
194
|
+
else if (q === "{") {
|
|
195
|
+
const close = this.src.indexOf("}", this.pos + 1);
|
|
196
|
+
if (close < 0)
|
|
197
|
+
failPattern("InvalidQuantifier", "unclosed {m,n} quantifier");
|
|
198
|
+
const inner = this.src.slice(this.pos + 1, close);
|
|
199
|
+
if (!inner)
|
|
200
|
+
failPattern("InvalidQuantifier", "empty {} quantifier");
|
|
201
|
+
const comma = inner.indexOf(",");
|
|
202
|
+
if (comma < 0) {
|
|
203
|
+
if (!/^\d+$/.test(inner))
|
|
204
|
+
failPattern("InvalidQuantifier", `invalid count ${inner}`);
|
|
205
|
+
min = Number(inner);
|
|
206
|
+
max = min;
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
const lo = inner.slice(0, comma);
|
|
210
|
+
const hi = inner.slice(comma + 1);
|
|
211
|
+
if (!/^\d+$/.test(lo) || (hi && !/^\d+$/.test(hi))) {
|
|
212
|
+
failPattern("InvalidQuantifier", `invalid range ${inner}`);
|
|
213
|
+
}
|
|
214
|
+
min = Number(lo);
|
|
215
|
+
max = hi ? Number(hi) : null;
|
|
216
|
+
if (max !== null && max < min)
|
|
217
|
+
failPattern("InvalidQuantifier", `max < min in ${inner}`);
|
|
218
|
+
}
|
|
219
|
+
if (min > 256 || (max !== null && max > 256))
|
|
220
|
+
failPattern("InvalidQuantifier", "quantifier bound too large");
|
|
221
|
+
this.pos = close + 1;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (this.pos < this.src.length) {
|
|
227
|
+
if (this.src[this.pos] === "+")
|
|
228
|
+
failPattern("BacktrackControlUnsupported", "possessive quantifiers are unsupported");
|
|
229
|
+
if (this.src[this.pos] === "?")
|
|
230
|
+
this.pos += 1;
|
|
231
|
+
}
|
|
232
|
+
const atom = this.cloneSlice(this.insts.slice(atomStart), -atomStart);
|
|
233
|
+
this.insts.length = atomStart;
|
|
234
|
+
for (let i = 0; i < min; i += 1) {
|
|
235
|
+
const base = this.insts.length;
|
|
236
|
+
for (const inst of this.cloneSlice(atom, base))
|
|
237
|
+
this.emit(inst);
|
|
238
|
+
}
|
|
239
|
+
if (max !== null) {
|
|
240
|
+
const splits = [];
|
|
241
|
+
for (let i = min; i < max; i += 1) {
|
|
242
|
+
const sp = this.emit({ op: "split", a: 0, b: 0 });
|
|
243
|
+
const bodyBase = this.insts.length;
|
|
244
|
+
for (const inst of this.cloneSlice(atom, bodyBase))
|
|
245
|
+
this.emit(inst);
|
|
246
|
+
const spInst = this.insts[sp];
|
|
247
|
+
if (spInst.op === "split")
|
|
248
|
+
spInst.a = bodyBase;
|
|
249
|
+
splits.push(sp);
|
|
250
|
+
}
|
|
251
|
+
const end = this.insts.length;
|
|
252
|
+
for (const sp of splits) {
|
|
253
|
+
const spInst = this.insts[sp];
|
|
254
|
+
if (spInst.op === "split")
|
|
255
|
+
spInst.b = end;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
const sp = this.emit({ op: "split", a: 0, b: 0 });
|
|
260
|
+
const bodyBase = this.insts.length;
|
|
261
|
+
for (const inst of this.cloneSlice(atom, bodyBase))
|
|
262
|
+
this.emit(inst);
|
|
263
|
+
this.emit({ op: "jmp", target: sp });
|
|
264
|
+
const spInst = this.insts[sp];
|
|
265
|
+
if (spInst.op === "split") {
|
|
266
|
+
spInst.a = bodyBase;
|
|
267
|
+
spInst.b = this.insts.length;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
parseAtom() {
|
|
272
|
+
const ch = this.src[this.pos];
|
|
273
|
+
if (ch === "^") {
|
|
274
|
+
this.pos += 1;
|
|
275
|
+
this.emit({ op: "assert_start" });
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
if (ch === "$") {
|
|
279
|
+
this.pos += 1;
|
|
280
|
+
this.emit({ op: "assert_end" });
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (ch === ".") {
|
|
284
|
+
this.pos += 1;
|
|
285
|
+
const cls = makeEmptyClass();
|
|
286
|
+
for (let c = 0; c < 256; c += 1) {
|
|
287
|
+
if (c !== 0x0a)
|
|
288
|
+
setBit(cls, c);
|
|
289
|
+
}
|
|
290
|
+
this.emit({ op: "class", clsIdx: this.addClass(cls) });
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (ch === "[") {
|
|
294
|
+
this.parseBracketClass();
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (ch === "(") {
|
|
298
|
+
this.pos += 1;
|
|
299
|
+
if (this.pos < this.src.length && this.src[this.pos] === "?") {
|
|
300
|
+
const tail = this.src.slice(this.pos + 1);
|
|
301
|
+
if (tail.startsWith("=") || tail.startsWith("!") || tail.startsWith("<=") || tail.startsWith("<!")) {
|
|
302
|
+
failPattern("LookaroundUnsupported", "lookaround assertions are unsupported");
|
|
303
|
+
}
|
|
304
|
+
if (tail.startsWith(":")) {
|
|
305
|
+
this.pos += 2;
|
|
306
|
+
}
|
|
307
|
+
else if (tail.startsWith(">")) {
|
|
308
|
+
failPattern("BacktrackControlUnsupported", "atomic groups are unsupported");
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
failPattern("LookaroundUnsupported", "unsupported special group");
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
this.parseAlternation();
|
|
315
|
+
if (this.pos >= this.src.length || this.src[this.pos] !== ")") {
|
|
316
|
+
failPattern("UnbalancedParen", "missing closing )");
|
|
317
|
+
}
|
|
318
|
+
this.pos += 1;
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (ch === "\\") {
|
|
322
|
+
const esc = parseEscapeChar(this.src, this.pos + 1);
|
|
323
|
+
this.pos = esc.nextPos;
|
|
324
|
+
const cls = makeEmptyClass();
|
|
325
|
+
if (esc.shorthand)
|
|
326
|
+
addShorthand(cls, esc.shorthand);
|
|
327
|
+
else
|
|
328
|
+
setBit(cls, esc.ch, this.caseInsensitive);
|
|
329
|
+
this.emit({ op: "class", clsIdx: this.addClass(cls) });
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if ("*+?{|)".includes(ch))
|
|
333
|
+
return;
|
|
334
|
+
this.pos += 1;
|
|
335
|
+
const cls = makeEmptyClass();
|
|
336
|
+
setBit(cls, ch.charCodeAt(0) & 0xff, this.caseInsensitive);
|
|
337
|
+
this.emit({ op: "class", clsIdx: this.addClass(cls) });
|
|
338
|
+
}
|
|
339
|
+
parseBracketClass() {
|
|
340
|
+
this.pos += 1;
|
|
341
|
+
let negated = false;
|
|
342
|
+
if (this.pos < this.src.length && this.src[this.pos] === "^") {
|
|
343
|
+
negated = true;
|
|
344
|
+
this.pos += 1;
|
|
345
|
+
}
|
|
346
|
+
const cls = makeEmptyClass();
|
|
347
|
+
let first = true;
|
|
348
|
+
while (this.pos < this.src.length) {
|
|
349
|
+
const c = this.src[this.pos];
|
|
350
|
+
if (c === "]" && !first) {
|
|
351
|
+
this.pos += 1;
|
|
352
|
+
if (negated)
|
|
353
|
+
invertClass(cls);
|
|
354
|
+
this.emit({ op: "class", clsIdx: this.addClass(cls) });
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
first = false;
|
|
358
|
+
let code;
|
|
359
|
+
if (c === "\\") {
|
|
360
|
+
const esc = parseEscapeChar(this.src, this.pos + 1);
|
|
361
|
+
this.pos = esc.nextPos;
|
|
362
|
+
if (esc.shorthand) {
|
|
363
|
+
addShorthand(cls, esc.shorthand);
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
code = esc.ch;
|
|
367
|
+
}
|
|
368
|
+
else {
|
|
369
|
+
code = c.charCodeAt(0) & 0xff;
|
|
370
|
+
this.pos += 1;
|
|
371
|
+
}
|
|
372
|
+
if (this.pos + 1 < this.src.length && this.src[this.pos] === "-" && this.src[this.pos + 1] !== "]") {
|
|
373
|
+
this.pos += 1;
|
|
374
|
+
const endChar = this.src[this.pos];
|
|
375
|
+
let endCode;
|
|
376
|
+
if (endChar === "\\") {
|
|
377
|
+
const esc2 = parseEscapeChar(this.src, this.pos + 1);
|
|
378
|
+
if (esc2.shorthand)
|
|
379
|
+
failPattern("InvalidEscape", "cannot use shorthand in range");
|
|
380
|
+
this.pos = esc2.nextPos;
|
|
381
|
+
endCode = esc2.ch;
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
endCode = endChar.charCodeAt(0) & 0xff;
|
|
385
|
+
this.pos += 1;
|
|
386
|
+
}
|
|
387
|
+
if (endCode < code)
|
|
388
|
+
failPattern("UnbalancedBracket", "reversed range in [...]");
|
|
389
|
+
for (let k = code; k <= endCode; k += 1)
|
|
390
|
+
setBit(cls, k, this.caseInsensitive);
|
|
391
|
+
}
|
|
392
|
+
else {
|
|
393
|
+
setBit(cls, code, this.caseInsensitive);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
failPattern("UnbalancedBracket", "missing closing ]");
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function extractSimpleLiterals(pattern, caseInsensitive) {
|
|
400
|
+
const parts = pattern.replace(/^\(\?[ims]+\)/i, "").split("|");
|
|
401
|
+
if (parts.length > 1) {
|
|
402
|
+
const lits = parts.map((p) => p.replace(/[^A-Za-z0-9_./:-]/g, "")).filter((l) => l.length >= 3); // wide-ignore wide/flagsan-dash-permissive-sanitizer
|
|
403
|
+
return lits.length === parts.length ? lits.map((l) => (caseInsensitive ? l.toLowerCase() : l)) : [];
|
|
404
|
+
}
|
|
405
|
+
const runs = pattern.match(/[A-Za-z0-9_./:-]{3,}/g) || [];
|
|
406
|
+
let best = "";
|
|
407
|
+
for (const r of runs) {
|
|
408
|
+
const idx = pattern.indexOf(r);
|
|
409
|
+
const nextChar = pattern[idx + r.length];
|
|
410
|
+
if (nextChar === "*" || nextChar === "?" || nextChar === "{")
|
|
411
|
+
continue;
|
|
412
|
+
if (r.length > best.length)
|
|
413
|
+
best = r;
|
|
414
|
+
}
|
|
415
|
+
if (!best)
|
|
416
|
+
return [];
|
|
417
|
+
return [caseInsensitive ? best.toLowerCase() : best];
|
|
418
|
+
}
|
|
419
|
+
export function compileSafePattern(pattern, flags = "") {
|
|
420
|
+
if (!pattern)
|
|
421
|
+
failPattern("EmptyPattern", "pattern cannot be empty");
|
|
422
|
+
if (pattern.length > MAX_PATTERN_BYTES)
|
|
423
|
+
failPattern("PatternTooLong", "pattern exceeds length limit");
|
|
424
|
+
let cleanPat = pattern;
|
|
425
|
+
let ci = flags.includes("i");
|
|
426
|
+
if (cleanPat.startsWith("(?i)")) {
|
|
427
|
+
ci = true;
|
|
428
|
+
cleanPat = cleanPat.slice(4);
|
|
429
|
+
}
|
|
430
|
+
if (/\\[1-9]/.test(cleanPat))
|
|
431
|
+
failPattern("BackreferenceUnsupported", "backreferences are unsupported");
|
|
432
|
+
if (/\(\?(?:[=!<]|>)/.test(cleanPat)) {
|
|
433
|
+
failPattern("LookaroundUnsupported", "lookarounds and atomic groups are unsupported");
|
|
434
|
+
}
|
|
435
|
+
const compiler = new RegexCompiler(cleanPat, ci);
|
|
436
|
+
compiler.parseAlternation();
|
|
437
|
+
if (compiler.pos !== cleanPat.length)
|
|
438
|
+
failPattern("UnbalancedParen", "unexpected trailing )");
|
|
439
|
+
compiler.insts.push({ op: "match" });
|
|
440
|
+
return {
|
|
441
|
+
pattern,
|
|
442
|
+
caseInsensitive: ci,
|
|
443
|
+
insts: compiler.insts,
|
|
444
|
+
classes: compiler.classes,
|
|
445
|
+
prefilterLiterals: extractSimpleLiterals(cleanPat, ci),
|
|
446
|
+
hasStartAnchor: cleanPat.startsWith("^"),
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
export function isSafePattern(pattern) {
|
|
450
|
+
try {
|
|
451
|
+
compileSafePattern(pattern);
|
|
452
|
+
return { safe: true };
|
|
453
|
+
}
|
|
454
|
+
catch (err) {
|
|
455
|
+
return { safe: false, reason: String(err?.message || err) };
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
export function matchSafePattern(prog, input) {
|
|
459
|
+
if (prog.prefilterLiterals.length > 0) {
|
|
460
|
+
const hay = prog.caseInsensitive ? input.toLowerCase() : input;
|
|
461
|
+
let any = false;
|
|
462
|
+
for (const lit of prog.prefilterLiterals) {
|
|
463
|
+
if (hay.includes(lit)) {
|
|
464
|
+
any = true;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (!any)
|
|
469
|
+
return false;
|
|
470
|
+
}
|
|
471
|
+
const insts = prog.insts;
|
|
472
|
+
const n = insts.length;
|
|
473
|
+
let clist = [];
|
|
474
|
+
let nlist = [];
|
|
475
|
+
const mark = new Int32Array(n).fill(-1);
|
|
476
|
+
let gen = 0;
|
|
477
|
+
const addState = (target, idx, pos, len) => {
|
|
478
|
+
const stack = [idx];
|
|
479
|
+
while (stack.length > 0) {
|
|
480
|
+
const pc = stack.pop();
|
|
481
|
+
if (mark[pc] === gen)
|
|
482
|
+
continue;
|
|
483
|
+
mark[pc] = gen;
|
|
484
|
+
const inst = insts[pc];
|
|
485
|
+
if (inst.op === "split") {
|
|
486
|
+
stack.push(inst.b, inst.a);
|
|
487
|
+
}
|
|
488
|
+
else if (inst.op === "jmp") {
|
|
489
|
+
stack.push(inst.target);
|
|
490
|
+
}
|
|
491
|
+
else if (inst.op === "assert_start") {
|
|
492
|
+
if (pos === 0 || input.charCodeAt(pos - 1) === 0x0a)
|
|
493
|
+
stack.push(pc + 1);
|
|
494
|
+
}
|
|
495
|
+
else if (inst.op === "assert_end") {
|
|
496
|
+
if (pos === len || input.charCodeAt(pos) === 0x0a)
|
|
497
|
+
stack.push(pc + 1);
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
target.push(pc);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
const startMax = prog.hasStartAnchor ? 1 : input.length + 1;
|
|
505
|
+
for (let startPos = 0; startPos < startMax; startPos += 1) {
|
|
506
|
+
if (prog.hasStartAnchor && startPos > 0)
|
|
507
|
+
break;
|
|
508
|
+
clist.length = 0;
|
|
509
|
+
gen += 1;
|
|
510
|
+
addState(clist, 0, startPos, input.length);
|
|
511
|
+
for (const pc of clist) {
|
|
512
|
+
if (insts[pc].op === "match")
|
|
513
|
+
return true;
|
|
514
|
+
}
|
|
515
|
+
for (let pos = startPos; pos < input.length && clist.length > 0; pos += 1) {
|
|
516
|
+
const byte = input.charCodeAt(pos) & 0xff;
|
|
517
|
+
nlist.length = 0;
|
|
518
|
+
gen += 1;
|
|
519
|
+
for (const pc of clist) {
|
|
520
|
+
const inst = insts[pc];
|
|
521
|
+
if (inst.op === "class" && hasBit(prog.classes[inst.clsIdx], byte)) {
|
|
522
|
+
addState(nlist, pc + 1, pos + 1, input.length);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
for (const pc of nlist) {
|
|
526
|
+
if (insts[pc].op === "match")
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
const tmp = clist;
|
|
530
|
+
clist = nlist;
|
|
531
|
+
nlist = tmp;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return false;
|
|
535
|
+
}
|
|
536
|
+
export function hasNestedQuantifiers(pattern) {
|
|
537
|
+
const groupHasRepeat = /\((?:[^()\\]|\\.)*[+*](?:[^()\\]|\\.)*\)\s*[+*{]/.test(pattern);
|
|
538
|
+
return groupHasRepeat || /\\[1-9]/.test(pattern);
|
|
539
|
+
}
|
|
540
|
+
export function testUserRegexSafe(pattern, flags, sample) {
|
|
541
|
+
if (!pattern)
|
|
542
|
+
return { ok: false, error: "No pattern." };
|
|
543
|
+
if (hasNestedQuantifiers(pattern)) {
|
|
544
|
+
return { ok: false, error: "Unsafe / ReDoS-prone regex pattern rejected (nested quantifiers or backreference)." };
|
|
545
|
+
}
|
|
546
|
+
const safeFlags = typeof flags === "string" ? flags.replace(/[^gimsuy]/g, "") : "";
|
|
547
|
+
const lines = String(sample || "").split("\n");
|
|
548
|
+
try {
|
|
549
|
+
const prog = compileSafePattern(pattern, safeFlags);
|
|
550
|
+
const matches = [];
|
|
551
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
552
|
+
if (matchSafePattern(prog, lines[i])) {
|
|
553
|
+
matches.push({ line: i + 1, text: lines[i].slice(0, 200) });
|
|
554
|
+
if (matches.length >= 100)
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
return { ok: true, engine: "nfa", matches };
|
|
559
|
+
}
|
|
560
|
+
catch (err) {
|
|
561
|
+
const code = err?.code;
|
|
562
|
+
if (code === "BackreferenceUnsupported" || code === "BacktrackControlUnsupported" || code === "PatternTooLong") {
|
|
563
|
+
return { ok: false, error: String(err?.message || err) };
|
|
564
|
+
}
|
|
565
|
+
let re;
|
|
566
|
+
try {
|
|
567
|
+
re = new RegExp(pattern, safeFlags);
|
|
568
|
+
}
|
|
569
|
+
catch (reErr) {
|
|
570
|
+
return { ok: false, error: String(reErr?.message || reErr) };
|
|
571
|
+
}
|
|
572
|
+
const matches = [];
|
|
573
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
574
|
+
re.lastIndex = 0;
|
|
575
|
+
if (re.test(lines[i]))
|
|
576
|
+
matches.push({ line: i + 1, text: lines[i].slice(0, 200) });
|
|
577
|
+
if (matches.length >= 100)
|
|
578
|
+
break;
|
|
579
|
+
}
|
|
580
|
+
return { ok: true, engine: "guarded", matches };
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
export class AhoCorasick {
|
|
584
|
+
nodes = [{ next: new Map(), fail: 0, out: [] }];
|
|
585
|
+
caseInsensitive;
|
|
586
|
+
constructor(patterns, caseInsensitive = true) {
|
|
587
|
+
this.caseInsensitive = caseInsensitive;
|
|
588
|
+
for (const p of patterns) {
|
|
589
|
+
const lit = caseInsensitive ? p.literal.toLowerCase() : p.literal;
|
|
590
|
+
if (!lit)
|
|
591
|
+
continue;
|
|
592
|
+
let cur = 0;
|
|
593
|
+
for (let i = 0; i < lit.length; i += 1) {
|
|
594
|
+
const c = lit.charCodeAt(i) & 0xff;
|
|
595
|
+
let nx = this.nodes[cur].next.get(c);
|
|
596
|
+
if (nx === undefined) {
|
|
597
|
+
nx = this.nodes.length;
|
|
598
|
+
this.nodes.push({ next: new Map(), fail: 0, out: [] });
|
|
599
|
+
this.nodes[cur].next.set(c, nx);
|
|
600
|
+
}
|
|
601
|
+
cur = nx;
|
|
602
|
+
}
|
|
603
|
+
this.nodes[cur].out.push({ ruleId: p.ruleId, len: lit.length });
|
|
604
|
+
}
|
|
605
|
+
const queue = [];
|
|
606
|
+
for (const [, child] of this.nodes[0].next)
|
|
607
|
+
queue.push(child);
|
|
608
|
+
while (queue.length > 0) {
|
|
609
|
+
const u = queue.shift();
|
|
610
|
+
for (const [ch, v] of this.nodes[u].next) {
|
|
611
|
+
let f = this.nodes[u].fail;
|
|
612
|
+
while (f !== 0 && !this.nodes[f].next.has(ch))
|
|
613
|
+
f = this.nodes[f].fail;
|
|
614
|
+
const fNext = this.nodes[f].next.get(ch) ?? 0;
|
|
615
|
+
this.nodes[v].fail = fNext;
|
|
616
|
+
if (this.nodes[fNext].out.length > 0) {
|
|
617
|
+
this.nodes[v].out.push(...this.nodes[fNext].out);
|
|
618
|
+
}
|
|
619
|
+
queue.push(v);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
scan(input) {
|
|
624
|
+
const src = this.caseInsensitive ? input.toLowerCase() : input;
|
|
625
|
+
const hits = [];
|
|
626
|
+
let state = 0;
|
|
627
|
+
for (let i = 0; i < src.length; i += 1) {
|
|
628
|
+
const c = src.charCodeAt(i) & 0xff;
|
|
629
|
+
while (state !== 0 && !this.nodes[state].next.has(c))
|
|
630
|
+
state = this.nodes[state].fail;
|
|
631
|
+
state = this.nodes[state].next.get(c) ?? 0;
|
|
632
|
+
const out = this.nodes[state].out;
|
|
633
|
+
for (let k = 0; k < out.length; k += 1) {
|
|
634
|
+
hits.push({ ruleId: out[k].ruleId, end: i + 1, len: out[k].len });
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
return hits;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
//# sourceMappingURL=safe-pattern.js.map
|