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
9 changes: 9 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
</properties>

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Bugfixes">
<action type="fix" dev="Vidya m">
Method document.cookie (and meta http-equiv set-cookie) no longer sets cookies for another domain;
the domain attribute has to match the page (RFC 6265 section 5.3). Cookies for a public suffix
(e.g. co.uk) are ignored also for Set-Cookie headers.
</action>
</release>


<release version="5.8.0" date="November 17, 2026" description="Chrome/Edge 157, Security, CSP, Bugfixes">
<action type="fix" dev="rbri">
Security: drawing an image of another origin (also via redirect) into a canvas sets the
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/htmlunit/httpclient/HtmlUnitDomainHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.net.UnknownHostException;
import java.util.Locale;

import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieRestrictionViolationException;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.cookie.SetCookie;
import org.apache.http.impl.cookie.BasicDomainHandler;
Expand All @@ -37,6 +40,8 @@
* @author Ahmed Ashour
*/
final class HtmlUnitDomainHandler extends BasicDomainHandler {
private static final PublicSuffixMatcher PUBLIC_SUFFIX_MATCHER = PublicSuffixMatcherLoader.getDefault();

private final BrowserVersion browserVersion_;

HtmlUnitDomainHandler(final BrowserVersion browserVersion) {
Expand Down Expand Up @@ -69,6 +74,25 @@ public void parse(final SetCookie cookie, final String value)
cookie.setDomain(domain);
}

/**
* {@inheritDoc}
* Like the browsers, cookies for a public suffix (e.g. 'co.uk')
* are rejected unless the domain is the host itself (RFC 6265 section 5.3, step 5).
*/
@Override
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException {
super.validate(cookie, origin);

final String domain = cookie.getDomain();
final String normalizedDomain = domain.startsWith(".") ? domain.substring(1) : domain;
if (normalizedDomain.indexOf('.') > 0
&& !normalizedDomain.equalsIgnoreCase(origin.getHost())
&& PUBLIC_SUFFIX_MATCHER.matches(normalizedDomain)) {
throw new CookieRestrictionViolationException("Illegal 'domain' attribute \"" + domain
+ "\"; cookies for a public suffix are not allowed");
}
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ public static List<org.htmlunit.http.Cookie> parseCookie(final String cookieStri
buffer.append(cookieString);

final CookieSpec cookieSpec = new HtmlUnitBrowserCompatCookieSpec(browserVersion);
final List<Cookie> cookies = cookieSpec.parse(new BufferedHeader(buffer), buildCookieOrigin(pageUrl));
final CookieOrigin cookieOrigin = buildCookieOrigin(pageUrl);
final List<Cookie> cookies = cookieSpec.parse(new BufferedHeader(buffer), cookieOrigin);

final List<org.htmlunit.http.Cookie> htmlUnitCookies = new ArrayList<>(cookies.size());
for (final Cookie cookie : cookies) {
// the same checks HttpClient does for Set-Cookie headers (e.g. the domain has to match the page)
cookieSpec.validate(cookie, cookieOrigin);

final org.htmlunit.http.Cookie htmlUnitCookie = new HttpClientCookie((ClientCookie) cookie);
htmlUnitCookies.add(htmlUnitCookie);
}
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/htmlunit/CookieManager2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
package org.htmlunit;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -164,4 +165,32 @@ public void getCookiesShouldReturnACopyOfCurentState() throws Exception {
assertEquals(1, initialCookies.size());
assertEquals(2, webClient.getCookieManager().getCookies().size());
}

/**
* Method document.cookie must not set cookies for another domain or for
* a public suffix (RFC 6265 section 5.3, steps 5 and 6).
* @throws Exception if the test fails
*/
@Test
public void documentCookieForeignDomain() throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ " document.cookie = 'own=1; domain=attacker.co.uk; path=/';\n"
+ " document.cookie = 'foreign=1; domain=victim.example; path=/';\n"
+ " document.cookie = 'suffix=1; domain=co.uk; path=/';\n"
+ "</script></head><body></body></html>";

final WebClient webClient = getWebClient();
final MockWebConnection webConnection = new MockWebConnection();
webConnection.setResponse(new URL("https://www.attacker.co.uk/"), html);
webClient.setWebConnection(webConnection);

webClient.getPage("https://www.attacker.co.uk/");

assertEquals("[own=1;domain=.attacker.co.uk;path=/]",
webClient.getCookieManager().getCookies().toString());
assertTrue(webClient.getCookies(new URL("https://victim.example/")).isEmpty());
assertTrue(webClient.getCookies(new URL("https://victim.co.uk/")).isEmpty());
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/htmlunit/httpclient/HttpClientConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.net.URL;

import org.apache.http.cookie.MalformedCookieException;
import org.htmlunit.BrowserVersion;

import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -132,4 +138,23 @@ public void ipv4AddressIsNeverRunThroughPublicSuffixList() {
public void ipv6AddressIsNeverRunThroughPublicSuffixList() {
assertEquals("::1", HttpClientConverter.registrableDomain("::1"));
}

/**
* @throws Exception if the test fails
*/
@Test
public void parseCookieDomain() throws Exception {
final URL url = new URL("https://www.example.co.uk/");
final BrowserVersion browserVersion = BrowserVersion.BEST_SUPPORTED;

assertEquals(".example.co.uk",
HttpClientConverter.parseCookie("a=1; domain=example.co.uk", url, browserVersion).get(0).getDomain());
assertEquals("www.example.co.uk",
HttpClientConverter.parseCookie("a=1", url, browserVersion).get(0).getDomain());

assertThrows(MalformedCookieException.class,
() -> HttpClientConverter.parseCookie("a=1; domain=other.co.uk", url, browserVersion));
assertThrows(MalformedCookieException.class,
() -> HttpClientConverter.parseCookie("a=1; domain=co.uk", url, browserVersion));
}
}
Loading