Fix http socket encoding check (#20031)

Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2025-05-29 17:05:51 -07:00
committed by GitHub
parent 9f5adfefe3
commit f5bfda9699
2 changed files with 32 additions and 0 deletions

View File

@@ -1408,6 +1408,12 @@ const NodeHTTPServerSocket = class Socket extends Duplex {
return this;
}
setEncoding(_encoding) {
const err = new Error("Changing the socket encoding is not allowed per RFC7230 Section 3.");
err.code = "ERR_HTTP_SOCKET_ENCODING";
throw err;
}
unref() {
return this;
}

View File

@@ -0,0 +1,26 @@
const common = require('../common');
const assert = require('assert');
const http = require('http');
const server = http.createServer().listen(0, connectToServer);
server.on('connection', common.mustCall((socket) => {
assert.throws(
() => {
socket.setEncoding('');
},
{
code: 'ERR_HTTP_SOCKET_ENCODING',
name: 'Error',
message: 'Changing the socket encoding is not allowed per RFC7230 Section 3.'
}
);
socket.end();
}));
function connectToServer() {
const client = new http.Agent().createConnection(this.address().port, () => {
client.end();
}).on('end', () => server.close());
}