add `access` route rule to restrict routes to internal or external callers only

Open
#4,273 1 comment 1 reaction 0 assignees View on GitHub

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

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

enhancement v3
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

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.