Bug 1330487 - Part 3: Propagate referrer policy from doc/sheet to URLExtraData r=heycam

Referrer policy argurment is passed from sheet/doc to URLExtraData, default
value is RP_Unset. We use default value in some cases, particularly when there's
no certain spec talks about that (svg for example)

MozReview-Commit-ID: 5VAX1ZUXD3i

Differential Revision: https://phabricator.services.mozilla.com/D1922

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Thomas Nguyen 2018-09-17 05:36:45 +00:00
parent a6c1be6216
commit 02f4dbbbf9
14 changed files with 74 additions and 30 deletions

View File

@ -440,7 +440,8 @@ nsIContent::GetURLDataForStyleAttr(nsIPrincipal* aSubjectPrincipal) const
// TODO: Cache this?
return MakeAndAddRef<URLExtraData>(OwnerDoc()->GetDocBaseURI(),
OwnerDoc()->GetDocumentURI(),
aSubjectPrincipal);
aSubjectPrincipal,
OwnerDoc()->GetReferrerPolicy());
}
// This also ignores the case that SVG inside XBL binding.
// But it is probably fine.

View File

@ -1737,12 +1737,13 @@ nsAttrValue::ParseStyleAttribute(const nsAString& aString,
}
}
RefPtr<URLExtraData> data = new URLExtraData(baseURI, docURI, principal);
RefPtr<DeclarationBlock> decl =
DeclarationBlock::FromCssText(aString,
data,
ownerDoc->GetCompatibilityMode(),
ownerDoc->CSSLoader());
RefPtr<URLExtraData> data = new URLExtraData(baseURI, docURI,
principal,
ownerDoc->GetReferrerPolicy());
RefPtr<DeclarationBlock> decl = DeclarationBlock::
FromCssText(aString, data,
ownerDoc->GetCompatibilityMode(),
ownerDoc->CSSLoader());
if (!decl) {
return false;
}

View File

@ -3739,11 +3739,13 @@ nsIDocument::DefaultStyleAttrURLData()
nsIURI* baseURI = GetDocBaseURI();
nsIURI* docURI = GetDocumentURI();
nsIPrincipal* principal = NodePrincipal();
mozilla::net::ReferrerPolicy policy = GetReferrerPolicy();
if (!mCachedURLData ||
mCachedURLData->BaseURI() != baseURI ||
mCachedURLData->GetReferrer() != docURI ||
mCachedURLData->GetReferrerPolicy() != policy ||
mCachedURLData->GetPrincipal() != principal) {
mCachedURLData = new URLExtraData(baseURI, docURI, principal);
mCachedURLData = new URLExtraData(baseURI, docURI, principal, policy);
}
return mCachedURLData;
}

View File

@ -2639,7 +2639,8 @@ CreateDeclarationForServo(nsCSSPropertyID aProperty,
RefPtr<URLExtraData> data =
new URLExtraData(aDocument->GetDocBaseURI(),
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
aDocument->NodePrincipal(),
aDocument->GetReferrerPolicy());
ServoCSSParser::ParsingEnvironment env(data,
aDocument->GetCompatibilityMode(),

View File

@ -20,6 +20,7 @@
#include "mozilla/URLExtraData.h"
#include "SVGObserverUtils.h"
#include "nsSVGUseFrame.h"
#include "mozilla/net/ReferrerPolicy.h"
NS_IMPL_NS_NEW_NAMESPACED_SVG_ELEMENT(Use)
@ -326,10 +327,12 @@ SVGUseElement::UpdateShadowTree()
newSVGElement->SetLength(nsGkAtoms::height, mLengthAttributes[ATTR_HEIGHT]);
}
// Store the base URI
// The specs do not say which referrer policy we should use, pass RP_Unset for
// now
mContentURLData = new URLExtraData(baseURI.forget(),
do_AddRef(OwnerDoc()->GetDocumentURI()),
do_AddRef(NodePrincipal()));
do_AddRef(NodePrincipal()),
mozilla::net::RP_Unset);
targetElement->AddMutationObserver(this);
}

View File

