-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add support for X-Hub-Signature headers in webhooks #8473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
97f1779
3ebf01f
ee4bc10
4309549
f8bb7f4
d0dc83b
5034858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-xorm/xorm" | ||
) | ||
|
||
func specifyWebhookSignatureType(x *xorm.Engine) error { | ||
var err error | ||
|
||
switch x.Dialect().DriverName() { | ||
case "mysql": | ||
_, err = x.Exec("ALTER TABLE webhook CHANGE COLUMN signature signature_sha1 text") | ||
case "postgres": | ||
_, err = x.Exec("ALTER TABLE webhook RENAME COLUMN signature TO signature_sha1") | ||
case "mssql": | ||
_, err = x.Exec("sp_rename @objname = 'webhook.signature', @newname = 'signature_sha1', @objtype = 'COLUMN'") | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error renaming webhook signature column to signature_sha1: %v", err) | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ package models | |||||
|
||||||
import ( | ||||||
"crypto/hmac" | ||||||
"crypto/sha1" | ||||||
"crypto/sha256" | ||||||
"crypto/tls" | ||||||
"encoding/hex" | ||||||
|
@@ -106,21 +107,22 @@ const ( | |||||
|
||||||
// Webhook represents a web hook object. | ||||||
type Webhook struct { | ||||||
ID int64 `xorm:"pk autoincr"` | ||||||
RepoID int64 `xorm:"INDEX"` | ||||||
OrgID int64 `xorm:"INDEX"` | ||||||
URL string `xorm:"url TEXT"` | ||||||
Signature string `xorm:"TEXT"` | ||||||
HTTPMethod string `xorm:"http_method"` | ||||||
ContentType HookContentType | ||||||
Secret string `xorm:"TEXT"` | ||||||
Events string `xorm:"TEXT"` | ||||||
*HookEvent `xorm:"-"` | ||||||
IsSSL bool `xorm:"is_ssl"` | ||||||
IsActive bool `xorm:"INDEX"` | ||||||
HookTaskType HookTaskType | ||||||
Meta string `xorm:"TEXT"` // store hook-specific attributes | ||||||
LastStatus HookStatus // Last delivery status | ||||||
ID int64 `xorm:"pk autoincr"` | ||||||
RepoID int64 `xorm:"INDEX"` | ||||||
OrgID int64 `xorm:"INDEX"` | ||||||
URL string `xorm:"url TEXT"` | ||||||
SignatureSha1 string `xorm:"TEXT"` | ||||||
SignatureSha256 string `xorm:"TEXT"` | ||||||
HTTPMethod string `xorm:"http_method"` | ||||||
ContentType HookContentType | ||||||
Secret string `xorm:"TEXT"` | ||||||
Events string `xorm:"TEXT"` | ||||||
*HookEvent `xorm:"-"` | ||||||
IsSSL bool `xorm:"is_ssl"` | ||||||
IsActive bool `xorm:"INDEX"` | ||||||
HookTaskType HookTaskType | ||||||
Meta string `xorm:"TEXT"` // store hook-specific attributes | ||||||
LastStatus HookStatus // Last delivery status | ||||||
|
||||||
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` | ||||||
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` | ||||||
|
@@ -572,7 +574,8 @@ type HookTask struct { | |||||
UUID string | ||||||
Type HookTaskType | ||||||
URL string `xorm:"TEXT"` | ||||||
Signature string `xorm:"TEXT"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a database migration
This comment was marked as outdated.
Sorry, something went wrong. |
||||||
SignatureSha1 string `xorm:"TEXT"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
SignatureSha256 string `xorm:"TEXT"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There still needs to be fields:
|
||||||
api.Payloader `xorm:"-"` | ||||||
PayloadContent string `xorm:"TEXT"` | ||||||
HTTPMethod string `xorm:"http_method"` | ||||||
|
@@ -740,7 +743,21 @@ func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType, | |||||
payloader = p | ||||||
} | ||||||
|
||||||
var signature string | ||||||
var signatureSha1 string | ||||||
if len(w.Secret) > 0 { | ||||||
data, err := payloader.JSONPayload() | ||||||
if err != nil { | ||||||
log.Error("prepareWebhooks.JSONPayload: %v", err) | ||||||
} | ||||||
sig := hmac.New(sha1.New, []byte(w.Secret)) | ||||||
_, err = sig.Write(data) | ||||||
if err != nil { | ||||||
log.Error("prepareWebhooks.sigWrite: %v", err) | ||||||
} | ||||||
signatureSha1 = hex.EncodeToString(sig.Sum(nil)) | ||||||
} | ||||||
|
||||||
var signatureSha256 string | ||||||
if len(w.Secret) > 0 { | ||||||
data, err := payloader.JSONPayload() | ||||||
if err != nil { | ||||||
|
@@ -751,20 +768,21 @@ func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType, | |||||
if err != nil { | ||||||
log.Error("prepareWebhooks.sigWrite: %v", err) | ||||||
} | ||||||
signature = hex.EncodeToString(sig.Sum(nil)) | ||||||
signatureSha256 = hex.EncodeToString(sig.Sum(nil)) | ||||||
} | ||||||
|
||||||
if err = createHookTask(e, &HookTask{ | ||||||
RepoID: repo.ID, | ||||||
HookID: w.ID, | ||||||
Type: w.HookTaskType, | ||||||
URL: w.URL, | ||||||
Signature: signature, | ||||||
Payloader: payloader, | ||||||
HTTPMethod: w.HTTPMethod, | ||||||
ContentType: w.ContentType, | ||||||
EventType: event, | ||||||
IsSSL: w.IsSSL, | ||||||
RepoID: repo.ID, | ||||||
HookID: w.ID, | ||||||
Type: w.HookTaskType, | ||||||
URL: w.URL, | ||||||
SignatureSha1: signatureSha1, | ||||||
SignatureSha256: signatureSha256, | ||||||
Payloader: payloader, | ||||||
HTTPMethod: w.HTTPMethod, | ||||||
ContentType: w.ContentType, | ||||||
EventType: event, | ||||||
IsSSL: w.IsSSL, | ||||||
}); err != nil { | ||||||
return fmt.Errorf("CreateHookTask: %v", err) | ||||||
} | ||||||
|
@@ -852,10 +870,11 @@ func (t *HookTask) deliver() error { | |||||
|
||||||
req.Header.Add("X-Gitea-Delivery", t.UUID) | ||||||
req.Header.Add("X-Gitea-Event", string(t.EventType)) | ||||||
req.Header.Add("X-Gitea-Signature", t.Signature) | ||||||
req.Header.Add("X-Gitea-Signature", t.SignatureSha256) | ||||||
req.Header.Add("X-Gogs-Delivery", t.UUID) | ||||||
req.Header.Add("X-Gogs-Event", string(t.EventType)) | ||||||
req.Header.Add("X-Gogs-Signature", t.Signature) | ||||||
req.Header.Add("X-Gogs-Signature", t.SignatureSha256) | ||||||
req.Header.Add("X-Hub-Signature", fmt.Sprintf("sha1=%v", t.SignatureSha1)) | ||||||
req.Header["X-GitHub-Delivery"] = []string{t.UUID} | ||||||
req.Header["X-GitHub-Event"] = []string{string(t.EventType)} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things. 1. Rename columns is tricky (for example SQLite also needs to be added to this migration) so perhaps instead of a rename we keep signature column with same content we just have the strict field be renamed, and then add the new sha column. 2. Could you add the adding of the new column to this migration, while it is added by the default migration,sometimes we’ve been bitten by changing a column that doesn’t yet exist so usually we request explicit add of new columns.
Apologies if I am unclear, I am on mobile and can give better details when I get to a computer. Please ask any questions.