Skip to content

Lightpanda: the headless browser designed for AI and automation

fix uaf on attribute change

Karl Seguinfe9805a81b76
diff --git a/src/browser/tests/custom_elements/attribute_changed.html b/src/browser/tests/custom_elements/attribute_changed.html
index 101ea58df..660fc3824 100644
--- a/src/browser/tests/custom_elements/attribute_changed.html
+++ b/src/browser/tests/custom_elements/attribute_changed.html
@@ -199,3 +199,40 @@
testing.expectEqual(el.getAttribute('style'), calls[4]);
}
</script>
+
+<script id="cross_frame_long_name" type=module>
+ // The element's frame has no reaction scope open, so its reactions run
+ // later, after the caller's temporary copies of the name are gone.
+ const state = await testing.async();
+
+ const iframe = document.createElement('iframe');
+ document.body.appendChild(iframe);
+ const win = iframe.contentWindow;
+ win.seen = [];
+ win.eval(`
+ class CrossFrame extends HTMLElement {
+ static get observedAttributes() { return ['data-a-long-observed-name']; }
+ attributeChangedCallback(name) { window.seen.push(name); }
+ }
+ customElements.define('cross-frame', CrossFrame);
+ `);
+
+ const el = win.document.createElement('cross-frame');
+ const name = 'data-a-long-observed-name';
+ // this realm's methods, so the scope opened is this frame's, not el's
+ const setAttribute = Element.prototype.setAttribute;
+ const removeAttribute = Element.prototype.removeAttribute;
+ setAttribute.call(el, name.toUpperCase(), '1');
+ // more temporaries, to reuse the memory the name was lowercased into
+ document.createElement('X-' + 'B'.repeat(40));
+ setAttribute.call(el, name, '2');
+ removeAttribute.call(el, name.toUpperCase());
+ document.createElement('X-' + 'C'.repeat(40));
+
+ // the backup queue drains on the iframe's microtask queue
+ await new Promise(resolve => setTimeout(resolve, 0));
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual([name, name, name], win.seen);
+ });
+</script>
diff --git a/src/browser/webapi/CustomElementRegistry.zig b/src/browser/webapi/CustomElementRegistry.zig
index 27a4918da..fe5a80c3b 100644
--- a/src/browser/webapi/CustomElementRegistry.zig
+++ b/src/browser/webapi/CustomElementRegistry.zig
@@ -350,5 +350,13 @@ pub const JsApi = struct {
const testing = @import("../../testing.zig");
test "WebApi: CustomElementRegistry" {
+ if (comptime lp.IS_DEBUG) {
+ // Our reaction stack is per frame. The test opens the scope in the
+ // caller's frame, but attributeChange queues the reaction on the
+ // element's frame. When those differ, the reaction goes into the
+ // backup queue. We should probably move it from the Frame to the Page.
+ // this only logs in debug
+ testing.expectLog(&.{ .bug, .bug, .bug });
+ }
try testing.htmlRunner("custom_elements", .{});
}
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index 3f2e7da41..a335775cc 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -271,7 +271,7 @@ pub const List = struct {
try owner.addElementId(parent, element, entry.value());
}
owner.domChanged();
- owner.attributeChange(element, result.normalized, .wrap(entry.value()), old_value);
+ owner.attributeChange(element, .wrap(name), .wrap(entry.value()), old_value);
return name;
}
@@ -366,6 +366,7 @@ pub const List = struct {
const owner = element.ownerFrame(frame) orelse frame;
const is_id = shouldAddToIdMap(normalized, element);
const old_value = entry.value();
+ const name = entry.name();
if (is_id) {
owner.removeElementId(element, old_value);
@@ -383,7 +384,7 @@ pub const List = struct {
self._len -= 1;
owner.domChanged();
- owner.attributeRemove(element, normalized, .wrap(old_value));
+ owner.attributeRemove(element, .wrap(name), .wrap(old_value));
}
pub fn getNames(self: *const List, allocator: Allocator) ![][]const u8 {