leakshield 0.1.3 → 0.2.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 +294 -173
- package/README.zh-TW.md +290 -164
- package/dist/config/loader.js +14 -10
- package/dist/config/plain-object.js +14 -0
- package/dist/decision/pseudonymization.js +133 -82
- package/dist/decision/redaction.js +2 -0
- package/dist/decision/value-search.js +250 -0
- package/dist/doctor.js +429 -138
- package/dist/i18n/en.js +4 -0
- package/dist/i18n/zh-TW.js +4 -0
- package/dist/index.js +3 -2
- package/dist/opencode-env-tools.js +0 -75
- package/dist/opencode-hooks.js +4 -73
- package/dist/opencode-transparent.js +16 -1
- package/dist/opencode.js +12 -54
- package/dist/plugin-options.js +15 -21
- package/dist/scanner/encoding.js +291 -7
- package/dist/scanner/engine.js +68 -40
- package/dist/scanner/utils.js +26 -2
- package/dist/transports/cli.js +2 -2
- package/dist/transports/web.js +3 -3
- package/dist/types.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Exact-value search for the masking path: every occurrence of a set of
|
|
3
|
+
* detected values in one string, overlapping occurrences included.
|
|
4
|
+
*
|
|
5
|
+
* Searching each value with indexOf costs (values × text length), which is
|
|
6
|
+
* what limited how large a string the model path could afford. An Aho-Corasick
|
|
7
|
+
* automaton over UTF-16 code units costs (total value length + text length),
|
|
8
|
+
* plus the occurrences visited, but building one costs far more than a few
|
|
9
|
+
* indexOf calls and the value set can grow with every string of a request. So
|
|
10
|
+
* indexOf stays for small searches and the automaton takes over when the
|
|
11
|
+
* estimate favours it. Both strategies yield the same occurrences in the same
|
|
12
|
+
* order; tests/value-search.test.ts holds them to that.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Relative costs in units of one indexOf per character (about 0.2 ns). Measured
|
|
16
|
+
* at about 2-20 ns per text unit to scan and 15-150 ns per value unit to build,
|
|
17
|
+
* and rounded so a wrong guess near the boundary costs little either way.
|
|
18
|
+
*/
|
|
19
|
+
const AUTOMATON_SCAN_COST = 32;
|
|
20
|
+
const AUTOMATON_BUILD_COST_PER_UNIT = 256;
|
|
21
|
+
const ROOT = 0;
|
|
22
|
+
const NO_VALUE = -1;
|
|
23
|
+
class Automaton {
|
|
24
|
+
/** Children of ROOT, by code unit; 0 means none. Most positions stop here. */
|
|
25
|
+
rootChild = new Int32Array(0x10000);
|
|
26
|
+
// Open-addressed table of (node, code unit) -> child for every other node.
|
|
27
|
+
slotParent;
|
|
28
|
+
slotUnit;
|
|
29
|
+
slotChild;
|
|
30
|
+
slotMask;
|
|
31
|
+
fail;
|
|
32
|
+
/** Longest value that is a suffix of each node's string, or NO_VALUE. */
|
|
33
|
+
longest;
|
|
34
|
+
/** For each value, the next longest value that is a suffix of it. */
|
|
35
|
+
nextShorter;
|
|
36
|
+
constructor(values, totalUnits) {
|
|
37
|
+
const capacity = totalUnits + 1;
|
|
38
|
+
let slots = 16;
|
|
39
|
+
while (slots < capacity * 2)
|
|
40
|
+
slots *= 2;
|
|
41
|
+
this.slotParent = new Int32Array(slots).fill(-1);
|
|
42
|
+
this.slotUnit = new Uint16Array(slots);
|
|
43
|
+
this.slotChild = new Int32Array(slots);
|
|
44
|
+
this.slotMask = slots - 1;
|
|
45
|
+
this.fail = new Int32Array(capacity);
|
|
46
|
+
this.longest = new Int32Array(capacity).fill(NO_VALUE);
|
|
47
|
+
this.nextShorter = new Int32Array(values.length).fill(NO_VALUE);
|
|
48
|
+
// Trie. A shared prefix shares nodes, so `nodes` can end below `capacity`.
|
|
49
|
+
const parent = new Int32Array(capacity);
|
|
50
|
+
const unitOf = new Uint16Array(capacity);
|
|
51
|
+
const depth = new Int32Array(capacity);
|
|
52
|
+
const terminal = new Int32Array(values.length);
|
|
53
|
+
let nodes = 1;
|
|
54
|
+
for (let rank = 0; rank < values.length; rank++) {
|
|
55
|
+
const value = values[rank];
|
|
56
|
+
let node = ROOT;
|
|
57
|
+
for (let i = 0; i < value.length; i++) {
|
|
58
|
+
const unit = value.charCodeAt(i);
|
|
59
|
+
let child = this.child(node, unit);
|
|
60
|
+
if (child === ROOT) {
|
|
61
|
+
child = nodes++;
|
|
62
|
+
this.insert(node, unit, child);
|
|
63
|
+
parent[child] = node;
|
|
64
|
+
unitOf[child] = unit;
|
|
65
|
+
depth[child] = i + 1;
|
|
66
|
+
}
|
|
67
|
+
node = child;
|
|
68
|
+
}
|
|
69
|
+
terminal[rank] = node;
|
|
70
|
+
this.longest[node] = rank; // input is distinct, so each node ends one value
|
|
71
|
+
}
|
|
72
|
+
// Failure links, shallowest first: a link always points to a shallower node.
|
|
73
|
+
const startOfDepth = new Int32Array((values[0]?.length ?? 0) + 2);
|
|
74
|
+
for (let node = 1; node < nodes; node++)
|
|
75
|
+
startOfDepth[depth[node] + 1]++;
|
|
76
|
+
for (let d = 1; d < startOfDepth.length; d++)
|
|
77
|
+
startOfDepth[d] += startOfDepth[d - 1];
|
|
78
|
+
const byDepth = new Int32Array(nodes - 1);
|
|
79
|
+
for (let node = 1; node < nodes; node++)
|
|
80
|
+
byDepth[startOfDepth[depth[node]]++] = node;
|
|
81
|
+
for (const node of byDepth) {
|
|
82
|
+
if (parent[node] !== ROOT) {
|
|
83
|
+
const unit = unitOf[node];
|
|
84
|
+
let link = this.fail[parent[node]];
|
|
85
|
+
let child = this.child(link, unit);
|
|
86
|
+
while (child === ROOT && link !== ROOT) {
|
|
87
|
+
link = this.fail[link];
|
|
88
|
+
child = this.child(link, unit);
|
|
89
|
+
}
|
|
90
|
+
this.fail[node] = child;
|
|
91
|
+
}
|
|
92
|
+
// A node's own value is longer than any proper suffix of it.
|
|
93
|
+
if (this.longest[node] === NO_VALUE)
|
|
94
|
+
this.longest[node] = this.longest[this.fail[node]];
|
|
95
|
+
}
|
|
96
|
+
for (let rank = 0; rank < values.length; rank++) {
|
|
97
|
+
this.nextShorter[rank] = this.longest[this.fail[terminal[rank]]];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
child(node, unit) {
|
|
101
|
+
if (node === ROOT)
|
|
102
|
+
return this.rootChild[unit];
|
|
103
|
+
const mask = this.slotMask;
|
|
104
|
+
let slot = (Math.imul(node, 0x9e3779b1) ^ Math.imul(unit, 0x85ebca6b)) & mask;
|
|
105
|
+
for (;;) {
|
|
106
|
+
const owner = this.slotParent[slot];
|
|
107
|
+
if (owner === -1)
|
|
108
|
+
return ROOT;
|
|
109
|
+
if (owner === node && this.slotUnit[slot] === unit)
|
|
110
|
+
return this.slotChild[slot];
|
|
111
|
+
slot = (slot + 1) & mask;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
insert(node, unit, child) {
|
|
115
|
+
if (node === ROOT) {
|
|
116
|
+
this.rootChild[unit] = child;
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const mask = this.slotMask;
|
|
120
|
+
let slot = (Math.imul(node, 0x9e3779b1) ^ Math.imul(unit, 0x85ebca6b)) & mask;
|
|
121
|
+
while (this.slotParent[slot] !== -1)
|
|
122
|
+
slot = (slot + 1) & mask;
|
|
123
|
+
this.slotParent[slot] = node;
|
|
124
|
+
this.slotUnit[slot] = unit;
|
|
125
|
+
this.slotChild[slot] = child;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Reports, per end position in ascending order, only the longest value ending
|
|
129
|
+
* there; nextShorter gives the rest. Stops when `visit` returns false.
|
|
130
|
+
*/
|
|
131
|
+
scan(text, visit) {
|
|
132
|
+
const { rootChild, fail, longest } = this;
|
|
133
|
+
let node = ROOT;
|
|
134
|
+
for (let i = 0; i < text.length; i++) {
|
|
135
|
+
const unit = text.charCodeAt(i);
|
|
136
|
+
if (node === ROOT) {
|
|
137
|
+
node = rootChild[unit];
|
|
138
|
+
if (node === ROOT)
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
let child = this.child(node, unit);
|
|
143
|
+
while (child === ROOT && node !== ROOT) {
|
|
144
|
+
node = fail[node];
|
|
145
|
+
child = this.child(node, unit);
|
|
146
|
+
}
|
|
147
|
+
node = child;
|
|
148
|
+
}
|
|
149
|
+
const rank = longest[node];
|
|
150
|
+
if (rank !== NO_VALUE && !visit(i + 1, rank))
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
export function createValueSearch(source, sourceSize, strategy = "auto") {
|
|
156
|
+
// Stable sort: equal lengths keep input order, which decides which of two
|
|
157
|
+
// overlapping values of the same length is masked.
|
|
158
|
+
const values = [...new Set(source)]
|
|
159
|
+
.filter((value) => value.length > 0)
|
|
160
|
+
.sort((a, b) => b.length - a.length);
|
|
161
|
+
let totalUnits = 0;
|
|
162
|
+
for (const value of values)
|
|
163
|
+
totalUnits += value.length;
|
|
164
|
+
let automaton;
|
|
165
|
+
const automatonFor = (text) => {
|
|
166
|
+
if (strategy === "indexOf")
|
|
167
|
+
return undefined;
|
|
168
|
+
if (strategy === "auto") {
|
|
169
|
+
const indexOfCost = values.length * text.length;
|
|
170
|
+
const scanCost = AUTOMATON_SCAN_COST * text.length;
|
|
171
|
+
const buildCost = automaton === undefined ? AUTOMATON_BUILD_COST_PER_UNIT * totalUnits : 0;
|
|
172
|
+
if (indexOfCost < scanCost + buildCost)
|
|
173
|
+
return undefined;
|
|
174
|
+
}
|
|
175
|
+
automaton ??= new Automaton(values, totalUnits);
|
|
176
|
+
return automaton;
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
sourceSize,
|
|
180
|
+
visitOccurrences(text, visit) {
|
|
181
|
+
const built = automatonFor(text);
|
|
182
|
+
if (built === undefined) {
|
|
183
|
+
for (const value of values) {
|
|
184
|
+
let from = 0;
|
|
185
|
+
for (;;) {
|
|
186
|
+
const at = text.indexOf(value, from);
|
|
187
|
+
if (at === -1)
|
|
188
|
+
break;
|
|
189
|
+
from = visit(at, at + value.length, value) ? at + value.length : at + 1;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
// One pending list per value, holding end positions. A position sits in
|
|
195
|
+
// exactly one list at a time, so memory stays bounded by the text even
|
|
196
|
+
// when values nest inside each other at every position.
|
|
197
|
+
const pending = new Array(values.length);
|
|
198
|
+
built.scan(text, (end, rank) => {
|
|
199
|
+
(pending[rank] ??= []).push(end);
|
|
200
|
+
return true;
|
|
201
|
+
});
|
|
202
|
+
for (let rank = 0; rank < values.length; rank++) {
|
|
203
|
+
const ends = pending[rank];
|
|
204
|
+
if (ends === undefined)
|
|
205
|
+
continue;
|
|
206
|
+
pending[rank] = undefined;
|
|
207
|
+
ends.sort((a, b) => a - b);
|
|
208
|
+
const value = values[rank];
|
|
209
|
+
const shorter = built.nextShorter[rank];
|
|
210
|
+
for (const end of ends) {
|
|
211
|
+
// A shorter value ending here lies inside this occurrence, so taking
|
|
212
|
+
// this one settles it; otherwise it is still to be offered.
|
|
213
|
+
if (!visit(end - value.length, end, value) && shorter !== NO_VALUE) {
|
|
214
|
+
(pending[shorter] ??= []).push(end);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
},
|
|
219
|
+
everyOccurrenceCovered(text, spans) {
|
|
220
|
+
const coveredFrom = (cursor, start, end) => {
|
|
221
|
+
while (cursor < spans.length && spans[cursor].end < end)
|
|
222
|
+
cursor++;
|
|
223
|
+
return cursor < spans.length && spans[cursor].start <= start ? cursor : -1;
|
|
224
|
+
};
|
|
225
|
+
const built = automatonFor(text);
|
|
226
|
+
if (built === undefined) {
|
|
227
|
+
for (const value of values) {
|
|
228
|
+
let cursor = 0;
|
|
229
|
+
for (let at = text.indexOf(value); at !== -1; at = text.indexOf(value, at + 1)) {
|
|
230
|
+
cursor = coveredFrom(cursor, at, at + value.length);
|
|
231
|
+
if (cursor === -1)
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
// Only the longest value per end position needs checking: the shorter
|
|
238
|
+
// ones lie inside it, so if it is covered they are, and if it is not the
|
|
239
|
+
// answer is already false.
|
|
240
|
+
let cursor = 0;
|
|
241
|
+
let covered = true;
|
|
242
|
+
built.scan(text, (end, rank) => {
|
|
243
|
+
cursor = coveredFrom(cursor, end - values[rank].length, end);
|
|
244
|
+
covered = cursor !== -1;
|
|
245
|
+
return covered;
|
|
246
|
+
});
|
|
247
|
+
return covered;
|
|
248
|
+
},
|
|
249
|
+
};
|
|
250
|
+
}
|