@@ -9,26 +9,57 @@ 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_FRAGMENT );
35
+ $ this ->assertStringStartsWith (
36
+ '? ' ,
37
+ $ query_string ,
38
+ 'Should have found URL fragment in "src" attribute resembling a query string. '
39
+ );
40
+
41
+ $ query_string = substr ( $ query_string , 1 );
42
+ $ query_args = array ();
43
+ parse_str ( $ query_string , $ query_args );
44
+
45
+ $ this ->assertArrayHasKey (
46
+ 'secret ' ,
47
+ $ query_args ,
48
+ 'Failed to find expected query arg "secret" in IFRAME "src" attribute. '
49
+ );
50
+
51
+ $ this ->assertSame (
52
+ $ query_args ['secret ' ],
53
+ $ processor ->get_attribute ( 'data-secret ' ),
54
+ 'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute. '
55
+ );
25
56
}
26
57
27
58
public function test_filter_oembed_result_only_one_iframe_is_allowed () {
28
59
$ html = '<div><iframe></iframe><iframe></iframe><p></p></div> ' ;
29
60
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
30
61
31
- $ this ->assertSame ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
62
+ $ this ->assertEqualHTML ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
32
63
}
33
64
34
65
public function test_filter_oembed_result_with_newlines () {
@@ -41,7 +72,7 @@ public function test_filter_oembed_result_with_newlines() {
41
72
42
73
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
43
74
44
- $ this ->assertSame ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
75
+ $ this ->assertEqualHTML ( '<iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"></iframe> ' , $ actual );
45
76
}
46
77
47
78
public function test_filter_oembed_result_without_iframe () {
@@ -60,18 +91,48 @@ public function test_filter_oembed_result_secret_param_available() {
60
91
$ html = '<iframe src="https://wordpress.org"></iframe> ' ;
61
92
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
62
93
63
- $ matches = array ();
64
- preg_match ( '|src="https://wordpress.org#\?secret=([\w\d]+)" data-secret="([\w\d]+)"| ' , $ actual , $ matches );
94
+ $ processor = new WP_HTML_Tag_Processor ( $ actual );
65
95
66
- $ this ->assertArrayHasKey ( 1 , $ matches );
67
- $ this ->assertArrayHasKey ( 2 , $ matches );
68
- $ this ->assertSame ( $ matches [1 ], $ matches [2 ] );
96
+ $ this ->assertTrue (
97
+ $ processor ->next_tag ( 'IFRAME ' ),
98
+ 'Failed to find expected IFRAME element in filtered output. '
99
+ );
100
+
101
+ $ src = $ processor ->get_attribute ( 'src ' );
102
+ $ this ->assertMatchesRegularExpression (
103
+ '~^https://wordpress.org~ ' ,
104
+ $ src ,
105
+ 'Failed to find expected "src" attribute on IFRAME element. '
106
+ );
107
+
108
+ $ query_string = parse_url ( $ src , PHP_URL_FRAGMENT );
109
+ $ this ->assertStringStartsWith (
110
+ '? ' ,
111
+ $ query_string ,
112
+ 'Should have found URL fragment in "src" attribute resembling a query string. '
113
+ );
114
+
115
+ $ query_string = substr ( $ query_string , 1 );
116
+ $ query_args = array ();
117
+ parse_str ( $ query_string , $ query_args );
118
+
119
+ $ this ->assertArrayHasKey (
120
+ 'secret ' ,
121
+ $ query_args ,
122
+ 'Failed to find expected query arg "secret" in IFRAME "src" attribute. '
123
+ );
124
+
125
+ $ this ->assertSame (
126
+ $ query_args ['secret ' ],
127
+ $ processor ->get_attribute ( 'data-secret ' ),
128
+ 'Expected to find identical copy of secret from IFRAME "src" in the "data-secret" attribute. '
129
+ );
69
130
}
70
131
71
132
public function test_filter_oembed_result_wrong_type_provided () {
72
133
$ actual = wp_filter_oembed_result ( 'some string ' , (object ) array ( 'type ' => 'link ' ), '' );
73
134
74
- $ this ->assertSame ( 'some string ' , $ actual );
135
+ $ this ->assertEqualHTML ( 'some string ' , $ actual );
75
136
}
76
137
77
138
public function test_filter_oembed_result_invalid_result () {
@@ -83,14 +144,14 @@ public function test_filter_oembed_result_blockquote_adds_style_to_iframe() {
83
144
$ html = '<blockquote></blockquote><iframe></iframe> ' ;
84
145
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
85
146
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 );
147
+ $ 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
148
}
88
149
89
150
public function test_filter_oembed_result_allowed_html () {
90
151
$ html = '<blockquote class="foo" id="bar"><strong><a href="" target=""></a></strong></blockquote><iframe></iframe> ' ;
91
152
$ actual = wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' );
92
153
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 );
154
+ $ 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
155
}
95
156
96
157
public function data_wp_filter_pre_oembed_custom_result () {
@@ -124,7 +185,7 @@ public function test_wp_filter_pre_oembed_custom_result( $html, $expected ) {
124
185
'html ' => $ html ,
125
186
);
126
187
$ actual = _wp_oembed_get_object ()->data2html ( $ data , 'https://untrusted.localhost ' );
127
- $ this ->assertSame ( $ expected , $ actual );
188
+ $ this ->assertEqualHTML ( $ expected , $ actual );
128
189
}
129
190
130
191
/**
@@ -134,6 +195,6 @@ public function test_filter_feed_content() {
134
195
$ html = '<blockquote></blockquote><iframe></iframe> ' ;
135
196
$ actual = _oembed_filter_feed_content ( wp_filter_oembed_result ( $ html , (object ) array ( 'type ' => 'rich ' ), '' ) );
136
197
137
- $ this ->assertSame ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe> ' , $ actual );
198
+ $ this ->assertEqualHTML ( '<blockquote class="wp-embedded-content"></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" ></iframe> ' , $ actual );
138
199
}
139
200
}
0 commit comments