simple csrf protection
This commit is contained in:
parent
fa0237b546
commit
d6c2ba5812
2 changed files with 13 additions and 2 deletions
|
|
@ -43,6 +43,10 @@ func (h *Handler) Start() {
|
|||
}
|
||||
}
|
||||
|
||||
func unsafeMethod(method string) bool {
|
||||
return method == "POST" || method == "PUT" || method == "DELETE"
|
||||
}
|
||||
|
||||
func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
route, vars := getRoute(req)
|
||||
if route == nil {
|
||||
|
|
@ -51,6 +55,10 @@ func (h Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|||
}
|
||||
|
||||
if h.requiresAuth() && !route.manualAuth {
|
||||
if unsafeMethod(req.Method) && req.Header.Get("X-Requested-By") != "yarr" {
|
||||
rw.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if !userIsAuthenticated(req, h.Username, h.Password) {
|
||||
rw.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue