Notes/src/services/html_sanitizer.js

49 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-06-30 23:37:06 +02:00
const sanitizeHtml = require('sanitize-html');
// intended mainly as protection against XSS via import
// secondarily it (partly) protects against "CSS takeover"
function sanitize(dirtyHtml) {
// avoid H1 per https://github.com/zadam/trilium/issues/1552
// demote H1, and if that conflicts with existing H2, demote that, etc
let transformTags = {};
const loweraseHtml = dirtyHtml.toLowerCase();
for (let i = 1; i < 6; ++i)
{
if (loweraseHtml.includes(`<h${i}`))
{
transformTags[`h${i}`] = `h${i+1}`;
}
else
{
break;
}
}
// to minimize document changes, compress H
2020-06-30 23:37:06 +02:00
return sanitizeHtml(dirtyHtml, {
allowedTags: [
2020-11-01 20:38:39 +01:00
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'li', 'b', 'i', 'strong', 'em', 'strike', 's', 'del', 'abbr', 'code', 'hr', 'br', 'div',
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'section', 'img',
'figure', 'figcaption', 'span', 'label', 'input'
2020-06-30 23:37:06 +02:00
],
allowedAttributes: {
'a': [ 'href', 'class', 'data-note-path' ],
2020-06-30 23:37:06 +02:00
'img': [ 'src' ],
'section': [ 'class', 'data-note-id' ],
'figure': [ 'class' ],
'span': [ 'class', 'style' ],
'label': [ 'class' ],
'input': [ 'class', 'type', 'disabled' ],
'code': [ 'class' ]
2020-07-27 23:40:14 +02:00
},
allowedSchemes: ['http', 'https', 'ftp', 'mailto', 'data', 'evernote'],
transformTags,
2020-06-30 23:37:06 +02:00
});
}
module.exports = {
sanitize
};