diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index f7c96f5691..7265e45ae7 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,11 @@
+
+ XMLHttpRequest.getResponseHeader() and XMLHttpRequest.getAllResponseHeaders() (and by this the
+ headers of fetch() responses) no longer expose the Set-Cookie and Set-Cookie2 headers; the value
+ of HttpOnly cookies was readable from JavaScript.
+
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
diff --git a/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java b/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java
index 87abfe0801..334a744b33 100644
--- a/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java
+++ b/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java
@@ -663,6 +663,9 @@ public String getAllResponseHeaders() {
if (webResponse_ != null) {
final StringBuilder builder = new StringBuilder();
for (final NameValuePair header : webResponse_.getResponseHeaders()) {
+ if (isForbiddenResponseHeader(header.getName())) {
+ continue;
+ }
builder
.append(header.getName())
.append(": ")
@@ -694,6 +697,9 @@ public String getResponseHeader(final String headerName) {
return null;
}
if (webResponse_ != null) {
+ if (isForbiddenResponseHeader(headerName)) {
+ return null;
+ }
return webResponse_.getResponseHeaderValue(headerName);
}
@@ -704,6 +710,17 @@ public String getResponseHeader(final String headerName) {
return null;
}
+ /**
+ * Set-Cookie and Set-Cookie2 are never exposed to scripts, otherwise HttpOnly cookies are readable.
+ *
+ * @see Fetch Standard
+ * @param name the header name
+ * @return {@code true} if the header is a forbidden response header
+ */
+ private static boolean isForbiddenResponseHeader(final String name) {
+ return "set-cookie".equalsIgnoreCase(name) || "set-cookie2".equalsIgnoreCase(name);
+ }
+
/**
* Initializes the request by specifying the destination URL, method, and other optional attributes.
*
diff --git a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequest2Test.java b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequest2Test.java
index e790006617..2ef10aabcd 100644
--- a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequest2Test.java
+++ b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequest2Test.java
@@ -1341,6 +1341,47 @@ public void getResponseHeader() throws Exception {
loadPageVerifyTitle2(html);
}
+ /**
+ * Set-Cookie and Set-Cookie2 are forbidden response header names; scripts must not see them
+ * (otherwise the value of HttpOnly cookies is readable).
+ * @throws Exception if the test fails
+ */
+ @Test
+ @Alerts({"null", "null", "null", "false", "false", "abc"})
+ public void getResponseHeaderSetCookie() throws Exception {
+ final String html = DOCTYPE_HTML
+ + "\n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + " \n"
+ + "";
+
+ final List responseHeaders = new ArrayList<>();
+ responseHeaders.add(new NameValuePair("Set-Cookie", "session=secret; HttpOnly"));
+ responseHeaders.add(new NameValuePair("Set-Cookie2", "legacy=secret"));
+ responseHeaders.add(new NameValuePair("X-Custom", "abc"));
+ getMockWebConnection().setResponse(URL_SECOND, "", 200, "OK", MimeType.TEXT_XML,
+ responseHeaders);
+
+ loadPageVerifyTitle2(html);
+ }
+
/**
* @throws Exception if the test fails
*/