OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.dom.html; | |
6 | |
7 | |
8 /** | |
9 * Interface used to validate that only accepted elements and attributes are | |
10 * allowed while parsing HTML strings into DOM nodes. | |
11 */ | |
12 abstract class NodeValidator { | |
13 | |
14 /** | |
15 * Construct a default NodeValidator which only accepts whitelisted HTML5 | |
16 * elements and attributes. | |
17 * | |
18 * If a uriPolicy is not specified then the default uriPolicy will be used. | |
19 */ | |
20 factory NodeValidator({UriPolicy uriPolicy}) => | |
21 new _Html5NodeValidator(uriPolicy: uriPolicy); | |
22 | |
23 /** | |
24 * Returns true if the tagName is an accepted type. | |
25 */ | |
26 bool allowsElement(Element element); | |
27 | |
28 /** | |
29 * Returns true if the attribute is allowed. | |
30 * | |
31 * The attributeName parameter will always be in lowercase. | |
32 * | |
33 * See [allowsElement] for format of tagName. | |
34 */ | |
35 bool allowsAttribute(Element element, String attributeName, String value); | |
36 } | |
37 | |
38 /** | |
39 * Performs sanitization of a node tree after construction to ensure that it | |
40 * does not contain any disallowed elements or attributes. | |
41 * | |
42 * In general custom implementations of this class should not be necessary and | |
43 * all validation customization should be done in custom NodeValidators, but | |
44 * custom implementations of this class can be created to perform more complex | |
45 * tree sanitization. | |
46 */ | |
47 abstract class NodeTreeSanitizer { | |
48 | |
49 /** | |
50 * Constructs a default tree sanitizer which will remove all elements and | |
51 * attributes which are not allowed by the provided validator. | |
52 */ | |
53 factory NodeTreeSanitizer(NodeValidator validator) => | |
54 new _ValidatingTreeSanitizer(validator); | |
55 | |
56 /** | |
57 * Called with the root of the tree which is to be sanitized. | |
58 * | |
59 * This method needs to walk the entire tree and either remove elements and | |
60 * attributes which are not recognized as safe or throw an exception which | |
61 * will mark the entire tree as unsafe. | |
62 */ | |
63 void sanitizeTree(Node node); | |
64 } | |
65 | |
66 /** | |
67 * Defines the policy for what types of uris are allowed for particular | |
68 * attribute values. | |
69 * | |
70 * This can be used to provide custom rules such as allowing all http:// URIs | |
71 * for image attributes but only same-origin URIs for anchor tags. | |
72 */ | |
73 abstract class UriPolicy { | |
74 /** | |
75 * Constructs the default UriPolicy which is to only allow Uris to the same | |
76 * origin as the application was launched from. | |
77 * | |
78 * This will block all ftp: mailto: URIs. It will also block accessing | |
79 * https://example.com if the app is running from http://example.com. | |
80 */ | |
81 factory UriPolicy() => new _SameOriginUriPolicy(); | |
82 | |
83 /** | |
84 * Checks if the uri is allowed on the specified attribute. | |
85 * | |
86 * The uri provided may or may not be a relative path. | |
87 */ | |
88 bool allowsUri(String uri); | |
89 } | |
90 | |
91 /** | |
92 * Allows URIs to the same origin as the current application was loaded from | |
93 * (such as https://example.com:80). | |
94 */ | |
95 class _SameOriginUriPolicy implements UriPolicy { | |
96 final AnchorElement _hiddenAnchor = new AnchorElement(); | |
97 final Location _loc = window.location; | |
98 | |
99 bool allowsUri(String uri) { | |
100 _hiddenAnchor.href = uri; | |
101 return _hiddenAnchor.hostname == _loc.hostname && | |
102 _hiddenAnchor.port == _loc.port && | |
103 _hiddenAnchor.protocol == _loc.protocol; | |
104 } | |
105 } | |
106 | |
107 | |
108 /** | |
109 * Standard tree sanitizer which validates a node tree against the provided | |
110 * validator and removes any nodes or attributes which are not allowed. | |
111 */ | |
112 class _ValidatingTreeSanitizer implements NodeTreeSanitizer { | |
113 final NodeValidator validator; | |
114 _ValidatingTreeSanitizer(this.validator) {} | |
115 | |
116 void sanitizeTree(Node node) { | |
117 void walk(Node node) { | |
118 sanitizeNode(node); | |
119 | |
120 var child = node.$dom_lastChild; | |
121 while (child != null) { | |
122 // Child may be removed during the walk. | |
123 var nextChild = child.previousNode; | |
124 walk(child); | |
125 child = nextChild; | |
126 } | |
127 } | |
128 walk(node); | |
129 } | |
130 | |
131 void sanitizeNode(Node node) { | |
132 switch (node.nodeType) { | |
133 case Node.ELEMENT_NODE: | |
134 Element element = node; | |
135 var attrs = element.attributes; | |
136 if (!validator.allowsElement(element)) { | |
137 element.remove(); | |
138 break; | |
139 } | |
140 | |
141 var isAttr = attrs['is']; | |
142 if (isAttr != null) { | |
143 if (!validator.allowsAttribute(element, 'is', isAttr)) { | |
144 element.remove(); | |
145 break; | |
146 } | |
147 } | |
148 | |
149 // TODO(blois): Need to be able to get all attributes, irrespective of | |
150 // XMLNS. | |
151 var keys = attrs.keys.toList(); | |
152 for (var i = attrs.length - 1; i >= 0; --i) { | |
153 var name = keys[i]; | |
154 if (!validator.allowsAttribute(element, name, attrs[name])) { | |
155 attrs.remove(name); | |
156 } | |
157 } | |
158 | |
159 if (element is TemplateElement) { | |
Jennifer Messerly
2013/06/06 19:31:32
I don't think it matters because usually you're sa
| |
160 TemplateElement template = element; | |
161 sanitizeTree(template.content); | |
162 } | |
163 break; | |
164 case Node.COMMENT_NODE: | |
165 case Node.DOCUMENT_FRAGMENT_NODE: | |
166 case Node.TEXT_NODE: | |
167 case Node.CDATA_SECTION_NODE: | |
168 break; | |
169 default: | |
170 node.remove(); | |
171 } | |
172 } | |
173 } | |
OLD | NEW |