Skip to content

Lightpanda: the headless browser designed for AI and automation

css: raise the external stylesheet cap to 4 MiB

Adrià Arrufatbba49da1b60d
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index 483e408f5..4477c18e9 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -2555,11 +2555,11 @@ pub fn queueHashChange(self: *Frame, old_url: []const u8, new_url: []const u8) !
}
// Hard cap on a single external stylesheet body. CSS rule storage is per-
-// arena so a hostile sheet could otherwise inflate page memory; 2 MiB is
-// well above anything seen on real sites (Tailwind's `preflight + utilities`
-// build is ~400 KiB gzipped, ~3 MiB raw — at which point a site should be
-// splitting by route anyway).
-const MAX_STYLESHEET_BYTES: usize = 2 * 1024 * 1024;
+// arena so a hostile sheet could otherwise inflate page memory. Real sites
+// ship aggregated sheets above 2 MiB (caltrain.com's Drupal theme is
+// 2.35 MB, with Bootstrap's `.dropdown-menu { display: none }` inside), and a
+// dropped sheet leaves hidden menus and tab panels in innerText.
+pub const MAX_STYLESHEET_BYTES: usize = 4 * 1024 * 1024;
// Synchronously fetch and parse an external `<link rel=stylesheet>`.
// href is passed in as an optimization since the [currently] only callsite has
diff --git a/src/browser/tests/css/external_stylesheet.html b/src/browser/tests/css/external_stylesheet.html
index 4a2b19bdd..fba7b0c41 100644
--- a/src/browser/tests/css/external_stylesheet.html
+++ b/src/browser/tests/css/external_stylesheet.html
@@ -68,6 +68,31 @@
});
</script>
+ <script id="ext_stylesheet_large_applies" type=module>
+ const state = await testing.async();
+ const el = document.createElement('div');
+ el.className = 'large-hide';
+ el.textContent = 'hidden by large sheet';
+ document.body.appendChild(el);
+
+ const link = document.createElement('link');
+ link.rel = 'stylesheet';
+ link.href = '/styles/large.css';
+
+ const evt = await new Promise(resolve => {
+ link.addEventListener('load', () => resolve('load'));
+ link.addEventListener('error', () => resolve('error'));
+ document.head.appendChild(link);
+ });
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual('load', evt);
+ testing.expectEqual('none', getComputedStyle(el).display);
+ testing.expectFalse(document.body.innerText.includes('hidden by large sheet'));
+ });
+ </script>
+
<script id="ext_stylesheet_oversize_fires_error" type=module>
const state = await testing.async();
const link = document.createElement('link');
diff --git a/src/testing.zig b/src/testing.zig
index 2538685f3..f1b651c56 100644
--- a/src/testing.zig
+++ b/src/testing.zig
@@ -1162,13 +1162,29 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
});
}
+ if (std.mem.eql(u8, path, "/styles/large.css")) {
+ // 2.5 MiB, past the old 2 MiB cap, with the rule under test last so
+ // the whole body has to be parsed for it to apply.
+ const chunk = ".pad { color: #abcdef; } "; // 25 bytes
+ const tail = ".large-hide { display: none; }";
+ const repeats = 5 * 1024 * 1024 / 2 / chunk.len;
+ var body = try std.ArrayList(u8).initCapacity(req_allocator, chunk.len * repeats + tail.len);
+ for (0..repeats) |_| body.appendSliceAssumeCapacity(chunk);
+ body.appendSliceAssumeCapacity(tail);
+ return req.respond(body.items, .{
+ .extra_headers = &.{
+ .{ .name = "Content-Type", .value = "text/css" },
+ },
+ });
+ }
+
if (std.mem.eql(u8, path, "/styles/oversize.css")) {
- // Body that exceeds Frame.MAX_STYLESHEET_BYTES (2 MiB) — written as a
+ // Body that exceeds Frame.MAX_STYLESHEET_BYTES — written as a
// long sequence of valid declarations so the response itself parses
// fine and the error path is exercised by the size cap, not by a
// CSS parse failure.
const chunk = ".pad { color: #abcdef; } "; // 25 bytes
- const repeats = (2 * 1024 * 1024 / chunk.len) + 1024;
+ const repeats = (Frame.MAX_STYLESHEET_BYTES / chunk.len) + 1024;
var body = try std.ArrayList(u8).initCapacity(req_allocator, chunk.len * repeats);
for (0..repeats) |_| body.appendSliceAssumeCapacity(chunk);
return req.respond(body.items, .{