Skip to content

Commit b0b1ba5

Browse files
committed
Fix to sanitize by default
The docs have always said `remark-html` is safe by default. It wasn’t and this patches that. If you do want to be unsafe, use `remark-html` with `sanitize: false`: ```diff -.use(remarkHtml) +.use(remarkHtml, {sanitize: false}) ```
1 parent c0b2f69 commit b0b1ba5

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@ var sanitize = require('hast-util-sanitize')
77
module.exports = plugin
88

99
function plugin(options) {
10-
var settings = options || {}
11-
var clean = settings.sanitize
12-
var schema = clean && typeof clean === 'object' ? clean : null
13-
var handlers = settings.handlers || {}
10+
var settings = Object.assign({}, options || {})
11+
let clean
12+
13+
if (typeof settings.sanitize === 'boolean') {
14+
clean = settings.sanitize
15+
settings.sanitize = undefined
16+
}
17+
18+
if (typeof clean !== 'boolean') {
19+
clean = true
20+
}
1421

1522
this.Compiler = compiler
1623

1724
function compiler(node, file) {
1825
var root = node && node.type && node.type === 'root'
19-
var hast = toHast(node, {allowDangerousHtml: !clean, handlers: handlers})
26+
var hast = toHast(node, {
27+
allowDangerousHtml: !clean,
28+
handlers: settings.handlers
29+
})
2030
var result
2131

2232
if (file.extname) {
2333
file.extname = '.html'
2434
}
2535

2636
if (clean) {
27-
hast = sanitize(hast, schema)
37+
hast = sanitize(hast, settings.sanitize)
2838
}
2939

3040
result = toHtml(

test/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test('remark-html()', function (t) {
3737
'should throw when not given a node'
3838
)
3939

40-
processor = remark().use(html)
40+
processor = remark().use(html, {sanitize: false})
4141

4242
t.equal(
4343
processor.stringify({type: 'alpha'}),
@@ -69,6 +69,7 @@ test('remark-html()', function (t) {
6969
)
7070

7171
processor = remark().use(html, {
72+
sanitize: false,
7273
handlers: {
7374
paragraph: function (h, node) {
7475
node.children[0].value = 'changed'
@@ -91,7 +92,7 @@ test('remark-html()', function (t) {
9192
}
9293
}
9394
})
94-
.use(html)
95+
.use(html, {sanitize: false})
9596

9697
t.equal(
9798
processor.processSync('![hello](example.jpg "overwritten")').toString(),
@@ -105,7 +106,7 @@ test('remark-html()', function (t) {
105106
ast.children[0].children[0].data = {hName: 'b'}
106107
}
107108
})
108-
.use(html)
109+
.use(html, {sanitize: false})
109110

110111
t.equal(
111112
processor.processSync('**Bold!**').toString(),
@@ -130,7 +131,7 @@ test('remark-html()', function (t) {
130131
}
131132
}
132133
})
133-
.use(html)
134+
.use(html, {sanitize: false})
134135

135136
t.equal(
136137
processor.processSync('`var`').toString(),
@@ -171,7 +172,7 @@ test('remark-html()', function (t) {
171172
}
172173
}
173174
})
174-
.use(html)
175+
.use(html, {sanitize: false})
175176

176177
t.equal(
177178
processor.processSync('```js\nvar\n```\n').toString(),
@@ -180,7 +181,10 @@ test('remark-html()', function (t) {
180181
)
181182

182183
t.equal(
183-
remark().use(html).processSync('## Hello <span>world</span>').toString(),
184+
remark()
185+
.use(html, {sanitize: false})
186+
.processSync('## Hello <span>world</span>')
187+
.toString(),
184188
'<h2>Hello <span>world</span></h2>\n',
185189
'should be `sanitation: false` by default'
186190
)
@@ -199,7 +203,7 @@ test('remark-html()', function (t) {
199203
.use(html, {sanitize: null})
200204
.processSync('## Hello <span>world</span>')
201205
.toString(),
202-
'<h2>Hello <span>world</span></h2>\n',
206+
'<h2>Hello world</h2>\n',
203207
'should support sanitation: null'
204208
)
205209

@@ -267,7 +271,7 @@ test('CommonMark', function (t) {
267271

268272
var actual = unified()
269273
.use(parse)
270-
.use(html)
274+
.use(html, {sanitize: false})
271275
.processSync(example.markdown)
272276
.toString()
273277

0 commit comments

Comments
 (0)