Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb/CSS: Block shadow host matching in DOM #2552

Merged

Conversation

ycarmon
Copy link
Contributor

@ycarmon ycarmon commented Nov 24, 2024

Fixes an issue where selectors inside a shadow root could incorrectly match their shadow host directly using selectors like #host, instead of requiring :host pseudo-class selectors.

Fixes issue #2319

Screenshot 2024-11-24 at 9 05 04 PM

@ycarmon ycarmon requested a review from AtkinsSJ as a code owner November 24, 2024 19:17
@@ -867,6 +867,11 @@ static bool fast_matches_simple_selector(CSS::Selector::SimpleSelector const& si
static bool fast_matches_compound_selector(CSS::Selector::CompoundSelector const& compound_selector, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, DOM::Element const& element, GC::Ptr<DOM::Element const> shadow_host)
Copy link
Member

Choose a reason for hiding this comment

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

This commit only touches the non-recursive fast_matches code path. What about the recursive matches path?

See can_use_fast_matches for how we decide whether a selector is fast-matchable :)

Also, please add a test!

@ycarmon ycarmon force-pushed the prevent-shadow-host-direct-selectors branch 4 times, most recently from 19a0df3 to 8cbbaac Compare November 29, 2024 13:30
@ycarmon ycarmon marked this pull request as draft November 30, 2024 13:04
@ycarmon ycarmon force-pushed the prevent-shadow-host-direct-selectors branch from 8cbbaac to 8cf83eb Compare November 30, 2024 13:30
@ycarmon ycarmon marked this pull request as ready for review November 30, 2024 13:30
@ycarmon ycarmon force-pushed the prevent-shadow-host-direct-selectors branch from 8cf83eb to d6cb319 Compare December 1, 2024 11:01
Comment on lines 758 to 760
if (shadow_host && &element == shadow_host.ptr() && component.type != CSS::Selector::SimpleSelector::Type::PseudoClass && component.type != CSS::Selector::SimpleSelector::Type::PseudoElement) {
return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

It'd be nice to move this check into its own static function, to save having to remember to modify both places.

Also... I think this doesn't block enough. It'll allow other kinds of pseudo-class, such as :first-child.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated it to allow only host and compound pseudo classes

@ycarmon ycarmon force-pushed the prevent-shadow-host-direct-selectors branch 2 times, most recently from 95be6cc to 4cf8374 Compare December 3, 2024 21:27
Fixes an issue where selectors inside a shadow root could incorrectly
match their shadow host directly using selectors like #host,
instead of requiring :host pseudo-class selectors.

Fixes issue LadybirdBrowser#2319
Add a test that verifies selectors inside a shadow
root can only match their host element through :host pseudo-class.
Tests both simple selectors (#id, .class)
and complex selectors (:not, :where) to ensure they are blocked from
matching the host element directly.

Fixes issue LadybirdBrowser#2319
@ycarmon ycarmon force-pushed the prevent-shadow-host-direct-selectors branch from 4cf8374 to a7a71ca Compare December 4, 2024 10:38
Copy link
Member

@AtkinsSJ AtkinsSJ left a comment

Choose a reason for hiding this comment

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

I think this is good, thanks for working on it! If there are any edge cases, we can fix them when we find them.

@AtkinsSJ AtkinsSJ merged commit be979a1 into LadybirdBrowser:master Dec 4, 2024
6 checks passed
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