fix: restore req/res prototypes when a mounted app hands control back - #7434
Closed
santusht06 wants to merge 1 commit into
Closed
fix: restore req/res prototypes when a mounted app hands control back#7434santusht06 wants to merge 1 commit into
santusht06 wants to merge 1 commit into
Conversation
When an express() sub-app is mounted on a router via router.use(), the request and response prototypes are altered in app.handle but never restored upon delegating back to the caller callback. This commit captures the incoming prototypes when a callback is provided in app.handle and wraps the done callback to restore the original request and response prototypes on both success and error exit paths. Fixes expressjs#7427
Contributor
|
Duplicate of #7428 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an
express()sub-app is mounted viarouter.use()instead ofapp.use(),app.handlealters the prototypes ofreqandresto point tosubApp.requestandsubApp.response, but never restores them.Because
router.use()does not wrap the sub-app in themounted_appcleanup closure found inapp.use(), the prototype swap is permanent for the remainder of the request. Any middleware or handler executed downstream (aftersubAppcallsnext()ornext(err)) readsreq.app,req.ip,req.secure, andreq.hostnameunder the sub-app's configuration rather than the parent app's settings, and any prototype extensions on the parent app are lost.Solution
Inside
app.handle()inlib/application.js, capture the incomingreqandresprototypes before mutating them whenever acallbackis provided (indicating a mounted sub-app or delegated caller). Wrapdoneto restore the captured prototypes viaObject.setPrototypeOf()prior to invokingcallback(err).This ensures that regardless of whether the app was mounted through
app.use(),router.use(), directly invoked as middleware, or passed an error vianext(err), the prototype swap is always safely restored when control returns to the caller.Changes Made
lib/application.js:app.handle, wrapdonewhencallbackis present to restorereqandresprototypes to their pre-invocation state upon completion.test/app.use.js:next().next(err)on a router.Test Plan
npm testto ensure all 1,259 test cases pass cleanly.npm run lintto verify style compliance.Fixes #7427