ソースを参照

fix(integration): keep Discord and Slack webhook URLs out of debug logs

The webhook URL is the credential for both services, and it was logged
with every notification.
Frédéric Guillot 8 時間 前
親
コミット
6b1748a76d

+ 0 - 1
internal/integration/discord/discord.go

@@ -28,7 +28,6 @@ func NewClient(webhookURL string) *Client {
 func (c *Client) SendDiscordMsg(feed *model.Feed, entries model.Entries) error {
 	for _, entry := range entries {
 		slog.Debug("Sending Discord notification",
-			slog.String("webhookURL", c.webhookURL),
 			slog.String("title", feed.Title),
 			slog.String("entry_url", entry.URL),
 		)

+ 33 - 0
internal/integration/discord/discord_test.go

@@ -4,6 +4,8 @@
 package discord
 
 import (
+	"bytes"
+	"log/slog"
 	"net/http"
 	"net/http/httptest"
 	"strings"
@@ -74,6 +76,37 @@ func TestSendDiscordMsgErrorsDoNotContainWebhookURL(t *testing.T) {
 	}
 }
 
+func TestSendDiscordMsgDoesNotLogWebhookURL(t *testing.T) {
+	configureIntegrationAllowPrivateNetworksOption(t)
+
+	var logs bytes.Buffer
+	previousLogger := slog.Default()
+	slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
+	t.Cleanup(func() { slog.SetDefault(previousLogger) })
+
+	const secret = "api/webhooks/123/0123456789abcdef"
+
+	server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
+		writer.WriteHeader(http.StatusOK)
+	}))
+	defer server.Close()
+
+	feed := &model.Feed{ID: 1, Title: "Feed", SiteURL: "https://example.org/"}
+	entries := model.Entries{&model.Entry{ID: 1, Title: "Entry", URL: "https://example.org/entry"}}
+
+	if err := NewClient(server.URL+"/"+secret).SendDiscordMsg(feed, entries); err != nil {
+		t.Fatalf("expected no error, got %v", err)
+	}
+
+	if !strings.Contains(logs.String(), "Sending Discord notification") {
+		t.Fatalf("expected the notification to be logged, got:\n%s", logs.String())
+	}
+
+	if strings.Contains(logs.String(), secret) {
+		t.Errorf("expected the logs to not contain the secret, got:\n%s", logs.String())
+	}
+}
+
 func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
 	t.Helper()
 

+ 0 - 1
internal/integration/slack/slack.go

@@ -28,7 +28,6 @@ func NewClient(webhookURL string) *Client {
 func (c *Client) SendSlackMsg(feed *model.Feed, entries model.Entries) error {
 	for _, entry := range entries {
 		slog.Debug("Sending Slack notification",
-			slog.String("webhookURL", c.webhookURL),
 			slog.String("title", feed.Title),
 			slog.String("entry_url", entry.URL),
 		)

+ 33 - 0
internal/integration/slack/slack_test.go

@@ -4,6 +4,8 @@
 package slack
 
 import (
+	"bytes"
+	"log/slog"
 	"net/http"
 	"net/http/httptest"
 	"strings"
@@ -74,6 +76,37 @@ func TestSendSlackMsgErrorsDoNotContainWebhookURL(t *testing.T) {
 	}
 }
 
+func TestSendSlackMsgDoesNotLogWebhookURL(t *testing.T) {
+	configureIntegrationAllowPrivateNetworksOption(t)
+
+	var logs bytes.Buffer
+	previousLogger := slog.Default()
+	slog.SetDefault(slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug})))
+	t.Cleanup(func() { slog.SetDefault(previousLogger) })
+
+	const secret = "api/webhooks/123/0123456789abcdef"
+
+	server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
+		writer.WriteHeader(http.StatusOK)
+	}))
+	defer server.Close()
+
+	feed := &model.Feed{ID: 1, Title: "Feed", SiteURL: "https://example.org/"}
+	entries := model.Entries{&model.Entry{ID: 1, Title: "Entry", URL: "https://example.org/entry"}}
+
+	if err := NewClient(server.URL+"/"+secret).SendSlackMsg(feed, entries); err != nil {
+		t.Fatalf("expected no error, got %v", err)
+	}
+
+	if !strings.Contains(logs.String(), "Sending Slack notification") {
+		t.Fatalf("expected the notification to be logged, got:\n%s", logs.String())
+	}
+
+	if strings.Contains(logs.String(), secret) {
+		t.Errorf("expected the logs to not contain the secret, got:\n%s", logs.String())
+	}
+}
+
 func configureIntegrationAllowPrivateNetworksOption(t *testing.T) {
 	t.Helper()