|
| 1 | +import * as DOMPurify from 'dompurify'; |
| 2 | +import { JSDOM } from 'jsdom'; |
| 3 | +import { ConfigType, isDOMPurifyConfig, SanitizeRequest, SanitizeResponse } from './types'; |
| 4 | + |
| 5 | +const svgTags = { |
| 6 | + altGlyphDef: /(<\/?)altGlyphDef([> ])/gi, |
| 7 | + altGlyphItem: /(<\/?)altGlyphItem([> ])/gi, |
| 8 | + altGlyph: /(<\/?)altGlyph([> ])/gi, |
| 9 | + animateColor: /(<\/?)animateColor([> ])/gi, |
| 10 | + animateMotion: /(<\/?)animateMotion([> ])/gi, |
| 11 | + animateTransform: /(<\/?)animateTransform([> ])/gi, |
| 12 | + clipPath: /(<\/?)clipPath([> ])/gi, |
| 13 | + feBlend: /(<\/?)feBlend([> ])/gi, |
| 14 | + feColorMatrix: /(<\/?)feColorMatrix([> ])/gi, |
| 15 | + feComponentTransfer: /(<\/?)feComponentTransfer([> ])/gi, |
| 16 | + feComposite: /(<\/?)feComposite([> ])/gi, |
| 17 | + feConvolveMatrix: /(<\/?)feConvolveMatrix([> ])/gi, |
| 18 | + feDiffuseLighting: /(<\/?)feDiffuseLighting([> ])/gi, |
| 19 | + feDisplacementMap: /(<\/?)feDisplacementMap([> ])/gi, |
| 20 | + feDistantLight: /(<\/?)feDistantLight([> ])/gi, |
| 21 | + feDropShadow: /(<\/?)feDropShadow([> ])/gi, |
| 22 | + feFlood: /(<\/?)feFlood([> ])/gi, |
| 23 | + feFuncA: /(<\/?)feFuncA([> ])/gi, |
| 24 | + feFuncB: /(<\/?)feFuncB([> ])/gi, |
| 25 | + feFuncG: /(<\/?)feFuncG([> ])/gi, |
| 26 | + feFuncR: /(<\/?)feFuncR([> ])/gi, |
| 27 | + feGaussianBlur: /(<\/?)feGaussianBlur([> ])/gi, |
| 28 | + feImage: /(<\/?)feImage([> ])/gi, |
| 29 | + feMergeNode: /(<\/?)feMergeNode([> ])/gi, |
| 30 | + feMerge: /(<\/?)feMerge([> ])/gi, |
| 31 | + feMorphology: /(<\/?)feMorphology([> ])/gi, |
| 32 | + feOffset: /(<\/?)feOffset([> ])/gi, |
| 33 | + fePointLight: /(<\/?)fePointLight([> ])/gi, |
| 34 | + feSpecularLighting: /(<\/?)feSpecularLighting([> ])/gi, |
| 35 | + feSpotLight: /(<\/?)feSpotLight([> ])/gi, |
| 36 | + feTile: /(<\/?)feTile([> ])/gi, |
| 37 | + feTurbulence: /(<\/?)feTurbulence([> ])/gi, |
| 38 | + foreignObject: /(<\/?)foreignObject([> ])/gi, |
| 39 | + glyphRef: /(<\/?)glyphRef([> ])/gi, |
| 40 | + linearGradient: /(<\/?)linearGradient([> ])/gi, |
| 41 | + radialGradient: /(<\/?)radialGradient([> ])/gi, |
| 42 | + textPath: /(<\/?)textPath([> ])/gi, |
| 43 | +}; |
| 44 | + |
| 45 | +const svgFilePrefix = '<?xml version="1.0" encoding="utf-8"?>'; |
| 46 | + |
| 47 | +export class Sanitizer { |
| 48 | + constructor(private domPurify: DOMPurify.DOMPurifyI) {} |
| 49 | + |
| 50 | + private sanitizeUseTagHook = (node) => { |
| 51 | + if (node.nodeName === 'use') { |
| 52 | + if ( |
| 53 | + (node.hasAttribute('xlink:href') && !node.getAttribute('xlink:href').match(/^#/)) || |
| 54 | + (node.hasAttribute('href') && !node.getAttribute('href').match(/^#/)) |
| 55 | + ) { |
| 56 | + node.remove(); |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + private sanitizeSvg = (req: SanitizeRequest<ConfigType.DOMPurify>): SanitizeResponse => { |
| 62 | + if (req.config.allowAllLinksInSvgUseTags !== true) { |
| 63 | + this.domPurify.addHook('afterSanitizeAttributes', this.sanitizeUseTagHook); |
| 64 | + } |
| 65 | + |
| 66 | + const dirty = req.content.toString(); |
| 67 | + let sanitized = this.domPurify.sanitize(dirty, req.config.domPurifyConfig ?? {}) as string; |
| 68 | + |
| 69 | + // ensure tags have the correct capitalization, as dompurify converts them to lowercase |
| 70 | + Object.entries(svgTags).forEach(([regex, tag]) => { |
| 71 | + sanitized = sanitized.replace(regex, '$1' + tag + '$2'); |
| 72 | + }); |
| 73 | + |
| 74 | + this.domPurify.removeHook('afterSanitizeAttributes'); |
| 75 | + return { sanitized: Buffer.from([svgFilePrefix, sanitized].join('\n')) }; |
| 76 | + }; |
| 77 | + |
| 78 | + sanitize = (req: SanitizeRequest): SanitizeResponse => { |
| 79 | + const configType = req.configType; |
| 80 | + if (!isDOMPurifyConfig(req)) { |
| 81 | + throw new Error('unsupported config type: ' + configType); |
| 82 | + } |
| 83 | + |
| 84 | + if (req.config.domPurifyConfig?.USE_PROFILES?.['svg']) { |
| 85 | + return this.sanitizeSvg(req); |
| 86 | + } |
| 87 | + |
| 88 | + const dirty = req.content.toString(); |
| 89 | + const sanitized = this.domPurify.sanitize(dirty, req.config.domPurifyConfig ?? {}) as string; |
| 90 | + return { |
| 91 | + sanitized: Buffer.from(sanitized), |
| 92 | + }; |
| 93 | + }; |
| 94 | +} |
| 95 | + |
| 96 | +export const createSanitizer = () => { |
| 97 | + return new Sanitizer(DOMPurify(new JSDOM('').window as any)); |
| 98 | +}; |
0 commit comments