@@ -9,26 +9,50 @@ public function test_filter_oembed_result_trusted_malicious_iframe() {
9
9
10
10
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), 'https://www.youtube.com/watch?v=72xdCU__XCk ' );
11
11
12
- $ this ->assertSame ( $ html , $ actual );
12
+ $ this ->assertEqualHTML ( $ html , $ actual );
13
13
}
14
14
15
15
public function test_filter_oembed_result_with_untrusted_provider () {
16
16
$ html = '<p></p><iframe onload="alert(1)" src="http://example.com/sample-page/"></iframe> ' ;
17
17
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), 'http://example.com/sample-page/ ' );
18
18
19
- $ matches = array ();
20
- preg_match ( '|src=".*#\?secret=([\w\d]+)" data-secret="([\w\d]+)"| ' , $ actual , $ matches );
19
+ $ processor = new WP_HTML_Tag_Processor ( $ actual );
21
20
22
- $ this ->assertArrayHasKey ( 1 , $ matches );
23
- $ this ->assertArrayHasKey ( 2 , $ matches );
24
- $ this ->assertSame ( $ matches [1 ], $ matches [2 ] );
21
+ $ this ->assertTrue (
22
+ $ processor ->next_tag ( 'IFRAME ' ),
23
+ 'Failed to find expected IFRAME element in filtered output. '
24
+ );
25
+
26
+ $ src = $ processor ->get_attribute ( 'src ' );
27
+ $ this ->assertIsString (
28
+ $ src ,
29
+ isset ( $ src )
30
+ ? 'Expected "src" attribute on IFRAME with string value but found boolean attribute instead. '
31
+ : 'Failed to find expected "src" attribute on IFRAME element. '
32
+ );
33
+
34
+ $ query_string = parse_url ( $ src , PHP_URL_QUERY );
35
+ $ query_args = array ();
36
+ parse_str ( $ query_string , $ query_args );
37
+
38
+ $ this ->assertArrayHasKey (
39
+ 'secret ' ,
40
+ $ query_args ,
41
+ 'Failed to find expected query arg "secret" in IFRAME "src" attribute. '
42
+ );
43
+
44
+ $ this ->assertSame (
45
+ $ query_args ['secret ' ],
46
+ $ processor ->get_attribute ( 'data-secret ' ),
47
+ 'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute. '
48
+ );
25
49
}
26
50
27
51
public function test_filter_oembed_result_only_one_iframe_is_allowed () {
28
52
$ html = '<div><iframe></iframe><iframe></iframe><p></p></div> ' ;
29
53
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
30
54
31
- $ this ->assertSame ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
55
+ $ this ->assertEqualHTML ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
32
56
}
33
57
34
58
public function test_filter_oembed_result_with_newlines () {
@@ -41,7 +65,7 @@ public function test_filter_oembed_result_with_newlines() {
41
65
42
66
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
43
67
44
- $ this ->assertSame ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
68
+ $ this ->assertEqualHTML ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
45
69
}
46
70
47
71
public function test_filter_oembed_result_without_iframe () {
@@ -60,18 +84,41 @@ public function test_filter_oembed_result_secret_param_available() {
60
84
$ html = '<iframe src="https://wordpress.org"></iframe> ' ;
61
85
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
62
86
63
- $ matches = array ();
64
- preg_match ( '|src="https://wordpress.org#\?secret=([\w\d]+)" data-secret="([\w\d]+)"| ' , $ actual , $ matches );
87
+ $ processor = new WP_HTML_Tag_Processor ( $ actual );
65
88
66
- $ this ->assertArrayHasKey ( 1 , $ matches );
67
- $ this ->assertArrayHasKey ( 2 , $ matches );
68
- $ this ->assertSame ( $ matches [1 ], $ matches [2 ] );
89
+ $ this ->assertTrue (
90
+ $ processor ->next_tag ( 'IFRAME ' ),
91
+ 'Failed to find expected IFRAME element in filtered output. '
92
+ );
93
+
94
+ $ src = $ processor ->get_attribute ( 'src ' );
95
+ $ this ->assertMatchesRegularExpression (
96
+ '~^https://wordpress.org~ ' ,
97
+ $ src ,
98
+ 'Failed to find expected "src" attribute on IFRAME element. '
99
+ );
100
+
101
+ $ query_string = parse_url ( $ src , PHP_URL_QUERY );
102
+ $ query_args = array ();
103
+ parse_str ( $ query_string , $ query_args );
104
+
105
+ $ this ->assertArrayHasKey (
106
+ 'secret ' ,
107
+ $ query_args ,
108
+ 'Failed to find expected query arg "secret" in IFRAME "src" attribute. '
109
+ );
110
+
111
+ $ this ->assertSame (
112
+ $ query_args ['secret ' ],
113
+ $ processor ->get_attribute ( 'data-secret ' ),
114
+ 'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute. '
115
+ );
69
116
}
70
117
71
118
public function test_filter_oembed_result_wrong_type_provided () {
72
119
$ actual = wp_filter_oembed_result ( 'some string ' , (object ) array ( 'type ' => 'link ' ), '' );
73
120
74
- $ this ->assertSame ( 'some string ' , $ actual );
121
+ $ this ->assertEqualHTML ( 'some string ' , $ actual );
75
122
}
76
123
77
124
public function test_filter_oembed_result_invalid_result () {
@@ -83,14 +130,14 @@ public function test_filter_oembed_result_blockquote_adds_style_to_iframe() {
83
130
$ html = '<blockquote></blockquote><iframe></iframe> ' ;
84
131
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
85
132
86
- $ this ->assertSame ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe> ' , $ actual );
133
+ $ this ->assertEqualHTML ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe> ' , $ actual );
87
134
}
88
135
89
136
public function test_filter_oembed_result_allowed_html () {
90
137
$ html = '<blockquote class="foo" id="bar"><strong><a href="" target=""></a></strong></blockquote><iframe></iframe> ' ;
91
138
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
92
139
93
- $ this ->assertSame ( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe> ' , $ actual );
140
+ $ this ->assertEqualHTML ( '<blockquote class="wp-embedded-content"><a href=""></a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; visibility: hidden;"></iframe> ' , $ actual );
94
141
}
95
142
96
143
public function data_wp_filter_pre_oembed_custom_result () {
@@ -124,7 +171,7 @@ public function test_wp_filter_pre_oembed_custom_result( $html, $expected ) {
124
171
'html ' => $ html ,
125
172
);
126
173
$ actual = _wp_oembed_get_object ()->data2html ( $ data , 'https://untrusted.localhost ' );
127
- $ this ->assertSame ( $ expected , $ actual );
174
+ $ this ->assertEqualHTML ( $ expected , $ actual );
128
175
}
129
176
130
177
/**
@@ -134,6 +181,6 @@ public function test_filter_feed_content() {
134
181
$ html = '<blockquote></blockquote><iframe></iframe> ' ;
135
182
$ actual = _oembed_filter_feed_content ( wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' ) );
136
183
137
- $ this ->assertSame ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe> ' , $ actual );
184
+ $ this ->assertEqualHTML ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe> ' , $ actual );
138
185
}
139
186
}
0 commit comments