diff options
author | S7evinK <tfaelligen@gmail.com> | 2020-07-31 00:04:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-30 18:04:19 -0400 |
commit | bf6014644401dd3fdf9031670b3a00ccd866f49f (patch) | |
tree | d9e62ccb8f385aca9207c33e6a8484f9ee5ef170 /modules | |
parent | f6d5303e022a1d11bdf16279bb82aabd7c48427e (diff) | |
download | gitea-bf6014644401dd3fdf9031670b3a00ccd866f49f.tar.gz gitea-bf6014644401dd3fdf9031670b3a00ccd866f49f.zip |
Don't use legacy method to send Matrix Webhook (#12348)
* Don't use legacy send for messages
* Add migrations to ensure Matrix webhooks use PUT
* Set HTTP method to PUT as default
* Fix sql condition..
Signed-off-by: Till Faelligen <tfaelligen@gmail.com>
* Rename getTxnID -> getMatrixTxnID
* Use local variable instead of constant value
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r-- | modules/webhook/deliver.go | 17 | ||||
-rw-r--r-- | modules/webhook/matrix.go | 21 | ||||
-rw-r--r-- | modules/webhook/matrix_test.go | 29 |
3 files changed, 59 insertions, 8 deletions
diff --git a/modules/webhook/deliver.go b/modules/webhook/deliver.go index 7b0c651733..c29fcb6fa9 100644 --- a/modules/webhook/deliver.go +++ b/modules/webhook/deliver.go @@ -77,17 +77,20 @@ func Deliver(t *models.HookTask) error { if err != nil { return err } + case http.MethodPut: + switch t.Type { + case models.MATRIX: + req, err = getMatrixHookRequest(t) + if err != nil { + return err + } + default: + return fmt.Errorf("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod) + } default: return fmt.Errorf("Invalid http method for webhook: [%d] %v", t.ID, t.HTTPMethod) } - if t.Type == models.MATRIX { - req, err = getMatrixHookRequest(t) - if err != nil { - return err - } - } - req.Header.Add("X-Gitea-Delivery", t.UUID) req.Header.Add("X-Gitea-Event", t.EventType.Event()) req.Header.Add("X-Gitea-Signature", t.Signature) diff --git a/modules/webhook/matrix.go b/modules/webhook/matrix.go index 68c65623f7..d6309000a8 100644 --- a/modules/webhook/matrix.go +++ b/modules/webhook/matrix.go @@ -5,6 +5,7 @@ package webhook import ( + "crypto/sha1" "encoding/json" "errors" "fmt" @@ -291,7 +292,14 @@ func getMatrixHookRequest(t *models.HookTask) (*http.Request, error) { } t.PayloadContent = string(payload) - req, err := http.NewRequest("POST", t.URL, strings.NewReader(string(payload))) + txnID, err := getMatrixTxnID(payload) + if err != nil { + return nil, fmt.Errorf("getMatrixHookRequest: unable to hash payload: %+v", err) + } + + t.URL = fmt.Sprintf("%s/%s", t.URL, txnID) + + req, err := http.NewRequest(t.HTTPMethod, t.URL, strings.NewReader(string(payload))) if err != nil { return nil, err } @@ -301,3 +309,14 @@ func getMatrixHookRequest(t *models.HookTask) (*http.Request, error) { return req, nil } + +// getMatrixTxnID creates a txnID based on the payload to ensure idempotency +func getMatrixTxnID(payload []byte) (string, error) { + h := sha1.New() + _, err := h.Write(payload) + if err != nil { + return "", err + } + + return fmt.Sprintf("%x", h.Sum(nil)), nil +} diff --git a/modules/webhook/matrix_test.go b/modules/webhook/matrix_test.go index 4e8b878ad4..3d1c660126 100644 --- a/modules/webhook/matrix_test.go +++ b/modules/webhook/matrix_test.go @@ -154,3 +154,32 @@ func TestMatrixHookRequest(t *testing.T) { assert.Equal(t, "Bearer dummy_access_token", req.Header.Get("Authorization")) assert.Equal(t, wantPayloadContent, h.PayloadContent) } + +func Test_getTxnID(t *testing.T) { + type args struct { + payload []byte + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + name: "dummy payload", + args: args{payload: []byte("Hello World")}, + want: "0a4d55a8d778e5022fab701977c5d840bbc486d0", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := getMatrixTxnID(tt.args.payload) + if (err != nil) != tt.wantErr { + t.Errorf("getMatrixTxnID() error = %v, wantErr %v", err, tt.wantErr) + return + } + assert.Equal(t, tt.want, got) + }) + } +} |