Lightpanda: the headless browser designed for AI and automation
Don't navigate to unsupported protocols
Karl Seguin199e00856cf8
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 5f591170a..fb49bcd0f 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -1028,6 +1028,13 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_urlreturn;}+ if (URL.isFetchable(resolved_url) == false) {+ log.info(.browser, "navigate unsupported scheme", .{ .url = resolved_url });+ // don't defer this, the caller is responsible for freeing it on error+ arena.release();+ return;+ }+const session = target._session;// Per the HTML "navigate" steps, this is a fragment navigation only whendiff --git a/src/browser/URL.zig b/src/browser/URL.zigindex ea682b6f9..43345d981 100644--- a/src/browser/URL.zig+++ b/src/browser/URL.zig@@ -150,6 +150,16 @@ pub fn isJavascriptUrl(url: []const u8) bool {return std.ascii.startsWithIgnoreCase(url[i..], "javascript:");}+pub fn isFetchable(url: []const u8) bool {+ const protocol = getProtocol(url);+ inline for (.{ "about:", "blob:", "data:", "file:", "http:", "https:" }) |scheme| {+ if (std.ascii.eqlIgnoreCase(protocol, scheme)) {+ return true;+ }+ }+ return false;+}+pub fn isCompleteHTTPUrl(url: []const u8) bool {if (url.len < 3) { // Minimum is "x://"return false;@@ -743,6 +753,20 @@ test "URL: isAboutBlank" {try testing.expectEqual(false, isAboutBlank("https://example.com/blob:x"));}+test "URL: isFetchable" {+ try testing.expectEqual(true, isFetchable("http://example.com/mailto:x"));+ try testing.expectEqual(true, isFetchable("HTTPS://example.com"));+ try testing.expectEqual(true, isFetchable("about:blank"));+ try testing.expectEqual(true, isFetchable("blob:http://example.com/1"));+ try testing.expectEqual(true, isFetchable("data:text/html,x"));+ try testing.expectEqual(true, isFetchable("file:///tmp/x"));++ try testing.expectEqual(false, isFetchable("mailto:[email protected]"));+ try testing.expectEqual(false, isFetchable("TEL:+15555550100"));+ try testing.expectEqual(false, isFetchable("ftp://example.com"));+ try testing.expectEqual(false, isFetchable("example.com"));+}+test "URL: resolve regression (#1093)" {const Case = struct {base: [:0]const u8,diff --git a/src/browser/tests/window/location_external_scheme.html b/src/browser/tests/window/location_external_scheme.htmlnew file mode 100644index 000000000..bf4e6a4b2--- /dev/null+++ b/src/browser/tests/window/location_external_scheme.html@@ -0,0 +1,24 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!--+ A URL whose scheme isn't fetchable (mailto:, tel:, ...) is handed off to+ external software: nothing is fetched and the document stays.+-->++<a id=mailto href="mailto:[email protected]">mail</a>+<a id=tel href="TEL:+15555550100">tel</a>+<script id=link type=module>+{+ const state = await testing.async();+ window.__marker = 'kept';+ document.getElementById('mailto').click();+ document.getElementById('tel').click();+ location.href = 'sms:+15555550100';+ setTimeout(() => state.resolve(), 50);+ await state.done(() => {+ testing.expectEqual('kept', window.__marker);+ testing.expectEqual(testing.BASE_URL + 'window/location_external_scheme.html', location.href);+ });+}+</script>