This repository has been archived by the owner on May 20, 2024. It is now read-only.
forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-amp-blacklist-sanitizer.php
194 lines (158 loc) · 5.4 KB
/
test-amp-blacklist-sanitizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
class AMP_Blacklist_Sanitizer_Test extends WP_UnitTestCase {
public function get_data() {
return array(
'empty' => array(
'',
'',
),
'blacklisted_tag_with_innertext' => array(
'<script>alert("")</script>',
'',
),
'multiple_blacklisted_tags_only' => array(
'<input type="text" /><script>alert("")</script><style>body{ color: red; }</style><label>This is a label</label>',
'',
),
'multiple_blacklisted_tags_only_in_child' => array(
'<p><input type="text" /><script>alert("")</script><style>body{ color: red; }</style></p>',
'',
),
'whitelisted_tag_only' => array(
'<p>Text</p><img src="/path/to/file.jpg" />',
'<p>Text</p><img src="/path/to/file.jpg"/>', // LIBXML_NOEMPTYTAG
),
'blacklisted_attributes' => array(
'<a href="/path/to/file.jpg" style="border: 1px solid red;">Link</a>',
'<a href="/path/to/file.jpg">Link</a>',
),
'onclick_attribute' => array(
'<a href="/path/to/file.jpg" onclick="alert(e);">Link</a>',
'<a href="/path/to/file.jpg">Link</a>',
),
'on_attribute' => array(
'<button on="tap:my-lightbox">Tap Me</button>',
'<button on="tap:my-lightbox">Tap Me</button>',
),
'multiple_blacklisted_attributes' => array(
'<a href="/path/to/file.jpg" style="border: 1px solid red;" onclick="alert(e);">Link</a>',
'<a href="/path/to/file.jpg">Link</a>',
),
'javascript_protocol' => array(
'<a href="javascript:alert(\'Hello\');">Click</a>',
'Click',
),
'attribute_recursive' => array(
'<div style="border: 1px solid red;"><a href="/path/to/file.jpg" onclick="alert(e);">Hello World</a></div>',
'<div><a href="/path/to/file.jpg">Hello World</a></div>',
),
'mixed_tags' => array(
'<input type="text" /><p>Text</p><script>alert("")</script><style>body{ color: red; }</style>',
'<p>Text</p>',
),
'no_strip_amp_tags' => array(
'<amp-img src="http://example.com/path/to/file.jpg" width="300" height="300"></amp-img>',
'<amp-img src="http://example.com/path/to/file.jpg" width="300" height="300"></amp-img>',
),
'a_with_attachment_rel' => array(
'<a href="http://example.com" rel="wp-att-1686">Link</a>',
'<a href="http://example.com">Link</a>',
),
'a_with_attachment_rel_plus_another_valid_value' => array(
'<a href="http://example.com" rel="attachment wp-att-1686">Link</a>',
'<a href="http://example.com" rel="attachment">Link</a>',
),
'a_with_rev' => array(
'<a href="http://example.com" rev="footnote">Link</a>',
'<a href="http://example.com">Link</a>',
),
'a_with_target_blank' => array(
'<a href="http://example.com" target="_blank">Link</a>',
'<a href="http://example.com" target="_blank">Link</a>',
),
'a_with_target_uppercase_blank' => array(
'<a href="http://example.com" target="_BLANK">Link</a>',
'<a href="http://example.com" target="_blank">Link</a>',
),
'a_with_target_new' => array(
'<a href="http://example.com" target="_new">Link</a>',
'<a href="http://example.com" target="_blank">Link</a>',
),
'a_with_target_self' => array(
'<a href="http://example.com" target="_self">Link</a>',
'<a href="http://example.com">Link</a>',
),
'a_with_target_invalid' => array(
'<a href="http://example.com" target="boom">Link</a>',
'<a href="http://example.com">Link</a>',
),
'a_with_href_invalid' => array(
'<a href="some random text">Link</a>',
'Link',
),
'a_with_href_scheme_invalid' => array(
'<a href="wp://alinktosomething">Link</a>',
'Link',
),
'a_with_href_scheme_tel' => array(
'<a href="tel:4166669999">Call Me, Maybe</a>',
'<a href="tel:4166669999">Call Me, Maybe</a>',
),
'a_with_href_scheme_sms' => array(
'<a href="sms:4166669999">SMS Me, Maybe</a>',
'<a href="sms:4166669999">SMS Me, Maybe</a>',
),
'a_with_href_scheme_mailto' => array(
'<a href="mailto:[email protected]">Email Me, Maybe</a>',
'<a href="mailto:[email protected]">Email Me, Maybe</a>',
),
'a_with_href_relative' => array(
'<a href="/home">Home</a>',
'<a href="/home">Home</a>',
),
'a_with_anchor' => array(
'<a href="#section2">Home</a>',
'<a href="#section2">Home</a>',
),
'a_is_anchor' => array(
'<a name="section2"></a>',
'<a name="section2"></a>',
),
'a_is_achor_with_id' => array(
'<a id="section3"></a>',
'<a id="section3"></a>',
),
'a_empty' => array(
'<a>Hello World</a>',
'Hello World',
),
'a_empty_with_children_with_restricted_attributes' => array(
'<a><span style="color: red;">Red</span>&<span style="color: blue;">Orange</span></a>',
'<span>Red</span>&<span>Orange</span>',
),
'h1_with_size' => array(
'<h1 size="1">Headline</h1>',
'<h1>Headline</h1>',
),
'font' => array(
'<font size="1">Headline</font>',
'Headline',
),
// font is removed so we should check that other elements are checked as well
'font_with_other_bad_elements' => array(
'<font size="1">Headline</font><span style="color: blue">Span</span>',
'Headline<span>Span</span>',
),
);
}
/**
* @dataProvider get_data
*/
public function test_sanitizer( $source, $expected ) {
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$sanitizer = new AMP_Blacklist_Sanitizer( $dom );
$sanitizer->sanitize();
$content = AMP_DOM_Utils::get_content_from_dom( $dom );
$this->assertEquals( $expected, $content );
}
}