summaryrefslogtreecommitdiff
path: root/srv/src/http/csrf.go
blob: d0f7b6ad964a5f0c7e40e010187f45a668dee0ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package http

import (
	"errors"
	"net/http"
	"net/url"

	"github.com/mediocregopher/blog.mediocregopher.com/srv/http/apiutil"
)

func (a *api) checkCSRFMiddleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {

		refererURL, err := url.Parse(r.Referer())
		if err != nil {
			apiutil.BadRequest(rw, r, errors.New("invalid Referer"))
			return
		}

		if refererURL.Scheme != a.params.PublicURL.Scheme ||
			refererURL.Host != a.params.PublicURL.Host {
			apiutil.BadRequest(rw, r, errors.New("invalid Referer"))
			return
		}

		h.ServeHTTP(rw, r)
	})
}