Turns an HTML string into unescaped HTML. Do not use
m.trust on unsanitized user input.
Always try to use an alternative method first, before considering using
vnode = m.trust(html)
||Yes||A string containing HTML text|
||A trusted HTML vnode that represents the input string|
How to read signatures
How it works
By default, Mithril escapes all values in order to prevent a class of security problems called XSS injections.
var userContent = "<script>alert('evil')</script>" var view = m("div", userContent) m.render(document.body, view) // equivalent HTML // <div><script>alert('evil')</script></div>
However, sometimes it is desirable to render rich text and formatting markup. To fill that need,
m.trust creates trusted HTML vnodes which are rendered as HTML.
var view = m("div", [ m.trust("<h1>Here's some <em>HTML</em></h1>") ]) m.render(document.body, view) // equivalent HTML // <div><h1>Here's some <em>HTML</em></h1></div>
Trusted HTML vnodes are objects, not strings; therefore they cannot be concatenated with regular strings.
You must sanitize the input of
There are many ways in which an HTML string may contain executable code. The most common ways to inject security attacks are to add an
onerror attributes in
<iframe> tags, and to use unbalanced quotes such as
" onerror="alert(1) to inject executable contexts in unsanitized string interpolations.
There are countless non-obvious ways of creating malicious code, so it is highly recommended that you use a whitelist of permitted HTML tags, attributes and attribute values, as opposed to a blacklist to sanitize the user input. It's also highly recommended that you use a proper HTML parser, instead of regular expressions for sanitization, because regular expressions are extremely difficult to test for all edge cases.
Scripts that do not run
<script> tags are one thing that does not run when it appears in an HTML string.
For historical reasons, browsers ignore
<script> tags that are inserted into the DOM via innerHTML. They do this because once the element is ready (and thus, has an accessible innerHTML property), the rendering engines cannot backtrack to the parsing-stage if the script calls something like document.write("