logoalt Hacker News

philipwhiuklast Wednesday at 5:39 PM5 repliesview on HN

The downside of a new method is that it leaves innerHtml as a source of future security issues.


Replies

crotelast Wednesday at 5:48 PM

Yes, but you can also easily lint on it: all uses of `context.innerHTML` are now suspect and should get a suggestion to use `context.setHTML` instead.

With `const clean = DOMPurify.sanitize(input); context.innerHTML = clean;` your linter suddenly needs to do complex code analysis and keep track if each variable passed to `context.innerHTML` is clean or tainted.

wbobeirnelast Wednesday at 5:52 PM

I feel like calling this a downside implies there's an alternative, but there's no way that `innerHtml`'s behavior could be changed. There are a lot of valid reasons for arbitrary HTML to be set, and changing that would break so many things.

show 1 reply
ricticlast Thursday at 4:19 PM

You can disable it for your site using a trusted types content security policy.

cluckindanlast Wednesday at 5:46 PM

Yes, one could simply make a setter for innerHTML which calls setHTML(). No code changes needed.

show 1 reply