showdownjs / showdown

A bidirectional Markdown to HTML to Markdown converter written in Javascript
http://www.showdownjs.com/
MIT License
14.32k stars 1.57k forks source link

Incomplete string escaping or encoding #1011

Open flyingzebra opened 5 months ago

flyingzebra commented 5 months ago

Following function insufficiently sanitises the input. Directly using the string replace method to perform escaping is notoriously error-prone and therefore hackable.

showdown.helper.unescapeHTMLEntities = function (txt) {
  'use strict';

  return txt
    .replace(/"/g, '"')
    .replace(/&lt;/g, '<')
    .replace(/&gt;/g, '>')
    .replace(/&amp;/g, '&');
};

DOMPurify looks over engineered, but it does the job of sanitising. Example code: var clean = DOMPurify.sanitize(dirty);