Skip to content

Commit 9fb0e54

Browse files
authored
Merge pull request #251 from KManolov3/feat/uepr-238-sanitize-svgs-on-open
Feat/uepr 238 sanitize svgs on open
2 parents 4e5b7fa + b720b45 commit 9fb0e54

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

packages/scratch-svg-renderer/src/load-svg-string.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const DOMPurify = require('isomorphic-dompurify');
21
const SvgElement = require('./svg-element');
32
const convertFonts = require('./font-converter');
4-
const fixupSvgString = require('./fixup-svg-string');
53
const transformStrokeWidths = require('./transform-applier');
4+
const {sanitizeSvgText} = require('./sanitize-svg');
65

76
/**
87
* @param {SVGElement} svgTag the tag to search within
@@ -208,24 +207,10 @@ const transformMeasurements = svgTag => {
208207
// which returns the full bounding-box of all drawn SVG
209208
// elements, similar to how Scratch 2.0 did measurement.
210209
const svgSpot = document.createElement('span');
211-
// Since we're adding user-provided SVG to document.body,
212-
// sanitizing is required. This should not affect bounding box calculation.
213-
// outerHTML is attribute of Element (and not HTMLElement), so use it instead of
214-
// calling serializer or toString()
215-
// NOTE: svgTag remains untouched!
216-
const rawValue = svgTag.outerHTML;
217-
const sanitizedValue = DOMPurify.sanitize(rawValue, {
218-
// Use SVG profile (no HTML elements)
219-
USE_PROFILES: {svg: true},
220-
// Remove some tags that Scratch does not use.
221-
FORBID_TAGS: ['a', 'audio', 'canvas', 'video'],
222-
// Allow data URI in image tags (e.g. SVGs converted from bitmap)
223-
ADD_DATA_URI_TAGS: ['image']
224-
});
225210
let bbox;
226211
try {
227212
// Insert sanitized value.
228-
svgSpot.innerHTML = sanitizedValue;
213+
svgSpot.innerHTML = svgTag.outerHTML;
229214
document.body.appendChild(svgSpot);
230215
// Take the bounding box. We have to get elements via svgSpot
231216
// because we added it via innerHTML.
@@ -320,8 +305,11 @@ const normalizeSvg = (svgTag, fromVersion2) => {
320305
const loadSvgString = (svgString, fromVersion2) => {
321306
// Parse string into SVG XML.
322307
const parser = new DOMParser();
323-
svgString = fixupSvgString(svgString);
324-
const svgDom = parser.parseFromString(svgString, 'text/xml');
308+
309+
// Since we're adding user-provided SVG to document.body as part of normalization,
310+
// sanitization is required. This should not affect bounding box calculation.
311+
const sanitizedSvgString = sanitizeSvgText(svgString);
312+
const svgDom = parser.parseFromString(sanitizedSvgString, 'text/xml');
325313
if (svgDom.childNodes.length < 1 ||
326314
svgDom.documentElement.localName !== 'svg') {
327315
throw new Error('Document does not appear to be SVG.');

packages/scratch-svg-renderer/src/sanitize-svg.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ sanitizeSvg.sanitizeByteStream = function (rawData) {
128128
*/
129129
sanitizeSvg.sanitizeSvgText = function (rawSvgText) {
130130
let sanitizedText = DOMPurify.sanitize(rawSvgText, {
131-
USE_PROFILES: {svg: true}
131+
USE_PROFILES: {svg: true},
132+
FORBID_TAGS: ['a', 'audio', 'canvas', 'video'],
133+
// Allow data URI in image tags (e.g. SVGs converted from bitmap)
134+
ADD_DATA_URI_TAGS: ['image']
132135
});
133136

134137
// Remove partial XML comment that is sometimes left in the HTML

0 commit comments

Comments
 (0)