2015-10-08 23:27:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func strictReferrerCheck(r *http.Request, prefix string, whitelistHeaders []string) bool {
|
2015-10-12 03:28:01 -04:00
|
|
|
p := strings.TrimSuffix(prefix, "/")
|
|
|
|
if origin := r.Header.Get("Origin"); origin != "" && !strings.HasPrefix(origin, p) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:27:04 -04:00
|
|
|
for _, header := range whitelistHeaders {
|
|
|
|
if r.Header.Get(header) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 02:31:57 -04:00
|
|
|
if referrer := r.Header.Get("Referer"); !strings.HasPrefix(referrer, p) {
|
2015-10-08 23:27:04 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|