2015-10-09 05:27:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func strictReferrerCheck(r *http.Request, prefix string, whitelistHeaders []string) bool {
|
2015-10-12 09:28:01 +02:00
|
|
|
p := strings.TrimSuffix(prefix, "/")
|
2015-10-12 10:23:06 +02:00
|
|
|
if origin := r.Header.Get("Origin"); origin != "" {
|
|
|
|
// if there's an Origin header, check it and ignore the rest
|
|
|
|
return strings.HasPrefix(origin, p)
|
2015-10-12 09:28:01 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 05:27:04 +02:00
|
|
|
for _, header := range whitelistHeaders {
|
|
|
|
if r.Header.Get(header) != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 08:31:57 +02:00
|
|
|
if referrer := r.Header.Get("Referer"); !strings.HasPrefix(referrer, p) {
|
2015-10-09 05:27:04 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|