Skip to content

Commit 020da46

Browse files
committed
http2: do not end compat requests reset before END_STREAM
When the peer reset a stream before sending END_STREAM, the compat `Http2ServerRequest` still pushed EOF from its 'close' handler, so it emitted 'end' for a truncated body (on main even after 'error' and 'close'). Unlike http1, where an aborted `IncomingMessage` is destroyed and never emits 'end', applications listening for 'end' then processed the partial body as if it were complete. Record in `onStreamClose()` that the peer closed the stream before END_STREAM, since `destroy()` ends the readable side afterwards, and destroy the request instead of ending it in that case. Requests whose body was complete before the reset still end normally. Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 98dbf60 commit 020da46

4 files changed

Lines changed: 186 additions & 5 deletions

File tree

‎lib/internal/http2/compat.js‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const {
5757
kSocket,
5858
kRequest,
5959
kProxySocket,
60+
kIncomplete,
6061
assertValidPseudoHeader,
6162
getAuthority,
6263
} = require('internal/http2/util');
@@ -154,9 +155,12 @@ function onStreamTrailers(trailers, flags, rawTrailers) {
154155
}
155156

156157
function onStreamEnd() {
157-
// Cause the request stream to end as well.
158+
// Cause the request stream to end as well. Skip when the body is
159+
// incomplete (the peer never sent END_STREAM and the stream was torn down,
160+
// e.g. reset, local close, or session teardown); destroying the readable on
161+
// close emits 'end' spuriously, so it must not reach the compat request.
158162
const request = this[kRequest];
159-
if (request !== undefined)
163+
if (request !== undefined && !this[kIncomplete])
160164
this[kRequest].push(null);
161165
}
162166

@@ -291,15 +295,24 @@ function onStreamCloseRequest() {
291295
const state = req[kState];
292296
state.closed = true;
293297

298+
this[kProxySocket] = null;
299+
this[kRequest] = undefined;
300+
301+
// The peer closed the stream before END_STREAM, so the request body is
302+
// incomplete. Like http1's IncomingMessage on abort, destroy the request
303+
// instead of ending it, so 'end' is not emitted for a truncated body.
304+
// destroy() emits 'close'.
305+
if (this[kIncomplete]) {
306+
req.destroy();
307+
return;
308+
}
309+
294310
req.push(null);
295311
// If the user didn't interact with incoming data and didn't pipe it,
296312
// dump it for compatibility with http1
297313
if (!state.didRead && !req._readableState.resumeScheduled)
298314
req.resume();
299315

300-
this[kProxySocket] = null;
301-
this[kRequest] = undefined;
302-
303316
req.emit('close');
304317
}
305318

