Bug 732209 part 5. Enforce CORS on stylesheet loads as needed. r=sicking

This commit is contained in:
Boris Zbarsky 2012-08-28 13:10:08 -04:00
parent f6bb4154ad
commit 28fcdcb432
4 changed files with 133 additions and 2 deletions

View File

@ -51,6 +51,7 @@
#include "nsGkAtoms.h"
#include "nsDocShellCID.h"
#include "nsIThreadInternal.h"
#include "nsCrossSiteListenerProxy.h"
#ifdef MOZ_XUL
#include "nsXULPrototypeCache.h"
@ -1558,9 +1559,36 @@ Loader::LoadSheet(SheetLoadData* aLoadData, StyleSheetState aSheetState)
// which owns us
nsCOMPtr<nsIUnicharStreamLoader> streamLoader;
rv = NS_NewUnicharStreamLoader(getter_AddRefs(streamLoader), aLoadData);
if (NS_FAILED(rv)) {
#ifdef DEBUG
mSyncCallback = false;
#endif
LOG_ERROR((" Failed to create stream loader"));
SheetComplete(aLoadData, rv);
return rv;
}
if (NS_SUCCEEDED(rv))
rv = channel->AsyncOpen(streamLoader, nullptr);
nsCOMPtr<nsIStreamListener> channelListener;
CORSMode ourCORSMode = aLoadData->mSheet->GetCORSMode();
if (ourCORSMode != CORS_NONE) {
bool withCredentials = (ourCORSMode == CORS_USE_CREDENTIALS);
LOG((" Doing CORS-enabled load; credentials %d", withCredentials));
channelListener =
new nsCORSListenerProxy(streamLoader, aLoadData->mLoaderPrincipal,
channel, withCredentials, &rv);
if (NS_FAILED(rv)) {
#ifdef DEBUG
mSyncCallback = false;
#endif
LOG_ERROR((" Initial CORS check failed"));
SheetComplete(aLoadData, rv);
return rv;
}
} else {
channelListener = streamLoader;
}
rv = channel->AsyncOpen(channelListener, nullptr);
#ifdef DEBUG
mSyncCallback = false;

View File

@ -186,6 +186,8 @@ MOCHITEST_FILES = test_acid3_test46.html \
test_load_events_on_stylesheets.html \
test_bug721136.html \
test_bug732153.html \
test_bug732209.html \
bug732209-css.sjs \
$(NULL)
ifdef MOZ_FLEXBOX

View File

@ -0,0 +1,19 @@
function handleRequest(request, response)
{
// First item will be the ID; other items are optional
var query = request.queryString.split(/&/);
response.setHeader("Content-Type", "text/css", false);
if (query.indexOf("cors-anonymous") != -1) {
response.setHeader("Access-Control-Allow-Origin", "*", false);
} else if (query.indexOf("cors-credentials") != -1 &&
request.hasHeader("Origin")) {
response.setHeader("Access-Control-Allow-Origin",
request.getHeader("Origin"), false)
response.setHeader("Access-Control-Allow-Credentials", "true", false);
}
response.write("#" + query[0] + " { color: green !important }" + "\n" +
"#" + query[0] + ".reverse { color: red !important }");
}

View File

@ -0,0 +1,82 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=732209
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 732209</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<style>
#content span { color: red; }
#content span.reverse { color: green; }
#content { display: block !important; }
#content span::before { content: attr(id); }
</style>
<link rel="stylesheet" href="bug732209-css.sjs?one">
<link rel="stylesheet" href="bug732209-css.sjs?two" crossorigin>
<link rel="stylesheet" href="bug732209-css.sjs?three" crossorigin="use-credentials">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?four">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?five"
crossorigin>
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?six"
crossorigin="use-credentials">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?seven&cors-anonymous">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?eight&cors-anonymous"
crossorigin>
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?nine&cors-anonymous"
crossorigin="use-credentials">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?ten&cors-credentials">
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?eleven&cors-credentials"
crossorigin>
<link rel="stylesheet"
href="http://example.com/tests/layout/style/test/bug732209-css.sjs?twelve&cors-credentials"
crossorigin="use-credentials">
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=732209">Mozilla Bug 732209</a>
<p id="display"></p>
<div id="content" style="display: none">
<span id="one"></span>
<span id="two"></span>
<span id="three"></span>
<span id="four"></span>
<span id="five" class="reverse"></span>
<span id="six" class="reverse"></span>
<span id="seven"></span>
<span id="eight"></span>
<span id="nine" class="reverse"></span>
<span id="ten"></span>
<span id="eleven"></span>
<span id="twelve"></span>
</div>
<pre id="test" style="color: red">
<script type="application/javascript">
/** Test for Bug 732209 **/
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
var spans = $("content").querySelectorAll("span");
for (var i = 0; i < spans.length; ++i) {
is(getComputedStyle(spans[i], "").color, "rgb(0, 128, 0)",
"Span " + spans[i].id + " should be green");
}
SimpleTest.finish();
});
</script>
</pre>
</body>
</html>