Skip to content

tree: do not match slash in regexp placeholder with suffix (#1188) - #1189

Open
littfed wants to merge 1 commit into
go-chi:masterfrom
littfed:fix/regexp-suffix-slash
Open

littfed wants to merge 1 commit into
go-chi:masterfrom
littfed:fix/regexp-suffix-slash

Conversation

@littfed

@littfed littfed commented Sep 23, 2026

Copy link
Copy Markdown

Fixes #1188.

When a regexp placeholder is followed by a literal suffix (such as {name:[^.]+}.json), the check preventing matches across path segments (strings.IndexByte(xsearch[:p], '/') != -1) was placed inside an else if branch following ntyp == ntRegexp && xn.rex != nil.

Because ntRegexp took the first if branch, it bypassed the slash check entirely when a suffix was present. Consequently, a request like /re/a/b.json evaluated xsearch[:p] as "a/b", which matched [^.]+ and crossed path segments, contrary to the specification that regexp placeholders do not match /.

This change moves the segment boundary slash check before the regexp matching condition so that it guards both regexp and parameter nodes with tail suffixes.

Tests

  • Added TestMuxRegexpSuffixSlash in mux_test.go verifying that /re/a.json matches "a" and /re/a/b.json returns 404.
  • Ran full test suite: go test -v ./... passes.

When a regexp placeholder is followed by a literal suffix, the slash
check was previously bypassed because it was in an else-if branch after
the regexp check. Consequently, a regexp parameter could span path
segments (e.g. matching 'a/b' in '/re/a/b.json').

Move the slash check outside of the else-if so it applies to both regexp
and parameter nodes with suffixes.

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.

A regexp placeholder followed by a suffix matches a value containing /

1 participant