3 namespace BookStack\Util;
10 class HtmlContentFilter
13 * Remove all the script elements from the given HTML.
15 public static function removeScripts(string $html): string
21 $html = '<body>' . $html . '</body>';
22 libxml_use_internal_errors(true);
23 $doc = new DOMDocument();
24 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
25 $xPath = new DOMXPath($doc);
27 // Remove standard script tags
28 $scriptElems = $xPath->query('//script');
29 static::removeNodes($scriptElems);
31 // Remove clickable links to JavaScript URI
32 $badLinks = $xPath->query('//*[' . static::xpathContains('@href', 'javascript:') . ']');
33 static::removeNodes($badLinks);
35 // Remove forms with calls to JavaScript URI
36 $badForms = $xPath->query('//*[' . static::xpathContains('@action', 'javascript:') . '] | //*[' . static::xpathContains('@formaction', 'javascript:') . ']');
37 static::removeNodes($badForms);
39 // Remove meta tag to prevent external redirects
40 $metaTags = $xPath->query('//meta[' . static::xpathContains('@content', 'url') . ']');
41 static::removeNodes($metaTags);
43 // Remove data or JavaScript iFrames
44 $badIframes = $xPath->query('//*[' . static::xpathContains('@src', 'data:') . '] | //*[' . static::xpathContains('@src', 'javascript:') . '] | //*[@srcdoc]');
45 static::removeNodes($badIframes);
47 // Remove elements with a xlink:href attribute
48 // Used in SVG but deprecated anyway, so we'll be a bit more heavy-handed here.
49 $xlinkHrefAttributes = $xPath->query('//@*[contains(name(), \'xlink:href\')]');
50 static::removeAttributes($xlinkHrefAttributes);
52 // Remove 'on*' attributes
53 $onAttributes = $xPath->query('//@*[starts-with(name(), \'on\')]');
54 static::removeAttributes($onAttributes);
57 $topElems = $doc->documentElement->childNodes->item(0)->childNodes;
58 foreach ($topElems as $child) {
59 $html .= $doc->saveHTML($child);
66 * Create a xpath contains statement with a translation automatically built within
67 * to affectively search in a cases-insensitive manner.
69 protected static function xpathContains(string $property, string $value): string
71 $value = strtolower($value);
72 $upperVal = strtoupper($value);
74 return 'contains(translate(' . $property . ', \'' . $upperVal . '\', \'' . $value . '\'), \'' . $value . '\')';
78 * Remove all the given DOMNodes.
80 protected static function removeNodes(DOMNodeList $nodes): void
82 foreach ($nodes as $node) {
83 $node->parentNode->removeChild($node);
88 * Remove all the given attribute nodes.
90 protected static function removeAttributes(DOMNodeList $attrs): void
92 /** @var DOMAttr $attr */
93 foreach ($attrs as $attr) {
94 $attrName = $attr->nodeName;
95 $attr->parentNode->removeAttribute($attrName);