summaryrefslogtreecommitdiffstats
path: root/modules/webhook/matrix.go
diff options
context:
space:
mode:
authorS7evinK <tfaelligen@gmail.com>2020-07-31 00:04:19 +0200
committerGitHub <noreply@github.com>2020-07-30 18:04:19 -0400
commitbf6014644401dd3fdf9031670b3a00ccd866f49f (patch)
treed9e62ccb8f385aca9207c33e6a8484f9ee5ef170 /modules/webhook/matrix.go
parentf6d5303e022a1d11bdf16279bb82aabd7c48427e (diff)
downloadgitea-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/webhook/matrix.go')
-rw-r--r--modules/webhook/matrix.go21
1 files changed, 20 insertions, 1 deletions
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
+}