From b158a1ba09a87d0c44001d3d9479290de6e80bea Mon Sep 17 00:00:00 2001 From: Vidya m Date: Thu, 19 Nov 2026 02:46:46 +0530 Subject: [PATCH] do not send the credentials of the page url in Referer and Origin WebRequest.setRefererHeader() and the origin helpers UrlUtils.getUrlWithProtocolAndAuthority() / getUrlWithoutPathRefQuery() copied URL.getAuthority(), which includes the user info. A page loaded from http://user:password@host/ sent the credentials in the Referer and Origin headers to every site it referenced, and they showed up in document.referrer, MessageEvent.origin and the WebSocket Origin. The referrer is stripped of username and password (Fetch standard, strip url for use as a referrer) and an origin has no user info. --- src/changes/changes.xml | 6 ++++ src/main/java/org/htmlunit/WebRequest.java | 7 ++++- src/main/java/org/htmlunit/util/UrlUtils.java | 30 +++++++++++++++---- .../java/org/htmlunit/WebRequestTest.java | 17 +++++++++++ .../java/org/htmlunit/util/UrlUtilsTest.java | 22 ++++++++++++++ 5 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index f64ae62c85c..95278b77f7e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,12 @@ + + The Referer and Origin request headers (and by this document.referrer and the origin of + message events and WebSocket requests) no longer contain the user name and password of the + page url; the credentials of a page like http://user:password@host/ were sent to every site + the page referenced. + A document without browsing context (created by DOMImplementation.createHTMLDocument(), DOMImplementation.createDocument() or DOMParser) is cookie-averse like in the browsers: document.cookie diff --git a/src/main/java/org/htmlunit/WebRequest.java b/src/main/java/org/htmlunit/WebRequest.java index 3badc0b5d7d..1cf24cf1b08 100644 --- a/src/main/java/org/htmlunit/WebRequest.java +++ b/src/main/java/org/htmlunit/WebRequest.java @@ -718,7 +718,12 @@ public void setRefererHeader(final URL url) { } try { - setAdditionalHeader(HttpHeader.REFERER, UrlUtils.getUrlWithoutRef(url).toExternalForm()); + URL referer = UrlUtils.getUrlWithoutRef(url); + if (referer.getUserInfo() != null) { + // the referrer never contains the credentials + referer = UrlUtils.getUrlWithNewUserName(UrlUtils.getUrlWithNewUserPassword(referer, null), null); + } + setAdditionalHeader(HttpHeader.REFERER, referer.toExternalForm()); } catch (final MalformedURLException ignored) { // bad luck use the whole url from the pager diff --git a/src/main/java/org/htmlunit/util/UrlUtils.java b/src/main/java/org/htmlunit/util/UrlUtils.java index cf3c3b8ae49..4d6d9dc8066 100644 --- a/src/main/java/org/htmlunit/util/UrlUtils.java +++ b/src/main/java/org/htmlunit/util/UrlUtils.java @@ -413,13 +413,14 @@ else if (state == 1 || state == 2) { } /** - * Creates and returns a new URL using only the protocol and authority from the given one. + * Creates and returns a new URL using only the protocol and authority (without the user info) + * from the given one. * @param u the URL on which to base the returned URL * @return a new URL using only the protocol and authority from the given one * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithoutPathRefQuery(final URL u) throws MalformedURLException { - return createNewUrl(u.getProtocol(), u.getAuthority(), null, null, null); + return createNewUrl(u.getProtocol(), getAuthorityWithoutUserInfo(u), null, null, null); } /** @@ -516,13 +517,32 @@ public static URL getUrlWithNewQuery(final URL u, final String newQuery) throws } /** - * Creates and returns a new URL identical to the specified URL, ignoring path, protocol and query. + * Creates and returns a new URL identical to the specified URL, ignoring path, ref, query + * and user info. * @param u the URL on which to base the returned URL - * @return a new URL identical to the specified URL, ignoring path, protocol and query + * @return a new URL identical to the specified URL, ignoring path, ref, query and user info * @throws MalformedURLException if there is a problem creating the new URL */ public static URL getUrlWithProtocolAndAuthority(final URL u) throws MalformedURLException { - return createNewUrl(u.getProtocol(), u.getAuthority(), null, null, null); + return createNewUrl(u.getProtocol(), getAuthorityWithoutUserInfo(u), null, null, null); + } + + /** + * Returns the authority of the given URL without the user info; an origin never + * contains the credentials. + * @param u the URL + * @return the authority without the user info + */ + private static String getAuthorityWithoutUserInfo(final URL u) { + final String authority = u.getAuthority(); + if (authority == null) { + return null; + } + final int atPos = authority.lastIndexOf('@'); + if (atPos == -1) { + return authority; + } + return authority.substring(atPos + 1); } /** diff --git a/src/test/java/org/htmlunit/WebRequestTest.java b/src/test/java/org/htmlunit/WebRequestTest.java index 3ef7ea3c4e1..ffa781f9ea9 100644 --- a/src/test/java/org/htmlunit/WebRequestTest.java +++ b/src/test/java/org/htmlunit/WebRequestTest.java @@ -346,4 +346,21 @@ public void serialization() throws Exception { assertEquals(StandardCharsets.UTF_8, deserialized.getCharset()); assertEquals(StandardCharsets.US_ASCII, deserialized.getDefaultResponseContentCharset()); } + + /** + * The Referer header never contains the credentials of the referring url. + * + * @throws Exception if the test fails + */ + @Test + public void refererHeaderWithoutCredentials() throws Exception { + WebRequest request = new WebRequest(new URL("http://evil.example/"), StandardCharsets.UTF_8, + new URL("http://admin:s3cret@intranet.example/app/index.html?q=1#frag")); + assertEquals("http://intranet.example/app/index.html?q=1", + request.getAdditionalHeader(HttpHeader.REFERER)); + + request = new WebRequest(new URL("http://evil.example/"), StandardCharsets.UTF_8, + new URL("https://admin@intranet.example:8443/")); + assertEquals("https://intranet.example:8443/", request.getAdditionalHeader(HttpHeader.REFERER)); + } } diff --git a/src/test/java/org/htmlunit/util/UrlUtilsTest.java b/src/test/java/org/htmlunit/util/UrlUtilsTest.java index 5bdc7bcd6f1..0baf95da631 100644 --- a/src/test/java/org/htmlunit/util/UrlUtilsTest.java +++ b/src/test/java/org/htmlunit/util/UrlUtilsTest.java @@ -216,6 +216,28 @@ public void getUrlWithProtocolAndAuthority() throws Exception { a = new URL("http://my.home.com/#href"); b = UrlUtils.getUrlWithProtocolAndAuthority(a); assertEquals("http://my.home.com", b.toExternalForm()); + + a = new URL("http://user:secret@my.home.com:8080/index.html"); + b = UrlUtils.getUrlWithProtocolAndAuthority(a); + assertEquals("http://my.home.com:8080", b.toExternalForm()); + } + + /** + * @throws Exception if the test fails + */ + @Test + public void getUrlWithoutPathRefQuery() throws Exception { + URL a = new URL("http://my.home.com/folder/index.html?query#ref"); + URL b = UrlUtils.getUrlWithoutPathRefQuery(a); + assertEquals("http://my.home.com", b.toExternalForm()); + + a = new URL("https://user:secret@my.home.com:8443/index.html"); + b = UrlUtils.getUrlWithoutPathRefQuery(a); + assertEquals("https://my.home.com:8443", b.toExternalForm()); + + a = new URL("http://user@my.home.com/"); + b = UrlUtils.getUrlWithoutPathRefQuery(a); + assertEquals("http://my.home.com", b.toExternalForm()); } /**