forbidden-strings 0.5.0

Out-of-band scanner for forbidden literal strings and regex patterns. Gitignore-aware, fast, dependency-light: built for CI deny-listing of leaked credentials and banned tokens.
Documentation
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
//! What:
//!  Target-independent Windows prefix forms driven through production normalization,
//!  counting and scanning.
//! Why:
//!  Windows's native prefix parser runs only on Windows,
//!  so these controls supply the prefix bytes it returns
//! and check that the scan skips exactly that prefix before treating every following component as a name.
//!
//! Each fixture prefix is the raw `Component::Prefix` byte prefix the standard library produces for its path:
//! `parse_prefix` in `library/std/src/sys/path/windows_prefix.rs` selects the prefix kind,
//! and `Prefix::len` in `library/std/src/path.rs` fixes how many leading path bytes it spans.
//! Both were read in the installed `nightly-2026-09-22` standard library source.
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! // const records = scanNormalized(normalize(path, "windows"), countPrefixParts(nativePrefix), rules);
//! // expect(records.display).toBe(expectedDisplay);
//! ```

/// Import the private canonical scan and the structured record it returns.
use super::{scan_normalized_records, PathScanRecords};
/// Import the production Windows normalization and prefix counting rather than a test-local copy.
use crate::path_name_bytes::{count_prefix_parts, normalize_bytes};
/// Import loaded matcher sets,
///  the type the scan borrows.
use crate::frx_load::LoadedRules;
/// Import the structured finding compared by every assertion.
use crate::ScanFinding;

/// What:
///  One native Windows prefix with a forbidden-name candidate and a clean control under the same prefix.
/// Why:
///  Every prefix form needs both a positive and a negative observation from one shared assertion.
///
/// `&'static [u8]` is a borrowed byte slice baked into the test binary.
/// Siblings are `Vec<u8>` (owned,
///  growable) and `[u8; N]` (fixed length);
/// fixture literals never change,
///  so a borrowed static slice needs no allocation.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type PrefixForm = { prefix: Uint8Array; hit: Uint8Array; hitDisplay: string; clean: Uint8Array; cleanDisplay: string };
/// ```
struct PrefixForm {
    /// Raw native prefix bytes that Windows's parser returns for both pathnames.
    prefix: &'static [u8],
    /// Native pathname whose first name after the prefix is forbidden.
    hit: &'static [u8],
    /// Expected display with that first name masked.
    hit_display: &'static str,
    /// Native pathname under the same prefix whose names are all clean.
    clean: &'static [u8],
    /// Expected display of the clean control,
    ///  with nothing masked.
    clean_display: &'static str,
}

/// What:
///  Load the single forbidden fixture name used by every form.
/// Why:
///  Rules compile in memory,
///  so no user-owned cache is read or written.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function loadRules(): LoadedRules { return compileRules("VAULTTOKEN_LONG\n"); }
/// ```
fn load_rules() -> LoadedRules {
    return crate::frx_load::test_rules("VAULTTOKEN_LONG\n");
}

/// What:
///  Scan one native Windows pathname with its separately supplied native prefix bytes.
/// Why:
///  This is the production Windows composition,
///  `normalized_path` plus `prefix_parts`,
/// minus only the native `Component::Prefix` detection that Linux cannot run.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function scanWindowsForm(path: Uint8Array, prefix: Uint8Array, loaded: LoadedRules): PathScanRecords {
///   if (!startsWith(path, prefix)) throw new Error("fixture prefix must be a byte prefix of its path");
///   return scanNormalized(normalize(path, true), countPrefixParts(prefix), loaded);
/// }
/// ```
fn scan_windows_form(path: &[u8], prefix: &[u8], loaded: &LoadedRules) -> PathScanRecords {
    // A native prefix always spans the leading bytes of its path; reject a fixture that violates that.
    assert!(path.starts_with(prefix), "fixture prefix must be a byte prefix of its path");
    // `true` selects Windows separator semantics; the Vec owns the normalized copy.
    let normalized: Vec<u8> = normalize_bytes(path, true);
    // usize counts prefix parts, matching the scan parameter; u32 or u64 would need a cast.
    let parts: usize = count_prefix_parts(prefix);
    // `&normalized` lends the bytes to the scan without giving up ownership.
    return scan_normalized_records(&normalized, parts, loaded);
}

