Skip to content

Commit 240506e

Browse files
duonglaiquangrbri
authored andcommitted
XMLHttpRequest: fix a bug where 'Access-Control-Allow-Origin: *' was not handled correctly
1 parent 684e362 commit 240506e

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/main/java/org/htmlunit/javascript/host/xml/XMLHttpRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,10 @@ private boolean isPreflightAuthorized(final WebResponse preflightResponse) {
11331133
if (HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS.equalsIgnoreCase(pair.getName())) {
11341134
String value = pair.getValue();
11351135
if (value != null) {
1136+
if ("*".equals(value)) {
1137+
// all headers are allowed
1138+
return true;
1139+
}
11361140
value = org.htmlunit.util.StringUtils.toRootLowerCase(value);
11371141
final String[] values = org.htmlunit.util.StringUtils.splitAtComma(value);
11381142
for (String part : values) {

src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequestCORSTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,45 @@ public void preflight_many_header_values() throws Exception {
615615
verifyTitle2(getWebDriver(), getExpectedAlerts());
616616
}
617617

618+
/**
619+
* @throws Exception if the test fails.
620+
*/
621+
@Test
622+
@Alerts({"4", "200"})
623+
public void preflight_wildcard_allow_headers() throws Exception {
624+
expandExpectedAlertsVariables(new URL("http://localhost:" + PORT));
625+
626+
final String html = DOCTYPE_HTML
627+
+ "<html><head>\n"
628+
+ "<script>\n"
629+
+ LOG_TITLE_FUNCTION
630+
+ "var xhr = new XMLHttpRequest();\n"
631+
+ "function test() {\n"
632+
+ " try {\n"
633+
+ " var url = 'http://' + window.location.hostname + ':" + PORT2 + "/preflight2';\n"
634+
+ " xhr.open('GET', url, false);\n"
635+
+ " xhr.setRequestHeader('X-PING', 'ping');\n"
636+
+ " xhr.setRequestHeader('X-PONG', 'pong');\n"
637+
+ " xhr.send();\n"
638+
+ " log(xhr.readyState);\n"
639+
+ " log(xhr.status);\n"
640+
+ " } catch(e) { logEx(e) }\n"
641+
+ "}\n"
642+
+ "</script>\n"
643+
+ "</head>\n"
644+
+ "<body onload='test()'></body></html>";
645+
646+
PreflightServerServlet.ACCESS_CONTROL_ALLOW_ORIGIN_ = "http://localhost:" + PORT;
647+
PreflightServerServlet.ACCESS_CONTROL_ALLOW_METHODS_ = "POST, GET, OPTIONS";
648+
PreflightServerServlet.ACCESS_CONTROL_ALLOW_HEADERS_ = "*";
649+
final Map<String, Class<? extends Servlet>> servlets2 = new HashMap<>();
650+
servlets2.put("/preflight2", PreflightServerServlet.class);
651+
startWebServer2(".", servlets2);
652+
653+
loadPage2(html, new URL(URL_FIRST, "/preflight1"));
654+
verifyTitle2(getWebDriver(), getExpectedAlerts());
655+
}
656+
618657
/**
619658
* @throws Exception if the test fails.
620659
*/

0 commit comments

Comments
 (0)