Commit 534cd7bb authored by Maja Kabus's avatar Maja Kabus Committed by Commit Bot

isXXX methods added to TrustedTypePolicyFactory

isHTML(), isScript(), isScriptURL() and isURL() added to
TrustedTypePolicyFactory class as part of Trusted Types API update to
current JS polyfill.
The methods require additional code to be fully matched to polyfill
expected behaviour.

Added a helper private method GetWrapperTypeInfoFromScriptValue.

Bug: 739170
Change-Id: I027e43ab6432405c686255a4d0ce24248c59a4dc
Reviewed-on: https://chromium-review.googlesource.com/1238433
Commit-Queue: Daniel Vogelheim <vogelheim@chromium.org>
Reviewed-by: default avatarDaniel Vogelheim <vogelheim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#595527}
parent f553d08d
...@@ -7,8 +7,10 @@ ...@@ -7,8 +7,10 @@
//HTML tests //HTML tests
function createHTMLTest(policyName, policy, expectedHTML, t) { function createHTMLTest(policyName, policy, expectedHTML, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy); let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createHTML('whatever') instanceof TrustedHTML); let html = p.createHTML('whatever');
assert_equals(p.createHTML('whatever') + "", expectedHTML); assert_true(html instanceof TrustedHTML);
assert_true(TrustedTypes.isHTML(html));
assert_equals(html + "", expectedHTML);
} }
test(t => { test(t => {
...@@ -77,8 +79,10 @@ ...@@ -77,8 +79,10 @@
//Script tests //Script tests
function createScriptTest(policyName, policy, expectedScript, t) { function createScriptTest(policyName, policy, expectedScript, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy); let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createScript('whatever') instanceof TrustedScript); let script = p.createScript('whatever');
assert_equals(p.createScript('whatever') + "", expectedScript); assert_true(script instanceof TrustedScript);
assert_true(TrustedTypes.isScript(script));
assert_equals(script + "", expectedScript);
} }
test(t => { test(t => {
...@@ -150,8 +154,10 @@ ...@@ -150,8 +154,10 @@
//ScriptURL tests //ScriptURL tests
function createScriptURLTest(policyName, policy, expectedScriptURL, t) { function createScriptURLTest(policyName, policy, expectedScriptURL, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy); let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL); let scriptUrl = p.createScriptURL(INPUTS.SCRIPTURL);
assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", expectedScriptURL); assert_true(scriptUrl instanceof TrustedScriptURL);
assert_true(TrustedTypes.isScriptURL(scriptUrl));
assert_equals(scriptUrl + "", expectedScriptURL);
} }
test(t => { test(t => {
...@@ -223,8 +229,10 @@ ...@@ -223,8 +229,10 @@
//URL tests //URL tests
function createURLTest(policyName, policy, expectedURL, t) { function createURLTest(policyName, policy, expectedURL, t) {
let p = window.TrustedTypes.createPolicy(policyName, policy); let p = window.TrustedTypes.createPolicy(policyName, policy);
assert_true(p.createURL(INPUTS.URL) instanceof TrustedURL); let url = p.createURL(INPUTS.URL);
assert_equals(p.createURL(INPUTS.URL) + "", expectedURL); assert_true(url instanceof TrustedURL);
assert_true(TrustedTypes.isURL(url));
assert_equals(url + "", expectedURL);
} }
test(t => { test(t => {
......
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="support/helper.sub.js"></script>
<meta http-equiv="Content-Security-Policy" content="trusted-types">
<body>
<script>
// Policy settings for all tests
const noopPolicy = {
'createHTML': (s) => s,
'createScriptURL': (s) => s,
'createURL': (s) => s,
'createScript': (s) => s,
};
// isHTML tests
test(t => {
const p = TrustedTypes.createPolicy('html', noopPolicy);
let html = p.createHTML(INPUTS.HTML);
assert_true(TrustedTypes.isHTML(html));
let html2 = Object.create(html);
// instanceof can pass, but we rely on isHTML
assert_true(html2 instanceof TrustedHTML);
assert_false(TrustedTypes.isHTML(html2));
let html3 = Object.assign({}, html, {toString: () => 'fake'});
assert_false(TrustedTypes.isHTML(html3));
}, 'TrustedTypePolicyFactory.isHTML requires the object to be created via policy.');
// isScript tests
test(t => {
const p = TrustedTypes.createPolicy('script', noopPolicy);
let script = p.createScript(INPUTS.SCRIPT);
assert_true(TrustedTypes.isScript(script));
let script2 = Object.create(script);
// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScript);
assert_false(TrustedTypes.isScript(script2));
let script3 = Object.assign({}, script, {toString: () => 'fake'});
assert_false(TrustedTypes.isScript(script3));
}, 'TrustedTypePolicyFactory.isScript requires the object to be created via policy.');
// isScriptURL tests
test(t => {
const p = TrustedTypes.createPolicy('script_url', noopPolicy);
let script = p.createScriptURL(INPUTS.SCRIPTURL);
assert_true(TrustedTypes.isScriptURL(script));
let script2 = Object.create(script);
// instanceof can pass, but we rely on isScript
assert_true(script2 instanceof TrustedScriptURL);
assert_false(TrustedTypes.isScriptURL(script2));
let script3 = Object.assign({}, script, {toString: () => 'fake'});
assert_false(TrustedTypes.isScriptURL(script3));
}, 'TrustedTypePolicyFactory.isScriptURL requires the object to be created via policy.');
// isURL tests
test(t => {
const p = TrustedTypes.createPolicy('url', noopPolicy);
let url = p.createURL(INPUTS.URL);
assert_true(TrustedTypes.isURL(url));
let url2 = Object.create(url);
// instanceof can pass, but we rely on isScript
assert_true(url2 instanceof TrustedURL);
assert_false(TrustedTypes.isURL(url2));
let url3 = Object.assign({}, url, {toString: () => 'fake'});
assert_false(TrustedTypes.isURL(url3));
}, 'TrustedTypePolicyFactory.isURL requires the object to be created via policy.');
// Redefinition tests
// TODO(vogelheim): Implement TrustedTypes (& policy objects) as 'frozen'.
/* test(t => {
assert_throws(new TypeError(), _ => {
TrustedTypes.isHTML = () => true;
});
assert_false(TrustedTypes.isHTML({}));
}, 'TrustedTypePolicyFactory.IsHTML cannot be redefined.');
test(t => {
assert_throws(new TypeError(), _ => {
TrustedTypes.isScript = () => true;
});
assert_false(TrustedTypes.isScript({}));
}, 'TrustedTypePolicyFactory.isScript cannot be redefined.');
test(t => {
assert_throws(new TypeError(), _ => {
TrustedTypes.isScriptURL = () => true;
});
assert_false(TrustedTypes.isScriptURL({}));
}, 'TrustedTypePolicyFactory.isScriptURL cannot be redefined.');
test(t => {
assert_throws(new TypeError(), _ => {
TrustedTypes.isURL = () => true;
});
assert_false(TrustedTypes.isURL({}));
}, 'TrustedTypePolicyFactory.isURL cannot be redefined.');*/
</script>
...@@ -7434,6 +7434,10 @@ interface TrustedTypePolicyFactory ...@@ -7434,6 +7434,10 @@ interface TrustedTypePolicyFactory
method createPolicy method createPolicy
method getExposedPolicy method getExposedPolicy
method getPolicyNames method getPolicyNames
method isHTML
method isScript
method isScriptURL
method isURL
interface TrustedURL interface TrustedURL
attribute @@toStringTag attribute @@toStringTag
method constructor method constructor
......
...@@ -4,6 +4,11 @@ ...@@ -4,6 +4,11 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_factory.h" #include "third_party/blink/renderer/core/trustedtypes/trusted_type_policy_factory.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_html.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_script.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_script_url.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_url.h"
#include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h" #include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame.h"
...@@ -59,6 +64,51 @@ Vector<String> TrustedTypePolicyFactory::getPolicyNames() const { ...@@ -59,6 +64,51 @@ Vector<String> TrustedTypePolicyFactory::getPolicyNames() const {
return policyNames; return policyNames;
} }
const WrapperTypeInfo*
TrustedTypePolicyFactory::GetWrapperTypeInfoFromScriptValue(
ScriptState* script_state,
const ScriptValue& script_value) {
v8::Local<v8::Value> value = script_value.V8Value();
v8::Isolate* isolate = script_state->GetIsolate();
if (value.IsEmpty() || !value->IsObject() ||
!V8DOMWrapper::IsWrapper(isolate, value))
return nullptr;
v8::Local<v8::Object> object = script_value.V8Value()->ToObject(isolate);
return ToWrapperTypeInfo(object);
}
bool TrustedTypePolicyFactory::isHTML(ScriptState* script_state,
const ScriptValue& script_value) {
const WrapperTypeInfo* wrapper_type_info =
GetWrapperTypeInfoFromScriptValue(script_state, script_value);
return wrapper_type_info &&
wrapper_type_info->Equals(&V8TrustedHTML::wrapperTypeInfo);
}
bool TrustedTypePolicyFactory::isScript(ScriptState* script_state,
const ScriptValue& script_value) {
const WrapperTypeInfo* wrapper_type_info =
GetWrapperTypeInfoFromScriptValue(script_state, script_value);
return wrapper_type_info &&
wrapper_type_info->Equals(&V8TrustedScript::wrapperTypeInfo);
}
bool TrustedTypePolicyFactory::isScriptURL(ScriptState* script_state,
const ScriptValue& script_value) {
const WrapperTypeInfo* wrapper_type_info =
GetWrapperTypeInfoFromScriptValue(script_state, script_value);
return wrapper_type_info &&
wrapper_type_info->Equals(&V8TrustedScriptURL::wrapperTypeInfo);
}
bool TrustedTypePolicyFactory::isURL(ScriptState* script_state,
const ScriptValue& script_value) {
const WrapperTypeInfo* wrapper_type_info =
GetWrapperTypeInfoFromScriptValue(script_state, script_value);
return wrapper_type_info &&
wrapper_type_info->Equals(&V8TrustedURL::wrapperTypeInfo);
}
void TrustedTypePolicyFactory::Trace(blink::Visitor* visitor) { void TrustedTypePolicyFactory::Trace(blink::Visitor* visitor) {
ScriptWrappable::Trace(visitor); ScriptWrappable::Trace(visitor);
DOMWindowClient::Trace(visitor); DOMWindowClient::Trace(visitor);
......
...@@ -16,6 +16,8 @@ namespace blink { ...@@ -16,6 +16,8 @@ namespace blink {
class ExceptionState; class ExceptionState;
class LocalFrame; class LocalFrame;
class ScriptState;
class ScriptValue;
class TrustedTypePolicy; class TrustedTypePolicy;
class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable, class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
...@@ -28,6 +30,7 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable, ...@@ -28,6 +30,7 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
return new TrustedTypePolicyFactory(frame); return new TrustedTypePolicyFactory(frame);
} }
// TrustedTypePolicyFactory.idl
TrustedTypePolicy* createPolicy(const String&, TrustedTypePolicy* createPolicy(const String&,
const TrustedTypePolicyOptions&, const TrustedTypePolicyOptions&,
bool exposed, bool exposed,
...@@ -37,11 +40,19 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable, ...@@ -37,11 +40,19 @@ class CORE_EXPORT TrustedTypePolicyFactory final : public ScriptWrappable,
Vector<String> getPolicyNames() const; Vector<String> getPolicyNames() const;
bool isHTML(ScriptState*, const ScriptValue&);
bool isScript(ScriptState*, const ScriptValue&);
bool isScriptURL(ScriptState*, const ScriptValue&);
bool isURL(ScriptState*, const ScriptValue&);
void Trace(blink::Visitor*) override; void Trace(blink::Visitor*) override;
private: private:
explicit TrustedTypePolicyFactory(LocalFrame*); explicit TrustedTypePolicyFactory(LocalFrame*);
const WrapperTypeInfo* GetWrapperTypeInfoFromScriptValue(ScriptState*,
const ScriptValue&);
HeapHashMap<String, Member<TrustedTypePolicy>> policy_map_; HeapHashMap<String, Member<TrustedTypePolicy>> policy_map_;
}; };
......
...@@ -12,4 +12,8 @@ ...@@ -12,4 +12,8 @@
TrustedTypePolicy getExposedPolicy(DOMString policyName); TrustedTypePolicy getExposedPolicy(DOMString policyName);
// All the policy object names that have been created // All the policy object names that have been created
[Affects=Nothing] sequence<DOMString> getPolicyNames(); [Affects=Nothing] sequence<DOMString> getPolicyNames();
[CallWith=ScriptState] boolean isHTML(object? checkedObject);
[CallWith=ScriptState] boolean isScript(object? checkedObject);
[CallWith=ScriptState] boolean isScriptURL(object? checkedObject);
[CallWith=ScriptState] boolean isURL(object? checkedObject);
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment