From 7f57c68b4b7c54903941ce48a80f118787a03e01 Mon Sep 17 00:00:00 2001 From: Harri Lehtola Date: Tue, 14 Apr 2020 20:44:54 +0300 Subject: [PATCH] fix($sanitize): remove browser bug detections from inert strategy selection Default to using DOMParser if it is available and fall back to createHTMLDocument if needed. This is the approach suggested in the related pull request #17013 and used by DOMPurify too. It also safely avoids using an inline style tag that causes CSP violation errors if inline CSS is prohibited. The related unit tests in `sanitizeSpec.js`, "should not allow JavaScript execution when creating inert document" and "should not allow JavaScript hidden in badly formed HTML to get through sanitization (Firefox bug)", are left untouched to assert that the behavior hasn't changed in those scenarios. Fixes #16463. --- src/ngSanitize/sanitize.js | 48 +++++++++++--------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/src/ngSanitize/sanitize.js b/src/ngSanitize/sanitize.js index 48ddad82341c..cebf0228fd88 100644 --- a/src/ngSanitize/sanitize.js +++ b/src/ngSanitize/sanitize.js @@ -421,50 +421,28 @@ function $SanitizeProvider() { } /** - * Create an inert document that contains the dirty HTML that needs sanitizing - * Depending upon browser support we use one of three strategies for doing this. - * Support: Safari 10.x -> XHR strategy - * Support: Firefox -> DomParser strategy + * Create an inert document that contains the dirty HTML that needs sanitizing. + * We use the DOMParser API by default and fall back to createHTMLDocument if DOMParser is not + * available. */ var getInertBodyElement /* function(html: string): HTMLBodyElement */ = (function(window, document) { - var inertDocument; - if (document && document.implementation) { - inertDocument = document.implementation.createHTMLDocument('inert'); - } else { - throw $sanitizeMinErr('noinert', 'Can\'t create an inert html document'); + if (isDOMParserAvailable()) { + return getInertBodyElement_DOMParser; } - var inertBodyElement = (inertDocument.documentElement || inertDocument.getDocumentElement()).querySelector('body'); - // Check for the Safari 10.1 bug - which allows JS to run inside the SVG G element - inertBodyElement.innerHTML = ''; - if (!inertBodyElement.querySelector('svg')) { - return getInertBodyElement_XHR; - } else { - // Check for the Firefox bug - which prevents the inner img JS from being sanitized - inertBodyElement.innerHTML = '

'; - if (inertBodyElement.querySelector('svg img')) { - return getInertBodyElement_DOMParser; - } else { - return getInertBodyElement_InertDocument; - } + if (!document || !document.implementation) { + throw $sanitizeMinErr('noinert', 'Can\'t create an inert html document'); } + var inertDocument = document.implementation.createHTMLDocument('inert'); + var inertBodyElement = (inertDocument.documentElement || inertDocument.getDocumentElement()).querySelector('body'); + return getInertBodyElement_InertDocument; - function getInertBodyElement_XHR(html) { - // We add this dummy element to ensure that the rest of the content is parsed as expected - // e.g. leading whitespace is maintained and tags like `` do not get hoisted to the `` tag. - html = '' + html; + function isDOMParserAvailable() { try { - html = encodeURI(html); + return !!getInertBodyElement_DOMParser(''); } catch (e) { - return undefined; + return false; } - var xhr = new window.XMLHttpRequest(); - xhr.responseType = 'document'; - xhr.open('GET', 'data:text/html;charset=utf-8,' + html, false); - xhr.send(null); - var body = xhr.response.body; - body.firstChild.remove(); - return body; } function getInertBodyElement_DOMParser(html) {