Skip to content

Commit 850ce31

Browse files
Ahmad-S792Ahmad Saleem
authored andcommitted
NavigationEvent#canIntercept is true when navigating to a different port
https://bugs.webkit.org/show_bug.cgi?id=307197 rdar://169845691 Reviewed by Basuke Suzuki. The canIntercept property was incorrectly returning true when navigating between different ports on the same host (e.g., localhost:3000 to localhost:3001). According to the spec [1], canIntercept should be false when the document URL and target URL differ in scheme, username, password, host, or port components. The bug was in documentCanHaveURLRewritten(), which returned true for any HTTP(s) URL without checking if the port (or other components) matched between the document and target URLs. This patch fixes by adding explicit checks for scheme, username, password, host, and port equality before allowing URL rewriting. [1] https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten * Source/WebCore/page/Navigation.cpp: (WebCore::documentCanHaveURLRewritten): * LayoutTests/imported/w3c/web-platform-tests/navigation-api/navigate-event/navigate-event-canintercept-cross-port-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/navigation-api/navigate-event/navigate-event-canintercept-cross-port.html: Added. Canonical link: https://commits.webkit.org/307316@main
1 parent 54e001a commit 850ce31

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
PASS canIntercept should be false for cross-port navigations
3+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8">
3+
<title>Navigation API: canIntercept should be false for cross-port navigations</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<body>
7+
<script>
8+
promise_test(async t => {
9+
const currentPort = location.port || (location.protocol === 'https:' ? '443' : '80');
10+
const otherPort = currentPort === '8800' ? '8000' : '8800';
11+
const targetURL = `${location.protocol}//${location.hostname}:${otherPort}/`;
12+
13+
const navigatePromise = new Promise((resolve) => {
14+
navigation.addEventListener("navigate", t.step_func(event => {
15+
// Cross-port navigation should have canIntercept === false
16+
assert_equals(event.canIntercept, false,
17+
`canIntercept should be false when navigating from port ${currentPort} to port ${otherPort}`);
18+
19+
// Prevent the actual navigation
20+
event.preventDefault();
21+
resolve();
22+
}), { once: true });
23+
});
24+
25+
// Trigger the cross-port navigation
26+
const anchor = document.createElement('a');
27+
anchor.href = targetURL;
28+
document.body.appendChild(anchor);
29+
anchor.click();
30+
31+
await navigatePromise;
32+
}, "canIntercept should be false for cross-port navigations");
33+
</script>
34+
</body>

Source/WebCore/page/Navigation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,14 @@ static bool documentCanHaveURLRewritten(const Document& document, const URL& tar
820820
if (!isSameSite && !isSameOrigin)
821821
return false;
822822

823+
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#can-have-its-url-rewritten
824+
if (documentURL.protocol() != targetURL.protocol()
825+
|| documentURL.user() != targetURL.user()
826+
|| documentURL.password() != targetURL.password()
827+
|| documentURL.host() != targetURL.host()
828+
|| documentURL.port() != targetURL.port())
829+
return false;
830+
823831
if (targetURL.protocolIsInHTTPFamily())
824832
return true;
825833

0 commit comments

Comments
 (0)