My application dynamically builds HTML content as a string and when finished the content is being attached to the DOM. In WinJS however this throws exceptions once I try to attach the string to the DOM. In order to work around these exceptions I have to sanitize the HTML by running it through toStaticHTML, which is globally defined in WinJS as well as in Internet Explorer. The issue that I am having is that there are quite a lot of use of data-* html5 attributes. Once I run those through toStaticHTML they are being stripped. Why does toStaticHTML remove data-* attributes? What is the real security concern with them?
Note that I cannot wrap the DOM insertion in MSApp.execUnsafeLocalFunction because I am using jQuery and I am not allowed to modify the jQuery code.
var html = "<ul><li data-role='list-node'>My list node</li></ul>";
$('#container').html(toStaticHTML(html));
Produces:
<ul>
<li>My list node</li>
</ul>
This is because of security concerns about inserting random bits of HTML into the document, and potentially allowing unsafe code to execute inside a protected context (your app, with full access to the WinRT, and users documents).
toStaticHtml
is intended to remain 'secure' in the case of evolving HTML/web patterns so it is a whitelist rather than a blacklist.Given your challenge that you have here I see the following options:
msExecUnsafeLocalFunction
(see below). This means for the life of that call, all Dom insertions will be fine. This doesn't require a change to jquery, just your code.msExecUnsafeLocalFunction
WinJS.Binding.Template
to render your content rather than Jquery. This clones the nodes rather than stringifying the HTMLsetAttribute
after the safe node is inserted.Example usage of msExecUnsafeLocalFunction:
The
data-role
-attribute is not listed here: http://msdn.microsoft.com/en-us/library/windows/apps/hh465388.aspxIt's an unknown attribute and will be removed.
As mention in the docs
data
is not allowed: http://msdn.microsoft.com/en-us/library/windows/apps/hh465388.aspx