Skip to content

feat: respond with 405 for routes matched only by other methods - #4811

Merged
pi0 merged 3 commits into
mainfrom
fix/method-not-allowed
Oct 17, 2026
Merged

pi0 merged 3 commits into
mainfrom
fix/method-not-allowed

Conversation

@pi0x

@pi0x pi0x commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

closes #1727

When a request path has method-specific routes (e.g. hello.post.ts) but none for the request method, Nitro now responds with 405 Method Not Allowed and an Allow header instead of 404:

{ "error": true, "status": 405, "statusText": "Method Not Allowed",
  "message": "Method GET is not allowed for /api/hello (allowed: POST)" }
  • On a route miss, ~findRoute looks the path up again with the existing findRoute, once for each method used by method-specific routes (a list emitted at build time). A method-agnostic route matching the path would have matched the original request, so any hit is a route of that method.
  • No second matcher is compiled, so bundle size doesn't grow with the number of routes: the cost is a fixed ~0.6 KB raw / ~0.2 KB gzip, and zero for apps without method-specific routes (their output is byte-identical).
  • Matched requests are unaffected. A 404 costs up to one extra lookup per method (~45 ns each). The 405 error is returned rather than thrown, so a 405 response costs about the same as a 404.
  • Middleware still runs before the 405 handler, so CORS preflight keeps working.
  • Catch-all routes ([...].ts, renderer, server entry) still take precedence. Making method routes win over them would break setups like a /contact page with server/routes/contact.post.ts. This means Nuxt apps (which always have the renderer catch-all) are not affected.

The shared fixture has a server entry (/**), so the 405 can't be observed there; the existing it.todo stays and coverage is in unit tests. Verified manually in dev and prod builds, including baseURL and a CORS preflight.

Note: HEAD on a GET-only route now returns 405 (was 404). Nitro's compiled router has no HEAD -> GET fallback; that's a separate gap.

🤖 Generated with AI assistant

When no route matches the request method but the path has method-specific routes, respond with `405 Method Not Allowed` and an `Allow` header instead of `404`.
@pi0x
pi0x requested a review from pi0 as a code owner October 17, 2026 12:40
@vercel

vercel Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 17, 2026 2:56pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 765bee18-fd3e-4f0b-8fbf-c19274329d24
📥 Commits

Reviewing files that changed from the base of the PR and between 1321ba4 and fdde2f6.

📒 Files selected for processing: 2
  • docs/1.docs/5.routing.md
  • src/build/virtual/app.ts
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. - C.A.R. Hoare |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  \
   \   (\__/)
       (•ㅅ•)
       /   づ

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 87cfbe96-3070-4b4a-b3f5-39139bcb6082
📥 Commits

Reviewing files that changed from the base of the PR and between d23e29d and 1321ba4.

📒 Files selected for processing: 4
  • src/build/virtual/app.ts
  • src/runtime/internal/app.ts
  • test/unit/runtime-middleware.test.ts
  • test/unit/virtual-app.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

📝 Walkthrough

Generated app routing now returns 405 Method Not Allowed with an Allow header when a path matches routes for other methods. The routing documentation describes this response and the cases where a catch-all route, renderer, or server entry handles the request.

Changes: HTTP method mismatch handling

Layer / File(s) Summary
Create 405 matcher
src/runtime/internal/app.ts, test/unit/runtime-middleware.test.ts
The matcher checks configured methods for the same path. If another method has a matching route, its handler produces an HTTP 405 error with an Allow header. Tests cover unmatched paths and unsupported methods.
Wire fallback into generated app
src/build/virtual/app.ts, test/unit/virtual-app.test.ts, docs/1.docs/5.routing.md
The generated app collects route methods and uses the matcher when normal route lookup fails. Tests check that matcher wiring is conditional on method-specific routes. The documentation describes the 405 response and matching-handler exceptions.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~15 minutes

Change: Feature

Merge Risk: ⚪ Minimal · up to 1321b

The method-not-allowed fallback is ready to merge after normal checks; no unresolved issue was established.

Security architecture risk: 🔵 Low · up to 1321b

The change is limited to unsupported-method responses and preserves normal route precedence. The fallback does not execute handlers for other methods. No material security regression was identified, but end-to-end authentication and middleware behavior was not fully verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected exposure is the generated application's reachable method-specific route set where normal matching finds no handler. A matching method-agnostic route or catch-all prevents the new fallback. The inspected change adds response metadata exposure, not access to another service, tenant, or data store.

Trust Boundaries and Controls

  • inferred — An attacker can select the request path and method, but the fallback cannot turn that selection into execution of another method's protected handler. Middleware registration is preserved; live authentication and preflight short-circuit behavior remain outside the verified integration coverage.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 10 functions across 7 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title follows the Conventional Commits format with the valid feat: prefix and clearly describes the 405 response change.
Description check Passed The description directly explains the 405 response behavior, implementation approach, route precedence, performance impact, and test coverage.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4811

commit: 1321ba4

@pi0 pi0 added the v3 label Oct 17, 2026
Probe the existing route matcher with each method of the method-specific routes instead of compiling a second matcher, so bundle size no longer grows with the number of routes. Return the 405 error instead of throwing it, and name the allowed methods in its message.
@pi0 pi0 changed the title feat(runtime): respond with 405 for routes matched only by other methods feat: respond with 405 for routes matched only by other methods Oct 17, 2026
@pi0
pi0 merged commit efbb380 into main Oct 17, 2026
10 of 12 checks passed
@pi0
pi0 deleted the fix/method-not-allowed branch October 17, 2026 14:56
@pi0x pi0x removed the v3 label Nov 10, 2026

This branch was successfully deployed

1 active deployment
Preview — fdde2f61 Deployed Oct 17, 2026 by vercel[bot]
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.

Nuxt Server prefixed files do not respond with correct status code 405

2 participants