From b9b3dfeb6d99af9ac14a566776eab6eb504580c4 Mon Sep 17 00:00:00 2001 From: Germano Eichenberg Date: Fri, 8 Mar 2024 15:31:07 -0300 Subject: [PATCH] Merge dev into main Co-authored-by: Miles Moonlove --- lib/bucketpath.go | 34 ++++++++++++++++++++++++++++++++++ lib/bucketpath_test.go | 2 ++ 2 files changed, 36 insertions(+) diff --git a/lib/bucketpath.go b/lib/bucketpath.go index 527aeef..9a0062c 100644 --- a/lib/bucketpath.go +++ b/lib/bucketpath.go @@ -1,6 +1,7 @@ package lib import ( + "encoding/base64" "strings" "time" "unicode/utf8" @@ -109,6 +110,11 @@ func GetOptimisticBucketPath(url string, method string) string { return "/" + MajorGuilds + "/!/channels" } fallthrough + case MajorInteractions: + if numParts == 4 && parts[3] == "callback" { + return "/" + MajorInteractions + "/" + parts[1] + "/!/callback" + } + fallthrough case MajorWebhooks: fallthrough default: @@ -151,6 +157,34 @@ func GetOptimisticBucketPath(url string, method string) string { break } + // Strip webhook tokens, or extract interaction ID + if len(part) >= 64 { + // aW50ZXJhY3Rpb246 is base64 for "interaction:" + if !strings.HasPrefix(part, "aW50ZXJhY3Rpb246") { + bucket.WriteString("/!") + continue + } + + var interactionId string + + // fix padding + if i := len(part) % 4; i != 0 { + part += strings.Repeat("=", 4-i) + } + + decodedPart, err := base64.StdEncoding.DecodeString(part) + if err != nil { + interactionId = "Unknown" + } else { + interactionId = strings.Split(string(decodedPart), ":")[1] + } + + bucket.WriteByte('/') + bucket.WriteString(interactionId) + continue + } + + // Strip webhook tokens and interaction tokens if (currMajor == MajorWebhooks || currMajor == MajorInteractions) && len(part) >= 64 { bucket.WriteString("/!") diff --git a/lib/bucketpath_test.go b/lib/bucketpath_test.go index 04a1d0f..dee751d 100644 --- a/lib/bucketpath_test.go +++ b/lib/bucketpath_test.go @@ -27,6 +27,8 @@ func TestPaths(t *testing.T) { {"/api/v9/invites/dyno", "GET", "/invites/!"}, // Interactions major {"/api/v9/interactions/203039963636301824/aW50ZXJhY3Rpb246ODg3NTU5MDA01AY4NTUxNDU0OnZwS3QycDhvREk2aVF3U1BqN2prcXBkRmNqNlp4VEhGRjZvSVlXSGh4WG4yb3l6Z3B6NTBPNVc3OHphV05OULLMOHBMa2RTZmVKd3lzVDA2b2h3OTUxaFJ4QlN0dkxXallPcmhnSHNJb0tSV0M5ZzY1NkN4VGRvemFOSHY4b05c/callback", "GET", "/interactions/203039963636301824/!/callback"}, + // Interaction followup webhooks + {"/api/v10/webhooks/203039963636301824/aW50ZXJhY3Rpb246MTEwMzA0OTQyMDkzMDU2ODMyMjpOZUllWHdNU2J4RXBFMHVYRjBpU0pHMDdEb3BhM3ZlYklBODlMUmtlUXlRbzlpZzYyTnpLU0dqdWlyVlBvZnBSUlJHbUJHYlJ0N29MbE9KQUJVTFk4bTR4UzFtZEpEeXJyY0hBUERmTEhKVE9wRkNzU1FFWUkwTnlpWFY2WHdrRg/messages/@original", "POST", "/webhooks/203039963636301824/1103049420930568322/messages/@original"}, // No known major {"/api/v9/invalid/203039963636301824", "GET", "/invalid/203039963636301824"}, {"/api/v9/invalid/203039963636301824/route/203039963636301824", "GET", "/invalid/203039963636301824/route/!"},