Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0f6b2ef

Browse files
committed
refactor(sanitizer): turn sanitizer into a service
1 parent 1e258d1 commit 0f6b2ef

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

angularFiles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ angularFiles = {
66
'src/JSON.js',
77
'src/Injector.js',
88
'src/Resource.js',
9-
'src/sanitizer.js',
109
'src/jqLite.js',
1110
'src/apis.js',
1211
'src/service/anchorScroll.js',
@@ -34,6 +33,7 @@ angularFiles = {
3433
'src/service/route.js',
3534
'src/service/routeParams.js',
3635
'src/service/scope.js',
36+
'src/service/sanitize.js',
3737
'src/service/sniffer.js',
3838
'src/service/window.js',
3939
'src/service/http.js',

src/sanitizer.js renamed to src/service/sanitize.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,102 @@
1616
*
1717
*/
1818

19+
20+
21+
/**
22+
* @ngdoc service
23+
* @name angular.module.ng.$sanitize
24+
* @function
25+
*
26+
* @description
27+
* The input is sanitized by parsing the html into tokens. All safe tokens (from a whitelist) are
28+
* then serialized back to properly escaped html string. This means that no unsafe input can make
29+
* it into the returned string, however, since our parser is more strict than a typical browser
30+
* parser, it's possible that some obscure input, which would be recognized as valid HTML by a
31+
* browser, won't make it through the sanitizer.
32+
*
33+
* @param {string} html Html input.
34+
* @returns {string} Sanitized html.
35+
*
36+
* @example
37+
<doc:example>
38+
<doc:source>
39+
<script>
40+
function Ctrl() {
41+
this.snippet =
42+
'<p style="color:blue">an html\n' +
43+
'<em onmouseover="this.textContent=\'PWN3D!\'">click here</em>\n' +
44+
'snippet</p>';
45+
}
46+
</script>
47+
<div ng:controller="Ctrl">
48+
Snippet: <textarea ng:model="snippet" cols="60" rows="3"></textarea>
49+
<table>
50+
<tr>
51+
<td>Filter</td>
52+
<td>Source</td>
53+
<td>Rendered</td>
54+
</tr>
55+
<tr id="html-filter">
56+
<td>html filter</td>
57+
<td>
58+
<pre>&lt;div ng:bind-html="snippet"&gt;<br/>&lt;/div&gt;</pre>
59+
</td>
60+
<td>
61+
<div ng:bind-html="snippet"></div>
62+
</td>
63+
</tr>
64+
<tr id="escaped-html">
65+
<td>no filter</td>
66+
<td><pre>&lt;div ng:bind-="snippet"&gt;<br/>&lt;/div&gt;</pre></td>
67+
<td><div ng:bind="snippet"></div></td>
68+
</tr>
69+
<tr id="html-unsafe-filter">
70+
<td>unsafe html filter</td>
71+
<td><pre>&lt;div ng:bind-html-unsafe="snippet"&gt;<br/>&lt;/div&gt;</pre></td>
72+
<td><div ng:bind-html-unsafe="snippet"></div></td>
73+
</tr>
74+
</table>
75+
</div>
76+
</doc:source>
77+
<doc:scenario>
78+
it('should sanitize the html snippet ', function() {
79+
expect(using('#html-filter').element('div').html()).
80+
toBe('<p>an html\n<em>click here</em>\nsnippet</p>');
81+
});
82+
83+
it('should escape snippet without any filter', function() {
84+
expect(using('#escaped-html').element('div').html()).
85+
toBe("&lt;p style=\"color:blue\"&gt;an html\n" +
86+
"&lt;em onmouseover=\"this.textContent='PWN3D!'\"&gt;click here&lt;/em&gt;\n" +
87+
"snippet&lt;/p&gt;");
88+
});
89+
90+
it('should inline raw snippet if filtered as unsafe', function() {
91+
expect(using('#html-unsafe-filter').element("div").html()).
92+
toBe("<p style=\"color:blue\">an html\n" +
93+
"<em onmouseover=\"this.textContent='PWN3D!'\">click here</em>\n" +
94+
"snippet</p>");
95+
});
96+
97+
it('should update', function() {
98+
input('snippet').enter('new <b>text</b>');
99+
expect(using('#html-filter').binding('snippet')).toBe('new <b>text</b>');
100+
expect(using('#escaped-html').element('div').html()).toBe("new &lt;b&gt;text&lt;/b&gt;");
101+
expect(using('#html-unsafe-filter').binding("snippet")).toBe('new <b>text</b>');
102+
});
103+
</doc:scenario>
104+
</doc:example>
105+
*/
106+
107+
function $SanitizeProvider() {
108+
this.$get = valueFn(function(html) {
109+
var buf = [];
110+
htmlParser(html, htmlSanitizeWriter(buf));
111+
return buf.join('');
112+
});
113+
};
114+
19115
// Regular Expressions for parsing tags and attributes
20116
var START_TAG_REGEXP = /^<\s*([\w:-]+)((?:\s+[\w:-]+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)\s*>/,
21117
END_TAG_REGEXP = /^<\s*\/\s*([\w:-]+)[^>]*>/,

0 commit comments

Comments
 (0)