tailwindcss icon indicating copy to clipboard operation
tailwindcss copied to clipboard

fix: Drop invalid UTF-8 scanner candidates

Open mvanhorn opened this issue 3 months ago โ€ข 3 comments

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.

mvanhorn avatar Aug 05 '26 21:08 mvanhorn

Review Change Stack

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.md
  • crates/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.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Aug 05 '26 21:08 coderabbitai[bot]

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Reviews (2): Last reviewed commit: "update changelog" | Re-trigger Greptile

greptile-apps avatar Aug 05 '26 21:08 greptile-apps

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:

  1. The fuzzer still can't catch a regression of this. fuzz_targets/parsing.rs gates on if let Ok(s) = std::str::from_utf8(data) meaning only the &str path is ever fuzzed so the raw-byte path that fs::read feeds that this PR fixes never gets exercised. Feeding the fuzz target raw bytes would keep this class of bug from coming back silently.

  2. 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 tracing WARN event at the conveersation boundary would make that debuggable.

thejamesgore avatar Aug 05 '26 22:08 thejamesgore