-
Notifications
You must be signed in to change notification settings - Fork 178
/
Sanitization_Utils.php
482 lines (420 loc) · 13.7 KB
/
Sanitization_Utils.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
/**
* Trait Sanitization_Utils.
*
* @package Google\Web_Stories
* @copyright 2020 Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://github.com/google/web-stories-wp
*/
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Web_Stories\AMP\Traits;
use DOMElement;
use DOMNodeList;
use Google\Web_Stories_Dependencies\AmpProject\Dom\Document;
use \AmpProject\Dom\Document as AMP_Document;
/**
* Trait Sanitization_Utils
*
* @since 1.1.0
*/
trait Sanitization_Utils {
/**
* Replaces the HTML start tag to make the language attributes dynamic.
*
* @since 1.1.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function transform_html_start_tag( &$document ) {
$document->html->setAttribute( 'amp', '' );
// See get_language_attributes().
if ( is_rtl() ) {
$document->html->setAttribute( 'dir', 'rtl' );
}
$lang = get_bloginfo( 'language' );
if ( $lang ) {
$document->html->setAttribute( 'lang', esc_attr( $lang ) );
}
}
/**
* Transform all hyperlinks to ensure they're always valid.
*
* Adds target="_blank" and rel="noreferrer" attributes.
* Does not add rel="noreferrer" for same-origin links.
*
* Removes empty data-tooltip-icon and data-tooltip-text attributes
* to prevent validation issues.
*
* @since 1.1.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function transform_a_tags( &$document ) {
$links = $document->getElementsByTagName( 'a' );
/**
* The <a> element
*
* @var DOMElement $link The <a> element
*/
foreach ( $links as $link ) {
if ( ! $link->getAttribute( 'target' ) ) {
$link->setAttribute( 'target', '_blank' );
}
$is_link_to_same_origin = 0 === strpos( $link->getAttribute( 'href' ), home_url() );
$rel = $link->getAttribute( 'rel' );
// Links to the same site should not have "noreferrer".
// Other rel values should not be modified.
// See https://github.com/google/web-stories-wp/issues/9494.
$rel = str_replace( 'noreferrer', '', $rel );
if ( ! $is_link_to_same_origin ) {
$rel .= ' noreferrer';
}
if ( empty( $rel ) ) {
$link->removeAttribute( 'rel' );
} else {
$link->setAttribute( 'rel', trim( $rel ) );
}
if ( ! $link->getAttribute( 'data-tooltip-icon' ) ) {
$link->removeAttribute( 'data-tooltip-icon' );
}
if ( ! $link->getAttribute( 'data-tooltip-text' ) ) {
$link->removeAttribute( 'data-tooltip-text' );
}
}
}
/**
* Sanitizes <amp-story-page-outlink> elements to ensure they're always valid.
*
* Removes empty `cta-image` attributes.
* Ensures the element is always the last child of <amp-story-page>.
*
* @since 1.13.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function sanitize_amp_story_page_outlink( &$document ) {
$outlink_elements = $document->getElementsByTagName( 'amp-story-page-outlink' );
/**
* The <amp-story-page-outlink> element
*
* @var DOMElement $element The <amp-story-page-outlink> element
*/
foreach ( $outlink_elements as $element ) {
if ( ! $element->getAttribute( 'cta-image' ) ) {
$element->removeAttribute( 'cta-image' );
}
$amp_story_page = $element->parentNode;
if ( $amp_story_page && $element !== $amp_story_page->lastChild ) {
$amp_story_page->removeChild( $element );
$amp_story_page->appendChild( $element );
}
}
}
/**
* Replaces the placeholder of publisher logo in the content.
*
* @since 1.1.0
*
* @param Document|AMP_Document $document Document instance.
* @param string $publisher_logo Publisher logo.
* @return void
*/
private function add_publisher_logo( &$document, $publisher_logo ) {
/**
* The <amp-story> element.
*
* @var DOMElement $story_element The <amp-story> element.
*/
$story_element = $document->body->getElementsByTagName( 'amp-story' )->item( 0 );
if ( ! $story_element instanceof DOMElement ) {
return;
}
// Add a publisher logo if missing or just a placeholder.
$existing_publisher_logo = $story_element->getAttribute( 'publisher-logo-src' );
// Backward compatibility for when fallback-wordpress-publisher-logo.png was provided by the plugin.
if ( ! $existing_publisher_logo || false !== strpos( $existing_publisher_logo, 'fallback-wordpress-publisher-logo.png' ) ) {
$story_element->setAttribute( 'publisher-logo-src', $publisher_logo );
}
if ( ! $story_element->getAttribute( 'publisher-logo-src' ) ) {
$story_element->setAttribute( 'publisher-logo-src', $publisher_logo );
}
}
/**
* Replaces the placeholder of publisher in the content.
*
* Ensures the `publisher` attribute exists if missing.
*
* @since 1.7.0
*
* @param Document|AMP_Document $document Document instance.
* @param string $publisher Publisher logo.
* @return void
*/
private function add_publisher( &$document, $publisher ) {
/**
* The <amp-story> element.
*
* @var DOMElement $story_element The <amp-story> element.
*/
$story_element = $document->body->getElementsByTagName( 'amp-story' )->item( 0 );
if ( ! $story_element instanceof DOMElement ) {
return;
}
if ( $publisher || ! $story_element->hasAttribute( 'publisher' ) ) {
$story_element->setAttribute( 'publisher', $publisher );
}
}
/**
* Adds square, and landscape poster images to the <amp-story>.
*
* @since 1.1.0
*
* @param Document|AMP_Document $document Document instance.
* @param string[] $poster_images List of poster images, keyed by type.
* @return void
*/
private function add_poster_images( &$document, $poster_images ) {
/**
* The <amp-story> element.
*
* @var DOMElement $story_element The <amp-story> element.
*/
$story_element = $document->body->getElementsByTagName( 'amp-story' )->item( 0 );
if ( ! $story_element instanceof DOMElement ) {
return;
}
// The story sanitizer only passes valid, non-empty URLs that are already escaped.
// That means we don't need to do any additional checks here or worry about accidentally overriding
// an existing poster-portrait-src attribute value with an empty one.
foreach ( $poster_images as $attr => $url ) {
$story_element->setAttribute( $attr, $url );
}
if ( ! $story_element->getAttribute( 'poster-portrait-src' ) ) {
$story_element->setAttribute( 'poster-portrait-src', '' );
}
}
/**
* De-duplicate inline styles in the story.
*
* Greatly reduce the amount of `style[amp-custom]` CSS for stories by de-duplicating inline styles
* and moving to simple class selector style rules, avoiding the specificity hack
* that the AMP plugin's style sanitizer employs.
*
* @since 1.8.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function deduplicate_inline_styles( $document ) {
$elements_by_inline_style = [];
// Gather all elements based on their common inline styles.
$elements_with_style = $document->xpath->query( '//*[ @style ]' );
if ( $elements_with_style instanceof DOMNodeList && $elements_with_style->length > 0 ) {
/**
* The element with inline styles.
*
* @var DOMElement $styled_element The element.
*/
foreach ( $elements_with_style as $styled_element ) {
$value = $styled_element->getAttribute( 'style' );
$styled_element->removeAttribute( 'style' );
$elements_by_inline_style[ $value ][] = $styled_element;
}
}
if ( empty( $elements_by_inline_style ) ) {
return;
}
$style_element = $document->createElement( 'style' );
if ( ! $style_element ) {
return;
}
$document->head->appendChild( $style_element );
// Create style rule for each inline style and add class name to each element.
foreach ( $elements_by_inline_style as $inline_style => $styled_elements ) {
$inline_style_class_name = '_' . substr( md5( (string) $inline_style ), 0, 7 );
$style_rule = $document->createTextNode( sprintf( '.%s{%s}', $inline_style_class_name, $inline_style ) );
$style_element->appendChild( $style_rule );
/**
* The element with inline styles.
*
* @var DOMElement $styled_element The element.
*/
foreach ( $styled_elements as $styled_element ) {
$class_name = $inline_style_class_name;
if ( $styled_element->hasAttribute( 'class' ) ) {
$class_name .= ' ' . $styled_element->getAttribute( 'class' );
}
$styled_element->setAttribute( 'class', $class_name );
}
}
}
/**
* Enables using video cache by adding the necessary attribute to `<amp-video>`
*
* @since 1.10.0
*
* @param Document|AMP_Document $document Document instance.
* @param bool $video_cache_enabled Whether video cache is enabled.
* @return void
*/
private function add_video_cache( &$document, $video_cache_enabled ) {
if ( ! $video_cache_enabled ) {
return;
}
$videos = $document->body->getElementsByTagName( 'amp-video' );
/**
* The <amp-video> element
*
* @var DOMElement $video The <amp-video> element
*/
foreach ( $videos as $video ) {
$video->setAttribute( 'cache', 'google' );
}
}
/**
* Determines whether a URL is a `blob:` URL.
*
* @since 1.9.0
*
* @param string $url URL.
* @return bool Whether it's a blob URL.
*/
private function is_blob_url( string $url ): bool {
return 0 === strpos( $url, 'blob:' );
}
/**
* Remove `blob:` URLs from videos and images that might have slipped through.
*
* @since 1.9.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function remove_blob_urls( &$document ) {
/**
* List of <amp-video> elements.
*
* @var DOMElement[] $videos Video elements.
*/
$videos = $document->body->getElementsByTagName( 'amp-video' );
foreach ( $videos as $video ) {
if ( $this->is_blob_url( $video->getAttribute( 'poster' ) ) ) {
$video->setAttribute( 'poster', '' );
}
if ( $this->is_blob_url( $video->getAttribute( 'artwork' ) ) ) {
$video->setAttribute( 'artwork', '' );
}
/**
* List of <source> child elements.
*
* @var DOMElement[] $video_sources Video source elements.
*/
$video_sources = $video->getElementsByTagName( 'source' );
foreach ( $video_sources as $source ) {
if ( $this->is_blob_url( $source->getAttribute( 'src' ) ) ) {
$source->setAttribute( 'src', '' );
}
}
}
/**
* List of <amp-img> elements.
*
* @var DOMElement[] $images Image elements.
*/
$images = $document->body->getElementsByTagName( 'amp-img' );
foreach ( $images as $image ) {
if ( $this->is_blob_url( $image->getAttribute( 'src' ) ) ) {
$image->setAttribute( 'src', '' );
}
}
}
/**
* Sanitize amp-img[srcset] attributes to remove duplicates.
*
* @since 1.10.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function sanitize_srcset( &$document ) {
/**
* List of <amp-img> elements.
*
* @var DOMElement[] $images Image elements.
*/
$images = $document->body->getElementsByTagName( 'amp-img' );
foreach ( $images as $image ) {
$srcset = $image->getAttribute( 'srcset' );
if ( ! $srcset ) {
continue;
}
$matches = [];
// Matches every srcset entry (consisting of a URL and a width descriptor) within `srcset=""`.
// Not using explode(',') to not break with URLs containing commas.
// Given "foo1,2/image.png 123w, foo2,3/image.png 456w", the named capture group "entry"
// will contain "foo1,2/image.png 123w" and "foo2,3/image.png 456w", without the trailing commas.
preg_match_all( '/((?<entry>[^ ]+ [\d]+w),?)/', $srcset, $matches );
$entries = $matches['entry'] ?? [];
$entries_by_widths = [];
foreach ( $entries as $entry ) {
$entry_data = explode( ' ', $entry );
if ( ! isset( $entries_by_widths[ $entry_data[1] ] ) ) {
$entries_by_widths[ $entry_data[1] ] = $entry;
}
}
$image->setAttribute( 'srcset', implode( ', ', $entries_by_widths ) );
}
}
/**
* Remove images referencing the grid-placeholder.png file which has since been removed.
*
* Prevents 404 errors for non-existent image files when creators forget to replace/remove
* the placeholder image.
*
* The placeholder functionality was removed in v1.14.0, nevertheless older stories could still
* reference the files.
*
* @link https://github.com/google/web-stories-wp/issues/9530
*
* @since 1.14.0
*
* @param Document|AMP_Document $document Document instance.
* @return void
*/
private function remove_page_template_placeholder_images( &$document ) {
// Catches "assets/images/editor/grid-placeholder.png" as well as
// "web-stories/assets/images/adde98ae406d6b5c95d111a934487252.png" (v1.14.0)
// and potentially other variants.
$placeholder_img = 'plugins/web-stories/assets/images';
/**
* List of <amp-img> elements.
*
* @var DOMElement[] $images Image elements.
*/
$images = $document->body->getElementsByTagName( 'amp-img' );
foreach ( $images as $image ) {
$src = $image->getAttribute( 'src' );
if ( $image->parentNode && false !== strpos( $src, $placeholder_img ) ) {
$image->parentNode->removeChild( $image );
}
}
}
}