/// What:
///  The single expected finding for a forbidden first name after a prefix.
/// Why:
///  Component numbering starts at 1 after the prefix,
///  and the fixture rule is unnamed rule 0.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const firstName = [{ kind: "name", component: 1, rule: "0" }];
/// ```
fn first_name() -> Vec<ScanFinding> {
    // String::from copies the literal into the owned String the finding stores.
    return vec![ScanFinding::Name { component: 1, rule: String::from("0") }];
}

/// What:
///  Assert one form's positive candidate is reported and masked and its clean control stays visible.
/// Why:
///  Every prefix form shares the same contract,
///  so one assertion keeps the forms comparable.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// function assertPrefixForm(form: PrefixForm): void { ... }
/// ```
fn assert_prefix_form(form: &PrefixForm) {
    let loaded: LoadedRules = load_rules();
    let hit: PathScanRecords = scan_windows_form(form.hit, form.prefix, &loaded);
    assert_eq!(hit.display, form.hit_display);
    assert_eq!(hit.findings, first_name());
    let clean: PathScanRecords = scan_windows_form(form.clean, form.prefix, &loaded);
    assert_eq!(clean.display, form.clean_display);
    assert!(clean.findings.is_empty());
}

/// A drive prefix `C:` is one part;
///  the first directory after it is name 1.
#[test]
fn drive_prefix_form() {
    // br"..." is a raw byte-string literal: backslashes are literal bytes, never escapes.
    assert_prefix_form(&PrefixForm {
        prefix: br"C:",
        hit: br"C:\VAULTTOKEN_LONG\clean.txt",
        hit_display: r"C\x3a/[REDACTED]/clean.txt",
        clean: br"C:\clean\clean.txt",
        clean_display: r"C\x3a/clean/clean.txt",
    });
}

/// A drive-relative name directly after `C:` is still name 1.
#[test]
fn drive_relative_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"C:",
        hit: br"C:VAULTTOKEN_LONG.txt",
        hit_display: r"C\x3a/[REDACTED]",
        clean: br"C:clean.txt",
        clean_display: r"C\x3a/clean.txt",
    });
}

/// A backslash UNC prefix skips its server and share.
#[test]
fn unc_backslash_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\server\share",
        hit: br"\\server\share\VAULTTOKEN_LONG\clean.txt",
        hit_display: "//server/share/[REDACTED]/clean.txt",
        clean: br"\\server\share\clean\clean.txt",
        clean_display: "//server/share/clean/clean.txt",
    });
}

/// A forward-slash UNC prefix skips its server and share.
#[test]
fn unc_slash_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: b"//server/share",
        hit: b"//server/share/VAULTTOKEN_LONG/clean.txt",
        hit_display: "//server/share/[REDACTED]/clean.txt",
        clean: b"//server/share/clean/clean.txt",
        clean_display: "//server/share/clean/clean.txt",
    });
}

/// A verbatim drive prefix skips `?` and the drive.
#[test]
fn verbatim_drive_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?\C:",
        hit: br"\\?\C:\VAULTTOKEN_LONG\clean.txt",
        hit_display: r"//?/C\x3a/[REDACTED]/clean.txt",
        clean: br"\\?\C:\clean\clean.txt",
        clean_display: r"//?/C\x3a/clean/clean.txt",
    });
}

/// A verbatim UNC prefix skips `?`,
///  `UNC`,
///  server and share.
#[test]
fn verbatim_unc_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?\UNC\server\share",
        hit: br"\\?\UNC\server\share\VAULTTOKEN_LONG\clean.txt",
        hit_display: "//?/UNC/server/share/[REDACTED]/clean.txt",
        clean: br"\\?\UNC\server\share\clean\clean.txt",
        clean_display: "//?/UNC/server/share/clean/clean.txt",
    });
}

