msgpack5 3.6.0 → 3.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/index.js +4 -2
- package/lib/decoder.js +12 -1
- package/package.json +1 -1
- package/test/object-prototype-poisoning.js +49 -0
package/README.md
CHANGED
|
@@ -108,6 +108,7 @@ options:
|
|
|
108
108
|
|
|
109
109
|
- `forceFloat64`, a boolean to that forces all floats to be encoded as 64-bits floats. Defaults to false.
|
|
110
110
|
- `compatibilityMode`, a boolean that enables "compatibility mode" which doesn't use str 8 format. Defaults to false.
|
|
111
|
+
- `protoAction`, a string which can be `error|ignore|remove` that determines what happens when decoding a plain object with a `__proto__` property which would cause prototype poisoning. `error` (default) throws an error, `remove` removes the property, `ignore` (not recommended) allows the property, thereby causing prototype poisoning on the decoded object.
|
|
111
112
|
|
|
112
113
|
-------------------------------------------------------
|
|
113
114
|
<a name="encode"></a>
|
package/index.js
CHANGED
|
@@ -13,7 +13,9 @@ function msgpack (options) {
|
|
|
13
13
|
|
|
14
14
|
options = options || {
|
|
15
15
|
forceFloat64: false,
|
|
16
|
-
compatibilityMode: false
|
|
16
|
+
compatibilityMode: false,
|
|
17
|
+
// options.protoAction: 'error' (default) / 'remove' / 'ignore'
|
|
18
|
+
protoAction: 'error'
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
function registerEncoder (check, encode) {
|
|
@@ -68,7 +70,7 @@ function msgpack (options) {
|
|
|
68
70
|
|
|
69
71
|
return {
|
|
70
72
|
encode: buildEncode(encodingTypes, options.forceFloat64, options.compatibilityMode),
|
|
71
|
-
decode: buildDecode(decodingTypes),
|
|
73
|
+
decode: buildDecode(decodingTypes, options),
|
|
72
74
|
register: register,
|
|
73
75
|
registerEncoder: registerEncoder,
|
|
74
76
|
registerDecoder: registerDecoder,
|
package/lib/decoder.js
CHANGED
|
@@ -12,7 +12,7 @@ function IncompleteBufferError (message) {
|
|
|
12
12
|
|
|
13
13
|
util.inherits(IncompleteBufferError, Error)
|
|
14
14
|
|
|
15
|
-
module.exports = function buildDecode (decodingTypes) {
|
|
15
|
+
module.exports = function buildDecode (decodingTypes, options) {
|
|
16
16
|
return decode
|
|
17
17
|
|
|
18
18
|
function getSize (first) {
|
|
@@ -358,6 +358,17 @@ module.exports = function buildDecode (decodingTypes) {
|
|
|
358
358
|
var valueResult = tryDecode(buf, offset)
|
|
359
359
|
if (valueResult) {
|
|
360
360
|
key = keyResult.value
|
|
361
|
+
|
|
362
|
+
if (key === '__proto__') {
|
|
363
|
+
if (options.protoAction === 'error') {
|
|
364
|
+
throw new SyntaxError('Object contains forbidden prototype property')
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (options.protoAction === 'remove') {
|
|
368
|
+
continue
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
361
372
|
result[key] = valueResult.value
|
|
362
373
|
offset += valueResult.bytesConsumed
|
|
363
374
|
totalBytesConsumed += (keyResult.bytesConsumed + valueResult.bytesConsumed)
|
package/package.json
CHANGED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
var test = require('tape').test
|
|
4
|
+
var msgpack = require('../')
|
|
5
|
+
|
|
6
|
+
test('decode throws when object has forbidden __proto__ property', function (t) {
|
|
7
|
+
const encoder = msgpack()
|
|
8
|
+
|
|
9
|
+
const payload = { hello: 'world' }
|
|
10
|
+
Object.defineProperty(payload, '__proto__', {
|
|
11
|
+
value: { polluted: true },
|
|
12
|
+
enumerable: true
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const encoded = encoder.encode(payload)
|
|
16
|
+
|
|
17
|
+
t.throws(() => encoder.decode(encoded), /Object contains forbidden prototype property/)
|
|
18
|
+
t.end()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('decode ignores forbidden __proto__ property if protoAction is "ignore"', function (t) {
|
|
22
|
+
const encoder = msgpack({ protoAction: 'ignore' })
|
|
23
|
+
|
|
24
|
+
const payload = { hello: 'world' }
|
|
25
|
+
Object.defineProperty(payload, '__proto__', {
|
|
26
|
+
value: { polluted: true },
|
|
27
|
+
enumerable: true
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
const decoded = encoder.decode(encoder.encode(payload))
|
|
31
|
+
|
|
32
|
+
t.equal(decoded.polluted, true)
|
|
33
|
+
t.end()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('decode removes forbidden __proto__ property if protoAction is "remove"', function (t) {
|
|
37
|
+
const encoder = msgpack({ protoAction: 'remove' })
|
|
38
|
+
|
|
39
|
+
const payload = { hello: 'world' }
|
|
40
|
+
Object.defineProperty(payload, '__proto__', {
|
|
41
|
+
value: { polluted: true },
|
|
42
|
+
enumerable: true
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
const decoded = encoder.decode(encoder.encode(payload))
|
|
46
|
+
|
|
47
|
+
t.equal(decoded.polluted, undefined)
|
|
48
|
+
t.end()
|
|
49
|
+
})
|