Fix middleware.CleanPath panicking on a non-chi handler - #1193
Open
januththedev wants to merge 1 commit into
Open
januththedev wants to merge 1 commit into
januththedev wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
middleware.CleanPathpanics on a non-chi handlerDescription
CleanPathdereferences the chi routing context unconditionally:chi.RouteContextreturnsnilwhenever the handler is not achi.Mux.Why it's wrong
CleanPathis exported as a plainfunc CleanPath(next http.Handler) http.Handlerwith no chi-only restriction, so composing it onto anhttp.ServeMux, anhttp.FileServer, or any other non-chi handler panics with a nil-pointer dereference on the very first request.It has no chi-specific work to do — it only needs to normalise a path and write it back. And its sibling in the same package already handles this exact case:
with a dedicated regression test,
TestStripSlashesWithNilContext(middleware/strip_test.go:203), commented "This tests a http.Handler that is not chi.Router / In these cases, the routeContext is nil".CleanPathwas simply never given the same guard. I probed the rest of the package:StripSlashes,RedirectSlashes,URLFormat,HeartbeatandPageRouteall survive a nil context — onlyCleanPathandGetHeadpanic, andGetHeadis fundamentally chi-dependent so a panic there is more defensible.Issue #787 "middleware.CleanPath doesn't work with other routers" is still open (its PR #786 closed unmerged).
The fix
Tests
TestCleanPathWithNilContextinmiddleware/clean_path_test.go, mirroring the existing nil-context test's style.panic: runtime error: invalid memory address or nil pointer dereferenceatclean_path.go:16. After: passes, and the pre-existingTestCleanPathstill passes.go test -count=1 ./...(uncached):chiandchi/v5/middlewarebothok, exit 0, 0 failures, 0 skips — 129 top-level PASS. Identical to the baseline on unmodified HEAD, which had no pre-existing failures.go vet ./...clean.Upstream status
I checked 60 open issues and 55 open PRs and mapped each open PR to the files it touches.
middleware/clean_path.gois touched only by open PR #1162 (CONNECT authority-form, unrelated — it wraps thepath.Cleancall inif routePath != ""and does not address the nil context). No open issue or PR fixes the nil-context panic.I steered clear of the heavily-contended files:
tree.go/mux.go(11+ PRs),middleware/get_head.go(4),middleware/compress.go(10), plusstrip.go,supress_notfound.go,route_headers.go,nocache.go,recoverer.go,content_encoding.go,context.go.gofmt -lflags these files, but it flags 65 non-_examplesGo files including untouched ones (strip.go,tree.go,context.go) — this machine hascore.autocrlf=true, so the whole checkout is CRLF. Pre-existing checkout condition, not introduced here;git diffshows no line-ending churn.Real neighbouring defects I found but left alone
middleware.GetHeadbreaks explicitHEADhandlers inside a mounted subrouter — measured:HEAD /api/other→ 405 withGetHeadon the subrouter, 200 without.get_head.go:29usesrctx.Routes(set only by the root mux) with a sub-router-relativeroutePath. Not narrow: naively settingrctx.Routes = mxregressesSupressNotFoundinside subrouters, which I measured going 200 → 404.middleware.Heartbeatsilently never fires inside a subrouter (comparesr.URL.Path, notrctx.RoutePath) — measuredGET /api/ping→ 404. Open issue Heartbeat middleware is not invoked when added to a subrouter #821, no open PR;middleware/page_route.gohas the identical flaw with no issue at all./—tree.go:457-464skips the "avoid a match across path segments" check forntRegexp, so/re/{name:[^.]+}.jsonmatches/re/a/b.json. Issue A regexp placeholder followed by a suffix matches a value containing/#1188 with PR tree: do not match slash in regexp placeholder with suffix (#1188) #1189 already filed.