Skip to content

Commit 5e574da

Browse files
committedMay 19, 2020
Merge branch 'peterbe-65-yamlsafeload'
2 parents eaf33a5 + 188b598 commit 5e574da

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed
 

‎.travis.yml

-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ node_js:
55
- 10
66
- 9
77
- 8
8-
- 7
9-
- 6
108
before_install:
119
- npm install -g npm
1210
env:
@@ -20,10 +18,6 @@ env:
2018
script: make travis
2119
matrix:
2220
exclude:
23-
- node_js: 6
24-
env: TEST=browser
25-
- node_js: 7
26-
env: TEST=browser
2721
- node_js: 8
2822
env: TEST=browser
2923
- node_js: 9

‎README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ And end up with an object like this:
7272
var fm = require('front-matter')
7373
```
7474

75-
## fm(string)
75+
## fm(string, { allowUnsafe: false })
7676

7777
Return a `content` object with two properties:
7878

@@ -81,6 +81,9 @@ Return a `content` object with two properties:
8181
* `content.bodyBegin` contains the line number the body contents begins at
8282
* `content.frontmatter` contains the original yaml string contents
8383

84+
**NOTE:** By default `fm()` uses `ys-yaml`'s `safeLoad` unless you set
85+
`allowUnsafe` in the options object to true.
86+
8487
# fm.test(string)
8588

8689
Check if a string contains a front matter header of "---" or "= yaml =". Primarily used internally but is useful outside of the module.

‎examples/unsafe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"toString": !<tag:yaml.org,2002:js/function> "function (){very_evil_thing();}"
3+
---
4+
5+
Hi there!

‎index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ var regex = new RegExp(pattern, 'm')
1616
module.exports = extractor
1717
module.exports.test = test
1818

19-
function extractor (string) {
19+
function extractor (string, options) {
2020
string = string || ''
21-
21+
var defaultOptions = { allowUnsafe: false }
22+
options = options instanceof Object ? {...defaultOptions, ...options} : defaultOptions
23+
options.allowunsafe = Boolean(options.allowUnsafe)
2224
var lines = string.split(/(\r?\n)/)
2325
if (lines[0] && /= yaml =|---/.test(lines[0])) {
24-
return parse(string)
26+
return parse(string, options.allowUnsafe)
2527
} else {
2628
return {
2729
attributes: {},
@@ -47,7 +49,7 @@ function computeLocation (match, body) {
4749
return line
4850
}
4951

50-
function parse (string) {
52+
function parse (string, allowUnsafe) {
5153
var match = regex.exec(string)
5254
if (!match) {
5355
return {
@@ -57,8 +59,9 @@ function parse (string) {
5759
}
5860
}
5961

62+
var loader = allowUnsafe ? parser.load : parser.safeLoad
6063
var yaml = match[match.length - 1].replace(/^\s+|\s+$/g, '')
61-
var attributes = parser.load(yaml) || {}
64+
var attributes = loader(yaml) || {}
6265
var body = string.replace(match[0], '')
6366
var line = computeLocation(match, string)
6467

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"Kai Davenport <kaiyadavenport@gmail.com> (https://github.com/binocarlos)",
4141
"Jean-Philippe Monette <contact@jpmonette.net> (https://github.com/jpmonette)",
4242
"Marc-André Arseneault <marc-andre@arsnl.ca> (https://github.com/arsnl)",
43-
"Bret Comnes <bcomnes@gmail.com> (http://bret.io)"
43+
"Bret Comnes <bcomnes@gmail.com> (http://bret.io)",
44+
"Peter Bengtsson <mail@peterbe.com> (https://github.com/peterbe)"
4445
]
4546
}

‎test/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ test('fm(string) - string missing body', function (t) {
101101
})
102102
})
103103

104+
test('fm(string) - insecure yaml', function (t) {
105+
fs.readFile(
106+
path.resolve(__dirname, '../examples/unsafe.md'),
107+
'utf8',
108+
function (err, data) {
109+
t.error(err, 'read(...) should not error')
110+
t.throws(() => {
111+
fm(data)
112+
}, /YAMLException/)
113+
t.end()
114+
})
115+
})
116+
104117
test('fm(string) - wrapped test in yaml', function (t) {
105118
fs.readFile(
106119
path.resolve(__dirname, '../examples/wrapped-text.md'),
@@ -154,13 +167,13 @@ test('fm(string) - no front matter, markdown with hr', function (t) {
154167
})
155168
})
156169

157-
test('fm(string) - complex yaml', function (t) {
170+
test('fm(string, true) - complex and unsafe yaml', function (t) {
158171
fs.readFile(
159172
path.resolve(__dirname, '../examples/complex-yaml.md'),
160173
'utf8',
161174
function (err, data) {
162175
t.error(err, 'read(...) should not error')
163-
var content = fm(data)
176+
var content = fm(data, {allowUnsafe: true})
164177
t.ok(content.attributes, 'should have `attributes` key')
165178
t.equal(content.attributes.title, 'This is a title!')
166179
t.equal(content.attributes.contact, null)

0 commit comments

Comments
 (0)
Please sign in to comment.