Skip to content

Commit

Permalink
lib: throws when invalid hex string in http request
Browse files Browse the repository at this point in the history
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
juanarbol committed Oct 25, 2022
1 parent 0f3e531 commit 0161031
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,12 @@ OutgoingMessage.prototype.destroy = function destroy(error) {

// This abstract either writing directly to the socket or buffering it.
OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
if (typeof data === 'string' && encoding === 'hex') {
if (data.length % 2 !== 0) {
throw new ERR_INVALID_ARG_VALUE('data', data, 'is invalid hex string');
}
}

// This is a shameful hack to get the headers and first body chunk onto
// the same packet. Future versions of Node are going to take care of
// this at a lower level and in a more general way.
Expand Down
14 changes: 12 additions & 2 deletions test/parallel/test-http-hex-write.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ const http = require('http');

const expect = 'hex\nutf8\n';

http.createServer(function(q, s) {
const server = http.createServer(function(q, s) {
s.setHeader('content-length', expect.length);
s.write('6865780a', 'hex');
s.write('utf8\n');
s.end();
this.close();
}).listen(0, common.mustCall(function() {
http.request({ port: this.address().port })
.on('response', common.mustCall(function(res) {
Expand All @@ -46,4 +45,15 @@ http.createServer(function(q, s) {
assert.strictEqual(data, expect);
}));
})).end();

// Refs: https://github.com/nodejs/node/issues/45150
const invalidReq = http.request({ port: this.address().port });
assert.throws(
() => { invalidReq.write('boom!', 'hex'); },
{
code: 'ERR_INVALID_ARG_VALUE',
message: 'The argument \'data\' is invalid hex string. Received \'boom!\''
}
);
invalidReq.end(() => server.close());
}));

0 comments on commit 0161031

Please sign in to comment.