forked from ampproject/amp-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-amp-sanitizer.php
130 lines (113 loc) · 3.19 KB
/
class-amp-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
<?php
class AMP_Sanitizer {
private static $allowed_html;
private static $allowed_protocols;
/**
* Strips blacklisted tags and attributes from content.
*
* See following for blacklist:
* https://github.com/ampproject/amphtml/blob/master/spec/amp-html-format.md#html-tags
*/
static public function strip( $content ) {
if ( empty( $content ) ) {
return $content;
}
$blacklisted_tags = self::get_blacklisted_tags();
$blacklisted_attributes = self::get_blacklisted_attributes();
$blacklisted_protocols = self::get_blacklisted_protocols();
$libxml_previous_state = libxml_use_internal_errors( true );
$dom = new DOMDocument;
// Wrap in dummy tags, since XML needs one parent node.
// It also makes it easier to loop through nodes.
// We can later use this to extract our nodes.
$result = $dom->loadHTML( '<html><body>' . $content . '</body></html>' );
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
if ( ! $result ) {
return $content;
}
$body = $dom->getElementsByTagName( 'body' )->item( 0 );
self::strip_tags( $body, $blacklisted_tags );
self::strip_attributes_recursive( $body, $blacklisted_attributes, $blacklisted_protocols );
// Only want children of the body tag, since we have a subset of HTML.
$out = '';
foreach ( $body->childNodes as $node ) {
$out .= $dom->saveXML( $node, LIBXML_NOEMPTYTAG );
}
return $out;
}
static private function strip_attributes_recursive( $node, $bad_attributes, $bad_protocols ) {
if ( $node->nodeType !== XML_ELEMENT_NODE ) {
return;
}
if ( $node->hasAttributes() ) {
foreach ( $node->attributes as $attribute ) {
$attribute_name = strtolower( $attribute->name );
if ( in_array( $attribute_name, $bad_attributes ) ) {
$node->removeAttribute( $attribute_name );
continue;
}
// on* attributes (like onclick) are a special case
if ( 0 === stripos( $attribute_name, 'on' ) ) {
$node->removeAttribute( $attribute_name );
continue;
}
if ( 'href' === $attribute_name ) {
$protocol = strtok( $attribute->value, ':' );
if ( in_array( $protocol, $bad_protocols ) ) {
$node->removeAttribute( $attribute_name );
continue;
}
}
}
}
foreach ( $node->childNodes as $child_node ) {
self::strip_attributes_recursive( $child_node, $bad_attributes, $bad_protocols );
}
}
static private function strip_tags( $node, $tags ) {
foreach ( $tags as $tag_name ) {
$elements = $node->getElementsByTagName( $tag_name );
if ( $elements->length ) {
foreach ( $elements as $element ) {
$element->parentNode->removeChild( $element );
}
}
}
}
static private function get_blacklisted_protocols() {
return array(
'javascript',
);
}
static private function get_blacklisted_tags() {
return array(
'script',
'noscript',
'style',
'frame',
'frameset',
'object',
'param',
'applet',
'form',
'input',
'button',
'textarea',
'select',
'option',
'link',
'meta',
// These are converted into amp-* versions
//'img',
//'video',
//'audio',
//'iframe',
);
}
static private function get_blacklisted_attributes() {
return array(
'style',
);
}
}