Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: transform should sanitize html by default #266

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/transform/md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function initParser(md: MarkdownIt, options: OptionsType, env: EnvType) {
}

function initCompiler(md: MarkdownIt, options: OptionsType, env: EnvType) {
const {needToSanitizeHtml = false, sanitizeOptions} = options;
const {needToSanitizeHtml = true, sanitizeOptions} = options;

return (tokens: Token[]) => {
const html = md.renderer.render(tokens, md.options, env);
Expand Down
1 change: 1 addition & 0 deletions src/transform/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const htmlTags = [
'video',
'wbr',
'iframe',
'style',
];

const svgTags = [
Expand Down
46 changes: 40 additions & 6 deletions test/sanitize-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,47 @@ describe('Sanitize HTML utility', () => {
expect(sanitizeHtml('<img src=a onerror=alert(1)>')).toBe('<img src="a" />');
});

it('transform should sanitize html', () => {
expect(transformYfm('<img src=a onerror=alert(1)>', {needToSanitizeHtml: true})).toBe(
'<img src="a" />',
);
describe('by default transform should sanitize html', () => {
describe('html in markdown', () => {
it('should sanitize danger attributes', () => {
expect(transformYfm('<img src="a" onerror=alert(1)>')).toBe('<img src="a" />');
});

it('should not sanitize style tag', () => {
expect(transformYfm('<style>h2 {color: red;}</style>')).toBe(
'<style>h2 {color: red;}</style>',
);
});
});

describe('plugin markdown-it-attrs', () => {
it('should sanitize danger attributes', () => {
expect(transformYfm('Click {onfocus="alert(1)" onclick="alert(1)"}')).toBe(
'<p>Click</p>\n',
);
});

it('should not sanitize safe attributes', () => {
expect(transformYfm('Click {.style-me data-toggle=modal}')).toBe(
'<p class="style-me" data-toggle="modal">Click</p>\n',
);
});

it('should not sanitize style attribute', () => {
expect(
transformYfm(
'[example.com](https://example.com){style="position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: red; opacity: 0.5"}',
),
).toBe(
'<p><a href="https://example.com" style="position:fixed;top:0;left:0;width:100%;height:100%;background-color:red;opacity:0.5">example.com</a></p>\n',
);
});
});
});

it('by default transform should not sanitize html', () => {
expect(transformYfm('<img src=a onerror=alert(1)>')).toBe('<img src=a onerror=alert(1)>');
it('transform should not sanitize html', () => {
expect(transformYfm('<img src=a onerror=alert(1)>', {needToSanitizeHtml: false})).toBe(
'<img src=a onerror=alert(1)>',
);
});
});