add `access` route rule to restrict routes to internal or external callers only
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- typescript
- Domain
- backend-api-design
Research direction
Start with src/runtime/nitro.ts, src/runtime/internal/app.ts, and src/runtime/internal/route-rules.ts to understand caller detection and rule ordering. Review the route configuration and request-context types in src/types/route-rules.ts and src/types/h3.ts, then run the six cases in test/tests.ts. Done means the proposed internal, external, unrestricted, and inherited-rule behaviors are covered and existing tests pass.
Written by the indexing model from the issue text.
Description
Describe the feature
feat: add access route rule to restrict routes to internal or external callers only
Problem
Nitro's serverFetch() lets handlers call other routes in-process without going through the network. This is powerful, but currently there's no way to enforce who is allowed to reach a given route. Two common needs that are currently impossible to express:
- Internal-only helpers — routes that should only ever be called from within other handlers via
serverFetch(), and should not be reachable over HTTP (e.g. internal aggregation endpoints, service-layer helpers). - External-only routes — routes that should only accept real HTTP requests and never be called in-process (e.g. webhooks that rely on raw request integrity, or endpoints where calling them internally would be a programming error).
Proposed solution
A new access option in routeRules:
routeRules: {
// Only callable via serverFetch() from within other handlers
'/internal/**': { access: 'internal' },
// Only callable via real HTTP requests
'/api/webhook': { access: 'external' },
// Cancel a restriction inherited from a parent pattern (existing convention)
'/internal/public': { access: false },
}
| Value | Behavior |
|---|---|
"internal" |
Only reachable via serverFetch() in-process |
"external" |
Only reachable via real HTTP requests |
false |
Cancel a restriction inherited from a less-specific pattern |
| unset | Both (default — fully backwards-compatible) |
Disallowed callers receive 404 so the route's existence is not revealed.
Implementation
I've built a full implementation on a fork: MichaelDickBasecom/nitro @ feat/access-route-rule
Key pieces:
Detection — serverFetch() sets req.context.nitro.internal = true after the caller-provided context is merged, so it can't be spoofed by passing a crafted context object. Requests entering through the runtime's HTTP entry point never carry this flag. (src/runtime/nitro.ts, src/runtime/internal/app.ts)
Rule handler — registered as a runtime route rule with order: -2, so it runs before basicAuth (order: -1), redirect, proxy, and cache. Blocked callers are rejected before any auth challenge or forwarding happens. (src/runtime/internal/route-rules.ts)
Types — access is added to both NitroRouteConfig (user-facing) and the nitro request context. (src/types/route-rules.ts, src/types/h3.ts)
Tests — 6 test cases covering all combinations of caller type vs. restriction, including serverFetch being called from within a handler (the "proxy" fixture). (test/tests.ts)
All existing tests continue to pass.
Happy to open a PR if this direction looks good to the maintainers. Open to feedback on the rule name, the 404 vs 403 choice for blocked callers, or anything else.
Additional information
- Would you be willing to help implement this feature?
- Dominant language
- TypeScript
- Stars
- 11.5k
- Forks
- 968
- Avg merge
- 4d 9h
- Merged PRs (30d)
- 64
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a 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 nitrojs/nitro
-
pending triage
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 95/100
Maintainers usually reply within 2 days
-
cloudflare: `nitro deploy` fails when a parent directory has a Wrangler configPossibly taken @draarivpatel-ui claimed this 13 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 2 days
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
pending triage
Difficulty 5/5 Over a week Newbie friendliness 30/100
nitrojs/nitro#5205 · 1 reaction ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ranxianglei/billion-context#8319 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
JimLiu/open-artifact#126 ·
-
bug repro-ready
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
mksglu/context-mode#1454 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
laststance/design-world#98 ·
Maintainers usually reply within 1 day
-
Add: KCM True crimeOpenapproved check:passed streams:add
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day