Skip to content

Commit 40c334d

Browse files
trevnorrismscdex
authored andcommitted
buffer: prevent abort on bad proto
If an object's prototype is munged it's possible to bypass the instanceof check and cause the application to abort. Instead now use HasInstance() to verify that the object is a Buffer, and throw if not. This check will not work for JS only methods. So while the application won't abort, it also won't throw. In order to properly throw in all cases with toString() the JS optimization of checking that length is zero has been removed. In its place the native methods will now return early if a zero length string is detected. Ref: nodejs#1486 Ref: nodejs#1922 Fixes: nodejs#1485 PR-URL: nodejs#2012 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 6268f80 commit 40c334d

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

test/parallel/test-buffer-fakes.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const Buffer = require('buffer').Buffer;
6+
const Bp = Buffer.prototype;
7+
8+
function FakeBuffer() { }
9+
FakeBuffer.__proto__ = Buffer;
10+
FakeBuffer.prototype.__proto__ = Buffer.prototype;
11+
12+
const fb = new FakeBuffer();
13+
14+
assert.throws(function() {
15+
new Buffer(fb);
16+
}, TypeError);
17+
18+
assert.throws(function() {
19+
+Buffer.prototype;
20+
}, TypeError);
21+
22+
assert.throws(function() {
23+
Buffer.compare(fb, new Buffer(0));
24+
}, TypeError);
25+
26+
assert.throws(function() {
27+
fb.write('foo');
28+
}, TypeError);
29+
30+
assert.throws(function() {
31+
Buffer.concat([fb, fb]);
32+
}, TypeError);
33+
34+
assert.throws(function() {
35+
fb.toString();
36+
}, TypeError);
37+
38+
assert.throws(function() {
39+
fb.equals(new Buffer(0));
40+
}, TypeError);
41+
42+
assert.throws(function() {
43+
fb.indexOf(5);
44+
}, TypeError);
45+
46+
assert.throws(function() {
47+
fb.readFloatLE(0);
48+
}, TypeError);
49+
50+
assert.throws(function() {
51+
fb.writeFloatLE(0);
52+
}, TypeError);
53+
54+
assert.throws(function() {
55+
fb.fill(0);
56+
}, TypeError);

0 commit comments

Comments
 (0)