fix: Drop invalid UTF-8 scanner candidates
Summary
Replace the three unchecked scanner conversions in crates/oxide/src/scanner/mod.rs with checked conversions that omit only extracted slices that are not valid UTF-8. Apply the check in the shared extract pipeline so initial scans, incremental scan_content calls, and CSS-variable extraction cannot insert invalid strings into scanner state, and apply the same policy to both branches of get_candidates_with_positions while preserving byte offsets and the legacy -[] restoration. Keep the extractor's byte-oriented CSS identifier classification unchanged: accepting non-ASCII bytes during extraction is useful for valid multibyte code points, while the conversion boundary is the authoritative place to enforce the String contract.
The scanner currently converts extracted byte slices with unchecked UTF-8 constructors at the shared extraction boundary and both candidate-with-position branches. A source file containing a stray continuation byte can therefore produce an invalid String, violating Rust's string invariant and allowing corrupted candidates to persist in a long-lived scanner. The thread provides a deterministic reproduction using invalid bytes inside an arbitrary value, so the problem no longer depends on reproducing the originally reported Turbopack race. Valid candidates found alongside malformed byte sequences must continue to be returned normally.
Fixes #20368
Test plan
Not applicable to this change.
AI was used for assistance.
No actionable comments were generated in the recent review. ๐
โน๏ธ Recent review info
โ๏ธ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: da73a604-d95b-4fb9-bf95-8a38160f002c
๐ฅ Commits
Reviewing files that changed from the base of the PR and between 517f8ce89160c4ae0383d419cab46524b444d06d and a13b7789dc3432aef65233ba82ef6b228ce9b7a9.
๐ Files selected for processing (2)
CHANGELOG.mdcrates/oxide/tests/scanner.rs
๐ง Files skipped from review as they are similar to previous changes (1)
- crates/oxide/tests/scanner.rs
Walkthrough
The scanner replaced unsafe UTF-8 conversions with checked conversions. Invalid UTF-8 candidates are discarded during full, incremental, and generic extraction. Tests cover invalid candidate filtering, valid and Unicode candidate preservation, position tracking, and CSS variable extraction with invalid UTF-8 input.
Mergeability Score: โช Minimal ยท up to a13b7
This change drops invalid UTF-8 scanner candidates while preserving valid candidates and offsets; no actionable merge-blocking risk remains after normal checks and review.
๐ฅ Pre-merge checks | โ 4
โ Passed checks (4 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | โ Passed | The title clearly summarizes the main change: dropping invalid UTF-8 scanner candidates. |
| Description check | โ Passed | The description directly explains the UTF-8 validation change, affected scanner paths, preserved behavior, and testing scope. |
| Linked Issues check | โ Passed | The changes address issue #20368 by preventing invalid UTF-8 candidates from entering scanner state while preserving valid candidates and offsets. |
| Out of Scope Changes check | โ Passed | The implementation, focused scanner tests, invalid UTF-8 fixture, and changelog entry are all within the linked issue scope. |
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
Confidence Score: 5/5
The PR appears safe to merge.
No blocking failure remains.
Reviews (2): Last reviewed commit: "update changelog" | Re-trigger Greptile
Saw this was created after my fix comment #20368 on the issue so I ran the repro harness against this branch to check it closes what I found there.
On main 4be6110a the four invalid-byte cases fail as before, lone 0x80, 0xFE, 0xFF, and a truncated E2 80 all come back as ill formed strings. On this branch 517f8ce8 all three pass and the valid-ASCII control still returns its candidates and the full oxide scanner suite is green with 67 passed so this verifabily fixes the soundness issue including the deterministic repro.
Two things worth flagging while you're here though:
-
The fuzzer still can't catch a regression of this.
fuzz_targets/parsing.rsgates onif let Ok(s) = std::str::from_utf8(data)meaning only the&strpath is ever fuzzed so the raw-byte path thatfs::readfeeds that this PR fixes never gets exercised. Feeding the fuzz target raw bytes would keep this class of bug from coming back silently. -
The other thing is dropped candidates now disappear with no diagnostic and since this isn't limited to the Turbopack race it means any Latin-1/Windows-1252 source file triggers it and a user with a legacy encoded file will just see utilities silently not generating so a
tracingWARN event at the conveersation boundary would make that debuggable.