fix: reject ambiguous module routes
A module can register both `GET /events/:id` and `GET /events/new`. Driven through the pinned router, `/events/new` reached the parameter handler under `demo.events.read` when the parameter route registered first, and the static handler under `demo.events.create` when it registered second. Nothing rejected the pair, so declaration order decided which permission guarded the path. That is a permission policy, not a routing preference, and it must not be inferred from array position. Two routes of one module now overlap when they share a method and a segment count and, at every position, either both segments are the same static literal or at least one is a parameter. Under this grammar — static literals or `:name` parameters, no wildcard, regex, optional, or catch-all — that is exactly "some concrete request path matches both". `moduleRoutesOverlap` implements it in the contracts package, where path semantics already live and Hono cannot reach. Core checks it during staged registration, after the existing duplicate check, so identical shapes (including paths differing only in parameter name) keep returning MODULE_ROUTE_DUPLICATE and a genuine intersection returns the new MODULE_ROUTE_OVERLAP. The rejected module publishes none of its staged routes or capabilities, no start hook runs, and no listener binds. Rejection rather than a specificity rule: ranking static above parameter would still silently pick which permission applies. Disjoint routes keep their order; different methods, different segment counts, and the same local path in two different modules (each has its own platform-owned mount prefix) all remain legal. The same requests also pinned down what handlers actually receive. Route declarations are validated; route *parameters* are not, and the two are unrelated. A single encoded segment arrives decoded as `/../admin`, `a\b`, an embedded NUL, CR/LF, `1; drop`, or non-ASCII text — none of which any declared path could contain. The contract now says so explicitly: decoded values are untrusted, module-owned input that a handler must validate against its own exact domain grammar before any database, filesystem, command, or identifier use, and re-encoding is not a substitute. No generic validator, path-normalization layer, or route field was added, because module parameters are module-owned domain types and one platform-wide pattern would either reject legitimate values or approve meaningless ones. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5
parent
4a34a8203c
commit
800ecfdb92
@@ -141,6 +141,49 @@ export function moduleRouteShapeKey(method: ModuleHttpMethod, path: ModuleRouteP
|
||||
return `${method} /${shapeSegments.join("/")}`;
|
||||
}
|
||||
|
||||
/** `"/"` is the module root and has no segments; every other canonical path is its `/`-separated
|
||||
* segments. Takes an already-validated `ModuleRoutePath`. */
|
||||
function moduleRoutePathSegments(path: ModuleRoutePath): readonly string[] {
|
||||
return path === "/" ? [] : path.slice(1).split("/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether two routes of the same module can both match at least one request path.
|
||||
*
|
||||
* Two routes overlap when they share an HTTP method and a segment count, and at every position
|
||||
* either both segments are the same static literal or at least one is a parameter. Under this
|
||||
* route grammar — static kebab-case literals or `:name` parameters, with no wildcard, regex,
|
||||
* optional, or catch-all segment — that condition is exactly "some concrete path matches both".
|
||||
*
|
||||
* This exists because an overlap is a *permission* ambiguity, not a routing preference:
|
||||
* `GET /events/:id` and `GET /events/new` can carry different permissions, and with an
|
||||
* order-sensitive matcher whichever registered first decides which permission governs
|
||||
* `/events/new`. Core rejects the overlap at registration instead of picking a winner, so no
|
||||
* router's matching order can ever select the governing permission. Identical shapes (including
|
||||
* paths differing only in parameter name) remain the pre-existing duplicate result; callers check
|
||||
* that first.
|
||||
*
|
||||
* Deliberately framework-neutral: path semantics already live in this package, and nothing about
|
||||
* this rule may depend on Hono, which stays confined to `apps/api`.
|
||||
*/
|
||||
export function moduleRoutesOverlap(
|
||||
first: { readonly method: ModuleHttpMethod; readonly path: ModuleRoutePath },
|
||||
second: { readonly method: ModuleHttpMethod; readonly path: ModuleRoutePath },
|
||||
): boolean {
|
||||
if (first.method !== second.method) {
|
||||
return false;
|
||||
}
|
||||
const firstSegments = moduleRoutePathSegments(first.path);
|
||||
const secondSegments = moduleRoutePathSegments(second.path);
|
||||
if (firstSegments.length !== secondSegments.length) {
|
||||
return false;
|
||||
}
|
||||
return firstSegments.every((segment, index) => {
|
||||
const other = secondSegments[index] as string;
|
||||
return segment.startsWith(":") || other.startsWith(":") || segment === other;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* What a module's route handler receives about the caller, the match, and the platform services
|
||||
* available to it — never an authorization-store, grant-management, storage, jobs, configuration,
|
||||
@@ -149,6 +192,28 @@ export function moduleRouteShapeKey(method: ModuleHttpMethod, path: ModuleRouteP
|
||||
* before ever invoking its handler; this contracts-layer module never invokes a handler itself.
|
||||
* `services` is the same frozen `ModuleRuntimeServices` object the owning module's `start`/`stop`
|
||||
* hooks receive for this API runtime — never a fresh one built per request.
|
||||
*
|
||||
* ## `params` values are untrusted input
|
||||
*
|
||||
* `params` holds the HTTP router's **decoded** path values, frozen alongside this context. Decoding
|
||||
* is not validation, and the declaration grammar above constrains only what a module may *declare*
|
||||
* — it says nothing about what a request may *send*. A caller can percent-encode anything into a
|
||||
* single segment, so a decoded value routinely contains characters no declared path could:
|
||||
* verified against the pinned router, `%2F..%2Fadmin` arrives as `/../admin`, `a%5Cb` as `a\b`,
|
||||
* `a%00b` with an embedded NUL, `a%0D%0Ab` with CR/LF, and `%C3%A9v%C3%A9nement` as `événement`.
|
||||
*
|
||||
* Every handler must therefore validate each parameter it uses against its own exact domain
|
||||
* grammar — a UUID, slug, date, numeric id, or whatever that module owns — **before** any
|
||||
* database, filesystem, command, identifier, or other side-effecting use, and reject a missing,
|
||||
* repeated, malformed, or semantically invalid value with its own sanitized response. Re-encoding,
|
||||
* decode/re-encode round-tripping, or inspecting the raw URL is not a substitute for that check.
|
||||
* The platform deliberately supplies no generic parameter grammar: module parameters are
|
||||
* module-owned domain types, and one platform-wide pattern would either reject legitimate values
|
||||
* or approve meaningless ones.
|
||||
*
|
||||
* The platform continues to own authentication, authorization, `Origin`, CORS, body limits, cache
|
||||
* behavior, and uncaught-handler sanitization; permission enforcement always completes before a
|
||||
* handler — and therefore any parameter validation it performs — can run.
|
||||
*/
|
||||
export interface ModuleRouteHandlerContext {
|
||||
readonly moduleId: ModuleId;
|
||||
|
||||
Reference in New Issue
Block a user