Skip to content

Avoid index out of range panic on malformed request URIs#96

Open
AruneshDwivedi wants to merge 1 commit into
dimfeld:masterfrom
AruneshDwivedi:fix/lookup-panic-malformed-uri
Open

Avoid index out of range panic on malformed request URIs#96
AruneshDwivedi wants to merge 1 commit into
dimfeld:masterfrom
AruneshDwivedi:fix/lookup-panic-malformed-uri

Conversation

@AruneshDwivedi

Copy link
Copy Markdown

Fixes #88.

When PathSource is RequestURI (the default), lookup strips the query string from the request URI before searching. A malformed request line such as a bare ? has no path, so after the strip path becomes "" with pathLen 0. The code then evaluates path[pathLen-1] for the trailing slash check and path[1:] for the tree search, both of which index out of range and panic. Vulnerability scanners hit this routinely with junk URLs, which is how the reporter saw it through their LookupFunc based SPA fallback.

A request whose path is empty or does not start with a slash can never match a registered route, so lookup now returns not found for it before doing any slice indexing.

Added TestLookupMalformedRequestURI covering ?, *, and a path with no leading slash, all of which panicked before this change.

When PathSource is RequestURI, stripping the query string from a value like
"?" collapses the path to an empty string, after which path[pathLen-1] and
path[1:] index out of range and panic. Requests whose path is empty or lacks
a leading slash cannot match any route, so lookup now returns not found for
them instead of indexing past the slice.
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.

index out of range panic on bad URLs

1 participant