1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-23 15:29:42 +02:00

add gorilla/csrf

#199
This commit is contained in:
Kevan Ahlquist 2016-03-31 23:54:12 -05:00
parent 5bf922325a
commit 15d133324d
2 changed files with 34 additions and 1 deletions

View file

@ -10,12 +10,19 @@ import (
"net/url"
"os"
"strings"
"github.com/gorilla/csrf"
"github.com/gorilla/securecookie"
)
var (
endpoint = flag.String("e", "/var/run/docker.sock", "Dockerd endpoint")
addr = flag.String("p", ":9000", "Address and port to serve dockerui")
assets = flag.String("a", ".", "Path to the assets")
CSRF = csrf.Protect(
[]byte(securecookie.GenerateRandomKey(32)),
csrf.HttpOnly(false),
csrf.Secure(false),
)
)
type UnixHandler struct {
@ -87,7 +94,23 @@ func createHandler(dir string, e string) http.Handler {
mux.Handle("/dockerapi/", http.StripPrefix("/dockerapi", h))
mux.Handle("/", fileHandler)
return mux
return logWrapper(CSRF(mux))
}
func logWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Request starting: " + r.URL.Path)
c, err := r.Cookie ("_gorilla_csrf")
if err != nil {
log.Println("Unable to find session cookie _gorilla_csrf")
h.ServeHTTP(w, r)
} else {
log.Println("Cookie:" + c.Value)
log.Println("Header:" + r.Header.Get("X-CSRF-Token"))
h.ServeHTTP(w, r)
log.Println("Request ending")
}
})
}
func main() {