Skip to content

Lightpanda: the headless browser designed for AI and automation

css: cap nested selector growth, absolutize combinator-led selectors

Navid EMAD045abd9b7701
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 5edca978e..3e497bc62 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -142,23 +142,24 @@ fn parseSheet(self: *StyleManager, build_arena: Allocator, sheet: *CSSStyleSheet
while (it.next()) |parsed_rule| {
switch (parsed_rule) {
.style => |s| try self.addRawRule(build_arena, s.selector, s.block, 0, NO_LAYER),
- .at_rule => |a| {
- // Only `@media` and `@layer` participate in the cascade
- // here. Other at-rules (`@keyframes`, `@supports`,
- // `@font-face`, …) don't carry top-level declarations
- // relevant to the visibility filter and stay skipped as
- // before.
- if (std.ascii.eqlIgnoreCase(a.keyword, "media")) {
- try self.applyMediaAtRule(build_arena, a.text, 0, NO_LAYER, null);
- } else if (std.ascii.eqlIgnoreCase(a.keyword, "layer")) {
- try self.applyLayerAtRule(build_arena, a.text, 0, NO_LAYER, null);
- }
- },
+ .at_rule => |a| try self.applyAtRule(build_arena, a.keyword, a.text, 0, NO_LAYER, null),
}
}
}
}
+/// Apply an at-rule that takes part in the cascade. Only `@media` and `@layer`
+/// do here. Other at-rules (`@keyframes`, `@supports`, `@font-face`, …) don't
+/// carry declarations relevant to the visibility filter and are skipped.
+fn applyAtRule(self: *StyleManager, build_arena: Allocator, keyword: []const u8, text: []const u8, depth: u8, layer: u16, nest: ?[]const u8) Allocator.Error!void {
+ if (std.ascii.eqlIgnoreCase(keyword, "media")) {
+ return self.applyMediaAtRule(build_arena, text, depth, layer, nest);
+ }
+ if (std.ascii.eqlIgnoreCase(keyword, "layer")) {
+ return self.applyLayerAtRule(build_arena, text, depth, layer, nest);
+ }
+}
+
/// Apply an `@media` at-rule by evaluating its query against the current
/// viewport and, if it matches, parsing the inner block as if its declarations
/// lived at the top level. Non-matching queries silently drop the inner
@@ -254,13 +255,7 @@ fn applyInnerRules(self: *StyleManager, build_arena: Allocator, inner: []const u
while (it.next()) |nested_rule| {
switch (nested_rule) {
.style => |s| try self.addRawRule(build_arena, s.selector, s.block, depth, layer),
- .at_rule => |nested| {
- if (std.ascii.eqlIgnoreCase(nested.keyword, "media")) {
- try self.applyMediaAtRule(build_arena, nested.text, depth, layer, null);
- } else if (std.ascii.eqlIgnoreCase(nested.keyword, "layer")) {
- try self.applyLayerAtRule(build_arena, nested.text, depth, layer, null);
- }
- },
+ .at_rule => |nested| try self.applyAtRule(build_arena, nested.keyword, nested.text, depth, layer, null),
}
}
}
@@ -508,16 +503,10 @@ fn addRawRule(self: *StyleManager, build_arena: Allocator, selector_text: []cons
.rule => |nested| {
// The arena, not build_arena: a custom property's rule keeps
// its selector text past the rebuild.
- const selector = try CssParser.resolveNestedSelector(self.arena.allocator(), selector_text, nested.selector);
+ const selector = try CssParser.resolveNestedSelector(self.arena.allocator(), selector_text, nested.selector) orelse continue;
try self.addRawRule(build_arena, selector, nested.block, depth + 1, layer);
},
- .at_rule => |nested| {
- if (std.ascii.eqlIgnoreCase(nested.keyword, "media")) {
- try self.applyMediaAtRule(build_arena, nested.text, depth + 1, layer, selector_text);
- } else if (std.ascii.eqlIgnoreCase(nested.keyword, "layer")) {
- try self.applyLayerAtRule(build_arena, nested.text, depth + 1, layer, selector_text);
- }
- },
+ .at_rule => |nested| try self.applyAtRule(build_arena, nested.keyword, nested.text, depth + 1, layer, selector_text),
}
}
}
@@ -1311,7 +1300,7 @@ fn addNestedCssomRules(self: *StyleManager, build_arena: Allocator, parent: *CSS
switch (rule._type) {
.style => |sr| {
// The arena, as in addRawRule.
- const selector = try CssParser.resolveNestedSelector(self.arena.allocator(), parent_selector, sr._selector_text);
+ const selector = try CssParser.resolveNestedSelector(self.arena.allocator(), parent_selector, sr._selector_text) orelse continue;
_ = try self.addRuleAs(sr, selector);
try self.addNestedCssomRules(build_arena, sr, selector, depth + 1);
},
diff --git a/src/browser/css/Parser.zig b/src/browser/css/Parser.zig
index fc024bf41..1cacff9ba 100644
--- a/src/browser/css/Parser.zig
+++ b/src/browser/css/Parser.zig
@@ -566,30 +566,47 @@ fn consumeNestedRule(stream: *TokenStream, input: []const u8) ?StyleRule {
return null;
}
+/// Upper bound on a resolved nested selector, in bytes. Each `&` copies the
+/// whole parent, so `& &` nested N deep doubles the text N times: a hostile
+/// stylesheet would reach gigabytes well within MAX_AT_RULE_NESTING. A rule
+/// whose selector would outgrow this is dropped instead.
+pub const MAX_NESTED_SELECTOR_LEN: usize = 64 * 1024;
+
/// Resolve the selector list of a style rule nested in `parent` (CSS Nesting
-/// §3): each `&` becomes `:is(<parent>)`, and a selector without one is
-/// relative to the parent, as if it started with `& `. Using `:is()` keeps
-/// the spec's specificity (the most specific parent selector) and lets a
-/// parent selector list stand in for a single `&`. Selectors are split on
-/// the commas outside parentheses and brackets; tokens are copied verbatim.
-pub fn resolveNestedSelector(allocator: std.mem.Allocator, parent: []const u8, nested: []const u8) ![]const u8 {
+/// §3): each `&` becomes `:is(<parent>)`, and a relative selector (one that
+/// starts with a combinator, or has no `&`) is prefixed with `:is(<parent>) `.
+/// Using `:is()` keeps the spec's specificity (the most specific parent
+/// selector) and lets a parent selector list stand in for a single `&`.
+/// Selectors are split on the commas outside parentheses and brackets; tokens
+/// are copied verbatim. Returns null when the result would exceed
+/// MAX_NESTED_SELECTOR_LEN.
+pub fn resolveNestedSelector(allocator: std.mem.Allocator, parent: []const u8, nested: []const u8) !?[]const u8 {
var out: std.ArrayList(u8) = .empty;
errdefer out.deinit(allocator);
var stream = TokenStream.init(nested);
- // Start of the current complex selector in `out`, and whether it holds
- // an `&` yet. Each selector is written as it streams by, and the parent is
- // inserted at `selector_start` when it turns out to have no `&`.
+ // Start of the current complex selector in `out`, whether it holds an `&`
+ // yet, and whether it starts with a combinator. Each selector is written
+ // as it streams by, and the parent is inserted at `selector_start` when it
+ // turns out to be relative.
var selector_start: usize = 0;
var has_nesting = false;
+ var starts_with_combinator = false;
var pending_space = false;
+ // A comment with no whitespace on either side separates tokens without
+ // being a descendant combinator: it is kept rather than turned into one.
+ var pending_comment: ?[]const u8 = null;
var depth: usize = 0;
while (true) {
+ if (out.items.len > MAX_NESTED_SELECTOR_LEN) {
+ out.deinit(allocator);
+ return null;
+ }
const span = stream.next();
const at_comma = if (span) |sp| depth == 0 and sp.token == .comma else true;
if (at_comma) {
- if (out.items.len > selector_start and !has_nesting) {
+ if (out.items.len > selector_start and (starts_with_combinator or !has_nesting)) {
// Inserted back to front, to read `:is(<parent>) <selector>`.
try out.insertSlice(allocator, selector_start, ") ");
try out.insertSlice(allocator, selector_start, parent);
@@ -603,18 +620,39 @@ pub fn resolveNestedSelector(allocator: std.mem.Allocator, parent: []const u8, n
}
selector_start = out.items.len;
has_nesting = false;
+ starts_with_combinator = false;
pending_space = false;
+ pending_comment = null;
continue;
}
const sp = span.?;
- if (isWhitespaceOrComment(sp.token)) {
- pending_space = out.items.len > selector_start;
- continue;
+ switch (sp.token) {
+ .white_space => {
+ pending_space = out.items.len > selector_start;
+ pending_comment = null;
+ continue;
+ },
+ .comment => {
+ if (!pending_space and out.items.len > selector_start) {
+ pending_comment = nested[sp.start..sp.end];
+ }
+ continue;
+ },
+ else => {},
}
if (pending_space) {
try out.append(allocator, ' ');
- pending_space = false;
+ } else if (pending_comment) |comment| {
+ try out.appendSlice(allocator, comment);
+ }
+ pending_space = false;
+ pending_comment = null;
+ if (out.items.len == selector_start) {
+ starts_with_combinator = switch (sp.token) {
+ .delim => |c| c == '>' or c == '+' or c == '~',
+ else => false,
+ };
}
updateDepth(sp.token, &depth);
switch (sp.token) {
@@ -630,7 +668,11 @@ pub fn resolveNestedSelector(allocator: std.mem.Allocator, parent: []const u8, n
try out.appendSlice(allocator, nested[sp.start..sp.end]);
}
- return out.toOwnedSlice(allocator);
+ if (out.items.len > MAX_NESTED_SELECTOR_LEN) {
+ out.deinit(allocator);
+ return null;
+ }
+ return try out.toOwnedSlice(allocator);
}
const testing = std.testing;
@@ -794,8 +836,32 @@ test "resolveNestedSelector" {
.{ .parent = ".a", .nested = "&.b, .c , :is(.d, .e)", .want = ":is(.a).b, :is(.a) .c, :is(.a) :is(.d, .e)" },
.{ .parent = ".a", .nested = "[data-x='&']", .want = ":is(.a) [data-x='&']" },
.{ .parent = ".a", .nested = " /* c */ & .b ", .want = ":is(.a) .b" },
+ // A selector starting with a combinator is relative even with an `&`.
+ .{ .parent = ".a", .nested = "> .x &", .want = ":is(.a) > .x :is(.a)" },
+ // A comment between two tokens is not a descendant combinator.
+ .{ .parent = ".a", .nested = "&/* c */.b", .want = ":is(.a)/* c */.b" },
+ .{ .parent = ".a", .nested = "& /* c */.b", .want = ":is(.a) .b" },
};
for (cases) |c| {
- try testing.expectEqualStrings(c.want, try resolveNestedSelector(allocator, c.parent, c.nested));
+ const got = try resolveNestedSelector(allocator, c.parent, c.nested) orelse return error.TooLong;
+ try testing.expectEqualStrings(c.want, got);
+ }
+}
+
+test "resolveNestedSelector: `& &` stops doubling at the cap" {
+ var arena = std.heap.ArenaAllocator.init(testing.allocator);
+ defer arena.deinit();
+ const allocator = arena.allocator();
+
+ // Each level doubles the selector: without the cap, 32 levels would be
+ // tens of gigabytes.
+ var selector: []const u8 = ".a";
+ var levels: usize = 0;
+ while (try resolveNestedSelector(allocator, selector, "& &")) |resolved| {
+ try testing.expect(resolved.len <= MAX_NESTED_SELECTOR_LEN);
+ selector = resolved;
+ levels += 1;
+ try testing.expect(levels < 32);
}
+ try testing.expect(levels > 0);
}
diff --git a/src/browser/webapi/css/CSSStyleRule.zig b/src/browser/webapi/css/CSSStyleRule.zig
index 62d9229ee..e1cc206ba 100644
--- a/src/browser/webapi/css/CSSStyleRule.zig
+++ b/src/browser/webapi/css/CSSStyleRule.zig
@@ -42,7 +42,7 @@ pub fn getStyle(self: *CSSStyleRule, frame: *Frame) !*CSSStyleProperties {
return style;
}
-fn getCssRules(self: *CSSStyleRule, frame: *Frame) !*CSSRuleList {
+pub fn getCssRules(self: *CSSStyleRule, frame: *Frame) !*CSSRuleList {
if (self._rules) |rules| {
return rules;
}
diff --git a/src/browser/webapi/css/CSSStyleSheet.zig b/src/browser/webapi/css/CSSStyleSheet.zig
index 08f5077de..e8bc598d6 100644
--- a/src/browser/webapi/css/CSSStyleSheet.zig
+++ b/src/browser/webapi/css/CSSStyleSheet.zig
@@ -141,11 +141,8 @@ fn initStyleRule(selector: []const u8, block: []const u8, depth: u8, frame: *Fra
.rule => |r| try initStyleRule(r.selector, r.block, depth + 1, frame),
.at_rule => |a| try CSSRule.initAtRule(atRuleTypeFor(a.keyword), a.text, frame),
};
- const rules = style_rule._rules orelse blk: {
- const rules = try CSSRuleList.init(frame);
- style_rule._rules = rules;
- break :blk rules;
- };
+ // Created on the first nested rule: `_rules` stays null without one.
+ const rules = try style_rule.getCssRules(frame);
try rules.insert(rules.length(), nested, frame);
}
return style_rule._proto;