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

<body>
<release version="5.9.0" date="xxxx xx, 2026" description="Firefox 160, Chrome/Edge 158, Fetch, Intl, CSP, Cookies, Bugfixes">
<action type="fix" dev="Vidya m">
XMLHttpRequest and fetch(): cross-origin requests send the CORS preflight if the value of the
Accept, Accept-Language, Content-Language or Content-Type header is not CORS-safelisted
(e.g. 'Content-Type: application/x-www-form-urlencoded+json'); before only the name and the
prefix of the content type were checked.
</action>
<action type="fix" dev="rbri">
fetch() and XMLHttpRequest.setRequestHeader() ignore the method override headers (X-HTTP-Method,
X-HTTP-Method-Override, X-Method-Override) if the value contains a forbidden method (CONNECT, TRACE,
Expand Down
22 changes: 9 additions & 13 deletions src/main/java/org/htmlunit/javascript/host/fetch/CorsLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlunit.FormEncodingType;
import org.htmlunit.HttpHeader;
import org.htmlunit.HttpMethod;
import org.htmlunit.WebClient;
Expand Down Expand Up @@ -609,25 +608,22 @@ private static boolean isPreflightMethodAllowed(final String method, final WebRe
}

/**
* Checks whether the given header name requires a preflight request.
* Checks whether the given header requires a preflight request. Accept, Accept-Language,
* Content-Language and Content-Type need no preflight only if the value is CORS-safelisted.
*
* @param name the header name (MUST be lower-case for performance reasons)
* @param value the header value
* @return {@code true} if this header requires a preflight
* @see <a href="https://fetch.spec.whatwg.org/#cors-safelisted-request-header">Fetch Standard -
* CORS-safelisted request-header</a>
*/
private static boolean isPreflightHeader(final String name, final String value) {
if (HttpHeader.CONTENT_TYPE_LC.equals(name)) {
final String lcValue = value.toLowerCase(Locale.ROOT);
return !lcValue.startsWith(FormEncodingType.URL_ENCODED.getName())
&& !lcValue.startsWith(FormEncodingType.MULTIPART.getName())
&& !lcValue.startsWith(FormEncodingType.TEXT_PLAIN.getName());
}
return !(HttpHeader.ACCEPT_LC.equals(name)
|| HttpHeader.ACCEPT_LANGUAGE_LC.equals(name)
|| HttpHeader.CONTENT_LANGUAGE_LC.equals(name)
|| HttpHeader.REFERER_LC.equals(name)
if (HttpHeader.REFERER_LC.equals(name)
|| "accept-encoding".equals(name)
|| HttpHeader.ORIGIN_LC.equals(name));
|| HttpHeader.ORIGIN_LC.equals(name)) {
return false;
}
return !Headers.isNoCorsSafelistedRequestHeader(name, value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,92 @@ public void preflight_withCredentials_wildcardHeaders() throws Exception {
doPreflightTestReceived(true, "GET", "http://localhost:" + PORT, "true", null, "*");
}

/**
* The essence of the content type has to be one of the three safelisted ones,
* a prefix is not enough.
* @throws Exception if the test fails.
*/
@Test
@Alerts({"NetworkError/DOMException", "4", "0", "nothing"})
public void preflight_contentTypeNotSafelisted() throws Exception {
doPreflightTestReceivedHeader("Content-Type", "application/x-www-form-urlencoded+json");
}

/**
* Accept-Language values with other characters are not safelisted.
* @throws Exception if the test fails.
*/
@Test
@Alerts({"NetworkError/DOMException", "4", "0", "nothing"})
public void preflight_acceptLanguageNotSafelisted() throws Exception {
doPreflightTestReceivedHeader("Accept-Language", "en_US");
}

/**
* Values longer than 128 characters are not safelisted.
* @throws Exception if the test fails.
*/
@Test
@Alerts({"NetworkError/DOMException", "4", "0", "nothing"})
public void preflight_contentLanguageTooLong() throws Exception {
doPreflightTestReceivedHeader("Content-Language", "de".repeat(65));
}

/**
* Sends a POST with credentials and the given header; the preflight response does not allow
* any header. Logs afterwards the request that reached the server ('nothing' if the request was not sent).
*/
private void doPreflightTestReceivedHeader(final String headerName, final String headerValue)
throws Exception {
final String html = DOCTYPE_HTML
+ "<html><head>\n"
+ "<script>\n"
+ LOG_TITLE_FUNCTION
+ "function test() {\n"
+ " var url = 'http://' + window.location.hostname + ':" + PORT2 + "/preflight2';\n"
+ " var xhr = new XMLHttpRequest();\n"
+ " try {\n"

// a unique url for every test; browsers cache the preflight result per url
+ " xhr.open('POST', url + '?id=" + System.currentTimeMillis() + "', false);\n"

+ " xhr.withCredentials = true;\n"
+ " xhr.setRequestHeader('" + headerName + "', '" + headerValue + "');\n"
+ " xhr.send('{\"amount\":1000}');\n"
+ " } catch(e) { logEx(e) }\n"
+ " log(xhr.readyState);\n"
+ " log(xhr.status);\n"

+ " var received = new XMLHttpRequest();\n"
+ " received.open('GET', url + '?received', false);\n"
+ " received.send();\n"
+ " log(received.responseText);\n"
+ "}\n"
+ "</script>\n"
+ "</head>\n"
+ "<body onload='test()'></body></html>";

PreflightReceivedServlet.ACCESS_CONTROL_ALLOW_ORIGIN_ = "http://localhost:" + PORT;
PreflightReceivedServlet.ACCESS_CONTROL_ALLOW_CREDENTIALS_ = "true";
PreflightReceivedServlet.ACCESS_CONTROL_ALLOW_METHODS_ = null;
PreflightReceivedServlet.ACCESS_CONTROL_ALLOW_HEADERS_ = null;
PreflightReceivedServlet.RECEIVED_ = null;

final Map<String, Class<? extends Servlet>> servlets2 = new HashMap<>();
servlets2.put("/preflight2", PreflightReceivedServlet.class);
startWebServer2(".", servlets2);

final List<NameValuePair> responseHeader = new ArrayList<>();
responseHeader.add(new NameValuePair("Set-Cookie", "cookie=sweet"));

final URL url = new URL(URL_FIRST, "/preflight1");
getMockWebConnection().setResponse(url, html,
200, "OK", "text/html;charset=ISO-8859-1", ISO_8859_1, responseHeader);

loadPage2(url, null);
verifyTitle2(getWebDriver(), getExpectedAlerts());
}

/**
* Sends a request that needs a preflight and logs afterwards the request that
* reached the server ('nothing' if the request was not sent).
Expand Down Expand Up @@ -907,6 +993,15 @@ protected void doPut(final HttpServletRequest request, final HttpServletResponse
received(request, response);
}

/**
* {@inheritDoc}
*/
@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
throws IOException {
received(request, response);
}

private static void received(final HttpServletRequest request, final HttpServletResponse response)
throws IOException {
RECEIVED_ = request.getMethod() + " " + request.getHeader(HttpHeader.COOKIE);
Expand Down
Loading