Skip to content

Fix masking overlap leak (#15) and large-file read-before-check (#16), expand edge-case tests - #17

Merged
kimdzhekhon merged 3 commits into
mainfrom
fix/mask-overlap-leak-and-large-file-read
Aug 10, 2026
Merged

Fix masking overlap leak (#15) and large-file read-before-check (#16), expand edge-case tests#17
kimdzhekhon merged 3 commits into
mainfrom
fix/mask-overlap-leak-and-large-file-read

Conversation

@kimdzhekhon

Copy link
Copy Markdown
Contributor

Summary

Adversarial re-audit of the masking logic (mask()/mask_line_all()/fingerprint()), resource handling, and path handling, done as a follow-up to the earlier masking-leak fixes (#2, #5).

  • Fixes mask_line_all() leaks nearly all of a secret when two matches partially overlap without either containing the other #15: mask_line_all() could leave a secret almost entirely in plaintext when it partially overlapped another matched secret on the same line without either containing the other (confirmed with a direct repro before fixing). Rewritten to a byte-range/merge approach that can't leave any covered character unmasked, regardless of overlap shape.
  • Fixes builtin_scan() reads a file's full contents into memory before checking the 5MB size cap #16: builtin_scan() read a file's full contents into memory before checking the 5MB size cap, defeating the cap's purpose for very large files. Now checks entry.metadata().len() first and skips oversized files without reading them.
  • Expands regression coverage: empty dir/file, non-UTF8 content, a 150-level-deep directory tree, a secret split across two files, a secret wrapped across two lines (characterizes a known line-by-line detection limit — not a leak), a symlinked file (confirmed never followed), and a directory symlink cycle (confirmed the scan terminates instead of hanging).

Also checked and found not vulnerable (no fix needed): catastrophic regex backtracking — the regex crate guarantees linear-time matching, no PCRE-style backtracking engine; path traversal via scanner-reported paths — raw secret values and scanner-reported file paths never reach any filesystem write call, output paths are only ever the CLI --out arg.

Test plan

  • cargo build
  • cargo test --all-targets (24/24 passing)
  • cargo clippy --all-targets (0 new warnings vs. main)
  • cargo fmt --all -- --check

kimdzhekhon and others added 3 commits August 10, 2026 15:31
The previous approach sorted raw values longest-first and repeatedly did
result.contains(raw) -> result.replace(raw, mask(raw)) against a mutating
string. When two matched secrets on the same line partially overlapped
without either containing the other, masking the longer one first
consumed characters shared with the shorter one, so the shorter one's
exact text no longer existed in `result` and its .contains() check
silently skipped masking it -- leaving most of it in plaintext.

Rewritten to find every occurrence of every raw value as a byte range in
the original line, merge overlapping/adjacent ranges, and rebuild the
line in one pass masking each merged range as a block. This can't leave
a fragment of any value unmasked regardless of how matches overlap.

Fixes #15

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
std::fs::read() loaded a file's full contents into memory before the
5MB cap was checked against bytes.len(), so the cap never actually
bounded memory use -- a stray multi-GB file anywhere under the scan
target would be read in full regardless. Check entry.metadata().len()
first and skip oversized files without reading them at all.

Fixes #16

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…nks, deep trees, boundary-spanning secrets

Expands builtin_scan/mask coverage beyond the #15/#16 fixes:
- empty target dir, empty file
- non-UTF8 (corrupt) file content
- a directory tree 150 levels deep
- a secret split across two separate files (must not be merged)
- a secret wrapped across two lines in one file (characterizes the
  known line-by-line detection limit -- not a leak, just non-detection)
- a symlinked file is never followed/read (WalkDir's default), and a
  directory symlink cycle back to an ancestor does not hang the scan

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@kimdzhekhon
kimdzhekhon merged commit c263768 into main Aug 10, 2026
1 check passed
@kimdzhekhon
kimdzhekhon deleted the fix/mask-overlap-leak-and-large-file-read branch August 10, 2026 06:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant