Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Bound Discord alert throttle state

Neeraj Guptafe458a276770
diff --git a/server/pkg/controller/discord/discord.go b/server/pkg/controller/discord/discord.go
index a8511a34a0..975a058453 100644
--- a/server/pkg/controller/discord/discord.go
+++ b/server/pkg/controller/discord/discord.go
@@ -1,6 +1,7 @@
package discord
import (
+ "crypto/sha256"
"fmt"
"sync"
"time"
@@ -12,13 +13,15 @@ import (
"github.com/spf13/viper"
)
+const maxThrottleEntries = 1024
+
type DiscordController struct {
MonaLisa *discordgo.Session
ChaChing *discordgo.Session
HostName string
Environment string
UserRepo *repo.UserRepository
- lastSent map[string]time.Time
+ lastSent map[[sha256.Size]byte]time.Time
mu sync.Mutex
}
@@ -29,7 +32,7 @@ func NewDiscordController(userRepo *repo.UserRepository, hostName string, enviro
HostName: hostName,
Environment: environment,
UserRepo: userRepo,
- lastSent: make(map[string]time.Time),
+ lastSent: make(map[[sha256.Size]byte]time.Time),
}
}
@@ -73,11 +76,25 @@ func (c *DiscordController) NotifyThrottled(message string, window time.Duration
defer c.mu.Unlock()
now := time.Now()
- if lastTime, exists := c.lastSent[message]; exists && now.Sub(lastTime) < window {
+ digest := sha256.Sum256([]byte(message))
+ lastTime, exists := c.lastSent[digest]
+ if exists && now.Sub(lastTime) < window {
log.Infof("Skipping duplicate Discord notification: %s", message)
return
}
- c.lastSent[message] = now
+ if !exists && len(c.lastSent) >= maxThrottleEntries {
+ // Keep new alerts visible; an evicted alert may repeat before its window ends.
+ var oldestKey [sha256.Size]byte
+ var oldestTime time.Time
+ for candidateKey, lastTime := range c.lastSent {
+ if oldestTime.IsZero() || lastTime.Before(oldestTime) {
+ oldestKey = candidateKey
+ oldestTime = lastTime
+ }
+ }
+ delete(c.lastSent, oldestKey)
+ }
+ c.lastSent[digest] = now
c.Notify(message)
}
diff --git a/server/pkg/controller/discord/discord_test.go b/server/pkg/controller/discord/discord_test.go
new file mode 100644
index 0000000000..8ed35cd24f
--- /dev/null
+++ b/server/pkg/controller/discord/discord_test.go
@@ -0,0 +1,96 @@
+package discord
+
+import (
+ "crypto/sha256"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "strings"
+ "sync"
+ "testing"
+ "time"
+
+ "github.com/bwmarrin/discordgo"
+ "github.com/stretchr/testify/require"
+)
+
+func TestPotentialAbuseThrottle(t *testing.T) {
+ c, sent := recordingController(t)
+ const mismatch = "custom domain - domainMismatch"
+ const invalidDomain = "custom domain - domainToASCIIFailed"
+ c.NotifyPotentialAbuse(mismatch)
+
+ var wg sync.WaitGroup
+ for range 32 {
+ wg.Go(func() { c.NotifyPotentialAbuse(mismatch) })
+ }
+ wg.Wait()
+ require.Equal(t, []string{"host: " + mismatch}, sent.messages)
+
+ c.NotifyPotentialAbuse(invalidDomain)
+ require.Equal(t, []string{"host: " + mismatch, "host: " + invalidDomain}, sent.messages)
+
+ key := sha256.Sum256([]byte("host: " + mismatch))
+ c.lastSent[key] = time.Now().Add(-2 * time.Minute)
+ c.NotifyPotentialAbuse(mismatch)
+ require.Equal(t, []string{"host: " + mismatch, "host: " + invalidDomain, "host: " + mismatch}, sent.messages)
+}
+
+func TestNotifyThrottledBoundsCache(t *testing.T) {
+ c, sent := recordingController(t)
+ base := time.Now().Add(-30 * time.Second)
+ for i := range maxThrottleEntries {
+ key := sha256.Sum256([]byte(fmt.Sprintf("message %d", i)))
+ c.lastSent[key] = base.Add(time.Duration(i) * time.Millisecond)
+ }
+
+ c.NotifyThrottled("new message", time.Minute)
+ c.NotifyThrottled("new message", time.Minute)
+ require.Equal(t, []string{"new message"}, sent.messages)
+ require.Len(t, c.lastSent, maxThrottleEntries)
+ require.NotContains(t, c.lastSent, sha256.Sum256([]byte("message 0")))
+ require.Contains(t, c.lastSent, sha256.Sum256([]byte("new message")))
+
+ // Eviction admits new alerts at the cost of allowing an early repeat.
+ c.NotifyThrottled("message 0", time.Minute)
+ require.Equal(t, []string{"new message", "message 0"}, sent.messages)
+ var wg sync.WaitGroup
+ for i := range 2 * maxThrottleEntries {
+ wg.Go(func() { c.NotifyThrottled(fmt.Sprintf("distinct message %d", i), time.Minute) })
+ }
+ wg.Wait()
+ require.Len(t, c.lastSent, maxThrottleEntries)
+ require.Len(t, sent.messages, 2+2*maxThrottleEntries)
+}
+
+// Capture Discord payloads without contacting Discord or adding production hooks.
+type recordingTransport struct {
+ messages []string
+}
+
+func (r *recordingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
+ var message discordgo.MessageSend
+ if err := json.NewDecoder(req.Body).Decode(&message); err != nil {
+ return nil, err
+ }
+ r.messages = append(r.messages, message.Content)
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: make(http.Header),
+ Body: io.NopCloser(strings.NewReader(`{}`)),
+ }, nil
+}
+
+func recordingController(t *testing.T) (*DiscordController, *recordingTransport) {
+ t.Helper()
+ bot, err := discordgo.New("")
+ require.NoError(t, err)
+ sent := &recordingTransport{}
+ bot.Client = &http.Client{Transport: sent}
+ return &DiscordController{
+ MonaLisa: bot,
+ HostName: "host",
+ lastSent: make(map[[sha256.Size]byte]time.Time),
+ }, sent
+}
diff --git a/server/pkg/middleware/collection_link.go b/server/pkg/middleware/collection_link.go
index 67e5c682d7..5d9fcee429 100644
--- a/server/pkg/middleware/collection_link.go
+++ b/server/pkg/middleware/collection_link.go
@@ -303,7 +303,7 @@ func (m *CollectionLinkMiddleware) validateOrigin(c *gin.Context, ownerID int64)
"req_id": reqId,
"origin": origin,
})
- alertMessage := fmt.Sprintf("custom domain %s", reqId)
+ alertMessage := "custom domain"
domain, err := m.RemoteStoreRepo.GetEffectiveDomain(c, ownerID)
if err != nil {
logger.WithError(err).Error("domainFetchFailed")