Created
February 5, 2020 15:14
-
-
Save aeppert/c986b394115239b80a0261129c46a403 to your computer and use it in GitHub Desktop.
go-macaron Filter for Open Redirect attempts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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