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

<body>
<release version="5.8.0" date="xxxx xx, 2026" description="Chrome/Edge 157, Bugfixes">
<action type="fix" dev="Vidya m">
document.cookie (and meta http-equiv set-cookie) no longer creates HttpOnly cookies and no longer
replaces or deletes an existing HttpOnly cookie (RFC 6265 section 5.3).
</action>
<action type="fix" dev="Vidya m">
History.pushState() and History.replaceState() throw a SecurityError if the url is not same origin
with the document; a page was able to change its url (and by this its origin for cookies,
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/htmlunit/WebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3152,6 +3152,8 @@ public Set<Cookie> getCookies(final URL url) {

/**
* Parses the given cookie and adds this to our cookie store.
* The cookie is treated as set by a non-HTTP API (e.g. document.cookie);
* HttpOnly cookies are ignored and existing HttpOnly cookies are not replaced.
* @param cookieString the string to parse
* @param pageUrl the url of the page that likes to set the cookie
* @param origin the requester
Expand All @@ -3171,6 +3173,22 @@ public void addCookie(final String cookieString, final URL pageUrl, final Object
// final List<Cookie> cookies = CookieParser.parseCookie(cookieString, pageUrl, getBrowserVersion());

for (final Cookie cookie : cookies) {
// RFC 6265 5.3 steps 10 and 11: a non-HTTP API must neither
// create an HttpOnly cookie nor replace an existing one
if (cookie.isHttpOnly()) {
continue;
}
boolean replacesHttpOnly = false;
for (final Cookie existing : cookieManager.getCookies()) {
if (existing.isHttpOnly() && existing.equals(cookie)) {
replacesHttpOnly = true;
break;
}
}
if (replacesHttpOnly) {
continue;
}

cookieManager.addCookie(cookie);

if (LOG.isDebugEnabled()) {
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/org/htmlunit/CookieManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,58 @@ public void httpOnly() throws Exception {
}
}

/**
* Scripts must not be able to overwrite, delete or create HttpOnly cookies
* (RFC 6265 section 5.3, steps 10 and 11).
* @throws Exception if the test fails
*/
@Test
@Alerts("second=2; third=3")
public void httpOnlyNotWritableFromJs() throws Exception {
final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "first=1; path=/; HttpOnly"));
responseHeader.add(new NameValuePair("Set-Cookie", "second=2; path=/;"));

final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " document.cookie = 'first=evil; path=/';\n"
+ " document.cookie = 'third=3; path=/';\n"
+ " document.cookie = 'fourth=4; path=/; HttpOnly';\n"
+ " var c = document.cookie;\n"
+ " c = c.split('; ').sort().join('; ');\n"
+ " log(c);\n"
+ " }\n"
+ "</script>\n"
+ "</head>\n"
+ "<body onload='test()'>\n"
+ "</body>\n"
+ "</html>";

getMockWebConnection().setDefaultResponse("");
getMockWebConnection().setResponse(URL_FIRST, html, 200, "OK", MimeType.TEXT_HTML, responseHeader);

final WebDriver driver = loadPage2(URL_FIRST, StandardCharsets.ISO_8859_1);
verifyTitle2(driver, getExpectedAlerts());
driver.get(URL_FIRST + "foo");

final Map<String, String> lastHeaders = getMockWebConnection().getLastAdditionalHeaders();
final String lastCookies = lastHeaders.get(HttpHeader.COOKIE);
assertTrue("lastCookies: " + lastCookies, lastCookies.contains("first=1"));
assertFalse("lastCookies: " + lastCookies, lastCookies.contains("first=evil"));
assertFalse("lastCookies: " + lastCookies, lastCookies.contains("fourth=4"));

if (driver instanceof HtmlUnitDriver) {
final CookieManager mgr = getWebClient().getCookieManager();
assertEquals(3, mgr.getCookies().size());
assertEquals("1", mgr.getCookie("first").getValue());
assertTrue(mgr.getCookie("first").isHttpOnly());
assertNull(mgr.getCookie("fourth"));
}
}

/**
* @throws Exception if the test fails
*/
Expand Down
Loading