Repository navigation
[ENG-4591] Wire the Express guard when the body parser shares its app.use call - #502
Merged
daniloradovic merged 1 commit intoNov 20, 2026
Conversation
Setup only recognised a body parser on a line that started `app.use(` and
went straight into `json(`/`urlencoded(`. A path-scoped parser
(`app.use('/api', express.json())`), a parser after other middleware
(`app.use(cors(), express.json())`) and a call split over several lines
were all missed, so setup reported "body-parser anchor not found" and left
the app unwired.
The anchor now accepts a mount path and other arguments before the parser,
call-expression middleware after it, and calls that span lines. A parser
followed by a router in the same call (`app.use('/api', express.json(),
router)`) is still left alone: the router runs before anything registered
after that call, so the guard cannot be added there safely. The manual
hint now says to give the parser its own `app.use` in that case. A
path-scoped parser on its own is no longer counted as a route above the
guard.
The "routes registered before the guard" hint said to move the routes
below the guard, which is wrong when the line is middleware such as
`app.use(securityHeaders)`. It now says the guard must be registered
before that line (with the body parser above it), or the line moved below
the guard.
AGENT-INSTALL.md now says how to run tests with the guard installed:
`PATCHSTACK_MODE=dry-run` and `PATCHSTACK_TELEMETRY=off`, keeping the
registration unconditional so `protect --check` still sees it.
Amp-Thread-ID: https://ampcode.com/threads/T-2e659ca3-7bd0-4d70-b3d3-09638251060b
Co-authored-by: Danilo Radovic <danilo@patchstack.com>
|
🎯 Elite quality (96%) — Broadens Express body-parser detection with balanced-paren regex and sharper early-route hints, backed by targeted tests. 📦 Medium 🤖 Written by Amp — going by its own attribution. Unknown whether a person read it; agents can report that with the 🟠 Elevated risk — 68 lines of code, more than an AI review is trusted with (50). ✅ Reviewed — approved by @mariojgt. |
Contributor
Author
|
/review |
mariojgt
approved these changes
Nov 20, 2026
daniloradovic
deleted the
danilo/eng-4591-express-setup-fails-to-wire-the-guard-when
branch
November 20, 2026 08:39
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.
Closes ENG-4591
What changed
app.usecall has a mount path or other middleware before it (app.use('/api', express.json()),app.use(cors(), express.json())), call-expression middleware after it, or spans several lines.protect --checkhint for lines above the guard now reads right for both routers and middleware: register the guard (with the body parser above it) before that line, or move the line below the guard.PATCHSTACK_MODE=dry-runandPATCHSTACK_TELEMETRY=off, keeping the registration unconditional so--checkstill sees it.Why
Setup only matched a line starting
app.use(that went straight intojson(/urlencoded(. Ordinary Express apps failed with "body-parser anchor not found" and had to be wired by hand. The old hint also told people to move "routes" when the flagged line was middleware such as security headers.Not done, on purpose
app.use('/api', express.json(), router)is still left unwired: the router runs before anything registered after that call, so the guard can't be inserted safely. The hint now says to give the parser its ownapp.useabove the guard.app.use(securityHeaders)are still flagged, because they can be routers and there is no reliable way to tell.PATCHSTACK_MODE=off: installed guards are compared byte-for-byte against the templates, so this is docs only for now.How it was verified
main.npm run typecheck,npm run build,npx vitest run(4756 passed, 7 skipped). Ran setup on a multi-line sample and checked the result withnode --check.