Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Bugfixes">
<action type="fix" dev="Vidya m">
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.
</action>
<action type="fix" dev="rbri">
A document without browsing context (created by DOMImplementation.createHTMLDocument(),
DOMImplementation.createDocument() or DOMParser) is cookie-averse like in the browsers: document.cookie
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/htmlunit/WebRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 25 additions & 5 deletions src/main/java/org/htmlunit/util/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/htmlunit/WebRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
22 changes: 22 additions & 0 deletions src/test/java/org/htmlunit/util/UrlUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
Loading