Skip to content

Instantly share code, notes, and snippets.

@aeppert
Created February 5, 2020 15:14
Show Gist options
  • Save aeppert/c986b394115239b80a0261129c46a403 to your computer and use it in GitHub Desktop.
Save aeppert/c986b394115239b80a0261129c46a403 to your computer and use it in GitHub Desktop.
go-macaron Filter for Open Redirect attempts
// Use with:
// m.Before(filter.Filter)
//
package filter
import (
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
)
func isInvalidRequest(url string) bool {
return url[0] == '/' && (url[1] == '/' || url[1] == '\\')
}
// Filter - Filter for possible open redirect URIs
func Filter(rw http.ResponseWriter, req *http.Request) bool {
if req.Method == http.MethodGet {
urlLen := len(req.URL.Path)
if urlLen <= 1 {
return false
} else {
if isInvalidRequest(req.URL.Path) {
rw.WriteHeader(http.StatusNotFound)
log.Debug(fmt.Sprintf("Invalid URL Found: %s", req.URL.Path))
return true
}
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment