You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
681 B

  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. }