Skip to content

Lightpanda: the headless browser designed for AI and automation

ensure the right frame target is used

Karl Seguinf77ce51d952f
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index cde7102a4..3d393e092 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -991,9 +991,6 @@ fn recordNavigateTelemetry(self: *Frame, tls: bool) void {
// as the caller almost certainly doesn't expect the frame to go away during the
// call. So, we schedule the navigation for the next tick.
pub fn scheduleNavigation(self: *Frame, request_url: []const u8, opts: NavigateOpts, nt: Navigation) !void {
- if (self.canScheduleNavigation(std.meta.activeTag(nt)) == false) {
- return;
- }
const arena = try self._session.getArena(.small, "scheduleNavigation");
errdefer arena.release();
return self.scheduleNavigationWithArena(arena, request_url, opts, nt);
@@ -1035,6 +1032,14 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
.iframe => |iframe| iframe._window.?._frame, // only an frame with existing content (i.e. a window) can be navigated
};
+ // The competing navigation, if any, is queued on the target, which isn't
+ // the originator when a script navigates another frame.
+ if (target.canScheduleNavigation(std.meta.activeTag(nt)) == false) {
+ // don't defer this, the caller is responsible for freeing it on error
+ arena.release();
+ return;
+ }
+
// A javascript: URL is not fetched: its script runs in the target's
// document, and only for an initiator of the same origin.
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
@@ -3835,10 +3840,6 @@ pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.For
return;
}
- if (self.canScheduleNavigation(.form) == false) {
- return;
- }
-
const form_element = form.asElement();
const submit_button: ?*Element = blk: {
@@ -3862,6 +3863,10 @@ pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.For
break :blk self.resolveTargetFrame(target_name);
};
+ if (target == .frame and target.frame.canScheduleNavigation(.form) == false) {
+ return;
+ }
+
if (submit_opts.fire_event) {
// Prevent a submit on the form from firing while we're submit the form.
// This is both spec-correct AND prevents infinite recursion.
@@ -4007,7 +4012,8 @@ pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.For
// resolved encoding (accept-charset or document charset).
.plaintext => try arena.allocator().printSentinel("Content-Type: text/plain; charset={s}", .{charset}, 0),
};
- } else {
+ } else if (URL.isJavascriptUrl(action) == false) {
+ // A javascript: action is navigated to as is, without the entries.
action = try URL.concatQueryString(arena.allocator(), action, buf.written());
}
diff --git a/src/browser/URL.zig b/src/browser/URL.zig
index a3d7c1f33..39f10d6d1 100644
--- a/src/browser/URL.zig
+++ b/src/browser/URL.zig
@@ -142,6 +142,14 @@ pub fn isAboutBlank(url: []const u8) bool {
return std.mem.eql(u8, url, "about:blank");
}
+// For an unresolved URL, e.g. an attribute: the URL parser strips leading C0
+// controls and spaces, and the scheme is case-insensitive.
+pub fn isJavascriptUrl(url: []const u8) bool {
+ var i: usize = 0;
+ while (i < url.len and url[i] <= ' ') : (i += 1) {}
+ return std.ascii.startsWithIgnoreCase(url[i..], "javascript:");
+}
+
pub fn isCompleteHTTPUrl(url: []const u8) bool {
if (url.len < 3) { // Minimum is "x://"
return false;
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index 30f7a53be..132b3e5c2 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -25,6 +25,7 @@
const std = @import("std");
const lp = @import("lightpanda");
+const URL = @import("../URL.zig");
const Frame = @import("../Frame.zig");
const js = @import("../js/js.zig");
@@ -804,32 +805,29 @@ fn followLink(frame: *Frame, target: *Node, element: *Element, href: []const u8,
return;
}
- if (std.mem.startsWith(u8, href, "javascript:")) {
- // Navigating to a javascript: URL evaluates the script in the
- // node's frame as a queued task. (A string completion value
- // would replace the document; we ignore results.)
- return runJavascriptUrl(target.ownerFrame(frame) orelse return, href["javascript:".len..]);
- }
+ // The link's document initiates the navigation, which isn't `frame` when
+ // a script clicks a link in another frame.
+ const owner = target.ownerFrame(frame) orelse return;
- if (try element.hasAttribute(comptime .wrap("download"), frame)) {
- log.debug(.browser, "a.download", .{ .type = frame._type, .url = frame.url });
+ if (URL.isJavascriptUrl(href) == false and try element.hasAttribute(comptime .wrap("download"), frame)) {
+ log.debug(.browser, "a.download", .{ .type = owner._type, .url = owner.url });
return;
}
const target_frame = blk: {
if (target_name.len == 0) {
- break :blk target.ownerFrame(frame) orelse return;
+ break :blk owner;
}
- break :blk switch (frame.resolveTargetFrame(target_name)) {
+ break :blk switch (owner.resolveTargetFrame(target_name)) {
.frame => |f| f,
.blank => {
- _ = try (target.ownerFrame(frame) orelse return).openBlankTarget(element, href);
+ _ = try owner.openBlankTarget(element, href);
return;
},
};
};
- try frame.scheduleNavigation(href, .{
+ try owner.scheduleNavigation(href, .{
.reason = .script,
.kind = .{ .push = null },
}, .{ .anchor = target_frame });
diff --git a/src/browser/tests/frames/support/with_link.html b/src/browser/tests/frames/support/with_link.html
index bc31b190c..2fd9ca77c 100644
--- a/src/browser/tests/frames/support/with_link.html
+++ b/src/browser/tests/frames/support/with_link.html
@@ -1,2 +1,2 @@
<!DOCTYPE html>
-<a href="support/after_link.html" id=link>a link</a>
+<a href="after_link.html" id=link>a link</a>
diff --git a/src/browser/tests/window/location_javascript_url.html b/src/browser/tests/window/location_javascript_url.html
index 5c8a424fd..e44721e3a 100644
--- a/src/browser/tests/window/location_javascript_url.html
+++ b/src/browser/tests/window/location_javascript_url.html
@@ -49,3 +49,32 @@
});
}
</script>
+
+<form id=js_form action="javascript:window.__submitted++"><input name=a value=1></form>
+<script id=form_get type=module>
+{
+ // A GET submission navigates to the action as is, without "?a=1".
+ const state = await testing.async();
+ window.__submitted = 0;
+ document.getElementById('js_form').requestSubmit();
+ setTimeout(() => state.resolve(), 10);
+ await state.done(() => {
+ testing.expectEqual(1, window.__submitted);
+ testing.expectEqual('kept', window.__marker);
+ });
+}
+</script>
+
+<a id=js_download download href="javascript:window.__clicked++">x</a>
+<script id=link_download type=module>
+{
+ const state = await testing.async();
+ window.__clicked = 0;
+ document.getElementById('js_download').click();
+ setTimeout(() => state.resolve(), 10);
+ await state.done(() => {
+ testing.expectEqual(1, window.__clicked);
+ testing.expectEqual('kept', window.__marker);
+ });
+}
+</script>
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index e369f92db..d763d0c45 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -742,7 +742,7 @@ pub fn open(self: *Window, url_: ?[]const u8, target_: ?[]const u8, features_: ?
if (is_named) {
if (page.findPopupByName(target)) |existing| {
if (raw_url.len > 0) {
- try existing.scheduleNavigation(raw_url, .{
+ try frame.scheduleNavigation(raw_url, .{
.reason = .script,
.kind = .{ .push = null },
}, .{ .script = existing });