Bug 1304623 - Create a pref to control the default referrer policy - part 1. r=bkelly

MozReview-Commit-ID: 6R7kLB6jvhP
This commit is contained in:
Thomas Nguyen 2017-01-05 11:30:07 +08:00
parent 5724214b50
commit d1229b6f90
8 changed files with 141 additions and 7 deletions

View File

@ -76,6 +76,10 @@ var tests = (function*() {
var iframe = document.getElementById("testframe");
for (var j = 0; j < testCases.length; j++) {
if (testCases[j].PREFS) {
yield SpecialPowers.pushPrefEnv({"set": testCases[j].PREFS}, advance);
}
var actions = testCases[j].ACTION;
var tests = testCases[j].TESTS;
for (var k = 0; k < actions.length; k++) {

View File

@ -191,6 +191,25 @@ function createLinkPageUsingRefferer(aMetaPolicy, aAttributePolicy, aNewAttribut
</html>`;
}
function createFetchUserControlRPTestCase(aName, aSchemeFrom, aSchemeTo) {
var srcUrl = createTestUrl("", "test", aName, "iframe", aSchemeFrom, aSchemeTo);
return `<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test user control referrer policies</title>
</head>
<body>
<script>
fetch("${srcUrl}", {referrerPolicy: ""}).then(function (response) {
window.parent.postMessage("childLoadComplete", "http://mochi.test:8888");
});
</script>
</body>
</html>`;
}
function buildLinkString(aPolicy, aName, aRelString, aSchemeFrom, aSchemeTo, aTestType) {
var result;
var href = '';
@ -213,6 +232,8 @@ function handleRequest(request, response) {
var schemeFrom = params.get("SCHEME_FROM") || "http";
var schemeTo = params.get("SCHEME_TO") || "http";
response.setHeader("Access-Control-Allow-Origin", "*", false);
if (action === "resetState") {
setSharedState(SHARED_KEY, "{}");
response.write("");
@ -386,6 +407,11 @@ function handleRequest(request, response) {
return;
}
if (action === "generate-fetch-user-control-policy-test") {
response.write(createFetchUserControlRPTestCase(name, schemeFrom, schemeTo));
return;
}
response.write("I don't know action " + action);
return;
}

View File

@ -108,6 +108,8 @@ FetchDriver::Fetch(FetchDriverObserver* aObserver)
nsresult
FetchDriver::HttpFetch()
{
MOZ_ASSERT(NS_IsMainThread());
// Step 1. "Let response be null."
mResponse = nullptr;
nsresult rv;
@ -277,7 +279,9 @@ FetchDriver::HttpFetch()
// If requests referrer policy is the empty string,
// then set requests referrer policy to "no-referrer-when-downgrade".
if (mRequest->ReferrerPolicy_() == ReferrerPolicy::_empty) {
mRequest->SetReferrerPolicy(net::RP_No_Referrer_When_Downgrade);
net::ReferrerPolicy referrerPolicy =
static_cast<net::ReferrerPolicy>(NS_GetDefaultReferrerPolicy());
mRequest->SetReferrerPolicy(referrerPolicy);
}
rv = FetchUtil::SetRequestReferrer(mPrincipal,

View File

@ -82,7 +82,7 @@ InternalRequest::InternalRequest(const nsACString& aURL,
, mContentPolicyType(nsIContentPolicy::TYPE_FETCH)
, mReferrer(NS_LITERAL_STRING(kFETCH_CLIENT_REFERRER_STR))
, mReferrerPolicy(ReferrerPolicy::_empty)
, mEnvironmentReferrerPolicy(net::RP_Default)
, mEnvironmentReferrerPolicy(net::RP_Unset)
, mMode(RequestMode::No_cors)
, mCredentialsMode(RequestCredentials::Omit)
, mResponseTainting(LoadTainting::Basic)
@ -121,7 +121,7 @@ InternalRequest::InternalRequest(const nsACString& aURL,
, mContentPolicyType(aContentPolicyType)
, mReferrer(aReferrer)
, mReferrerPolicy(aReferrerPolicy)
, mEnvironmentReferrerPolicy(net::RP_Default)
, mEnvironmentReferrerPolicy(net::RP_Unset)
, mMode(aMode)
, mCredentialsMode(aRequestCredentials)
, mResponseTainting(LoadTainting::Basic)

View File

@ -31,6 +31,8 @@ support-files =
!/dom/xhr/tests/temporaryFileBlob.sjs
!/dom/html/test/form_submit_server.sjs
!/dom/security/test/cors/file_CrossSiteXHR_server.sjs
!/dom/base/test/referrer_helper.js
!/dom/base/test/referrer_testserver.sjs
[test_headers.html]
[test_headers_sw_reroute.html]
@ -47,6 +49,7 @@ skip-if = toolkit == 'android' && debug # Bug 1210282
skip-if = toolkit == 'android' && debug # Bug 1210282
[test_fetch_cors_sw_empty_reroute.html]
skip-if = toolkit == 'android' && debug # Bug 1210282
[test_fetch_user_control_rp.html]
[test_formdataparsing.html]
[test_formdataparsing_sw_reroute.html]
[test_request.html]

View File

@ -0,0 +1,97 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test fetch user control referrer policy Bug 1304623</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript;version=1.7">
const SJS = "://example.com/tests/dom/base/test/referrer_testserver.sjs?";
const PARAMS = ["SCHEME_FROM", "SCHEME_TO"];
const testCases = [
{ACTION: ["generate-fetch-user-control-policy-test"],
PREFS: [['network.http.referer.userControlPolicy', 0]],
TESTS: [
// 0. No referrer.
{NAME: 'default-policy-value-no-referrer-https-http',
DESC: 'default-policy-value-no-referrer-https-http',
SCHEME_FROM: 'https',
SCHEME_TO: 'http',
RESULT: 'none'},
{NAME: 'default-policy-value-no-referrer-https-https',
DESC: 'default-policy-value-no-referrer-https-https',
SCHEME_FROM: 'https',
SCHEME_TO: 'https',
RESULT: 'none'}],
},
{ACTION: ["generate-fetch-user-control-policy-test"],
PREFS: [['network.http.referer.userControlPolicy', 1]],
TESTS: [
// 1. Same origin.
{NAME: 'default-policy-value-same-origin-https-http',
DESC: 'default-policy-value-same-origin-https-http',
SCHEME_FROM: 'https',
SCHEME_TO: 'http',
RESULT: 'none'},
{NAME: 'default-policy-value-same-origin-http-https',
DESC: 'default-policy-value-same-origin-http-https',
SCHEME_FROM: 'http',
SCHEME_TO: 'https',
RESULT: 'none'},
{NAME: 'default-policy-value-same-origin-https-https',
DESC: 'default-policy-value-same-origin-https-https',
SCHEME_FROM: 'https',
SCHEME_TO: 'https',
RESULT: 'full'}],
},
{ACTION: ["generate-fetch-user-control-policy-test"],
PREFS: [['network.http.referer.userControlPolicy', 2]],
TESTS: [
// 2. strict-origin-when-cross-origin.
{NAME: 'default-policy-value-strict-origin-when-cross-origin-https-http',
DESC: 'default-policy-value-strict-origin-when-cross-origin-https-http',
SCHEME_FROM: 'https',
SCHEME_TO: 'http',
RESULT: 'none'},
{NAME: 'default-policy-value-strict-origin-when-cross-origin-http-https',
DESC: 'default-policy-value-strict-origin-when-cross-origin-http-https',
SCHEME_FROM: 'http',
SCHEME_TO: 'https',
RESULT: 'origin'},
{NAME: 'default-policy-value-strict-origin-when-cross-origin-https-https',
DESC: 'default-policy-value-strict-origin-when-cross-origin-https-https',
SCHEME_FROM: 'https',
SCHEME_TO: 'https',
RESULT: 'full'}],
},
{ACTION: ["generate-fetch-user-control-policy-test"],
PREFS: [['network.http.referer.userControlPolicy', 3]],
TESTS: [
// 3. Default no-referrer-when-downgrade.
{NAME: 'default-policy-value-no-referrer-when-downgrade-https-http',
DESC: 'default-policy-value-no-referrer-when-downgrade-https-http',
SCHEME_FROM: 'https',
SCHEME_TO: 'http',
RESULT: 'none'},
{NAME: 'default-policy-value-no-referrer-when-downgrade-http-https',
DESC: 'default-policy-value-no-referrer-when-downgrade-http-https',
SCHEME_FROM: 'http',
SCHEME_TO: 'https',
RESULT: 'full'},
{NAME: 'default-policy-value-no-referrer-when-downgrade-https-https',
DESC: 'default-policy-value-no-referrer-when-downgrade-https-https',
SCHEME_FROM: 'https',
SCHEME_TO: 'https',
RESULT: 'full'}],
},
];
</script>
<script type="application/javascript;version=1.7" src="/tests/dom/base/test/referrer_helper.js"></script>
</head>
<body onload="tests.next();">
<iframe id="testframe"></iframe>
</body>
</html>

View File

@ -206,7 +206,7 @@ ChannelFromScriptURL(nsIPrincipal* principal,
if (nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel)) {
mozilla::net::ReferrerPolicy referrerPolicy = parentDoc ?
parentDoc->GetReferrerPolicy() : mozilla::net::RP_Default;
parentDoc->GetReferrerPolicy() : mozilla::net::RP_Unset;
rv = nsContentUtils::SetFetchReferrerURIWithPolicy(principal, parentDoc,
httpChannel, referrerPolicy);
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -1233,7 +1233,7 @@ private:
// Set ReferrerPolicy, default value is set in GetReferrerPolicy
bool hasReferrerPolicy = false;
uint32_t rp = mozilla::net::RP_Default;
uint32_t rp = mozilla::net::RP_Unset;
rv = csp->GetReferrerPolicy(&rp, &hasReferrerPolicy);
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -1611,7 +1611,7 @@ NS_IMPL_ISUPPORTS(TimerThreadEventTarget, nsIEventTarget)
WorkerLoadInfo::WorkerLoadInfo()
: mWindowID(UINT64_MAX)
, mServiceWorkerID(0)
, mReferrerPolicy(net::RP_Default)
, mReferrerPolicy(net::RP_Unset)
, mFromWindow(false)
, mEvalAllowed(false)
, mReportCSPViolations(false)
@ -3501,7 +3501,7 @@ WorkerPrivateParent<Derived>::SetPrincipal(nsIPrincipal* aPrincipal,
&mLoadInfo.mEvalAllowed);
// Set ReferrerPolicy
bool hasReferrerPolicy = false;
uint32_t rp = mozilla::net::RP_Default;
uint32_t rp = mozilla::net::RP_Unset;
nsresult rv = mLoadInfo.mCSP->GetReferrerPolicy(&rp, &hasReferrerPolicy);
NS_ENSURE_SUCCESS_VOID(rv);