const { expect } = require("chai");
const { marked } = require("marked");
const sanitizeHtml = require("sanitize-html");
const sanitizeOptions = {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([
"img",
"video",
"input",
"details",
"summary",
"del",
"ins",
"sup",
"sub",
]),
allowedAttributes: {
...sanitizeHtml.defaults.allowedAttributes,
img: ["src", "srcset", "alt", "title", "width", "height", "loading"],
video: ["src", "controls", "title"],
input: ["type", "checked", "disabled"],
code: ["class"],
span: ["class"],
div: ["class"],
pre: ["class"],
td: ["align"],
th: ["align"],
},
allowedSchemes: ["http", "https", "mailto"],
};
/**
* Mirrors the server-side rendering pipeline in webview.ts:
* sanitizeHtml(marked(content, opts), sanitizeOptions)
*/
function renderAndSanitize(markdown) {
const raw = marked(markdown, { headerIds: false, mangle: false });
return sanitizeHtml(raw, sanitizeOptions);
}
describe("Markdown sanitization", function () {
// ---------------------------------------------------------------
// Script injection
// ---------------------------------------------------------------
describe("removes script tags", function () {
it("strips inline ');
expect(html).to.not.include("'
);
expect(html).to.not.include("\n\nWorld"
);
expect(html).to.not.include(""
);
expect(html).to.not.include("\n\n**Bold text**'
);
expect(html).to.not.include("