@ -1194,8 +1194,9 @@ MappedAttrParser::ParseMappedAttrValue(nsAtom* aMappedAttrName,
bool changed = false; // outparam for ParseProperty.
NS_ConvertUTF16toUTF8 value(aMappedAttrValue);
// FIXME (bug 1343964): Figure out a better solution for sending the base uri to servo
RefPtr<URLExtraData> data = new URLExtraData(mBaseURI, mDocURI,
mElement->NodePrincipal());
RefPtr<URLExtraData> data =
new URLExtraData(mBaseURI, mDocURI, mElement->NodePrincipal(),
mElement->OwnerDoc()->GetReferrerPolicy());
changed = Servo_DeclarationBlock_SetPropertyById(
mDecl->Raw(), propertyID, &value, false, data,
ParsingMode::AllowUnitlessLength,

View File

@ -1869,8 +1869,10 @@ nsXULPrototypeElement::SetAttrAt(uint32_t aPos, const nsAString& aValue,
// TODO: If we implement Content Security Policy for chrome documents
// as has been discussed, the CSP should be checked here to see if
// inline styles are allowed to be applied.
// XXX No specific specs talk about xul and referrer policy, pass Unset
RefPtr<URLExtraData> data =
new URLExtraData(aDocumentURI, aDocumentURI, principal);
new URLExtraData(aDocumentURI, aDocumentURI, principal,
mozilla::net::RP_Unset);
RefPtr<DeclarationBlock> declaration =
DeclarationBlock::FromCssText(
aValue, data, eCompatibility_FullStandards, nullptr);

View File

@ -21,6 +21,7 @@
#include "mozilla/StaticPrefs.h"
#include "nsIDocument.h"
#include "nsStyleUtil.h"
#include "mozilla/net/ReferrerPolicy.h"
namespace mozilla {
namespace dom {
@ -530,7 +531,10 @@ FontFace::GetURLExtraData() const
nsCOMPtr<nsIURI> docURI = window->GetDocumentURI();
nsCOMPtr<nsIURI> base = window->GetDocBaseURI();
RefPtr<URLExtraData> url = new URLExtraData(base, docURI, principal);
// We pass RP_Unset when creating URLExtraData object here because it's not
// going to result to change referer policy in a resource request.
RefPtr<URLExtraData> url = new URLExtraData(base, docURI, principal,
net::RP_Unset);
return url.forget();
}

View File

@ -86,7 +86,8 @@ ServoCSSParser::GetURLExtraData(nsIDocument* aDocument)
// FIXME this is using the wrong base uri (bug 1343919)
RefPtr<URLExtraData> url = new URLExtraData(aDocument->GetDocumentURI(),
aDocument->GetDocumentURI(),
aDocument->NodePrincipal());
aDocument->NodePrincipal(),
aDocument->GetReferrerPolicy());
return url.forget();
}

View File

@ -277,6 +277,15 @@ StyleSheet::SetDisabled(bool aDisabled)
}
}
already_AddRefed<URLExtraData>
StyleSheet::CreateURLExtraData() const
{
RefPtr<URLExtraData> data = new URLExtraData(GetBaseURI(),
GetSheetURI(),
Principal(),
GetReferrerPolicy());
return data.forget();
}
StyleSheetInfo::StyleSheetInfo(CORSMode aCORSMode,
ReferrerPolicy aReferrerPolicy,
const SRIMetadata& aIntegrity,
@ -1018,8 +1027,7 @@ StyleSheet::ParseSheet(css::Loader* aLoader,
MOZ_ASSERT(aLoadData);
MOZ_ASSERT(mParsePromise.IsEmpty());
RefPtr<StyleSheetParsePromise> p = mParsePromise.Ensure(__func__);
Inner().mURLData =
new URLExtraData(GetBaseURI(), GetSheetURI(), Principal()); // RefPtr
Inner().mURLData = CreateURLExtraData(); // RefPtr
const StyleUseCounters* useCounters = aLoader->GetDocument()
? aLoader->GetDocument()->GetStyleUseCounters()
@ -1079,7 +1087,7 @@ StyleSheet::ParseSheetSync(css::Loader* aLoader,
? aLoader->GetDocument()->GetStyleUseCounters()
: nullptr;
Inner().mURLData = new URLExtraData(GetBaseURI(), GetSheetURI(), Principal()); // RefPtr
Inner().mURLData = CreateURLExtraData(); // RefPtr
Inner().mContents = Servo_StyleSheet_FromUTF8Bytes(aLoader,
this,
aLoadData,

View File

@ -467,6 +467,8 @@ private:
// returns false.
bool AreRulesAvailable(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
already_AddRefed<URLExtraData> CreateURLExtraData() const;
protected:
// Internal methods which do not have security check and completeness check.
uint32_t InsertRuleInternal(const nsAString& aRule,

View File

@ -10,6 +10,7 @@
#include "mozilla/NullPrincipalURI.h"
#include "nsProxyRelease.h"
#include "mozilla/net/ReferrerPolicy.h"
namespace mozilla {
@ -22,7 +23,8 @@ URLExtraData::InitDummy()
RefPtr<nsIURI> referrer = baseURI;
sDummy = new URLExtraData(baseURI.forget(),
referrer.forget(),
NullPrincipal::CreateWithoutOriginAttributes());
NullPrincipal::CreateWithoutOriginAttributes(),
net::RP_Unset);
}
/* static */ void

View File

@ -12,6 +12,7 @@
#include "mozilla/dom/URL.h"
#include "mozilla/Move.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/net/ReferrerPolicy.h"
#include "nsCOMPtr.h"
#include "nsIPrincipal.h"
@ -23,9 +24,11 @@ struct URLExtraData
{
URLExtraData(already_AddRefed<nsIURI> aBaseURI,
already_AddRefed<nsIURI> aReferrer,
already_AddRefed<nsIPrincipal> aPrincipal)
already_AddRefed<nsIPrincipal> aPrincipal,
net::ReferrerPolicy aReferrerPolicy)
: mBaseURI(std::move(aBaseURI))
, mReferrer(std::move(aReferrer))
, mReferrerPolicy(aReferrerPolicy)
, mPrincipal(std::move(aPrincipal))
// When we hold the URI data of a style sheet, mReferrer is always
// equal to the sheet URI.
@ -34,15 +37,18 @@ struct URLExtraData
MOZ_ASSERT(mBaseURI);
}
URLExtraData(nsIURI* aBaseURI, nsIURI* aReferrer, nsIPrincipal* aPrincipal)
URLExtraData(nsIURI* aBaseURI, nsIURI* aReferrer, nsIPrincipal* aPrincipal,
net::ReferrerPolicy aReferrerPolicy)
: URLExtraData(do_AddRef(aBaseURI),
do_AddRef(aReferrer),
do_AddRef(aPrincipal)) {}
do_AddRef(aPrincipal),
aReferrerPolicy) {}
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(URLExtraData)
nsIURI* BaseURI() const { return mBaseURI; }
nsIURI* GetReferrer() const { return mReferrer; }
net::ReferrerPolicy GetReferrerPolicy() const { return mReferrerPolicy;}
nsIPrincipal* GetPrincipal() const { return mPrincipal; }
static URLExtraData* Dummy() {
@ -57,6 +63,7 @@ private:
nsCOMPtr<nsIURI> mBaseURI;
nsCOMPtr<nsIURI> mReferrer;
net::ReferrerPolicy mReferrerPolicy;
nsCOMPtr<nsIPrincipal> mPrincipal;
// True if mReferrer is a chrome:// URI.

View File

@ -11,6 +11,7 @@
#include "ServoBindings.h"
#include "mozilla/Encoding.h"
#include "mozilla/NullPrincipalURI.h"
#include "mozilla/net/ReferrerPolicy.h"
using namespace mozilla;
using namespace mozilla::css;
@ -31,8 +32,11 @@ static void ServoParsingBench(const StyleUseCounters* aCounters)
cssStr.Append(css);
ASSERT_EQ(Encoding::UTF8ValidUpTo(css), css.Length());
RefPtr<URLExtraData> data = new URLExtraData(
NullPrincipalURI::Create(), nullptr, NullPrincipal::CreateWithoutOriginAttributes());
RefPtr<URLExtraData> data =
new URLExtraData(NullPrincipalURI::Create(),
nullptr,
NullPrincipal::CreateWithoutOriginAttributes(),
mozilla::net::RP_Unset);
for (int i = 0; i < PARSING_REPETITIONS; i++) {
RefPtr<RawServoStyleSheetContents> stylesheet =
Servo_StyleSheet_FromUTF8Bytes(nullptr,
@ -52,9 +56,11 @@ static void ServoParsingBench(const StyleUseCounters* aCounters)
static void ServoSetPropertyByIdBench(const nsACString& css)
{
RefPtr<RawServoDeclarationBlock> block = Servo_DeclarationBlock_CreateEmpty().Consume();
RefPtr<URLExtraData> data = new URLExtraData(
NullPrincipalURI::Create(), nullptr, NullPrincipal::CreateWithoutOriginAttributes());
RefPtr<URLExtraData> data =
new URLExtraData(NullPrincipalURI::Create(),
nullptr,
NullPrincipal::CreateWithoutOriginAttributes(),
mozilla::net::RP_Unset);
ASSERT_TRUE(IsUTF8(css));
for (int i = 0; i < SETPROPERTY_REPETITIONS; i++) {
@ -74,8 +80,11 @@ static void ServoSetPropertyByIdBench(const nsACString& css)
static void ServoGetPropertyValueById() {
RefPtr<RawServoDeclarationBlock> block = Servo_DeclarationBlock_CreateEmpty().Consume();
RefPtr<URLExtraData> data = new URLExtraData(
NullPrincipalURI::Create(), nullptr, NullPrincipal::CreateWithoutOriginAttributes());
RefPtr<URLExtraData> data =
new URLExtraData(NullPrincipalURI::Create(),
nullptr,
NullPrincipal::CreateWithoutOriginAttributes(),
mozilla::net::RP_Unset);
NS_NAMED_LITERAL_CSTRING(css_, "10px");
const nsACString& css = css_;
Servo_DeclarationBlock_SetPropertyById(