diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cc5f31c466..26f62f6425 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. + navigator.sendBeacon(): a beacon whose body has a type that is not CORS-safelisted (e.g. a Blob of type application/json) is a cors request; it is only sent to another origin if the preflight allows it. 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));