Skip to content

armgit / ente

Public Imported from GitHub
0

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

Keep custom-domain alert fix narrowly scoped

Neeraj Gupta8c7d95888068
diff --git a/server/pkg/controller/discord/discord.go b/server/pkg/controller/discord/discord.go
index 975a058453..a8511a34a0 100644
--- a/server/pkg/controller/discord/discord.go
+++ b/server/pkg/controller/discord/discord.go
@@ -1,7 +1,6 @@
package discord
import (
- "crypto/sha256"
"fmt"
"sync"
"time"
@@ -13,15 +12,13 @@ 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[[sha256.Size]byte]time.Time
+ lastSent map[string]time.Time
mu sync.Mutex
}
@@ -32,7 +29,7 @@ func NewDiscordController(userRepo *repo.UserRepository, hostName string, enviro
HostName: hostName,
Environment: environment,
UserRepo: userRepo,
- lastSent: make(map[[sha256.Size]byte]time.Time),
+ lastSent: make(map[string]time.Time),
}
}
@@ -76,25 +73,11 @@ func (c *DiscordController) NotifyThrottled(message string, window time.Duration
defer c.mu.Unlock()
now := time.Now()
- digest := sha256.Sum256([]byte(message))
- lastTime, exists := c.lastSent[digest]
- if exists && now.Sub(lastTime) < window {
+ if lastTime, exists := c.lastSent[message]; exists && now.Sub(lastTime) < window {
log.Infof("Skipping duplicate Discord notification: %s", message)
return
}
- 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.lastSent[message] = now
c.Notify(message)
}
diff --git a/server/pkg/controller/discord/discord_test.go b/server/pkg/controller/discord/discord_test.go
deleted file mode 100644
index 8ed35cd24f..0000000000
--- a/server/pkg/controller/discord/discord_test.go
+++ /dev/null
@@ -1,96 +0,0 @@
-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
-}