/// A verbatim named prefix skips `?` and the name.
#[test]
fn verbatim_name_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?\name",
        hit: br"\\?\name\VAULTTOKEN_LONG\clean.txt",
        hit_display: "//?/name/[REDACTED]/clean.txt",
        clean: br"\\?\name\clean\clean.txt",
        clean_display: "//?/name/clean/clean.txt",
    });
}

/// A device-namespace port prefix skips its `.` marker and device name,
///  and nothing after them.
#[test]
fn device_namespace_port_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\COM1",
        hit: br"\\.\COM1\VAULTTOKEN_LONG\clean.txt",
        hit_display: "//./COM1/[REDACTED]/clean.txt",
        clean: br"\\.\COM1\clean\clean.txt",
        clean_display: "//./COM1/clean/clean.txt",
    });
}

/// A device-namespace volume prefix skips its `.` marker and drive,
///  and nothing after them.
#[test]
fn device_namespace_drive_prefix_form() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\C:",
        hit: br"\\.\C:\VAULTTOKEN_LONG\clean.txt",
        hit_display: r"//./C\x3a/[REDACTED]/clean.txt",
        clean: br"\\.\C:\clean\clean.txt",
        clean_display: r"//./C\x3a/clean/clean.txt",
    });
}

/// A pipe name containing dots follows the `\\.\pipe` prefix and is name 1.
#[test]
fn device_namespace_pipe_name_with_dots() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\pipe",
        hit: br"\\.\pipe\VAULTTOKEN_LONG.with.dots",
        hit_display: "//./pipe/[REDACTED]",
        clean: br"\\.\pipe\name.with.dots",
        clean_display: "//./pipe/name.with.dots",
    });
}

/// A device name that itself contains dots is one prefix part.
#[test]
fn device_name_containing_dots() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\device.with.dots",
        hit: br"\\.\device.with.dots\VAULTTOKEN_LONG",
        hit_display: "//./device.with.dots/[REDACTED]",
        clean: br"\\.\device.with.dots\clean.txt",
        clean_display: "//./device.with.dots/clean.txt",
    });
}

/// Navigation markers after a prefix are neither names nor prefix parts.
#[test]
fn navigation_markers_after_prefix_are_not_names() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\COM1",
        hit: br"\\.\COM1\.\..\VAULTTOKEN_LONG\clean.txt",
        hit_display: "//./COM1/./../[REDACTED]/clean.txt",
        clean: br"\\.\COM1\.\..\clean.txt",
        clean_display: "//./COM1/./../clean.txt",
    });
    assert_prefix_form(&PrefixForm {
        prefix: br"\\server\share",
        hit: br"\\server\share\..\.\VAULTTOKEN_LONG",
        hit_display: "//server/share/.././[REDACTED]",
        clean: br"\\server\share\..\.\clean.txt",
        clean_display: "//server/share/.././clean.txt",
    });
}

/// Empty components after a prefix are neither names nor prefix parts.
#[test]
fn empty_components_after_prefix_are_not_names() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\COM1",
        hit: br"\\.\COM1\\\VAULTTOKEN_LONG\\clean.txt",
        hit_display: "//./COM1///[REDACTED]//clean.txt",
        clean: br"\\.\COM1\\\clean.txt",
        clean_display: "//./COM1///clean.txt",
    });
}

/// Mixed separators inside and after a prefix keep its boundary.
#[test]
fn mixed_separators_keep_prefix_boundary() {
    assert_prefix_form(&PrefixForm {
        prefix: br"//.\COM1",
        hit: br"//.\COM1/VAULTTOKEN_LONG\clean.txt",
        hit_display: "//./COM1/[REDACTED]/clean.txt",
        clean: br"//.\COM1/clean\clean.txt",
        clean_display: "//./COM1/clean/clean.txt",
    });
    // A slash inside `\\?/` cancels verbatim parsing, so std parses a UNC server `?` and share `C:`.
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?/C:",
        hit: br"\\?/C:\VAULTTOKEN_LONG",
        hit_display: r"//?/C\x3a/[REDACTED]",
        clean: br"\\?/C:\clean.txt",
        clean_display: r"//?/C\x3a/clean.txt",
    });
}

