Skip to content

Commit b87006f

Browse files
committed
Merge remote-tracking branch 'origin'
2 parents b5d0cb0 + 56abf52 commit b87006f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

scm/driver/gitea/webhook.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
package gitea
66

77
import (
8+
"crypto/sha256"
89
"encoding/json"
910
"fmt"
1011
"io"
1112
"io/ioutil"
1213
"net/http"
1314

1415
"github.com/drone/go-scm/scm"
16+
"github.com/drone/go-scm/scm/driver/internal/hmac"
1517
)
1618

1719
type webhookService struct {
@@ -58,7 +60,20 @@ func (s *webhookService) Parse(req *http.Request, fn scm.SecretFunc) (scm.Webhoo
5860
}
5961

6062
secret := req.FormValue("secret")
61-
if secret != key {
63+
signature := req.Header.Get("X-Gitea-Signature")
64+
65+
// fail if no signature passed
66+
if signature == "" && secret == "" {
67+
return hook, scm.ErrSignatureInvalid
68+
}
69+
70+
// test signature if header not set and secret is in payload
71+
if signature == "" && secret != "" && secret != key {
72+
return hook, scm.ErrSignatureInvalid
73+
}
74+
75+
// test signature using header
76+
if signature != "" && !hmac.Validate(sha256.New, data, []byte(key), signature) {
6277
return hook, scm.ErrSignatureInvalid
6378
}
6479

scm/driver/gitea/webhook_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func TestWebhookInvalid(t *testing.T) {
192192
r, _ := http.NewRequest("GET", "/?secert=xxxxxxinvalidxxxxx", bytes.NewBuffer(f))
193193
r.Header.Set("X-Gitea-Event", "pull_request")
194194
r.Header.Set("X-Gitea-Delivery", "ee8d97b4-1479-43f1-9cac-fbbd1b80da55")
195+
r.Header.Set("X-Gitea-Signature", "failfailfailfail")
195196

196197
s := new(webhookService)
197198
_, err := s.Parse(r, secretFunc)
@@ -205,6 +206,7 @@ func TestWebhook_Validated(t *testing.T) {
205206
r, _ := http.NewRequest("GET", "/?secret=71295b197fa25f4356d2fb9965df3f2379d903d7", bytes.NewBuffer(f))
206207
r.Header.Set("X-Gitea-Event", "pull_request")
207208
r.Header.Set("X-Gitea-Delivery", "ee8d97b4-1479-43f1-9cac-fbbd1b80da55")
209+
r.Header.Set("X-Gitea-Signature", "a31111f057bafe895837f4a93c0f1f528919c199a20438b1fc8e23485780a33a")
208210

209211
s := new(webhookService)
210212
_, err := s.Parse(r, secretFunc)

0 commit comments

Comments
 (0)