Skip to content

Commit f6f61c3

Browse files
committed
buffer: prevent string write offset overflow
Reject offsets outside the destination buffer before subtracting from its length in SlowWriteString. Normalize wrapper arguments once so validated values reach the native binding. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent f43086d commit f6f61c3

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

lib/internal/buffer.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,30 +959,39 @@ function writeFloatBackwards(val, offset = 0) {
959959

960960
class FastBuffer extends Uint8Array {}
961961

962-
function asciiWrite(buf, string, offset = 0, length = buf.byteLength - offset) {
962+
function asciiWrite(buf, string, offset = 0, length) {
963+
offset = Number(offset);
963964
if (offset < 0 || offset > buf.byteLength) {
964965
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
965966
}
967+
if (length === undefined) length = buf.byteLength - offset;
968+
else length = Number(length);
966969
if (length < 0 || length > buf.byteLength - offset) {
967970
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
968971
}
969972
return asciiWriteStatic(buf, string, offset, length);
970973
}
971974

972-
function latin1Write(buf, string, offset = 0, length = buf.byteLength - offset) {
975+
function latin1Write(buf, string, offset = 0, length) {
976+
offset = Number(offset);
973977
if (offset < 0 || offset > buf.byteLength) {
974978
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
975979
}
980+
if (length === undefined) length = buf.byteLength - offset;
981+
else length = Number(length);
976982
if (length < 0 || length > buf.byteLength - offset) {
977983
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
978984
}
979985
return latin1WriteStatic(buf, string, offset, length);
980986
}
981987

982-
function utf8Write(buf, string, offset = 0, length = buf.byteLength - offset) {
988+
function utf8Write(buf, string, offset = 0, length) {
989+
offset = Number(offset);
983990
if (offset < 0 || offset > buf.byteLength) {
984991
throw new ERR_BUFFER_OUT_OF_BOUNDS('offset');
985992
}
993+
if (length === undefined) length = buf.byteLength - offset;
994+
else length = Number(length);
986995
if (length < 0 || length > buf.byteLength - offset) {
987996
throw new ERR_BUFFER_OUT_OF_BOUNDS('length');
988997
}

src/node_buffer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,6 +1727,11 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
17271727
size_t max_length = 0;
17281728

17291729
THROW_AND_RETURN_IF_OOB(ParseArrayIndex(env, args[2], 0, &offset));
1730+
if (offset > ts_obj_length) {
1731+
return node::THROW_ERR_BUFFER_OUT_OF_BOUNDS(
1732+
env, "\"offset\" is outside of buffer bounds");
1733+
}
1734+
17301735
THROW_AND_RETURN_IF_OOB(
17311736
ParseArrayIndex(env, args[3], ts_obj_length - offset, &max_length));
17321737

test/parallel/test-buffer-write.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,17 @@ assert.throws(() => {
127127
}, common.expectsError({
128128
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
129129
}));
130+
131+
for (const method of ['asciiWrite', 'latin1Write', 'utf8Write']) {
132+
let calls = 0;
133+
const offset = {
134+
valueOf() {
135+
calls++;
136+
return 2;
137+
},
138+
};
139+
assert.throws(() => Buffer.alloc(1)[method]('ww', offset, 1), common.expectsError({
140+
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
141+
}));
142+
assert.strictEqual(calls, 1);
143+
}

0 commit comments

Comments
 (0)