router.use() silently accepts express() sub-apps without prototype restoration
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 68/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- express, javascript, node.js
- Domain
- backend
Research direction
Read lib/application.js:169-170 and 230-237, then trace Router.prototype.handle in index.js around line 171 and its restore callback. Verify that request and response prototypes are restored after next(), next(err), and unhandled throws, and that later middleware reads req.ip, req.secure, and req.hostname under the original app’s trust policy.
Written by the indexing model from the issue text.
Description
When an express() sub-app is mounted via router.use() instead of app.use(), the request and response prototypes are swapped by app.handle but never restored. The sub-app also never fires the mount event, so it does not inherit the parent's trust proxy setting. Any middleware that runs after the sub-app calls next() reads req.ip, req.secure, and req.hostname under the wrong app's trust policy, silently and without any error.
app.handle (lib/application.js:169-170) performs:
js
Object.setPrototypeOf(req, this.request)
Object.setPrototypeOf(res, this.response)
The only code that reverses this swap is the mounted_app closure built by app.use at lib/application.js:230-237. router.use never builds that closure, so the swap is permanent for the lifetime of the request.
The router already saves and restores request state across boundaries at index.js:171:
js
let done = restore(callback, req, 'baseUrl', 'next', 'params')
The prototype is simply not on the list.
Add proto (or Object.getPrototypeOf equivalents) to the restore call so the router's own cleanup undoes the prototype swap on every exit path — next(), next(err), and unhandled throws alike.
js
// index.js — inside Router.prototype.handle, before the loop
var reqProto = Object.getPrototypeOf(req)
var resProto = Object.getPrototypeOf(res)
var done = restore(callback, req, 'baseUrl', 'next', 'params')
var _done = done
done = function(err) {
Object.setPrototypeOf(req, reqProto)
Object.setPrototypeOf(res, resProto)
_done(err)
}
- Dominant language
- JavaScript
- Stars
- 69.7k
- Forks
- 25.6k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 4
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from expressjs/express
-
Outdated res.send() syntax in lib/response.js documentation exampleMay be free again A pull request for this issue was closed without being merged. Opendocs
Difficulty 1/5 Under an hour Newbie friendliness 95/100
expressjs/express#7588 · 1 comment ·
Maintainers usually reply within 1 day
-
4.x deps module:body-parser
Difficulty 1/5 Under an hour Newbie friendliness 75/100
expressjs/express#7564 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Update to mocha 12.xPossibly taken @ganeshghutukade-04 claimed this 26 days ago. Opendeps
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
expressjs/express#7547 · 6 comments ·
Maintainers usually reply within 1 day
-
req.subdomains splits an IPv6 host that contains dotsPossibly taken @johnnyhuirilef claimed this 30 days ago. Open4.x 5.x bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
expressjs/express#7532 · 2 comments ·
Maintainers usually reply within 1 day
-
Readme: project name 'express.js' is not capitalizedMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 1 day
All issues in expressjs/express
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
OWASP/CheatSheetSeries#3018 ·
Maintainers usually reply within 1 day
-
Radix number block generates NaN after switching a negative or decimal number to binary, octal or hexPossibly taken @matthewalberto claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
mit-cml/appinventor-sources#4246 · 1 comment ·
Maintainers usually reply within 1 day
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 83/100
GoogleChrome/modern-web-guidance-src#3675 · 2 comments ·
Maintainers usually reply within 1 day