added webhook validation

This commit is contained in:
ston1th 2019-12-14 14:48:41 +01:00
commit dd88498e0d
6 changed files with 158 additions and 21 deletions

43
main.go
View file

@ -25,6 +25,8 @@ const (
commentAction = "X-Interceptor-Comment-Action"
labelHeader = "X-Interceptor-Label"
removeLabelsHeader = "X-Interceptor-Remove-Labels"
includeHeader = "X-Interceptor-Include-Repo"
excludeHeader = "X-Interceptor-Exclude-Repo"
githubEvent = "X-GitHub-Event"
giteaEvent = "X-Gitea-Event"
@ -42,28 +44,33 @@ type Handler struct {
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var valid bool
event := r.Header.Get(githubEvent)
switch event {
case "pull_request":
h.prHandler(w, r)
valid = h.prHandler(w, r)
case "push":
h.pushHandler(w, r)
valid = h.pushHandler(w, r)
case "issue_comment":
h.commentHandler(w, r)
valid = h.commentHandler(w, r)
default:
h.dumpHandler(w, r)
}
status := int(reflect.Indirect(reflect.ValueOf(w)).FieldByName("status").Int())
log.Printf("%s %s %s event:%s status:%d", r.RemoteAddr, r.Method, r.URL, event, status)
log.Printf("%s %s %s event:%s valid:%t status:%d", r.RemoteAddr, r.Method, r.URL, event, valid, status)
}
func (h *Handler) prHandler(w http.ResponseWriter, r *http.Request) {
func (h *Handler) prHandler(w http.ResponseWriter, r *http.Request) (valid bool) {
var prb PullRequestBody
body, err := readBody(r.Body, &prb)
if err != nil {
logError(w, err.Error())
return
}
if valid = h.git.validate(r, body, prb.Repository); !valid {
w.WriteHeader(http.StatusBadRequest)
return
}
if contains(r.Header[prAction], prb.Action) {
if l := r.Header.Get(labelHeader); l != "" {
@ -85,33 +92,54 @@ func (h *Handler) prHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusBadRequest)
return
}
func (h *Handler) pushHandler(w http.ResponseWriter, r *http.Request) {
func (h *Handler) pushHandler(w http.ResponseWriter, r *http.Request) (valid bool) {
var pb PushBody
body, err := readBody(r.Body, &pb)
if err != nil {
logError(w, err.Error())
return
}
if valid = h.git.validate(r, body, pb.Repository); !valid {
w.WriteHeader(http.StatusBadRequest)
return
}
push := r.Header.Get(pushRef)
//if contains(r.Header[pushAction], pb.Ref) {
if push != "" && push == pb.Ref {
inc := r.Header[includeHeader]
exc := r.Header[excludeHeader]
if inc != nil && !contains(inc, pb.Repository.FullName) {
w.WriteHeader(http.StatusBadRequest)
return
}
if exc != nil && contains(exc, pb.Repository.FullName) {
w.WriteHeader(http.StatusBadRequest)
return
}
h := w.Header()
h.Set(commitHeader, pb.After)
w.Write(body)
return
}
w.WriteHeader(http.StatusBadRequest)
return
}
func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) {
func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) (valid bool) {
var cb CommentBody
body, err := readBody(r.Body, &cb)
if err != nil {
logError(w, err.Error())
return
}
if valid = h.git.validate(r, body, cb.Repository); !valid {
w.WriteHeader(http.StatusBadRequest)
return
}
if !contains(r.Header[commentAction], cb.Action) {
w.WriteHeader(http.StatusBadRequest)
@ -163,6 +191,7 @@ func (h *Handler) commentHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusBadRequest)
return
}
func (h *Handler) dumpHandler(w http.ResponseWriter, r *http.Request) {