Core\Html::sanitize
Core\Html::sanitize(string $document): Core\Html\MarkupCore\Html::sanitize( string $document, ): Core\Html\Markup
Cleans a piece of HTML that somebody else wrote, so you can show it on your page safely.
Core\Html::sanitize reads the HTML the way a web browser does. Then it builds a new document from a fixed list of safe elements, like p, b, em, a, img, lists and tables. A tag that is not on the list is removed, and its text is kept. A script or style element is removed together with its content. Every attribute that could run code or change your page is removed, for example onclick, style, class and id. A link to javascript: loses its href. A link is kept when its URL starts with http:, https:, mailto: or tel:. A link to a page on the same site, such as /about, is kept too.
The result is a Core\Html\Markup value, so a page writes it exactly as it is. A program cannot change the list of safe elements.
Good to know: cleaning the result a second time gives the same result. If you want to show the HTML as plain text, use Core\Html::escape instead.
Parameters
$documentstringThe HTML to rebuild. Any text is a valid document, so this method never throws an error.
Return value
Returns Core\Html\Markup. A Core\Html\Markup with the rebuilt document. You can write it into a page without escaping it again. The method builds a new document and does not edit the old one. An element that is not allowed loses its tag, but its content is kept. An attribute that is not allowed is not written. A script, style or other raw-text element is removed with its content. The list of allowed elements is fixed, and no argument can extend it. A list you write yourself could have security holes. The text and the order of the kept content stay as in the document.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Html::sanitize keeps a link that starts with tel:. On a phone, such a link opens the dialer with the number. |
Examples
Formatting is kept a script is not
<?nvs// Shows how `Core\Html::sanitize` keeps simple formatting and removes a script.
// A comment that a visitor wrote with a few HTML tags.tainted string $comment = '<p>I <b>really</b> like this <em>recipe</em>!<script>alert(1)</script></p>';
// The `p`, `b` and `em` tags are kept. The `script` element is removed with its content.// This prints `<p>I <b>really</b> like this <em>recipe</em>!</p>`.echo Core\Html::sanitize($comment), "\n";
// A tag that is not allowed, like `font`, is removed. Its text is kept.// This prints `Hot and spicy`.echo Core\Html::sanitize('<font color="red">Hot</font> and spicy'), "\n";<p>I <b>really</b> like this <em>recipe</em>!</p>Hot and spicyLinks and attributes
<?nvs// Shows which attributes of a link and an image `Core\Html::sanitize` keeps.
// An `http` or `https` address in `href` is kept. The `onclick`, `class` and `target`// attributes are removed, because a link only needs its address.echo Core\Html::sanitize('<a href="https://example.com/help" onclick="steal()" class="button" target="_blank">Help</a>'), "\n";
// A `javascript:` address runs code when somebody clicks the link.// So the `href` is removed, and the link text stays.echo Core\Html::sanitize('<a href="javascript:alert(1)">Click me</a>'), "\n";
// An image keeps `src` and `alt`. The `onerror` attribute runs code, so it is removed.echo Core\Html::sanitize('<img src="/images/cake.png" alt="A cake" onerror="steal()">'), "\n";<a href="https://example.com/help">Help</a><a>Click me</a><img src="/images/cake.png" alt="A cake">A product description on a shop page
<?nvs// Shows how to put a product description from a text editor into a shop page.
// The description comes from the shop's admin area. It is `tainted`,// because a person typed it, so the program does not trust it.tainted string $description = <<<HTML<h2 id="intro">Green tea</h2><p>Picked by hand in <strong>spring</strong>.</p><ul><li>50 g<li>Loose leaves</ul><p style="color: red" onmouseover="steal()">Free shipping!</p>HTML;
// `Core\Html::sanitize` returns a `Core\Html\Markup` value.// You can add it to the rest of the page without escaping it again.Core\Html\Markup $page = ("<article class=\"product\">\n" as Core\Html\Markup) + Core\Html::sanitize($description) + ("\n</article>" as Core\Html\Markup);
// The missing `</li>` tags are added. The `id`, `style` and `onmouseover` attributes are removed.echo $page, "\n";<article class="product"><h2>Green tea</h2><p>Picked by hand in <strong>spring</strong>.</p><ul><li>50 g</li><li>Loose leaves</li></ul><p>Free shipping!</p></article>