diff --git a/api/webhook_manager.go b/api/webhook_manager.go index 10c9ce9..b2ec197 100644 --- a/api/webhook_manager.go +++ b/api/webhook_manager.go @@ -10,6 +10,8 @@ import ( "gorm.io/gorm" "io" "net/http" + "regexp" + "strings" ) type WebhookManager struct { @@ -63,6 +65,8 @@ func (whm *WebhookManager) sendWebhooks(request interface{}, r *http.Request) { } payload := getWebhookData(r, whm.logger) + payload = toNormalizedJson(payload) + signature := generatePayloadSignature(payload, app.WebhookSecret) req.Header.Add("X-Hub-Signature-256", signature) @@ -86,5 +90,27 @@ func (whm *WebhookManager) sendWebhooks(request interface{}, r *http.Request) { } }(app) } - +} + +/* +* Ported from https://github.com/octokit/webhooks.js/blob/984f3f51e077dbc7637aa55406c3bb114dbb7f82/src/to-normalized-json-string.ts + */ + +func toNormalizedJson(payload []byte) []byte { + jsonString := string(payload) + + // Regex to find Unicode escape sequences + re := regexp.MustCompile(`\\u([\da-fA-F]{4})`) + + // Function to convert found sequences to uppercase + replaceFunc := func(match string) string { + parts := strings.Split(match, "\\u") + // Convert the Unicode sequence part to uppercase + return parts[0] + "\\u" + strings.ToUpper(parts[1]) + } + + // Replace the matches in the jsonString + normalizedString := re.ReplaceAllStringFunc(jsonString, replaceFunc) + + return []byte(normalizedString) }