Skip to content

Commit aa9f6c0

Browse files
ryuhei shimaryuhei shima
authored andcommitted
http: reject addTrailers after finish
Fixes: #62809
1 parent d7e4108 commit aa9f6c0

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

lib/_http_outgoing.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const {
5454
ERR_HTTP_BODY_NOT_ALLOWED,
5555
ERR_HTTP_CONTENT_LENGTH_MISMATCH,
5656
ERR_HTTP_HEADERS_SENT,
57+
ERR_HTTP_TRAILERS_ALREADY_SENT,
5758
ERR_HTTP_INVALID_HEADER_VALUE,
5859
ERR_HTTP_TRAILER_INVALID,
5960
ERR_INVALID_ARG_TYPE,
@@ -977,6 +978,11 @@ function connectionCorkNT(conn) {
977978
}
978979

979980
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
981+
if (this.finished) {
982+
throw new ERR_HTTP_TRAILERS_ALREADY_SENT();
983+
984+
}
985+
980986
this._trailer = '';
981987
const keys = ObjectKeys(headers);
982988
const isArray = ArrayIsArray(headers);

lib/internal/errors.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const {
6161

6262
const kIsNodeError = Symbol('kIsNodeError');
6363

64+
6465
const isWindows = process.platform === 'win32';
6566

6667
const messages = new SafeMap();
@@ -1343,6 +1344,8 @@ E('ERR_HTTP_CONTENT_LENGTH_MISMATCH',
13431344
'Response body\'s content-length of %s byte(s) does not match the content-length of %s byte(s) set in header', Error);
13441345
E('ERR_HTTP_HEADERS_SENT',
13451346
'Cannot %s headers after they are sent to the client', Error);
1347+
E('ERR_HTTP_TRAILERS_ALREADY_SENT',
1348+
'Trailing headers have already been sent', Error);
13461349
E('ERR_HTTP_INVALID_HEADER_VALUE',
13471350
'Invalid value "%s" for header "%s"', TypeError, HideStackFramesError);
13481351
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);

test/parallel/test-http-outgoing-proto.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ assert.throws(() => {
129129
message: 'Invalid character in trailer content ["404"]'
130130
});
131131

132+
assert.throws(() => {
133+
const outgoingMessage = new OutgoingMessage();
134+
outgoingMessage.finished = true;
135+
outgoingMessage.addTrailers({ 'x-foo': 'bar' });
136+
}, {
137+
code: 'ERR_HTTP_TRAILERS_ALREADY_SENT',
138+
name: 'Error',
139+
message: 'Trailing headers have already been sent'
140+
});
141+
132142
{
133143
const outgoingMessage = new OutgoingMessage();
134144
assert.strictEqual(outgoingMessage.destroyed, false);

0 commit comments

Comments
 (0)