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

Add DOMPurify to sanitize HTML #196

Merged
merged 6 commits into from
Aug 12, 2021
Merged
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ marked:
autolink: true
mangle: true
sanitizeUrl: false
dompurify: false,
headerIds: true
lazyload: false
prependRoot: false
Expand Down Expand Up @@ -61,6 +62,7 @@ marked:
- **mangle** - Escape autolinked email address with HTML character references.
* This is to obscure email address from _basic_ crawler used by spam bot, while still readable to web browsers.
- **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`.
- **dompurify** - Enable [DOMPurify](https://github.com/cure53/DOMPurify) to be run on the rendered Markdown. See below for configuration
- **headerIds** - Insert header id, e.g. `<h1 id="value">text</h1>`. Useful for inserting anchor link to each paragraph with a heading.
- **anchorAlias** - Enables custom header id
* Example: `## [foo](#bar)`, id will be set as "bar".
Expand Down Expand Up @@ -91,6 +93,26 @@ For more options, see [Marked](https://marked.js.org/using_advanced#options). Du

## Extras

### Sanitize HTML with DOMPurify

[DOMPurify](https://github.com/cure53/DOMPurify) can be enabled to sanitize the rendered HTML.

To enable it, pass an object containing the DOMPurify options:

```json
dompurify: true
```

Or you can enable specific DOMPurify options (but according to DOMPurify authors, the default options are safe):

```yml
dompurify:
FORBID_TAGS:
- "style"
```

See https://github.com/cure53/DOMPurify#can-i-configure-dompurify for a full reference of available options

### Definition/Description Lists

`hexo-renderer-marked` also implements description/definition lists using the same syntax as [PHP Markdown Extra][PHP Markdown Extra].
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ hexo.config.marked = Object.assign({
autolink: true,
mangle: true,
sanitizeUrl: false,
dompurify: false,
headerIds: true,
anchorAlias: false,
lazyload: false,
Expand Down
25 changes: 22 additions & 3 deletions lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict';

const marked = require('marked');

let JSDOM,
createDOMPurify;

const { escape } = require('marked/src/helpers');
const { encodeURL, slugize, stripHTML, url_for, isExternalLink } = require('hexo-util');
const MarkedRenderer = marked.Renderer;
Expand Down Expand Up @@ -236,7 +240,7 @@ class Tokenizer extends MarkedTokenizer {

module.exports = function(data, options) {
const { post_asset_folder, marked: markedCfg, source_dir } = this.config;
const { prependRoot, postAsset } = markedCfg;
const { prependRoot, postAsset, dompurify } = markedCfg;
const { path, text } = data;

// exec filter to extend renderer.
Expand All @@ -258,8 +262,23 @@ module.exports = function(data, options) {
}
}

return marked(text, Object.assign({
let sanitizer = function(html) { return html; };

if (dompurify) {
if (createDOMPurify === undefined && JSDOM === undefined) {
createDOMPurify = require('dompurify');
JSDOM = require('jsdom').JSDOM;
}
const window = new JSDOM('').window;
const DOMPurify = createDOMPurify(window);
let param = {};
if (dompurify !== true) {
param = dompurify;
}
sanitizer = function(html) { return DOMPurify.sanitize(html, param); };
}
return sanitizer(marked(text, Object.assign({
renderer,
tokenizer
}, markedCfg, options, { postPath }));
}, markedCfg, options, { postPath })));
};
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
],
"license": "MIT",
"dependencies": {
"hexo-util": "^2.1.0",
"marked": "^2.0.0"
"hexo-util": "^2.5.0",
"marked": "^2.1.3",
"dompurify": "^2.3.0",
"jsdom": "^16.7.0"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,4 +949,37 @@ describe('Marked renderer', () => {
result.content.should.eql('<p><strong>foo</strong> {% lorem %}</p>\n');
});
});

describe('sanitize HTML with DOMPurify', () => {
const body = [
'**safe markdown**',
'',
'<a onclick="alert(1)">unsafe link</a>',
'',
'[Hexo](http://hexo.io)'
].join('\n');

it('sanitize enabled, default options', () => {
hexo.config.marked.dompurify = true;
const result = r({text: body});

result.should.eql([
'<p><strong>safe markdown</strong></p>\n',
'<p><a>unsafe link</a></p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
});

it('sanitize enabled, with options', () => {
hexo.config.marked.dompurify = { FORBID_TAGS: ['strong'] };
const result = r({text: body});

result.should.eql([
'<p>safe markdown</p>\n',
'<p><a>unsafe link</a></p>\n',
'<p><a href="http://hexo.io/">Hexo</a></p>\n'
].join(''));
});

});
});