/// Non-UTF-8 native bytes after a prefix are matched and masked as a whole name.
#[test]
fn non_utf8_name_after_prefix() {
    // `\xed\xa0\x80` is the WTF-8 spelling of an unpaired surrogate, which a Windows OsStr can hold.
    assert_prefix_form(&PrefixForm {
        prefix: b"\\\\.\\COM1",
        hit: b"\\\\.\\COM1\\\xed\xa0\x80VAULTTOKEN_LONG\\clean.txt",
        hit_display: "//./COM1/[REDACTED]/clean.txt",
        clean: b"\\\\.\\COM1\\\xed\xa0\x80\\clean.txt",
        clean_display: r"//./COM1/\xed\xa0\x80/clean.txt",
    });
}

/// A device name spelled `..` (`DeviceNS("..")`) is consumed as a prefix part.
#[test]
fn device_name_spelled_parent_marker_is_prefix() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\..",
        hit: br"\\.\..\VAULTTOKEN_LONG",
        hit_display: "//./../[REDACTED]",
        clean: br"\\.\..\clean.txt",
        clean_display: "//./../clean.txt",
    });
}

/// An empty device name (`DeviceNS("")`) leaves the four-byte prefix `\\.\` with the single part `.`.
#[test]
fn empty_device_name_prefix_is_one_part() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\.\",
        hit: br"\\.\\VAULTTOKEN_LONG",
        hit_display: "//.//[REDACTED]",
        clean: br"\\.\\clean.txt",
        clean_display: "//.//clean.txt",
    });
}

/// A UNC share spelled `..` (`UNC("server", "..")`) is consumed as a prefix part.
#[test]
fn unc_share_spelled_parent_marker_is_prefix() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\server\..",
        hit: br"\\server\..\VAULTTOKEN_LONG",
        hit_display: "//server/../[REDACTED]",
        clean: br"\\server\..\clean.txt",
        clean_display: "//server/../clean.txt",
    });
}

/// A verbatim prefix containing `/./` (`Verbatim("a/./b")`) counts the `.` run as a part,
///  because verbatim parsing
/// splits only at backslashes while the scan splits at both separators.
#[test]
fn verbatim_prefix_with_current_marker_run_is_prefix() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?\a/./b",
        hit: br"\\?\a/./b\VAULTTOKEN_LONG",
        hit_display: "//?/a/./b/[REDACTED]",
        clean: br"\\?\a/./b\clean.txt",
        clean_display: "//?/a/./b/clean.txt",
    });
}

/// A verbatim UNC server spelled `..` (`VerbatimUNC("..", "share")`) is consumed as a prefix part.
#[test]
fn verbatim_unc_server_spelled_parent_marker_is_prefix() {
    assert_prefix_form(&PrefixForm {
        prefix: br"\\?\UNC\..\share",
        hit: br"\\?\UNC\..\share\VAULTTOKEN_LONG",
        hit_display: "//?/UNC/../share/[REDACTED]",
        clean: br"\\?\UNC\..\share\clean.txt",
        clean_display: "//?/UNC/../share/clean.txt",
    });
}

/// A prefix with no following component yields no name and an unmasked display.
#[test]
fn prefix_without_following_component() {
    let loaded: LoadedRules = load_rules();
    // Each pair is a whole native path and the prefix std returns for it.
    let cases: [(&[u8], &[u8], &str); 5] = [
        (br"\\.\COM1", br"\\.\COM1", "//./COM1"),
        (br"\\.\COM1\", br"\\.\COM1", "//./COM1/"),
        (br"\\?\UNC\server", br"\\?\UNC\server", "//?/UNC/server"),
        (br"\\server\share", br"\\server\share", "//server/share"),
        (br"C:", br"C:", r"C\x3a"),
    ];
    // `&cases` lends the fixed array to the loop; each item is a borrowed tuple.
    for (path, prefix, display) in &cases {
        let records: PathScanRecords = scan_windows_form(path, prefix, &loaded);
        assert_eq!(records.display, *display);
        assert!(records.findings.is_empty());
    }
}