Skip to content

Commit

Permalink
http: unset F_CHUNKED on new Transfer-Encoding
Browse files Browse the repository at this point in the history
Duplicate `Transfer-Encoding` header should be a treated as a single,
but with original header values concatenated with a comma separator. In
the light of this, even if the past `Transfer-Encoding` ended with
`chunked`, we should be not let the `F_CHUNKED` to leak into the next
header, because mere presence of another header indicates that `chunked`
is not the last transfer-encoding token.

Ref: nodejs-private/llhttp-private#3
See: https://hackerone.com/bugs?report_id=1002188&subject=nodejs

CVE-ID: CVE-2020-8287
PR-URL: nodejs-private/node-private#236
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
  • Loading branch information
mcollina authored and richardlau committed Dec 23, 2020
1 parent 4a30ac8 commit 420244e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
36 changes: 34 additions & 2 deletions deps/llhttp/src/llhttp.c
Expand Up @@ -813,6 +813,14 @@ int llhttp__internal__c_or_flags_16(
return 0;
}

int llhttp__internal__c_and_flags(
llhttp__internal_t* state,
const unsigned char* p,
const unsigned char* endp) {
state->flags &= -9;
return 0;
}

int llhttp__internal__c_update_header_state_7(
llhttp__internal_t* state,
const unsigned char* p,
Expand Down Expand Up @@ -5974,10 +5982,18 @@ static llparse_state_t llhttp__internal__run(
/* UNREACHABLE */;
abort();
}
s_n_llhttp__internal__n_invoke_and_flags: {
switch (llhttp__internal__c_and_flags(state, p, endp)) {
default:
goto s_n_llhttp__internal__n_header_value_te_chunked;
}
/* UNREACHABLE */;
abort();
}
s_n_llhttp__internal__n_invoke_or_flags_16: {
switch (llhttp__internal__c_or_flags_16(state, p, endp)) {
default:
goto s_n_llhttp__internal__n_header_value_te_chunked;
goto s_n_llhttp__internal__n_invoke_and_flags;
}
/* UNREACHABLE */;
abort();
Expand Down Expand Up @@ -7625,6 +7641,14 @@ int llhttp__internal__c_or_flags_16(
return 0;
}

int llhttp__internal__c_and_flags(
llhttp__internal_t* state,
const unsigned char* p,
const unsigned char* endp) {
state->flags &= -9;
return 0;
}

int llhttp__internal__c_update_header_state_7(
llhttp__internal_t* state,
const unsigned char* p,
Expand Down Expand Up @@ -12522,10 +12546,18 @@ static llparse_state_t llhttp__internal__run(
/* UNREACHABLE */;
abort();
}
s_n_llhttp__internal__n_invoke_and_flags: {
switch (llhttp__internal__c_and_flags(state, p, endp)) {
default:
goto s_n_llhttp__internal__n_header_value_te_chunked;
}
/* UNREACHABLE */;
abort();
}
s_n_llhttp__internal__n_invoke_or_flags_16: {
switch (llhttp__internal__c_or_flags_16(state, p, endp)) {
default:
goto s_n_llhttp__internal__n_header_value_te_chunked;
goto s_n_llhttp__internal__n_invoke_and_flags;
}
/* UNREACHABLE */;
abort();
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-http-transfer-encoding-smuggling.js
@@ -0,0 +1,46 @@
'use strict';

const common = require('../common');

const assert = require('assert');
const http = require('http');
const net = require('net');

const msg = [
'POST / HTTP/1.1',
'Host: 127.0.0.1',
'Transfer-Encoding: chunked',
'Transfer-Encoding: chunked-false',
'Connection: upgrade',
'',
'1',
'A',
'0',
'',
'GET /flag HTTP/1.1',
'Host: 127.0.0.1',
'',
'',
].join('\r\n');

// Verify that the server is called only once even with a smuggled request.

const server = http.createServer(common.mustCall((req, res) => {
res.end();
}, 1));

function send(next) {
const client = net.connect(server.address().port, 'localhost');
client.setEncoding('utf8');
client.on('error', common.mustNotCall());
client.on('end', next);
client.write(msg);
client.resume();
}

server.listen(0, common.mustCall((err) => {
assert.ifError(err);
send(common.mustCall(() => {
server.close();
}));
}));

0 comments on commit 420244e

Please sign in to comment.