Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: throws when invalid hex string in http request #45173

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}));