stream: disallow writing string chunk with 'buffer' encoding

Signed-off-by: Renegade334 <contact.9a5d6388@renegade334.me.uk>
PR-URL: https://github.com/nodejs/node/pull/63062
Backport-PR-URL: https://github.com/nodejs/node/pull/64675
Refs: https://github.com/nodejs/node/pull/33075
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
René authored and Antoine du Hamel committed 2026-08-06 11:33:01 +02:00
1 parent 5658c631fa
commit fce3df74e9
4 files changed
+19 -30

No files matched your search

+3
View File
@@ -466,6 +466,9 @@ function _write(stream, chunk, encoding, cb) {
}
if (typeof chunk === 'string') {
if (encoding === 'buffer') {
throw new ERR_UNKNOWN_ENCODING(encoding);
}
if ((state[kState] & kDecodeStrings) !== 0) {
chunk = Buffer.from(chunk, encoding);
encoding = 'buffer';
+1 -5
View File
@@ -296,14 +296,10 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
CHECK(args[1]->IsUint8Array());
Environment* env = Environment::GetCurrent(args);
if (!args[1]->IsUint8Array()) {
node::THROW_ERR_INVALID_ARG_TYPE(env, "Second argument must be a buffer");
return 0;
}
Local<Object> req_wrap_obj = args[0].As<Object>();
uv_buf_t buf;
buf.base = Buffer::Data(args[1]);
@@ -1,18 +0,0 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');
const server = net.createServer().listen(0, common.mustCall(() => {
const client = net.connect(server.address().port, common.mustCall(() => {
assert.throws(() => {
client.write('broken', 'buffer');
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'Second argument must be a buffer'
});
client.destroy();
server.close();
}));
}));
@@ -31,7 +31,7 @@ function test(autoDestroy) {
{
const w = new Writable({
autoDestroy,
_write() {}
write() {}
});
w.end();
expectError(w, ['asd'], 'ERR_STREAM_WRITE_AFTER_END');
@@ -40,7 +40,7 @@ function test(autoDestroy) {
{
const w = new Writable({
autoDestroy,
_write() {}
write() {}
});
w.destroy();
}
@@ -48,7 +48,7 @@ function test(autoDestroy) {
{
const w = new Writable({
autoDestroy,
_write() {}
write() {}
});
expectError(w, [null], 'ERR_STREAM_NULL_VALUES', true);
}
@@ -56,18 +56,26 @@ function test(autoDestroy) {
{
const w = new Writable({
autoDestroy,
_write() {}
write() {}
});
expectError(w, [{}], 'ERR_INVALID_ARG_TYPE', true);
}
{
const w = new Writable({
decodeStrings: false,
autoDestroy,
_write() {}
write() {}
});
expectError(w, ['asd', 'noencoding'], 'ERR_UNKNOWN_ENCODING', true);
expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true);
}
{
const w = new Writable({
autoDestroy,
decodeStrings: false,
write() {}
});
expectError(w, ['asd', 'buffer'], 'ERR_UNKNOWN_ENCODING', true);
}
}