From 63543e10fdc8339c1925a7ab1ca014d4300efc64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 18 Mar 2018 23:41:47 +0100 Subject: [PATCH] Bug 1446342: Don't include forms in the scope chain for XBL datetime bindings. r=smaug Reviewers: smaug Bug #: 1446342 Differential Revision: https://phabricator.services.mozilla.com/D769 MozReview-Commit-ID: HK7nChYf0X6 --- dom/base/nsJSUtils.cpp | 52 +++++++++++++++++++++---- dom/base/nsJSUtils.h | 12 ++++++ dom/xbl/nsXBLProtoImplField.cpp | 8 ++-- dom/xbl/nsXBLProtoImplField.h | 2 +- dom/xbl/nsXBLProtoImplMethod.cpp | 8 ++-- dom/xbl/nsXBLProtoImplMethod.h | 2 +- dom/xbl/nsXBLPrototypeBinding.cpp | 16 +++++++- dom/xbl/nsXBLPrototypeBinding.h | 8 +++- dom/xbl/nsXBLPrototypeHandler.cpp | 2 +- dom/xbl/nsXBLSerialize.h | 13 ++++--- dom/xbl/test/file_fieldScopeChain.xml | 7 +++- dom/xbl/test/test_fieldScopeChain.html | 4 ++ toolkit/content/widgets/datetimebox.xml | 5 ++- xpcom/ds/nsGkAtomList.h | 1 + 14 files changed, 112 insertions(+), 28 deletions(-) diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index b35706ebb1cd..68e6b46b5911 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -25,6 +25,7 @@ #include "xpcpublic.h" #include "nsContentUtils.h" #include "nsGlobalWindow.h" +#include "nsXBLPrototypeBinding.h" #include "mozilla/CycleCollectedJSContext.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/Date.h" @@ -462,19 +463,31 @@ nsJSUtils::ModuleEvaluate(JSContext* aCx, JS::Handle aModule) return NS_OK; } +static bool +AddScopeChainItem(JSContext* aCx, + nsINode* aNode, + JS::AutoObjectVector& aScopeChain) +{ + JS::RootedValue val(aCx); + if (!GetOrCreateDOMReflector(aCx, aNode, &val)) { + return false; + } + + if (!aScopeChain.append(&val.toObject())) { + return false; + } + + return true; +} + /* static */ bool nsJSUtils::GetScopeChainForElement(JSContext* aCx, - mozilla::dom::Element* aElement, + Element* aElement, JS::AutoObjectVector& aScopeChain) { for (nsINode* cur = aElement; cur; cur = cur->GetScopeChainParent()) { - JS::RootedValue val(aCx); - if (!GetOrCreateDOMReflector(aCx, cur, &val)) { - return false; - } - - if (!aScopeChain.append(&val.toObject())) { + if (!AddScopeChainItem(aCx, cur, aScopeChain)) { return false; } } @@ -482,6 +495,31 @@ nsJSUtils::GetScopeChainForElement(JSContext* aCx, return true; } +/* static */ +bool +nsJSUtils::GetScopeChainForXBL(JSContext* aCx, + Element* aElement, + const nsXBLPrototypeBinding& aProtoBinding, + JS::AutoObjectVector& aScopeChain) +{ + if (!aElement) { + return true; + } + + if (!aProtoBinding.SimpleScopeChain()) { + return GetScopeChainForElement(aCx, aElement, aScopeChain); + } + + if (!AddScopeChainItem(aCx, aElement, aScopeChain)) { + return false; + } + + if (!AddScopeChainItem(aCx, aElement->OwnerDoc(), aScopeChain)) { + return false; + } + return true; +} + /* static */ void nsJSUtils::ResetTimeZone() diff --git a/dom/base/nsJSUtils.h b/dom/base/nsJSUtils.h index 7f0bf0f044bd..1ac3607dc1d9 100644 --- a/dom/base/nsJSUtils.h +++ b/dom/base/nsJSUtils.h @@ -25,6 +25,7 @@ class nsIScriptContext; class nsIScriptElement; class nsIScriptGlobalObject; +class nsXBLPrototypeBinding; namespace mozilla { namespace dom { @@ -202,6 +203,17 @@ public: mozilla::dom::Element* aElement, JS::AutoObjectVector& aScopeChain); + // Returns a scope chain suitable for XBL execution. + // + // This is by default GetScopeChainForElemenet, but will be different if the + // element had the simpleScopeChain attribute. + // + // This is to prevent footguns like bug 1446342. + static bool GetScopeChainForXBL(JSContext* aCx, + mozilla::dom::Element* aBoundElement, + const nsXBLPrototypeBinding& aProtoBinding, + JS::AutoObjectVector& aScopeChain); + static void ResetTimeZone(); }; diff --git a/dom/xbl/nsXBLProtoImplField.cpp b/dom/xbl/nsXBLProtoImplField.cpp index 1c9c134ea013..db5380c62bed 100644 --- a/dom/xbl/nsXBLProtoImplField.cpp +++ b/dom/xbl/nsXBLProtoImplField.cpp @@ -207,7 +207,7 @@ InstallXBLField(JSContext* cx, nsXBLProtoImplField* field = protoBinding->FindField(fieldName); MOZ_ASSERT(field); - nsresult rv = field->InstallField(thisObj, protoBinding->DocURI(), installed); + nsresult rv = field->InstallField(thisObj, *protoBinding, installed); if (NS_SUCCEEDED(rv)) { return true; } @@ -372,7 +372,7 @@ nsXBLProtoImplField::InstallAccessors(JSContext* aCx, nsresult nsXBLProtoImplField::InstallField(JS::Handle aBoundNode, - nsIURI* aBindingDocURI, + const nsXBLPrototypeBinding& aProtoBinding, bool* aDidInstall) const { NS_PRECONDITION(aBoundNode, @@ -389,7 +389,7 @@ nsXBLProtoImplField::InstallField(JS::Handle aBoundNode, nsAutoMicroTask mt; nsAutoCString uriSpec; - nsresult rv = aBindingDocURI->GetSpec(uriSpec); + nsresult rv = aProtoBinding.DocURI()->GetSpec(uriSpec); if (NS_WARN_IF(NS_FAILED(rv))) { return rv; } @@ -432,7 +432,7 @@ nsXBLProtoImplField::InstallField(JS::Handle aBoundNode, JS::CompileOptions options(cx); options.setFileAndLine(uriSpec.get(), mLineNumber); JS::AutoObjectVector scopeChain(cx); - if (!nsJSUtils::GetScopeChainForElement(cx, boundElement, scopeChain)) { + if (!nsJSUtils::GetScopeChainForXBL(cx, boundElement, aProtoBinding, scopeChain)) { return NS_ERROR_OUT_OF_MEMORY; } rv = NS_OK; diff --git a/dom/xbl/nsXBLProtoImplField.h b/dom/xbl/nsXBLProtoImplField.h index 89cc846f0035..d444e1729dbd 100644 --- a/dom/xbl/nsXBLProtoImplField.h +++ b/dom/xbl/nsXBLProtoImplField.h @@ -33,7 +33,7 @@ public: void SetNext(nsXBLProtoImplField* aNext) { mNext = aNext; } nsresult InstallField(JS::Handle aBoundNode, - nsIURI* aBindingDocURI, + const nsXBLPrototypeBinding& aProtoBinding, bool* aDidInstall) const; nsresult InstallAccessors(JSContext* aCx, diff --git a/dom/xbl/nsXBLProtoImplMethod.cpp b/dom/xbl/nsXBLProtoImplMethod.cpp index b78fda681ea5..f0c58713c8de 100644 --- a/dom/xbl/nsXBLProtoImplMethod.cpp +++ b/dom/xbl/nsXBLProtoImplMethod.cpp @@ -259,7 +259,8 @@ nsXBLProtoImplMethod::Write(nsIObjectOutputStream* aStream) } nsresult -nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement) +nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement, + const nsXBLPrototypeBinding& aProtoBinding) { MOZ_ASSERT(aBoundElement->IsElement()); NS_PRECONDITION(IsCompiled(), "Can't execute uncompiled method"); @@ -300,8 +301,9 @@ nsXBLProtoImplAnonymousMethod::Execute(nsIContent* aBoundElement) true); JSContext* cx = aes.cx(); JS::AutoObjectVector scopeChain(cx); - if (!nsJSUtils::GetScopeChainForElement(cx, aBoundElement->AsElement(), - scopeChain)) { + if (!nsJSUtils::GetScopeChainForXBL(cx, aBoundElement->AsElement(), + aProtoBinding, + scopeChain)) { return NS_ERROR_OUT_OF_MEMORY; } MOZ_ASSERT(scopeChain.length() != 0); diff --git a/dom/xbl/nsXBLProtoImplMethod.h b/dom/xbl/nsXBLProtoImplMethod.h index d2cafccf97df..58205e20681e 100644 --- a/dom/xbl/nsXBLProtoImplMethod.h +++ b/dom/xbl/nsXBLProtoImplMethod.h @@ -138,7 +138,7 @@ public: nsXBLProtoImplMethod(aName) {} - nsresult Execute(nsIContent* aBoundElement); + nsresult Execute(nsIContent* aBoundElement, const nsXBLPrototypeBinding&); // Override InstallMember; these methods never get installed as members on // binding instantiations (though they may hang out in mMembers on the diff --git a/dom/xbl/nsXBLPrototypeBinding.cpp b/dom/xbl/nsXBLPrototypeBinding.cpp index de5634752a96..f244596dac3d 100644 --- a/dom/xbl/nsXBLPrototypeBinding.cpp +++ b/dom/xbl/nsXBLPrototypeBinding.cpp @@ -120,6 +120,7 @@ nsXBLPrototypeBinding::nsXBLPrototypeBinding() mKeyHandlersRegistered(false), mChromeOnlyContent(false), mBindToUntrustedContent(false), + mSimpleScopeChain(false), mResources(nullptr), mBaseNameSpaceID(kNameSpaceID_None) { @@ -242,6 +243,11 @@ nsXBLPrototypeBinding::SetBindingElement(Element* aElement) mBindToUntrustedContent = mBinding->AttrValueIs(kNameSpaceID_None, nsGkAtoms::bindToUntrustedContent, nsGkAtoms::_true, eCaseMatters); + + // TODO(emilio): Should we imply mBindToUntrustedContent -> mSimpleScopeChain? + mSimpleScopeChain = mBinding->AttrValueIs(kNameSpaceID_None, + nsGkAtoms::simpleScopeChain, + nsGkAtoms::_true, eCaseMatters); } bool @@ -282,7 +288,7 @@ nsXBLPrototypeBinding::BindingAttached(nsIContent* aBoundElement) { if (mImplementation && mImplementation->CompiledMembers() && mImplementation->mConstructor) - return mImplementation->mConstructor->Execute(aBoundElement); + return mImplementation->mConstructor->Execute(aBoundElement, *this); return NS_OK; } @@ -291,7 +297,7 @@ nsXBLPrototypeBinding::BindingDetached(nsIContent* aBoundElement) { if (mImplementation && mImplementation->CompiledMembers() && mImplementation->mDestructor) - return mImplementation->mDestructor->Execute(aBoundElement); + return mImplementation->mDestructor->Execute(aBoundElement, *this); return NS_OK; } @@ -854,6 +860,8 @@ nsXBLPrototypeBinding::Read(nsIObjectInputStream* aStream, (aFlags & XBLBinding_Serialize_ChromeOnlyContent) ? true : false; mBindToUntrustedContent = (aFlags & XBLBinding_Serialize_BindToUntrustedContent) ? true : false; + mSimpleScopeChain = + (aFlags & XBLBinding_Serialize_SimpleScopeChain) ? true : false; // nsXBLContentSink::ConstructBinding doesn't create a binding with an empty // id, so we don't here either. @@ -1077,6 +1085,10 @@ nsXBLPrototypeBinding::Write(nsIObjectOutputStream* aStream) flags |= XBLBinding_Serialize_BindToUntrustedContent; } + if (mSimpleScopeChain) { + flags |= XBLBinding_Serialize_SimpleScopeChain; + } + nsresult rv = aStream->Write8(flags); NS_ENSURE_SUCCESS(rv, rv); diff --git a/dom/xbl/nsXBLPrototypeBinding.h b/dom/xbl/nsXBLPrototypeBinding.h index 6a1d47dcc218..3aa4a4cfa345 100644 --- a/dom/xbl/nsXBLPrototypeBinding.h +++ b/dom/xbl/nsXBLPrototypeBinding.h @@ -293,8 +293,9 @@ public: nsIContent* aCopyRoot, mozilla::dom::Element* aTemplChild); - bool ChromeOnlyContent() { return mChromeOnlyContent; } - bool BindToUntrustedContent() { return mBindToUntrustedContent; } + bool ChromeOnlyContent() const { return mChromeOnlyContent; } + bool SimpleScopeChain() const { return mSimpleScopeChain; } + bool BindToUntrustedContent() const { return mBindToUntrustedContent; } typedef nsClassHashtable, nsXBLAttributeEntry> InnerAttributeTable; @@ -331,6 +332,9 @@ protected: bool mKeyHandlersRegistered; bool mChromeOnlyContent; bool mBindToUntrustedContent; + // True if constructors, handlers, etc for this binding would skip the scope + // chain for parent elements and go directly to the document. + bool mSimpleScopeChain; nsAutoPtr mResources; // If we have any resources, this will be non-null. diff --git a/dom/xbl/nsXBLPrototypeHandler.cpp b/dom/xbl/nsXBLPrototypeHandler.cpp index a2bab803ffdd..c7f4d30d2e53 100644 --- a/dom/xbl/nsXBLPrototypeHandler.cpp +++ b/dom/xbl/nsXBLPrototypeHandler.cpp @@ -365,7 +365,7 @@ nsXBLPrototypeHandler::ExecuteHandler(EventTarget* aTarget, // Build a scope chain in the XBL scope. RefPtr targetElement = do_QueryObject(scriptTarget); JS::AutoObjectVector scopeChain(cx); - ok = nsJSUtils::GetScopeChainForElement(cx, targetElement, scopeChain); + ok = nsJSUtils::GetScopeChainForXBL(cx, targetElement, *mPrototypeBinding, scopeChain); NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY); // Next, clone the generic handler with our desired scope chain. diff --git a/dom/xbl/nsXBLSerialize.h b/dom/xbl/nsXBLSerialize.h index 21b7017fc082..317f086f86fe 100644 --- a/dom/xbl/nsXBLSerialize.h +++ b/dom/xbl/nsXBLSerialize.h @@ -16,19 +16,22 @@ typedef uint8_t XBLBindingSerializeDetails; // A version number to ensure we don't load cached data in a different // file format. -#define XBLBinding_Serialize_Version 0x00000004 +#define XBLBinding_Serialize_Version 0x00000005 // Set for the first binding in a document -#define XBLBinding_Serialize_IsFirstBinding 1 +#define XBLBinding_Serialize_IsFirstBinding (1 << 0) // Set to indicate that nsXBLPrototypeBinding::mInheritStyle should be true -#define XBLBinding_Serialize_InheritStyle 2 +#define XBLBinding_Serialize_InheritStyle (1 << 1) // Set to indicate that nsXBLPrototypeBinding::mChromeOnlyContent should be true -#define XBLBinding_Serialize_ChromeOnlyContent 4 +#define XBLBinding_Serialize_ChromeOnlyContent (1 << 2) // Set to indicate that nsXBLPrototypeBinding::mBindToUntrustedContent should be true -#define XBLBinding_Serialize_BindToUntrustedContent 8 +#define XBLBinding_Serialize_BindToUntrustedContent (1 << 3) + +// Set to indicate that nsXBLPrototypeBinding::mSimpleScopeChain should be true +#define XBLBinding_Serialize_SimpleScopeChain (1 << 4) // Appears at the end of the serialized data to indicate that no more bindings // are present for this document. diff --git a/dom/xbl/test/file_fieldScopeChain.xml b/dom/xbl/test/file_fieldScopeChain.xml index 80e6cf477690..ba946657cb88 100644 --- a/dom/xbl/test/file_fieldScopeChain.xml +++ b/dom/xbl/test/file_fieldScopeChain.xml @@ -1,8 +1,13 @@ - + baz + + + diff --git a/dom/xbl/test/test_fieldScopeChain.html b/dom/xbl/test/test_fieldScopeChain.html index c688ca64fa8a..083fd4412773 100644 --- a/dom/xbl/test/test_fieldScopeChain.html +++ b/dom/xbl/test/test_fieldScopeChain.html @@ -29,6 +29,10 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1095660
+
+ + +
diff --git a/toolkit/content/widgets/datetimebox.xml b/toolkit/content/widgets/datetimebox.xml index b6aa82c7d076..9b4deea31e46 100644 --- a/toolkit/content/widgets/datetimebox.xml +++ b/toolkit/content/widgets/datetimebox.xml @@ -16,6 +16,7 @@ xmlns:xbl="http://www.mozilla.org/xbl"> @@ -428,6 +429,7 @@ @@ -1192,7 +1194,8 @@ - + diff --git a/xpcom/ds/nsGkAtomList.h b/xpcom/ds/nsGkAtomList.h index 1d4595f5b609..89531782022b 100644 --- a/xpcom/ds/nsGkAtomList.h +++ b/xpcom/ds/nsGkAtomList.h @@ -1667,6 +1667,7 @@ GK_ATOM(saturation, "saturation") GK_ATOM(set, "set") GK_ATOM(seed, "seed") GK_ATOM(shape_rendering, "shape-rendering") +GK_ATOM(simpleScopeChain, "simpleScopeChain") GK_ATOM(skewX, "skewX") GK_ATOM(skewY, "skewY") GK_ATOM(slope, "slope")