Skip to content

Commit 0deb496

Browse files
committed
do not load local files for async xhr if file protocol is not allowed
1 parent b1bdd4d commit 0deb496

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ void doSend() {
998998
fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
999999
fireJavascriptEvent(Event.TYPE_ERROR);
10001000
fireJavascriptEvent(Event.TYPE_LOAD_END);
1001+
return;
10011002
}
10021003
else {
10031004
throw JavaScriptEngine.asJavaScriptException(

‎src/test/java/org/htmlunit/javascript/host/xml/XMLHttpRequest4Test.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,21 @@ public void sendLocalSubFileAllowed() throws Exception {
217217
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
218218
assertEquals(getExpectedAlerts()[0], page.getTitleText());
219219
}
220+
221+
/**
222+
* @throws Exception if the test fails
223+
*/
224+
@Test
225+
@Alerts("readystatechange§1§0§readystatechange§4§0§error§")
226+
public void sendLocalFileAsyncNotAllowed() throws Exception {
227+
final URL fileURL = getClass().getClassLoader().getResource("testfiles/localxmlhttprequest/index_async.html");
228+
final File file = new File(fileURL.toURI());
229+
assertTrue("File '" + file.getAbsolutePath() + "' does not exist", file.exists());
230+
231+
final WebClient client = getWebClient();
232+
final HtmlPage page = client.getPage(fileURL);
233+
234+
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(1000));
235+
assertEquals(getExpectedAlerts()[0], page.getTitleText());
236+
}
220237
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head><script>
3+
function log(msg) { window.document.title += msg + '\u00a7'; }
4+
function test() {
5+
try {
6+
var xhr = new XMLHttpRequest();
7+
xhr.onreadystatechange = function(event) {
8+
log(event.type);
9+
log(xhr.readyState);
10+
log(xhr.responseText.length);
11+
};
12+
xhr.onerror = function(event) {
13+
log(event.type);
14+
};
15+
xhr.open('GET', 'index_async.html', true);
16+
xhr.send('');
17+
} catch (exception) {
18+
log(exception.name);
19+
}
20+
}
21+
</script></head>
22+
<body onload='test()'>
23+
</body></html>

0 commit comments

Comments
 (0)