‎lib/internal/http2/core.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const {
156156
kStrictSingleValueFields,
157157
kSocket,
158158
kRequest,
159+
kIncomplete,
159160
kProtocol,
160161
kProxySocket,
161162
MAX_ADDITIONAL_SETTINGS,
@@ -652,6 +653,10 @@ function onStreamClose(code, peerReset) {
652653
const cleanCode = code === NGHTTP2_NO_ERROR || code === NGHTTP2_CANCEL;
653654

654655
if (!peerReadableEnded) {
656+
// The peer closed the stream before END_STREAM, so the incoming message
657+
// is incomplete. Bear in mind we must not 'end' the compat request. The
658+
// decision is recorded in _destroy() via this._readableState.ended, which
659+
// is the same signal as peerReadableEnded here.
655660
stream.destroy(cleanCode ?
656661
new ERR_HTTP2_STREAM_ABORTED() :
657662
new ERR_HTTP2_STREAM_ERROR(nameForErrorCode[code] || code));
@@ -2680,6 +2685,15 @@ class Http2Stream extends Duplex {
26802685

26812686
if (!this.closed)
26822687
closeStream(this, code, hasHandle ? kForceRstStream : kNoRstStream);
2688+
2689+
// If the peer never sent END_STREAM, the readable side was torn down
2690+
// before reaching EOF, so the incoming message is incomplete. _destroy()
2691+
// always ends the readable below, so record it here for the compat layer
2692+
// (onStreamCloseRequest). Doing it in one place covers remote reset,
2693+
// local close, and unexpected session teardown alike.
2694+
if (!this._readableState.ended)
2695+
this[kIncomplete] = true;
2696+
26832697
this.push(null);
26842698

26852699

‎lib/internal/http2/util.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const kSocket = Symbol('socket');
4646
const kProtocol = Symbol('protocol');
4747
const kProxySocket = Symbol('proxySocket');
4848
const kRequest = Symbol('request');
49+
const kIncomplete = Symbol('incomplete');
4950

5051
const {
5152
NGHTTP2_NV_FLAG_NONE,
@@ -1005,6 +1006,7 @@ module.exports = {
10051006
kStrictSingleValueFields,
10061007
kSocket,
10071008
kProtocol,
1009+
kIncomplete,
10081010
kProxySocket,
10091011
kRequest,
10101012
MAX_ADDITIONAL_SETTINGS,
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
// A request whose stream is reset by the peer before END_STREAM has an
8+
// incomplete body. Like http1, the compat request must not emit 'end' for it.
9+
10+
const assert = require('assert');
11+
const http2 = require('http2');
12+
const { finished } = require('stream');
13+
14+
const codes = [
15+
http2.constants.NGHTTP2_NO_ERROR,
16+
http2.constants.NGHTTP2_CANCEL,
17+
];
18+
19+
const server = http2.createServer(common.mustCall((req, res) => {
20+
// Tell the client the partial body arrived, so it can reset the stream.
21+
req.once('data', common.mustCall(() => res.writeHead(200).flushHeaders()));
22+
req.on('end', common.mustNotCall());
23+
req.on('aborted', common.mustCall());
24+
req.on('error', common.expectsError({ code: 'ERR_HTTP2_STREAM_ABORTED' }));
25+
req.on('close', common.mustCall(() => {
26+
assert.strictEqual(req.aborted, true);
27+
assert.strictEqual(req.readableEnded, false);
28+
}));
29+
finished(req, common.mustCall((err) => {
30+
assert.ok(err);
31+
}));
32+
}, codes.length));
33+
34+
server.listen(0, common.mustCall(() => {
35+
const client = http2.connect(`http://localhost:${server.address().port}`);
36+
let pending = codes.length;
37+
38+
for (const code of codes) {
39+
const req = client.request({
40+
':method': 'POST',
41+
'content-length': '1000',
42+
});
43+
req.on('error', () => {});
44+
req.on('close', common.mustCall(() => {
45+
if (--pending === 0) {
46+
client.close();
47+
server.close();
48+
}
49+
}));
50+
// Send part of the body, then reset the stream without END_STREAM once
51+
// the server has seen it.
52+
req.write('{"action":"partial"}');
53+
req.on('response', common.mustCall(() => req.close(code)));
54+
}
55+
}));
56+
57+
// A complete body followed by a reset still ends the request normally.
58+
{
59+
const server = http2.createServer(common.mustCall((req, res) => {
60+
let body = '';
61+
req.setEncoding('utf8');
62+
req.on('data', (chunk) => body += chunk);
63+
req.on('end', common.mustCall(() => {
64+
assert.strictEqual(body, 'complete');
65+
res.writeHead(200).flushHeaders();
66+
}));
67+
req.on('error', common.mustNotCall());
68+
}));
69+
70+
server.listen(0, common.mustCall(() => {
71+
const client = http2.connect(`http://localhost:${server.address().port}`);
72+
const req = client.request({ ':method': 'POST' });
73+
req.on('error', () => {});
74+
req.on('response', common.mustCall(() => {
75+
req.close(http2.constants.NGHTTP2_CANCEL);
76+
}));
77+
req.on('close', common.mustCall(() => {
78+
client.close();
79+
server.close();
80+
}));
81+
req.end('complete');
82+
}));
83+
}
84+
85+
// A stream closed locally by the server before the peer sends END_STREAM also
86+
// has an incomplete body and must not emit 'end'.
87+
{
88+
const server = http2.createServer(common.mustCall((req, res) => {
89+
req.once('data', common.mustCall(() => {
90+
// Tear the stream down locally while the body is still in flight.
91+
req.stream.destroy();
92+
}));
93+
req.on('end', common.mustNotCall());
94+
req.on('aborted', common.mustCall());
95+
req.on('error', common.mustNotCall());
96+
req.on('close', common.mustCall(() => {
97+
assert.strictEqual(req.aborted, true);
98+
assert.strictEqual(req.readableEnded, false);
99+
}));
100+
finished(req, common.mustCall((err) => {
101+
assert.ok(err);
102+
}));
103+
}));
104+
105+
server.listen(0, common.mustCall(() => {
106+
const client = http2.connect(`http://localhost:${server.address().port}`);
107+
const req = client.request({
108+
':method': 'POST',
109+
'content-length': '1000',
110+
});
111+
req.on('error', () => {});
112+
req.on('close', common.mustCall(() => {
113+
client.close();
114+
server.close();
115+
}));
116+
req.write('{"action":"partial"}');
117+
}));
118+
}
119+
120+
// An unexpected session teardown mid-body also produces an incomplete request
121+
// that must not emit 'end'.
122+
{
123+
const server = http2.createServer(common.mustCall((req, res) => {
124+
req.once('data', common.mustCall(() => {
125+
req.stream.session.destroy();
126+
}));
127+
req.on('end', common.mustNotCall());
128+
req.on('aborted', common.mustCall());
129+
req.on('error', common.mustNotCall());
130+
req.on('close', common.mustCall(() => {
131+
assert.strictEqual(req.aborted, true);
132+
assert.strictEqual(req.readableEnded, false);
133+
}));
134+
finished(req, common.mustCall((err) => {
135+
assert.ok(err);
136+
}));
137+
}));
138+
139+
server.listen(0, common.mustCall(() => {
140+
const client = http2.connect(`http://localhost:${server.address().port}`);
141+
const req = client.request({
142+
':method': 'POST',
143+
'content-length': '1000',
144+
});
145+
req.on('error', () => {});
146+
req.on('close', common.mustCall(() => {
147+
client.close();
148+
server.close();
149+
}));
150+
req.write('{"action":"partial"}');
151+
}));
152+
}

0 commit comments

Comments
 (0)