From 983d45615ccbeb51bcafeb66327a84abc1156754 Mon Sep 17 00:00:00 2001 From: Vidya m Date: Fri, 20 Nov 2026 15:18:16 +0530 Subject: [PATCH] do not let XMLHttpRequest.setRequestHeader() set the Origin header XMLHttpRequest.isAuthorizedHeader() did not reject Origin, so a page could replace the Origin header that open() sets. CorsLoader compares Access-Control-Allow-Origin with the Origin header of the request (also for the preflight) and Origin does not trigger a preflight, so a page from another origin that set Origin to the origin a server trusts could send a request with credentials to that server and read the response. The list now also contains the other forbidden request header names of the Fetch standard that were missing (Access-Control-Request-Headers, Access-Control-Request-Method, DNT, Set-Cookie); the Headers class of fetch() already rejects all of them. --- src/changes/changes.xml | 5 +++ .../javascript/host/xml/XMLHttpRequest.java | 7 ++-- .../host/xml/XMLHttpRequestCORSTest.java | 34 +++++++++++++++++++ .../host/xml/XMLHttpRequestTest.java | 7 ++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 557c46c74e..ca30b1f2a6 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -8,6 +8,11 @@ + + XMLHttpRequest.setRequestHeader() ignores the forbidden request headers Origin, DNT, Set-Cookie, + Access-Control-Request-Headers and Access-Control-Request-Method like the browsers; a page was able + to replace the Origin header and by this pass the CORS check of a server that trusts another origin. + csp: when simulating Chrome/Edge, the securitypolicyviolation event for an XMLHttpRequest blocked because of the redirect target is fired after the error event (like Chrome). 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 13d6109994..13b97138ad 100644 --- a/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java +++ b/src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java @@ -153,10 +153,11 @@ public class XMLHttpRequest extends XMLHttpRequestEventTarget { private static final HashSet PROHIBITED_HEADERS_ = new HashSet<>(Arrays.asList( "accept-charset", HttpHeader.ACCEPT_ENCODING_LC, + "access-control-request-headers", "access-control-request-method", HttpHeader.CONNECTION_LC, HttpHeader.CONTENT_LENGTH_LC, HttpHeader.COOKIE_LC, "cookie2", - "content-transfer-encoding", "date", "expect", - HttpHeader.HOST_LC, "keep-alive", HttpHeader.REFERER_LC, "te", "trailer", "transfer-encoding", - "upgrade", HttpHeader.USER_AGENT_LC, "via")); + "content-transfer-encoding", "date", "dnt", "expect", + HttpHeader.HOST_LC, "keep-alive", HttpHeader.ORIGIN_LC, HttpHeader.REFERER_LC, "set-cookie", + "te", "trailer", "transfer-encoding", "upgrade", HttpHeader.USER_AGENT_LC, "via")); private int state_; private WebRequest webRequest_; diff --git a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java index 18b7103888..2a6077942e 100644 --- a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java +++ b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java @@ -2094,6 +2094,40 @@ public void exposeHeadersAfterCrossOriginRedirect() throws Exception { loadAndVerify(headersPage("", originA() + "/pivot")); } + // --------------------------------------------------------------------------- + // the Origin header is a forbidden request header + // --------------------------------------------------------------------------- + + /** + * The Origin header can not be set by the page; the server gets the origin of the page. + * @throws Exception if the test fails. + */ + @Test + @Alerts({"4", "200", "origin=§§URL§§;cookie=null;auth=null"}) + public void setRequestHeaderOriginIgnored() throws Exception { + expandExpectedAlertsVariables(new URL(originA())); + startSecondServer(); + + loadAndVerify(xhrPage("", " xhr.setRequestHeader('Origin', '" + originB() + "');\n", + originB() + "/echo?acao=*")); + } + + /** + * A page can not pass the CORS check of a server that only trusts another origin + * by setting the Origin header to that origin. + * @throws Exception if the test fails. + */ + @Test + @Alerts({"NetworkError/DOMException", "4", "0", ""}) + public void setRequestHeaderOriginDoesNotPassCorsCheck() throws Exception { + startSecondServer(); + + loadAndVerify(xhrPage(" document.cookie = 'c=1; path=/';\n", + " xhr.withCredentials = true;\n" + + " xhr.setRequestHeader('Origin', '" + originB() + "');\n", + originB() + "/echo?acac=true&acao=" + enc(originB()))); + } + /** * Synchronous request; logs the (sorted, lower case) names of getAllResponseHeaders() * and the values of some headers using getResponseHeader(). diff --git a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestTest.java b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestTest.java index 849cd8371d..f4a739e854 100644 --- a/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestTest.java +++ b/src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestTest.java @@ -2380,10 +2380,11 @@ public void isAuthorizedHeader() throws Exception { assertTrue(XMLHttpRequest.isAuthorizedHeader(HttpHeader.CONTENT_TYPE)); final String[] headers = {"accept-charset", HttpHeader.ACCEPT_ENCODING_LC, + "access-control-request-headers", "access-control-request-method", HttpHeader.CONNECTION_LC, HttpHeader.CONTENT_LENGTH_LC, HttpHeader.COOKIE_LC, "cookie2", - "content-transfer-encoding", "date", - "expect", HttpHeader.HOST_LC, "keep-alive", HttpHeader.REFERER_LC, - "te", "trailer", "transfer-encoding", "upgrade", + "content-transfer-encoding", "date", "dnt", + "expect", HttpHeader.HOST_LC, "keep-alive", HttpHeader.ORIGIN_LC, HttpHeader.REFERER_LC, + "set-cookie", "te", "trailer", "transfer-encoding", "upgrade", HttpHeader.USER_AGENT_LC, "via" }; for (final String header : headers) { assertFalse(XMLHttpRequest.isAuthorizedHeader(header));