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

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Bugfixes">
<action type="fix" dev="Vidya m">
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.
</action>
<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
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(": ")
Expand Down Expand Up @@ -694,6 +697,9 @@ public String getResponseHeader(final String headerName) {
return null;
}
if (webResponse_ != null) {
if (isForbiddenResponseHeader(headerName)) {
return null;
}
return webResponse_.getResponseHeaderValue(headerName);
}

Expand All @@ -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 <a href="https://fetch.spec.whatwg.org/#forbidden-response-header-name">Fetch Standard</a>
* @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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
+ "<html>\n"
+ " <head>\n"
+ " <script>\n"
+ LOG_TITLE_FUNCTION
+ " function test() {\n"
+ " var request = new XMLHttpRequest();\n"
+ " request.open('GET', '" + URL_SECOND + "', false);\n"
+ " request.send();\n"
+ " log(request.getResponseHeader('Set-Cookie'));\n"
+ " log(request.getResponseHeader('set-cookie'));\n"
+ " log(request.getResponseHeader('Set-Cookie2'));\n"
+ " var headers = request.getAllResponseHeaders().toLowerCase();\n"
+ " log(headers.indexOf('set-cookie') != -1);\n"
+ " log(headers.indexOf('secret') != -1);\n"
+ " log(request.getResponseHeader('X-Custom'));\n"
+ " }\n"
+ " </script>\n"
+ " </head>\n"
+ " <body onload='test()'>\n"
+ " </body>\n"
+ "</html>";

final List<NameValuePair> 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, "<xml></xml>", 200, "OK", MimeType.TEXT_XML,
responseHeaders);

loadPageVerifyTitle2(html);
}

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