sanitize-svg.js 681 B

12345678910111213141516171819202122232425
  1. const fs = require('fs-extra')
  2. const { JSDOM } = require('jsdom')
  3. const createDOMPurify = require('dompurify')
  4. /* global WIKI */
  5. module.exports = async (svgPath) => {
  6. WIKI.logger.info(`Sanitizing SVG file upload...`)
  7. try {
  8. let svgContents = await fs.readFile(svgPath, 'utf8')
  9. const window = new JSDOM('').window
  10. const DOMPurify = createDOMPurify(window)
  11. svgContents = DOMPurify.sanitize(svgContents)
  12. await fs.writeFile(svgPath, svgContents)
  13. WIKI.logger.info(`Sanitized SVG file upload: [ COMPLETED ]`)
  14. } catch (err) {
  15. WIKI.logger.error(`Failed to sanitize SVG file upload: [ FAILED ]`)
  16. WIKI.logger.error(err.message)
  17. throw err
  18. }
  19. }