]> BookStack Code Mirror - bookstack/blob - app/Util/HtmlContentFilter.php
182f6e63529a3e283330b39a6fd35203eccbedef
[bookstack] / app / Util / HtmlContentFilter.php
1 <?php
2
3 namespace BookStack\Util;
4
5 use DOMAttr;
6 use DOMDocument;
7 use DOMElement;
8 use DOMNodeList;
9 use DOMXPath;
10
11 class HtmlContentFilter
12 {
13     /**
14      * Remove all the script elements from the given HTML.
15      */
16     public static function removeScripts(string $html): string
17     {
18         if (empty($html)) {
19             return $html;
20         }
21
22         $html = '<body>' . $html . '</body>';
23         libxml_use_internal_errors(true);
24         $doc = new DOMDocument();
25         $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
26         $xPath = new DOMXPath($doc);
27
28         // Remove standard script tags
29         $scriptElems = $xPath->query('//script');
30         static::removeNodes($scriptElems);
31
32         // Remove clickable links to JavaScript URI
33         $badLinks = $xPath->query('//*[' . static::xpathContains('@href', 'javascript:') . ']');
34         static::removeNodes($badLinks);
35
36         // Remove forms with calls to JavaScript URI
37         $badForms = $xPath->query('//*[' . static::xpathContains('@action', 'javascript:') . '] | //*[' . static::xpathContains('@formaction', 'javascript:') . ']');
38         static::removeNodes($badForms);
39
40         // Remove meta tag to prevent external redirects
41         $metaTags = $xPath->query('//meta[' . static::xpathContains('@content', 'url') . ']');
42         static::removeNodes($metaTags);
43
44         // Remove data or JavaScript iFrames
45         $badIframes = $xPath->query('//*[' . static::xpathContains('@src', 'data:') . '] | //*[' . static::xpathContains('@src', 'javascript:') . '] | //*[@srcdoc]');
46         static::removeNodes($badIframes);
47
48         // Remove tags hiding JavaScript or data uris in values attribute.
49         // For example, SVG animate tag can exploit javascript in values.
50         $badValuesTags = $xPath->query('//*[' . static::xpathContains('@values', 'data:') . '] | //*[' . static::xpathContains('@values', 'javascript:') . ']');
51         static::removeNodes($badValuesTags);
52
53         // Remove elements with a xlink:href attribute
54         // Used in SVG but deprecated anyway, so we'll be a bit more heavy-handed here.
55         $xlinkHrefAttributes = $xPath->query('//@*[contains(name(), \'xlink:href\')]');
56         static::removeAttributes($xlinkHrefAttributes);
57
58         // Remove 'on*' attributes
59         $onAttributes = $xPath->query('//@*[starts-with(name(), \'on\')]');
60         static::removeAttributes($onAttributes);
61
62         $html = '';
63         $topElems = $doc->documentElement->childNodes->item(0)->childNodes;
64         foreach ($topElems as $child) {
65             $html .= $doc->saveHTML($child);
66         }
67
68         return $html;
69     }
70
71     /**
72      * Create a xpath contains statement with a translation automatically built within
73      * to affectively search in a cases-insensitive manner.
74      */
75     protected static function xpathContains(string $property, string $value): string
76     {
77         $value = strtolower($value);
78         $upperVal = strtoupper($value);
79
80         return 'contains(translate(' . $property . ', \'' . $upperVal . '\', \'' . $value . '\'), \'' . $value . '\')';
81     }
82
83     /**
84      * Remove all the given DOMNodes.
85      */
86     protected static function removeNodes(DOMNodeList $nodes): void
87     {
88         foreach ($nodes as $node) {
89             $node->parentNode->removeChild($node);
90         }
91     }
92
93     /**
94      * Remove all the given attribute nodes.
95      */
96     protected static function removeAttributes(DOMNodeList $attrs): void
97     {
98         /** @var DOMAttr $attr */
99         foreach ($attrs as $attr) {
100             $attrName = $attr->nodeName;
101             /** @var DOMElement $parentNode */
102             $parentNode = $attr->parentNode;
103             $parentNode->removeAttribute($attrName);
104         }
105     }
106 }