Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

index out of range panic on bad URLs #88

Open
arussellsaw opened this issue Oct 5, 2022 · 1 comment
Open

index out of range panic on bad URLs #88

arussellsaw opened this issue Oct 5, 2022 · 1 comment

Comments

@arussellsaw
Copy link

👋 hello!

we often see panics coming from our router when we get hit by people vuln scanning our app. we use lookupFunc to serve our frontend if no backend routes match. I think we're just missing a range check before evaluating.

an example URL that panics: GET /images/../cgi/cgi_i_filter.js

Here's the rough shape of our setup:

// LookupFunc is associated with a mux router. It permits querying the router to see if it
// can respond to a request.
type LookupFunc func(w http.ResponseWriter, r *http.Request) (httptreemux.LookupResult, bool)

func SinglePageApp(urlPrefix, dirPath string, includeSourcemaps bool) func(h http.Handler, lookupFunc LookupFunc) http.Handler {
fs := static.LocalFile(dirPath, true)
fileserver := http.FileServer(fs)
if urlPrefix != "" {
fileserver = http.StripPrefix(urlPrefix, fileserver)
}

return func(h http.Handler, lookupFunc LookupFunc) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// If we have an official route for this request, we should skip our handler. We
		// only run when we can't find a match.
		if _, found := lookupFunc(w, r); found {
			h.ServeHTTP(w, r)
			return
		}

		if !fs.Exists(urlPrefix, r.URL.Path) {
			r.URL.Path = "/"
		}
                     // serving the SPA goes here

thanks! 🙏

@dimfeld
Copy link
Owner

dimfeld commented Oct 6, 2022

If you can come up with a reproducible test case I can take a look at it.

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

No branches or pull requests

2 participants