diff options
Diffstat (limited to 'dom/security/trusted-types')
-rw-r--r-- | dom/security/trusted-types/TrustedHTML.cpp | 13 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedHTML.h | 19 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedScript.cpp | 13 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedScript.h | 19 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedScriptURL.cpp | 14 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedScriptURL.h | 18 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedTypePolicy.cpp | 43 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedTypePolicy.h | 72 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedTypePolicyFactory.cpp | 50 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedTypePolicyFactory.h | 105 | ||||
-rw-r--r-- | dom/security/trusted-types/TrustedTypeUtils.h | 35 | ||||
-rw-r--r-- | dom/security/trusted-types/moz.build | 27 |
12 files changed, 428 insertions, 0 deletions
diff --git a/dom/security/trusted-types/TrustedHTML.cpp b/dom/security/trusted-types/TrustedHTML.cpp new file mode 100644 index 0000000000..005cab8c63 --- /dev/null +++ b/dom/security/trusted-types/TrustedHTML.cpp @@ -0,0 +1,13 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/TrustedHTML.h" + +namespace mozilla::dom { + +IMPL_TRUSTED_TYPE_CLASS(TrustedHTML) + +} // namespace mozilla::dom diff --git a/dom/security/trusted-types/TrustedHTML.h b/dom/security/trusted-types/TrustedHTML.h new file mode 100644 index 0000000000..68a642c83a --- /dev/null +++ b/dom/security/trusted-types/TrustedHTML.h @@ -0,0 +1,19 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDHTML_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDHTML_H_ + +#include "mozilla/dom/TrustedTypeUtils.h" + +namespace mozilla::dom { + +// https://w3c.github.io/trusted-types/dist/spec/#trusted-html +DECL_TRUSTED_TYPE_CLASS(TrustedHTML) + +} // namespace mozilla::dom + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDHTML_H_ diff --git a/dom/security/trusted-types/TrustedScript.cpp b/dom/security/trusted-types/TrustedScript.cpp new file mode 100644 index 0000000000..496c53278a --- /dev/null +++ b/dom/security/trusted-types/TrustedScript.cpp @@ -0,0 +1,13 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/TrustedScript.h" + +namespace mozilla::dom { + +IMPL_TRUSTED_TYPE_CLASS(TrustedScript) + +} // namespace mozilla::dom diff --git a/dom/security/trusted-types/TrustedScript.h b/dom/security/trusted-types/TrustedScript.h new file mode 100644 index 0000000000..0b6e0965ed --- /dev/null +++ b/dom/security/trusted-types/TrustedScript.h @@ -0,0 +1,19 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPT_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPT_H_ + +#include "mozilla/dom/TrustedTypeUtils.h" + +namespace mozilla::dom { + +// https://w3c.github.io/trusted-types/dist/spec/#trusted-script +DECL_TRUSTED_TYPE_CLASS(TrustedScript) + +} // namespace mozilla::dom + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPT_H_ diff --git a/dom/security/trusted-types/TrustedScriptURL.cpp b/dom/security/trusted-types/TrustedScriptURL.cpp new file mode 100644 index 0000000000..8de2448f91 --- /dev/null +++ b/dom/security/trusted-types/TrustedScriptURL.cpp @@ -0,0 +1,14 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/TrustedScriptURL.h" + +namespace mozilla::dom { + +// https://w3c.github.io/trusted-types/dist/spec/#trused-script-url +IMPL_TRUSTED_TYPE_CLASS(TrustedScriptURL) + +} // namespace mozilla::dom diff --git a/dom/security/trusted-types/TrustedScriptURL.h b/dom/security/trusted-types/TrustedScriptURL.h new file mode 100644 index 0000000000..6e71235cb9 --- /dev/null +++ b/dom/security/trusted-types/TrustedScriptURL.h @@ -0,0 +1,18 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPTURL_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPTURL_H_ + +#include "mozilla/dom/TrustedTypeUtils.h" + +namespace mozilla::dom { + +DECL_TRUSTED_TYPE_CLASS(TrustedScriptURL) + +} // namespace mozilla::dom + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDSCRIPTURL_H_ diff --git a/dom/security/trusted-types/TrustedTypePolicy.cpp b/dom/security/trusted-types/TrustedTypePolicy.cpp new file mode 100644 index 0000000000..3c4e758ed0 --- /dev/null +++ b/dom/security/trusted-types/TrustedTypePolicy.cpp @@ -0,0 +1,43 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/TrustedTypePolicy.h" + +#include "mozilla/AlreadyAddRefed.h" +#include "mozilla/dom/TrustedTypePolicyFactory.h" +#include "mozilla/dom/TrustedTypesBinding.h" + +namespace mozilla::dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TrustedTypePolicy, mParentObject) + +JSObject* TrustedTypePolicy::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + return TrustedTypePolicy_Binding::Wrap(aCx, this, aGivenProto); +} + +UniquePtr<TrustedHTML> TrustedTypePolicy::CreateHTML( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const { + // TODO: implement the spec. + return MakeUnique<TrustedHTML>(); +} + +UniquePtr<TrustedScript> TrustedTypePolicy::CreateScript( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const { + // TODO: implement the spec. + return MakeUnique<TrustedScript>(); +} + +UniquePtr<TrustedScriptURL> TrustedTypePolicy::CreateScriptURL( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const { + // TODO: implement the spec. + return MakeUnique<TrustedScriptURL>(); +} + +} // namespace mozilla::dom diff --git a/dom/security/trusted-types/TrustedTypePolicy.h b/dom/security/trusted-types/TrustedTypePolicy.h new file mode 100644 index 0000000000..22d99947b3 --- /dev/null +++ b/dom/security/trusted-types/TrustedTypePolicy.h @@ -0,0 +1,72 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_ + +#include "js/TypeDecls.h" +#include "js/Value.h" +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" +#include "mozilla/dom/BindingDeclarations.h" +#include "mozilla/dom/DOMString.h" +#include "mozilla/dom/TrustedHTML.h" +#include "mozilla/dom/TrustedScript.h" +#include "mozilla/dom/TrustedScriptURL.h" +#include "nsISupportsImpl.h" +#include "nsStringFwd.h" +#include "nsWrapperCache.h" + +namespace mozilla::dom { + +class TrustedTypePolicyFactory; + +// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy +class TrustedTypePolicy : public nsWrapperCache { + public: + NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TrustedTypePolicy) + NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(TrustedTypePolicy) + + explicit TrustedTypePolicy(TrustedTypePolicyFactory* aParentObject) + : mParentObject{aParentObject} {} + + // Required for Web IDL binding. + TrustedTypePolicyFactory* GetParentObject() const { return mParentObject; } + + // Required for Web IDL binding. + JSObject* WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) override; + + // https://w3c.github.io/trusted-types/dist/spec/#trustedtypepolicy-name + void GetName(DOMString& aResult) const { + // TODO: impl. + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createhtml + UniquePtr<TrustedHTML> CreateHTML( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const; + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscript + UniquePtr<TrustedScript> CreateScript( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const; + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicy-createscripturl + UniquePtr<TrustedScriptURL> CreateScriptURL( + JSContext* aJSContext, const nsAString& aInput, + const Sequence<JS::Value>& aArguments) const; + + private: + // Required because this class is ref-counted. + virtual ~TrustedTypePolicy() = default; + + RefPtr<TrustedTypePolicyFactory> mParentObject; +}; + +} // namespace mozilla::dom + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICY_H_ diff --git a/dom/security/trusted-types/TrustedTypePolicyFactory.cpp b/dom/security/trusted-types/TrustedTypePolicyFactory.cpp new file mode 100644 index 0000000000..448c51eb3b --- /dev/null +++ b/dom/security/trusted-types/TrustedTypePolicyFactory.cpp @@ -0,0 +1,50 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/TrustedTypePolicyFactory.h" + +#include "mozilla/AlreadyAddRefed.h" +#include "mozilla/RefPtr.h" +#include "mozilla/dom/TrustedTypePolicy.h" + +namespace mozilla::dom { + +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TrustedTypePolicyFactory, mGlobalObject) + +JSObject* TrustedTypePolicyFactory::WrapObject( + JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { + return TrustedTypePolicyFactory_Binding::Wrap(aCx, this, aGivenProto); +} + +already_AddRefed<TrustedTypePolicy> TrustedTypePolicyFactory::CreatePolicy( + const nsAString& aPolicyName, + const TrustedTypePolicyOptions& aPolicyOptions) { + // TODO: implement the spec. + return MakeRefPtr<TrustedTypePolicy>(this).forget(); +} + +UniquePtr<TrustedHTML> TrustedTypePolicyFactory::EmptyHTML() { + // Preserving the wrapper ensures: + // ``` + // const e = trustedTypes.emptyHTML; + // e === trustedTypes.emptyHTML; + // ``` + // which comes with the cost of keeping the factory, one per global, alive. + // An additional benefit is it saves the cost of re-instantiating potentially + // multiple emptyHML objects. Both, the JS- and the C++-objects. + dom::PreserveWrapper(this); + + return MakeUnique<TrustedHTML>(); +} + +UniquePtr<TrustedScript> TrustedTypePolicyFactory::EmptyScript() { + // See the explanation in `EmptyHTML()`. + dom::PreserveWrapper(this); + + return MakeUnique<TrustedScript>(); +} + +} // namespace mozilla::dom diff --git a/dom/security/trusted-types/TrustedTypePolicyFactory.h b/dom/security/trusted-types/TrustedTypePolicyFactory.h new file mode 100644 index 0000000000..fea5312cf8 --- /dev/null +++ b/dom/security/trusted-types/TrustedTypePolicyFactory.h @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICYFACTORY_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICYFACTORY_H_ + +#include "js/TypeDecls.h" +#include "mozilla/dom/TrustedHTML.h" +#include "mozilla/dom/TrustedScript.h" +#include "mozilla/RefPtr.h" +#include "mozilla/UniquePtr.h" +#include "nsIGlobalObject.h" +#include "nsISupportsImpl.h" +#include "nsStringFwd.h" +#include "nsWrapperCache.h" + +template <typename T> +struct already_AddRefed; + +class DOMString; + +namespace mozilla::dom { + +class TrustedTypePolicy; + +// https://w3c.github.io/trusted-types/dist/spec/#trusted-type-policy-factory +class TrustedTypePolicyFactory : public nsWrapperCache { + public: + NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(TrustedTypePolicyFactory) + NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(TrustedTypePolicyFactory) + + explicit TrustedTypePolicyFactory(nsIGlobalObject* aGlobalObject) + : mGlobalObject{aGlobalObject} {} + + // Required for Web IDL binding. + nsIGlobalObject* GetParentObject() const { return mGlobalObject; } + + // Required for Web IDL binding. + JSObject* WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) override; + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-createpolicy + already_AddRefed<TrustedTypePolicy> CreatePolicy( + const nsAString& aPolicyName, + const TrustedTypePolicyOptions& aPolicyOptions); + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-ishtml + bool IsHTML(JSContext* aJSContext, + const JS::Handle<JS::Value>& aValue) const { + // TODO: impl. + return false; + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscript + bool IsScript(JSContext* aJSContext, + const JS::Handle<JS::Value>& aValue) const { + // TODO: impl. + return false; + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-isscripturl + bool IsScriptURL(JSContext* aJSContext, + const JS::Handle<JS::Value>& aValue) const { + // TODO: impl. + return false; + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-emptyhtml + UniquePtr<TrustedHTML> EmptyHTML(); + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-emptyscript + UniquePtr<TrustedScript> EmptyScript(); + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-getattributetype + void GetAttributeType(const nsAString& aTagName, const nsAString& aAttribute, + const nsAString& aElementNs, const nsAString& aAttrNs, + DOMString& aResult) { + // TODO: impl. + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-getpropertytype + void GetPropertyType(const nsAString& aTagName, const nsAString& aProperty, + const nsAString& aElementNs, DOMString& aResult) { + // TODO: impl + } + + // https://w3c.github.io/trusted-types/dist/spec/#dom-trustedtypepolicyfactory-defaultpolicy + TrustedTypePolicy* GetDefaultPolicy() const { + // TODO: impl + return nullptr; + } + + private: + // Required because this class is ref-counted. + virtual ~TrustedTypePolicyFactory() = default; + + RefPtr<nsIGlobalObject> mGlobalObject; +}; + +} // namespace mozilla::dom + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEPOLICYFACTORY_H_ diff --git a/dom/security/trusted-types/TrustedTypeUtils.h b/dom/security/trusted-types/TrustedTypeUtils.h new file mode 100644 index 0000000000..90ffc50c38 --- /dev/null +++ b/dom/security/trusted-types/TrustedTypeUtils.h @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEUTILS_H_ +#define DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEUTILS_H_ + +#include "mozilla/dom/DOMString.h" +#include "mozilla/dom/NonRefcountedDOMObject.h" +#include "mozilla/dom/TrustedTypesBinding.h" +#include "nsStringFwd.h" + +#define DECL_TRUSTED_TYPE_CLASS(_class) \ + class _class : public mozilla::dom::NonRefcountedDOMObject { \ + public: \ + /* Required for Web IDL binding. */ \ + bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, \ + JS::MutableHandle<JSObject*> aObject); \ + \ + void Stringify(nsAString& aResult) const { /* TODO: impl. */ \ + } \ + \ + void ToJSON(DOMString& aResult) const { /* TODO: impl. */ \ + } \ + }; + +#define IMPL_TRUSTED_TYPE_CLASS(_class) \ + bool _class::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, \ + JS::MutableHandle<JSObject*> aObject) { \ + return _class##_Binding::Wrap(aCx, this, aGivenProto, aObject); \ + } + +#endif // DOM_SECURITY_TRUSTED_TYPES_TRUSTEDTYPEUTILS_H_ diff --git a/dom/security/trusted-types/moz.build b/dom/security/trusted-types/moz.build new file mode 100644 index 0000000000..159a54ff02 --- /dev/null +++ b/dom/security/trusted-types/moz.build @@ -0,0 +1,27 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "DOM: Security") + +EXPORTS.mozilla.dom += [ + "TrustedHTML.h", + "TrustedScript.h", + "TrustedScriptURL.h", + "TrustedTypePolicy.h", + "TrustedTypePolicyFactory.h", + "TrustedTypeUtils.h", +] + +UNIFIED_SOURCES += [ + "TrustedHTML.cpp", + "TrustedScript.cpp", + "TrustedScriptURL.cpp", + "TrustedTypePolicy.cpp", + "TrustedTypePolicyFactory.cpp", +] + +FINAL_LIBRARY = "xul" |