1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// What: `use memchr::memchr_iter;` imports a SIMD-accelerated
// "find every occurrence of byte B in slice S" iterator.
// memchr is the foundation that aho-corasick is also built
// on, so this dep is essentially free in our build.
// Why: `build_line_index` walks every byte of one file and
// records the offset of each `\n`. memchr_iter does that
// with AVX2/NEON (when available) instead of byte-at-a-time
// scalar code, so a 1M-line file builds the index in
// milliseconds instead of tens of milliseconds.
// TS map: No 1:1 equivalent. Closest is `String.prototype.matchAll`
// with a `/\n/g` regex, but that is slower than SIMD memchr.
//
// In TS you'd write (pseudocode):
// ```ts
// // No equivalent. Imagine:
// // for (const m of content.matchAll(/\n/g)) starts.push(m.index + 1);
// ```
use memchr_iter;
// What: The previous `is_likely_binary` heuristic lived here. It
// was removed (see BUG 5): a NUL byte in the first 8 KiB
// caused the entire scan to short-circuit, producing silent
// false negatives when a deny-listed literal appeared before
// a NUL in the file. AC scans raw bytes content-agnostic and
// the redacted output format makes a "binary file leaks
// secret" report useful; the perf saving from skipping
// binaries does not justify the soundness gap.
// What: `pub fn build_line_index(content: &[u8]) -> Vec<usize>`
// produces a sorted `Vec<usize>` of byte offsets where each
// line starts. The first entry is always `0` (line 1's start);
// subsequent entries are the offset of the byte JUST AFTER
// each `\n`. So a file `"abc\ndef"` yields `[0, 4]` --
// line 1 begins at 0, line 2 begins at 4.
// Why: Replacing the old per-hit byte walk with an O(n)-once index
// plus O(log L) lookups (L = line count). The win matters
// when a single file has many hits -- e.g. an agent that
// wrote a forbidden literal a million times: 2M walks of
// average length n/2 collapse to one O(n) build plus 2M
// binary searches. Building only happens lazily on the
// first hit, so 99%-clean files never pay this cost.
// TS map: `function buildLineIndex(content: Uint8Array): number[]`.
// Gotcha: The returned vec's length is `1 + count(\\n in content)`,
// NOT the visible line count when the file ends without a
// trailing newline. The last entry can equal `content.len()`
// when the file ends with `\n`; lookups must tolerate that.
//
// In TS you'd write (pseudocode):
// ```ts
// function buildLineIndex(content: Uint8Array): number[] {
// const starts = [0];
// for (let i = 0; i < content.length; i++) {
// if (content[i] === 0x0a) starts.push(i + 1);
// }
// return starts;
// }
// ```
// What: `pub fn line_and_col_indexed(line_starts: &[usize], offset: usize) -> (usize, usize)`
// is the indexed replacement for the old `line_and_col`. It
// does an O(log L) binary search instead of an O(offset)
// walk to find which line owns `offset`.
// Why: Same `(line, col)` output as before; faster when called
// many times on one file because the index is shared.
// TS map: `function lineAndColIndexed(lineStarts: number[], offset: number): [number, number]`.
//
// In TS you'd write (pseudocode):
// ```ts
// function lineAndColIndexed(lineStarts: number[], offset: number): [number, number] {
// // partition_point: first index whose value is > offset
// let lo = 0, hi = lineStarts.length;
// while (lo < hi) {
// const mid = (lo + hi) >> 1;
// if (lineStarts[mid] <= offset) lo = mid + 1; else hi = mid;
// }
// const lineIdx = Math.max(0, lo - 1);
// return [lineIdx + 1, offset - lineStarts[lineIdx] + 1];
// }
// ```
// What: `pub fn end_in_line_indexed(line_starts: &[usize], start: usize, end: usize) -> usize`
// returns the byte offset of the first `\n` in `[start, end)`
// if one exists, else returns `end` unchanged. Indexed
// replacement for the old `end_in_line`.
// Why: Same semantics as before -- clamping multi-line matches
// to one line for the report. Now O(log L) instead of
// O(end - start).
// TS map: `function endInLineIndexed(lineStarts: number[], start: number, end: number): number`.
//
// In TS you'd write (pseudocode):
// ```ts
// function endInLineIndexed(lineStarts: number[], start: number, end: number): number {
// const lineIdx = Math.max(0, partitionPoint(lineStarts, s => s <= start) - 1);
// if (lineIdx + 1 < lineStarts.length) {
// const nextLineStart = lineStarts[lineIdx + 1];
// if (nextLineStart > 0 && nextLineStart - 1 < end) return nextLineStart - 1;
// }
// return end;
// }
// ```
// What: `pub fn format_hit(path, line, col_start, col_end, rule_idx) -> String`
// builds the redacted `path:line:col_start..col_end rule=N`
// output string. Public so `scan.rs` can call it.
// Why: Output format must NEVER include the matched substring --
// the failing CI log itself is a leak surface. Centralizing
// the format string here ensures every hit is redacted the
// same way.
// TS map: `function formatHit(path: string, line: number, colStart: number, colEnd: number, ruleIdx: number): string`.
//
// In TS you'd write (pseudocode):
// ```ts
// function formatHit(path, line, colStart, colEnd, ruleIdx) {
// return `${path}:${line}:${colStart}..${colEnd} rule=${ruleIdx}`;
// }
// ```
// What: `pub fn emit_hit(li, path, start, end, rule_idx) -> String`
// composes the three-step (line, col_start, col_end) compute
// and `format_hit` call that every hit-emission site in
// `scan.rs` previously inlined. Takes `&[usize]` (the
// already-initialised line-start index) so the lazy
// `OnceLock::get_or_init` invariant stays at each call site.
// Why: `scan.rs::scan_content` emits hits from four near-identical
// code paths (AC literal, prefix-matched par_iter, residual
// Single shard, residual Combined par_iter). Each previously
// spelled out: `line_and_col_indexed` for start, then
// `end_in_line_indexed` to clamp multi-line matches to one
// line, then a second `line_and_col_indexed` on `end - 1` (or
// 0 when `end` is 0) to derive col_end, then `format_hit`.
// Centralising the sequence here removes ~60 logic lines from
// `scan.rs` and ensures every site emits the same
// `path:line:col_start..col_end rule=N` shape. `#[inline]`
// guarantees the helper compiles to the same machine code the
// inlined version produced (private to this crate, called
// from one consumer, LTO already inlines tiny crate-internal
// helpers, but the attribute removes any doubt on the hot
// path).
// TS map: `function emitHit(li: number[], path: string, start: number, end: number, ruleIdx: number): string`.
// Gotcha: This helper does NOT skip empty-span matches (`start ==
// end`). The three regex-result emission sites in scan.rs
// guard that with `if m.start == m.end { continue; }` before
// calling emit_hit; the AC literal site does not need the
// guard because AC patterns are non-empty by construction.
// Keeping the guard at the call site preserves that
// asymmetry and lets the `continue` skip the push entirely
// instead of building a hit string we would discard.
//
// In TS you'd write (pseudocode):
// ```ts
// function emitHit(li, path, start, end, ruleIdx) {
// const [line, colStart] = lineAndColIndexed(li, start);
// const endInLine = endInLineIndexed(li, start, end);
// const [, colEnd] = lineAndColIndexed(li, endInLine > 0 ? endInLine - 1 : 0);
// return formatHit(path, line, colStart, colEnd, ruleIdx);
// }
// ```
// What: Inline `#[cfg(test)] mod tests` covering the five publics
// above. Tests live next to the helpers (one file, one module)
// because scan_format.rs is short and the helpers are leaf
// utilities -- no cross-module fixtures to share, no
// pub(super) visibility hops to set up. Compiles only under
// `cargo test`, costs nothing in the release binary.
// Why: The four primitives (build_line_index, line_and_col_indexed,
// end_in_line_indexed, format_hit) and the composer (emit_hit)
// each have invariants documented in their Gotcha blocks
// above. The format-hit shape is a security-sensitive
// contract: the redacted output channel MUST NEVER include
// the matched substring (see README "Redacted output"). A
// silent change to any helper would either re-introduce BUG 5
// shaped behaviour (wrong line/col reports) or worse, change
// the format string so the matched substring leaks. These
// tests pin both shapes.
// TS map: `describe("scan_format helpers", () => { ... })`.
//
// In TS you'd write (pseudocode):
// ```ts
// describe("scan_format helpers", () => { /* tests below */ });
// ```