Skip to content

Avoid panic in parse_legacy_color on whitespace-only input - #431

Open
nicoburns wants to merge 1 commit into
servo:mainfrom
DioxusLabs:devin/1785182052-fix-legacy-color-panic
Open

Avoid panic in parse_legacy_color on whitespace-only input#431
nicoburns wants to merge 1 commit into
servo:mainfrom
DioxusLabs:devin/1785182052-fix-legacy-color-panic

Conversation

@nicoburns

Copy link
Copy Markdown
Collaborator

Bug

parse_legacy_color only checked emptiness before stripping HTML whitespace, so a value like <body bgcolor=" "> reached step 9's input.as_bytes()[0] with an empty string and panicked with an index-out-of-bounds, a crash triggerable from untrusted HTML.

Changes made

Two changes made in style/servo/attr.rs:

  1. Check for empty input again after stripping whitespace.
  2. Use the built-in stdlib strip_prefixes function which is tolerant of empty input. Technically not necessarily given fix, but it's a nice readability improvement.

This essentially fixes the bug twice, but I think a little defence in depth is nice here


AI declaration: this bug was found by an AI-security scan, and fix included here was proposed by the AI.
(it has been verified to be a genuine bug and sensible fix by me).

@mrobinson

Copy link
Copy Markdown
Member

It would be better to submit this upstream in Gecko, I think. As far as I know this is shared code.

@nicoburns

Copy link
Copy Markdown
Collaborator Author

It's in a dir called "servo", so I think it's probably not shared. But I can probably submit upstream anyway.

@mrobinson

Copy link
Copy Markdown
Member

It's in a dir called "servo", so I think it's probably not shared. But I can probably submit upstream anyway.

Ah, sorry. I missed that. It's not shared, so submitting it here makes sense.

@mrobinson

Copy link
Copy Markdown
Member

@nicoburns

  1. Is there a test for this change?
  2. I don't think you need an AI Declaration for this change. If you wrote it yourself, it doesn't matter if a LLM identified the original bug: https://book.servo.org/contributing/getting-started.html#ai-policy-faq

@nicoburns
nicoburns force-pushed the devin/1785182052-fix-legacy-color-panic branch from e665fab to e03f460 Compare July 28, 2026 23:28
@nicoburns

Copy link
Copy Markdown
Collaborator Author

I have added a test, although cargo test does not compile with Stylo main for other reasons. I have not included fixes for that as it seemed out of scope (but I did locally patch so I could run the included test).

Comment thread style/servo/attr.rs

// Step 3.
input = input.trim_matches(HTML_SPACE_CHARACTERS);
if input.is_empty() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Comment thread style/servo/attr.rs
Comment on lines +719 to +726
#[cfg(test)]
mod test {
use crate::attr::parse_legacy_color;
#[test]
fn parsing_whitespace_only_color_does_not_panic() {
assert!(parse_legacy_color(" ").is_err());
}
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This isn't checked, so please make sure there is WPT coverage

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.

3 participants