Skip to content

Commit b054c33

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

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

lib/_http_outgoing.js

Lines changed: 5 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,10 @@ 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+
980985
this._trailer = '';
981986
const keys = ObjectKeys(headers);
982987
const isArray = ArrayIsArray(headers);

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,8 @@ E('ERR_HTTP_CONTENT_LENGTH_MISMATCH',
13431343
'Response body\'s content-length of %s byte(s) does not match the content-length of %s byte(s) set in header', Error);
13441344
E('ERR_HTTP_HEADERS_SENT',
13451345
'Cannot %s headers after they are sent to the client', Error);
1346+
E('ERR_HTTP_TRAILERS_ALREADY_SENT',
1347+
'Trailing headers have already been sent', Error);
13461348
E('ERR_HTTP_INVALID_HEADER_VALUE',
13471349
'Invalid value "%s" for header "%s"', TypeError, HideStackFramesError);
13481350
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)