Skip to content

fix(tree): reject duplicate param keys before insert - #1207

Open
therealgofman wants to merge 1 commit into
go-chi:masterfrom
therealgofman:fix-insert-before-param-keys
Open

therealgofman wants to merge 1 commit into
go-chi:masterfrom
therealgofman:fix-insert-before-param-keys

Conversation

@therealgofman

Copy link
Copy Markdown

Problem

InsertRoute panics on a duplicate param key (chi: routing pattern '…' contains duplicate param key, '…'). That panic is intended.

patParamKeys runs from setEndpoint, after addChild has already written nodes. If the caller recovers and then serves on the same mux, findRoute never returns.

Seen with a recovered Get of about 25 empty {} segments plus a static tail, then ServeHTTP on "/{.

This is not the self-mount loop (Mount("/", r) / #843 / #663).
Same symptom, different entry.

Repro (v5.3.2)

r := chi.NewRouter()
h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
	w.WriteHeader(http.StatusOK)
})
func() {
	defer func() { recover() }()
	r.Get("/{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}0", h)
}()
r.ServeHTTP(w, req) // path "/{" — does not return

Fix

Call patParamKeys(pattern) at the start of InsertRoute, before any tree write. The panic is unchanged. The tree stays empty. After recover, ServeHTTP is 404. A later valid Get still works.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant