Skip to content

Commit

Permalink
fix: Add fallback for GitHub webhook signature check
Browse files Browse the repository at this point in the history
  • Loading branch information
nlecoy committed Jan 17, 2022
1 parent ed9176b commit 05ca388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scm/driver/github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
}

sig := req.Header.Get("X-Hub-Signature-256")
if sig == "" {
sig = req.Header.Get("X-Hub-Signature")
}
if !hmac.ValidatePrefix(data, []byte(key), sig) {
return hook, scm.ErrSignatureInvalid
}
Expand Down
17 changes: 17 additions & 0 deletions scm/driver/github/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,23 @@ func TestWebhookValid(t *testing.T) {
}
}

func TestWebhookSignatureFallback(t *testing.T) {
// the sha can be recalculated with the below command
// openssl dgst -sha1 -hmac <secret> <file>

f, _ := ioutil.ReadFile("testdata/webhooks/push.json")
r, _ := http.NewRequest("GET", "/", bytes.NewBuffer(f))
r.Header.Set("X-GitHub-Event", "push")
r.Header.Set("X-GitHub-Delivery", "ee8d97b4-1479-43f1-9cac-fbbd1b80da55")
r.Header.Set("X-Hub-Signature", "sha1=cf93f9ba3c8d3a789e61f91e1e5c6a360d036e98")

s := new(webhookService)
_, err := s.Parse(r, secretFunc)
if err != nil {
t.Errorf("Expect valid signature, got %v", err)
}
}

func secretFunc(scm.Webhook) (string, error) {
return "topsecret", nil
}

0 comments on commit 05ca388

Please sign in to comment.