Skip to content

[ENG-4591] Wire the Express guard when the body parser shares its app.use call - #502

Merged
daniloradovic merged 1 commit into
mainfrom
danilo/eng-4591-express-setup-fails-to-wire-the-guard-when
Nov 20, 2026
Merged

daniloradovic merged 1 commit into
mainfrom
danilo/eng-4591-express-setup-fails-to-wire-the-guard-when

Conversation

@daniloradovic

Copy link
Copy Markdown
Contributor

Closes ENG-4591

What changed

  • Express auto-wiring now finds the body parser when the app.use call 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.
  • A path-scoped parser on its own no longer counts as a route registered above the guard.
  • The protect --check hint 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.
  • AGENT-INSTALL.md says how to run tests with the guard installed: PATCHSTACK_MODE=dry-run and PATCHSTACK_TELEMETRY=off, keeping the registration unconditional so --check still sees it.

Why

Setup only matched a line starting app.use( that went straight into json(/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 own app.use above the guard.
  • Bare identifiers like app.use(securityHeaders) are still flagged, because they can be routers and there is no reliable way to tell.
  • No PATCHSTACK_MODE=off: installed guards are compared byte-for-byte against the templates, so this is docs only for now.

How it was verified

  • New tests for the four parser shapes, the parser-plus-router case and the hint wording; they fail on 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 with node --check.
  • Still to do: hostile field-test run after the release, since AGENT-INSTALL.md changed.

  • I reviewed the code, comments, fixtures, commit messages, generated output, and PR description for private Patchstack or customer information.
  • Nothing here is third-party confidential or proprietary material. The Vercel behaviour relied on is from Vercel's public docs (vercel.com/docs/deployments/vercel-ignore, vercel.com/docs/cli/env).

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>
@coderbuds

coderbuds Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🎯 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 report-ai-usage-tool tool.

🟠 Elevated risk — 68 lines of code, more than an AI review is trusted with (50).

✅ Reviewed — approved by @mariojgt.

See how your team is trending →

Copy link
Copy Markdown
Contributor Author

/review

@daniloradovic
daniloradovic merged commit 83d22de into main Nov 20, 2026
28 checks passed
@daniloradovic
daniloradovic deleted the danilo/eng-4591-express-setup-fails-to-wire-the-guard-when branch November 20, 2026 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants