Skip to content

Instantly share code, notes, and snippets.

@IvanPizhenko
Last active October 23, 2021 00:55
Show Gist options
  • Save IvanPizhenko/100c92f523a994fd425ff8f16420f12d to your computer and use it in GitHub Desktop.
Save IvanPizhenko/100c92f523a994fd425ff8f16420f12d to your computer and use it in GitHub Desktop.
sanitize-html bug
const sanitizeHtml = require('sanitize-html');
const ALLOWED_SCHEMES = ['data', 'http', 'https', 'mailto'];
const text = 'Below\n' +
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAMFJREFUOE+lU9sRg0AIZDNpym9rSAumJm0hNfidsgic5w1wGJ1kZ3zgwvI4AQtIAHrq4zKY5uJ715sGP7C44BdPnZj1gaRVERBPpYJfUSpoGLeyir2Glg64mxMQg9f6xQbU94zrBDBWgVCBBmecbyGWbcrLgpX+OkR+L4ShPw3bdtdCnMmZfSig2a+gtcD1R0LyA1mh6OdmsJNnmW0Sfwp75LYevQ5AsUI3g0aKI+llEe3KQbcx28SsnZi9LNO/6/wBmhVJ7HDmOd4AAAAASUVORK5CYII=" alt="image.png" />\n' +
'Above';
const sanitized = sanitizeHtml(text, {
allowedTags: ['img', 'span'],
allowedAttributes: { img: ['src'] },
allowedSchemes: ALLOWED_SCHEMES,
transformTags: {
'img': (_tagName, attribs) => {
return {
tagName: 'span',
attribs: {},
text: `[image: ${attribs.alt || attribs.title || 'no name'}]`
};
},
}
});
console.log("sanitized:\n" + sanitized);
{
"name": "sanitize-html-test",
"version": "1.0.0",
"description": "Sample project for https://github.com/apostrophecms/sanitize-html/issues/506",
"dependencies": {
"sanitize-html": "2.5.2"
},
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Flowcrypt a.s.",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment