Skip to content

Commit

Permalink
Set additional property descriptor flags when updating request 'heade…
Browse files Browse the repository at this point in the history
…rs' segment

Set the 'configurable', 'enumerable', and 'writable' flags when updating the 'headers' segment with the validated results. An update to header parsing in Node 15 (nodejs/node#35281) resulted in these no longer being set on the IncomingMessage.headers property which led to a 'Cannot assign to read only property' TypeError if the celebrate middleware tries to update the 'headers' segment multiple times per request.
  • Loading branch information
zakbalash committed Oct 4, 2022
1 parent cc7fec4 commit b3309c3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/celebrate.js
Expand Up @@ -25,6 +25,14 @@ const internals = {
},
};

internals.segmentPropertyDescriptors = {
[segments.HEADERS]: {
configurable: true,
enumerable: true,
writable: true,
}
};

internals.validateSegment = (segment) => (spec, joiConfig) => {
const finalValidate = (req) => spec.validateAsync(req[segment], joiConfig);
finalValidate.segment = segment;
Expand Down Expand Up @@ -87,6 +95,7 @@ internals.partialValidate = (steps, req) => steps.reduce((chain, validate) => ch
if (value != null) {
Object.defineProperty(req, validate.segment, {
value,
...internals.segmentPropertyDescriptors[validate.segment],
});
}
return null;
Expand Down Expand Up @@ -119,6 +128,7 @@ internals.fullValidate = (steps, req) => {
requestUpdates.forEach((result) => {
Object.defineProperty(req, result[0], {
value: result[1],
...internals.segmentPropertyDescriptors[result[0]],
});
});

Expand Down

0 comments on commit b3309c3

Please sign in to comment.