Skip to content

Commit b2248fd

Browse files
authored
http: reject addTrailers after finish
Fixes: #62809 PR-URL: #62832 Reviewed-By: Paolo Insogna <paolo@cowtech.it> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent a6e8368 commit b2248fd

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/_http_outgoing.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,10 @@ function connectionCorkNT(conn) {
982982
}
983983

984984
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
985+
if (this.finished) {
986+
throw new ERR_HTTP_HEADERS_SENT('set trailing');
987+
}
988+
985989
this._trailer = '';
986990
const keys = ObjectKeys(headers);
987991
const isArray = ArrayIsArray(headers);

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_HEADERS_SENT',
138+
name: 'Error',
139+
message: 'Cannot set trailing headers after they are sent to the client'
140+
});
141+
132142
{
133143
const outgoingMessage = new OutgoingMessage();
134144
assert.strictEqual(outgoingMessage.destroyed, false);

0 commit comments

Comments
 (0)