This repository has been archived by the owner on Apr 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Suborigin header and access control check for XHR
This adds the plumbing for adding the Suborigin header on requests, and it particularly adds it for XHR preflights. This also adds tests to verify the behavior for XHR. BUG=336894 Review URL: https://codereview.chromium.org/1435123002 Cr-Commit-Position: refs/heads/master@{#360220}
- Loading branch information
1 parent
ffa5a6c
commit 6ba78c3
Showing
12 changed files
with
159 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...http/tests/security/suborigins/crossorigin/suborigin-cors-xhr-failure-output-expected.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CONSOLE MESSAGE: line 9: If a Suborigin makes a request, a response without an Access-Control-Allow-Suborigin header should fail and should output a reasonable error message. | ||
CONSOLE ERROR: XMLHttpRequest cannot load http://127.0.0.1:8000/security/resources/cors-script.php?cors=false. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://foobar_127.0.0.1:8000' is therefore not allowed access. | ||
ALERT: PASS: XHR correctly failed | ||
|
33 changes: 33 additions & 0 deletions
33
...outTests/http/tests/security/suborigins/crossorigin/suborigin-cors-xhr-failure-output.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
header("Content-Security-Policy: suborigin foobar"); | ||
?> | ||
<!DOCTYPE html> | ||
<html> | ||
<body> | ||
<script> | ||
if (window.testRunner) { | ||
testRunner.waitUntilDone(); | ||
testRunner.dumpAsText(); | ||
} | ||
console.log("If a Suborigin makes a request, a response without an Access-Control-Allow-Suborigin header should fail and should output a reasonable error message."); | ||
|
||
function success() { | ||
alert("PASS: XHR correctly failed"); | ||
if (window.testRunner) | ||
testRunner.notifyDone(); | ||
} | ||
|
||
function failure() { | ||
alert("FAIL: XHR incorrectly succeeded"); | ||
if (window.testRunner) | ||
testRunner.notifyDone(); | ||
} | ||
|
||
var xhr = new XMLHttpRequest(); | ||
xhr.onerror = success; | ||
xhr.onload = failure; | ||
xhr.open("GET", "http://127.0.0.1:8000/security/resources/cors-script.php?cors=false"); | ||
xhr.send(); | ||
</script> | ||
</body> | ||
</html> |
76 changes: 76 additions & 0 deletions
76
...t/LayoutTests/http/tests/security/suborigins/crossorigin/suborigin-cors-xhr-preflight.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
header("Content-Security-Policy: suborigin foobar"); | ||
?> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Allow suborigin in HTTP header</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/security/suborigins/resources/suborigin-cors-lib.js"></script> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script> | ||
// XMLHttpRequest tests | ||
var SuboriginXHRTest = function(pass, name, src, crossoriginValue) { | ||
SuboriginTest.call(this, pass, "XHR: " + name, src, crossoriginValue); | ||
} | ||
|
||
SuboriginXHRTest.prototype.execute = function() { | ||
var test = async_test(this.name); | ||
var xhr = new XMLHttpRequest(); | ||
|
||
if (this.crossoriginValue === 'use-credentials') { | ||
xhr.withCredentials = true; | ||
} | ||
|
||
if (this.pass) { | ||
xhr.onload = function() { | ||
test.done(); | ||
}; | ||
xhr.onerror = function() { | ||
test.step(function() { assert_unreached("Good XHR fired error handler."); }); | ||
}; | ||
} else { | ||
xhr.onload = function() { | ||
test.step(function() { assert_unreached("Bad XHR successful."); }); | ||
}; | ||
xhr.onerror = function() { | ||
test.done(); | ||
}; | ||
} | ||
|
||
xhr.open("GET", this.src); | ||
xhr.send(); | ||
}; | ||
|
||
var xorigin_preflight_script = "http://127.0.0.1:8000/security/resources/cors-script.php"; | ||
|
||
// XHR preflight tests | ||
new SuboriginXHRTest( | ||
true, | ||
"Basic anonymous XHR preflight", | ||
xorigin_preflight_script + "?cors=http://foobar_127.0.0.1:8000", | ||
"anonymous").execute(); | ||
|
||
new SuboriginXHRTest( | ||
true, | ||
"Basic anonymous XHR preflight with '*' ACAO", | ||
xorigin_preflight_script + "?cors=*", | ||
"anonymous").execute(); | ||
|
||
new SuboriginXHRTest( | ||
true, | ||
"Basic XHR with credentials preflight", | ||
xorigin_preflight_script + "?cors=http://foobar_127.0.0.1:8000&credentials=true", | ||
"use-credentials").execute(); | ||
|
||
new SuboriginXHRTest( | ||
false, | ||
"Basic XHR with credentials preflight with '*' ACAO", | ||
xorigin_preflight_script + "?cors=*&credentials=true", | ||
"use-credentials").execute(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters