diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0754214a6f..66fd6fb25a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,10 @@
+
+ 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).
+
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,
diff --git a/src/main/java/org/htmlunit/WebClient.java b/src/main/java/org/htmlunit/WebClient.java
index 5ee9e93fd4..2aa84e67b9 100644
--- a/src/main/java/org/htmlunit/WebClient.java
+++ b/src/main/java/org/htmlunit/WebClient.java
@@ -3152,6 +3152,8 @@ public Set 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
@@ -3171,6 +3173,22 @@ public void addCookie(final String cookieString, final URL pageUrl, final Object
// final List 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()) {
diff --git a/src/test/java/org/htmlunit/CookieManagerTest.java b/src/test/java/org/htmlunit/CookieManagerTest.java
index dde1fc095c..c3ff2df2c7 100644
--- a/src/test/java/org/htmlunit/CookieManagerTest.java
+++ b/src/test/java/org/htmlunit/CookieManagerTest.java
@@ -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 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
+ + "\n"
+ + "\n"
+ + "\n"
+ + "\n"
+ + "\n"
+ + "";
+
+ 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 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
*/