Commit 5eb1a5d6 authored by Yifan Luo's avatar Yifan Luo Committed by Commit Bot

[Sanitizer API] Remove creationOptions from Sanitizer.

Bug: 1116418
Change-Id: Id58d7f0478abb73619d5077d9024800bd7b83c15
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461331Reviewed-by: default avatarDaniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Yifan Luo <lyf@chromium.org>
Cr-Commit-Position: refs/heads/master@{#815208}
parent 5be960a6
......@@ -107,11 +107,6 @@ DocumentFragment* Sanitizer::sanitize(ScriptState* script_state,
return fragment;
}
// TODO(lyf): https://github.com/WICG/sanitizer-api/issues/34
SanitizerConfig* Sanitizer::creationOptions() const {
return config_;
}
void Sanitizer::Trace(Visitor* visitor) const {
ScriptWrappable::Trace(visitor);
visitor->Trace(config_);
......
......@@ -27,8 +27,6 @@ class MODULES_EXPORT Sanitizer final : public ScriptWrappable {
String sanitizeToString(ScriptState*, const String&, ExceptionState&);
DocumentFragment* sanitize(ScriptState*, const String&, ExceptionState&);
SanitizerConfig* creationOptions() const;
void Trace(Visitor*) const override;
private:
......
......@@ -11,6 +11,4 @@
[RaisesException] constructor(optional SanitizerConfig config = {});
[CallWith=ScriptState, RaisesException] DOMString sanitizeToString(DOMString input);
[CallWith=ScriptState, RaisesException] DocumentFragment sanitize(DOMString input);
readonly attribute SanitizerConfig creationOptions;
};
......@@ -6,8 +6,6 @@
constructor(optional SanitizerConfig sanitizerConfig = {});
DocumentFragment sanitize(DOMString input);
DOMString sanitizeToString(DOMString input);
readonly attribute SanitizerConfig creationOptions;
};
dictionary SanitizerConfig {
......
......@@ -7851,7 +7851,6 @@ interface SVGViewElement : SVGElement
setter zoomAndPan
interface Sanitizer
attribute @@toStringTag
getter creationOptions
method constructor
method sanitize
method sanitizeToString
......
......@@ -7,67 +7,60 @@
<body>
<script>
const default_option = {};
test(t => {
let s = new Sanitizer();
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator without config.");
test(t => {
let s = new Sanitizer({});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with empty config.");
test(t => {
let s = new Sanitizer(null);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with null as config.");
test(t => {
let s = new Sanitizer(undefined);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with undefined as config.");
test(t => {
let s = new Sanitizer({testConfig: [1,2,3], attr: ["test", "i", "am"]});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config ignore unknown values.");
const config_names = ["dropElements", "dropAttributes"];
config_names.forEach(cname => {
test(t => {
let options = {};
options[cname] = cname.endsWith("Elements") ? ["div"] : ["script"];
let result = {};
result[cname] = cname.endsWith("Elements") ? ["DIV"] : ["script"];
test(t => {
let options = {dropElements: ["div"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, result);
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala</div><test>test</test>"), "<test>test</test>");
options[cname].push("test");
assert_object_equals(s.creationOptions, result);
options.dropElements.push("test");
assert_equals(s.sanitizeToString("<div>balabala</div><test>test</test>"), "<test>test</test>");
}, "SanitizerAPI config dropElements is not editable.");
let new_options = {};
new_options[cname] = ["test", "t"];
s.creationOptions = new_options;
assert_object_equals(s.creationOptions, result);
test(t => {
let options = {dropAttributes: ["onclick"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<button id='btn' onclick='submit()'>balabala</button>"), "<button id=\"btn\">balabala</button>");
s.creationOptions[cname] = [1,2,3];
assert_object_equals(s.creationOptions, result);
}, "SanitizerAPI config " + cname + " is not editable.");
options.dropAttributes.push("id");
assert_equals(s.sanitizeToString("<button id='btn' onclick='submit()'>balabala</button>"), "<button id=\"btn\">balabala</button>");
}, "SanitizerAPI config dropAttributes is not editable.");
const config_names = ["dropElements", "dropAttributes"];
config_names.forEach(cname => {
let options = {};
options[cname] = [];
test(t => {
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer)
assert_object_equals(s.creationOptions, options);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config " + JSON.stringify(options) + ".");
......@@ -76,7 +69,6 @@
test(t => {
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer)
assert_object_equals(s.creationOptions, default_option);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config " + JSON.stringify(options, function(k,v){return v===undefined?"::undefined::":v}).replace(new RegExp("\"::undefined::\"", 'g'), "undefined") + ".");
......
......@@ -16,13 +16,11 @@
test(t => {
let s = new Sanitizer({});
assert_object_equals(s.creationOptions, {});
assert_throws_js(TypeError, _ => s.sanitize());
}, "SanitizerAPI sanitize function without argument should throw an error.");
testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_object_equals(s.creationOptions, c.config_value);
fragment = s.sanitize(c.value);
assert_true(fragment instanceof DocumentFragment);
......
......@@ -10,13 +10,11 @@
<script>
test(t => {
let s = new Sanitizer({});
assert_object_equals(s.creationOptions, {});
assert_throws_js(TypeError, _ => s.sanitizeToString());
}, "SanitizerAPI sanitize function without argument should throw an error.");
testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_object_equals(s.creationOptions, c.config_value);
assert_equals(s.sanitizeToString(c.value), c.result);
}, "SanitizerAPI config: " + c.message + ", sanitizeToString function for " + c.message));
</script>
......
const testcases = [
{config_input: {}, config_value: {}, value: "test", result: "test", message: "string"},
{config_input: {}, config_value: {}, value: "<b>bla</b>", result: "<b>bla</b>", message: "html fragment"},
{config_input: {}, config_value: {}, value: "<a<embla", result: "", message: "broken html"},
{config_input: {}, config_value: {}, value: {}, result: "[object Object]", message: "empty object"},
{config_input: {}, config_value: {}, value: 1, result: "1", message: "number"},
{config_input: {}, config_value: {}, value: 000, result: "0", message: "zeros"},
{config_input: {}, config_value: {}, value: 1+2, result: "3", message: "arithmetic"},
{config_input: {}, config_value: {}, value: "", result: "", message: "empty string"},
{config_input: {}, config_value: {}, value: undefined, result: "undefined", message: "undefined"},
{config_input: {}, config_value: {}, value: null, result: "null", message: "null"},
{config_input: {}, config_value: {}, value: "<html><head></head><body>test</body></html>", result: "test", message: "document"},
{config_input: {}, config_value: {}, value: "<div>test", result: "<div>test</div>", message: "html without close tag"},
{config_input: {}, config_value: {}, value: "<script>alert('i am a test')<\/script>", result: "", message: "scripts for default configs"},
{config_input: {}, config_value: {}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "onclick scripts"},
{config_input: {test: 123}, config_value: {}, value: "test", result: "test", message: "invalid config_input"},
{config_input: {dropElements: []}, config_value: {dropElements:[]}, value: "test", result: "test", message: "empty dropElements list"},
{config_input: {dropElements: ["div"]}, config_value: {dropElements:["DIV"]}, value: "<div>test</div><c>bla", result: "<c>bla</c>", message: "test html without close tag with dropElements list ['div']"},
{config_input: {dropElements: ["script"]}, config_value: {dropElements:["SCRIPT"]}, value: "<script>alert('i am a test')<\/script>", result: "", message: "test script with [\"script\"] as dropElements list"},
{config_input: {dropElements: ["test", "i"]}, config_value: {dropElements:["TEST","I"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list [\"test\", \"i\"]}"},
{config_input: {dropElements: ["I", "AM"]}, config_value: {dropElements:["I", "AM"]}, value: "<div>balabala<am>test</am></div>", result: "<div>balabala</div>", message: "dropElements list [\"I\", \"AM\"]}"},
{config_input: {dropElements: ["am", "p"]}, config_value: {dropElements:["AM","P"]}, value: "<div>balabala<i>i</i><p>t</p><test>a</test></div>", result: "<div>balabala<i>i</i><test>a</test></div>", message: "dropElements list [\"am\", \"p\"]}"},
{config_input: {dropElements: [123, [], "test", "i"]}, config_value: {dropElements:["123","","TEST","I"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list with invalid values}"},
{config_input: {dropAttributes: ["onclick"]}, config_value: {dropAttributes: ["onclick"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "dropAttributes list [\"onclick\"] with onclick scripts"},
{config_input: {dropAttributes: []}, config_value: {dropAttributes: []}, value: "<p onclick='a= 123'>Click.</p>", result: "<p onclick=\"a= 123\">Click.</p>", message: "empty dropAttributes list with onclick scripts"},
{config_input: {dropAttributes: ["id"]}, config_value: {dropAttributes: ["id"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p onclick=\"a= 123\">Click.</p>", message: "dropAttributes list [\"id\"] with onclick scripts"},
{config_input: {dropAttributes: ["ONCLICK"]}, config_value: {dropAttributes: ["onclick"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "dropAttributes list [\"ONCLICK\"] with onclick scripts"},
{config_input: {dropAttributes: ["data-attribute-with-dashes"]}, config_value: {dropAttributes: ["data-attribute-with-dashes"]}, value: "<p id='p' data-attribute-with-dashes='123'>Click.</p><script>document.getElementById('p').dataset.attributeWithDashes=123;</script>", result: "<p id=\"p\">Click.</p><script></script>", message: "dropAttributes list [\"data-attribute-with-dashes\"] with dom dataset js access."},
{config_input: {}, value: "test", result: "test", message: "string"},
{config_input: {}, value: "<b>bla</b>", result: "<b>bla</b>", message: "html fragment"},
{config_input: {}, value: "<a<embla", result: "", message: "broken html"},
{config_input: {}, value: {}, result: "[object Object]", message: "empty object"},
{config_input: {}, value: 1, result: "1", message: "number"},
{config_input: {}, value: 000, result: "0", message: "zeros"},
{config_input: {}, value: 1+2, result: "3", message: "arithmetic"},
{config_input: {}, value: "", result: "", message: "empty string"},
{config_input: {}, value: undefined, result: "undefined", message: "undefined"},
{config_input: {}, value: null, result: "null", message: "null"},
{config_input: {}, value: "<html><head></head><body>test</body></html>", result: "test", message: "document"},
{config_input: {}, value: "<div>test", result: "<div>test</div>", message: "html without close tag"},
{config_input: {}, value: "<script>alert('i am a test')<\/script>", result: "", message: "scripts for default configs"},
{config_input: {}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "onclick scripts"},
{config_input: {test: 123}, value: "test", result: "test", message: "invalid config_input"},
{config_input: {dropElements: []}, value: "test", result: "test", message: "empty dropElements list"},
{config_input: {dropElements: ["div"]}, value: "<div>test</div><c>bla", result: "<c>bla</c>", message: "test html without close tag with dropElements list ['div']"},
{config_input: {dropElements: ["script"]}, value: "<script>alert('i am a test')<\/script>", result: "", message: "test script with [\"script\"] as dropElements list"},
{config_input: {dropElements: ["test", "i"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list [\"test\", \"i\"]}"},
{config_input: {dropElements: ["I", "AM"]}, value: "<div>balabala<am>test</am></div>", result: "<div>balabala</div>", message: "dropElements list [\"I\", \"AM\"]}"},
{config_input: {dropElements: ["am", "p"]}, value: "<div>balabala<i>i</i><p>t</p><test>a</test></div>", result: "<div>balabala<i>i</i><test>a</test></div>", message: "dropElements list [\"am\", \"p\"]}"},
{config_input: {dropElements: [123, [], "test", "i"]}, value: "<div>balabala<i>test</i></div><test>t</test>", result: "<div>balabala</div>", message: "dropElements list with invalid values}"},
{config_input: {dropAttributes: ["onclick"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "dropAttributes list [\"onclick\"] with onclick scripts"},
{config_input: {dropAttributes: []}, value: "<p onclick='a= 123'>Click.</p>", result: "<p onclick=\"a= 123\">Click.</p>", message: "empty dropAttributes list with onclick scripts"},
{config_input: {dropAttributes: ["id"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p onclick=\"a= 123\">Click.</p>", message: "dropAttributes list [\"id\"] with onclick scripts"},
{config_input: {dropAttributes: ["ONCLICK"]}, value: "<p onclick='a= 123'>Click.</p>", result: "<p>Click.</p>", message: "dropAttributes list [\"ONCLICK\"] with onclick scripts"},
{config_input: {dropAttributes: ["data-attribute-with-dashes"]}, value: "<p id='p' data-attribute-with-dashes='123'>Click.</p><script>document.getElementById('p').dataset.attributeWithDashes=123;</script>", result: "<p id=\"p\">Click.</p><script></script>", message: "dropAttributes list [\"data-attribute-with-dashes\"] with dom dataset js access."},
];
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