Skip to content

Make Parser::slice and slice_from sound - #464

Closed
samiarif02 wants to merge 1 commit into
servo:mainfrom
samiarif02:fix/parser-slice-soundness
Closed

samiarif02 wants to merge 1 commit into
servo:mainfrom
samiarif02:fix/parser-slice-soundness

Conversation

@samiarif02

@samiarif02 samiarif02 commented Nov 23, 2026 •

Copy link
Copy Markdown

Fixes #459.

Parser::slice and Parser::slice_from are safe, public methods, but they called self.input.get_unchecked(range.start.0..range.end.0) with a caller-supplied range. Safe code could therefore trigger undefined behavior:

  • passing an inverted range p1..p0 (both is_char_boundary debug-asserts still pass, and end - start underflows in get_unchecked);
  • rewinding with reset and then calling slice_from with a later position;
  • passing a SourcePosition from a parser over a longer input (the debug_assert!s are compiled out in release).

Fix: the public slice/slice_from now use checked string indexing (&self.input[..]). To keep @emilio's point about not adding codepoint-boundary overhead on the hot tokenizer paths, the unchecked implementation is retained as pub(crate) slice_unchecked / slice_from_unchecked, and the internal callers (which always pass valid, ordered positions for the current input) use those.

Tests: parser_slice_valid_range checks a valid range still returns the expected substring; parser_slice_inverted_range_panics_instead_of_ub checks an inverted range now panics (safe) instead of invoking UB. Full lib suite: 48 passed.

Note on repro case 2 (reset + slice_from): I left it out of the test because it needs a bit more setup to construct safely, but the checked indexing covers it the same way — any out-of-order or out-of-range position now panics instead of being UB. Happy to adjust the internal/public split if you'd prefer a different boundary.

🤖 Generated with Claude Code

`Parser::slice` and `Parser::slice_from` are safe, public methods but
called `get_unchecked` on the input with a caller-supplied range. Safe
code could trigger undefined behavior, e.g. by passing an inverted range
`p1..p0`, by reusing a `SourcePosition` after `reset`, or by passing a
position from a parser over a longer input (the `debug_assert!`s are
gone in release builds).

Make the public `slice`/`slice_from` use checked string indexing. Keep
the unchecked path for the hot internal tokenizer callers as
`pub(crate)` `slice_unchecked`/`slice_from_unchecked`, so there is no
bounds/char-boundary overhead where positions are known to be valid.

Adds regression tests: a valid range returns the expected substring, and
an inverted range now panics instead of invoking UB.

Fixes servo#459

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FwN6idBD37mgcgcdANVSjb

@emilio emilio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also use these APIs in hot paths in Gecko, but hopefully not hot enough for the bounds-checks to be an issue.

Comment thread src/tests.rs
Comment on lines +1388 to +1400
/// https://github.com/servo/rust-cssparser/issues/459
/// `Parser::slice` is a safe, public method, so it must not be able to trigger
/// undefined behavior. A valid range returns the expected substring.
#[test]
fn parser_slice_valid_range() {
let mut parser = Parser::new("abc def");
let start = parser.position();
parser.next().unwrap(); // consume `abc`
let end = parser.position();
assert_eq!(parser.slice(start..end), "abc");
// `slice_from` goes from the given position to the current one.
assert_eq!(parser.slice_from(start), "abc");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this test is useful?

Comment thread src/tests.rs
Comment on lines +1403 to +1406
/// An inverted range used to reach `get_unchecked` and was undefined behavior
/// from safe code. It now goes through checked string indexing, so it panics
/// instead of invoking UB.
#[test]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "used to" part of the comment also seems useless.

@emilio

emilio commented Nov 23, 2026

Copy link
Copy Markdown
Member

Not sure what the AI policy is for this crate, btw.

@emilio

emilio commented Nov 23, 2026

Copy link
Copy Markdown
Member

@SimonSapin @jdm do you know? ^

@SimonSapin

Copy link
Copy Markdown
Member

@emilio There’s an argument that being under https://github.com/servo/ means Servo’s policy applies if nothing more specific says otherwise. But really I think that’s up to us (specifically you and me) to decide. I would like to explicitly adopt Servo’s policy (and while we’re at it, Code of Conduct). What do you think?

@emilio

emilio commented Nov 23, 2026

Copy link
Copy Markdown
Member

I'm totally cool with the code of conduct of course.

In terms of AI policy, I strongly agree with communications having to be human-written. I'd personally be ok with AI-generated code as long as the author is accountable for it / understands it (and defends it as needed with human words)...

That said, I wouldn't object to plain banning AI-generated code I guess, it is a bit different from what we do in Firefox, but...

@SimonSapin

Copy link
Copy Markdown
Member

Alright, let’s make it explicit: #465

@samiarif02 I’m closing this PR according to the policy at https://book.servo.org/policy/ai-usage.html. If you’re interested feel free to write a fix yourself and submit another PR.

@SimonSapin SimonSapin closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Parser::slice and Parser::slice_from can trigger UB in safe Rust via unchecked slicing

3 participants