fix(ui): route malformed path escapes to catch-all (#11215)

Co-authored-by: Kevin Mok <48875246+kevmok@users.noreply.github.com>
This commit is contained in:
effect-bot
2026-12-02 10:11:24 +13:00
committed by GitHub
co-authored by Kevin Mok
parent 22564884ba
commit df99cfbf76
4 changed files with 28 additions and 10 deletions
@@ -0,0 +1,5 @@
---
"effect": patch
---
Make `Route.union` in `effect/ui` resolve malformed path escapes to the `"/*"` catch-all under the route base, preserving malformed segments in `params["*"]`.
+9 -8
View File
@@ -850,11 +850,12 @@ const build = (
? path.replace(/\/+$/, "") || "/"
: path
const input: Array<string> = []
let malformed = false
if (trimmed !== "/") {
for (const part of trimmed.slice(1).split("/")) {
const decoded = decodeComponent(part)
if (decoded === undefined) return Option.none()
input.push(decoded)
if (decoded === undefined) malformed = true
input.push(decoded ?? part)
}
}
const search = parseSearch(searchString)
@@ -871,7 +872,7 @@ const build = (
notFound: true,
invalid: []
})
if (search === undefined) return fallback()
if (malformed || search === undefined) return fallback()
for (const entry of entries) {
const raw = matchShape(entry.segments, input)
if (raw === undefined) continue
@@ -950,11 +951,11 @@ const build = (
* same argument and prints a link: `AppRoute.Person.href({ params: { id: 1 }
* })`. `Router.navigable` wraps the union to add `push` and `replace`.
*
* Declare a catch-all `"/*"` route, conventionally `NotFound`, whose tag is
* `catchAll`: it ranks last, and a required search field that fails to
* decode resolves to it, so `parse` returns a route for every URL under the
* base and the Model never holds an `Option`. Without one, `parse` returns
* `Option.none()` for such URLs and `catchAll` is `undefined`.
* A catch-all `"/*"` route, conventionally `NotFound`, ranks last; its tag is
* exposed as `catchAll`. Under the route base, it handles unmatched URLs,
* required search fields that fail to decode and malformed percent-escapes.
* Malformed path segments stay as written in `params["*"]`. Without a catch-all,
* `parse` returns `Option.none()` for these URLs and `catchAll` is `undefined`.
*
* **Gotchas**
*
+4 -2
View File
@@ -1690,8 +1690,10 @@ const commandLayers = Layer.mergeAll(
*
* The Router resolves every href through `routes`: redirects and a trailing
* slash are replaced with the canonical URL, and an href the routes don't
* parse is a full page load. It implements blocking, history restore and the
* settle timing; a host adds scroll, announcements and focus in `onSettled`.
* parse is a full page load. The Layer dies if the start URL doesn't resolve;
* a `"/*"` route covers URLs under the route base. The Router implements
* blocking, history restore and the settle timing; a host adds scroll,
* announcements and focus in `onSettled`.
*
* History restore: unless `restore` is `false`, before leaving an entry,
* the Router saves the encoded state of every mounted instance by path: its
+10
View File
@@ -250,6 +250,16 @@ describe("Route", () => {
invalid: []
}
)
// A malformed path escape falls back to the catch-all with the raw segment.
assert.deepStrictEqual(
Option.getOrThrow(routes.resolve("/people/a%ZZ")),
{
route: { _tag: "NotFound", params: { "*": "people/a%ZZ" }, search: {} },
redirected: false,
notFound: true,
invalid: []
}
)
const noCatchAll = Route.union({ Home: Route.make("/") })
assert.isFalse(Option.getOrThrow(noCatchAll.resolve("/")).notFound)
assert.isTrue(Option.isNone(noCatchAll.resolve("/missing")))