diff --git a/weed/notification/webhook/types.go b/weed/notification/webhook/types.go index 6309bb7009..f46f131c79 100644 --- a/weed/notification/webhook/types.go +++ b/weed/notification/webhook/types.go @@ -50,7 +50,7 @@ func newWebhookMessage(key string, message proto.Message) *webhookMessage { return &webhookMessage{ Key: key, EventType: eventType, - Notification: notification, + Notification: filer_pb.ScrubEventCipherKeys(notification), } } diff --git a/weed/notification/webhook/webhook_queue_test.go b/weed/notification/webhook/webhook_queue_test.go index ba75ecb147..75b1f1fc8a 100644 --- a/weed/notification/webhook/webhook_queue_test.go +++ b/weed/notification/webhook/webhook_queue_test.go @@ -1,6 +1,8 @@ package webhook import ( + "encoding/base64" + "io" "net/http" "net/http/httptest" "strings" @@ -413,6 +415,50 @@ func TestQueueNoDuplicateWebhooks(t *testing.T) { } } +func TestQueueSendsNoCipherKeys(t *testing.T) { + bodies := make(chan string, 1) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + bodies <- string(body) + w.WriteHeader(http.StatusOK) + })) + defer server.Close() + + q := newQueueForTest(t, &config{ + endpoint: server.URL, + timeoutSeconds: 5, + backoffSeconds: 1, + maxBackoffSeconds: 1, + nWorkers: 1, + bufferSize: 10, + }) + defer closeQueueForTest(q) + + key := []byte("0123456789abcdef0123456789abcdef") + msg := &filer_pb.EventNotification{ + OldEntry: &filer_pb.Entry{Name: "f", Chunks: []*filer_pb.FileChunk{{FileId: "1,aa", CipherKey: key}}}, + NewEntry: &filer_pb.Entry{Name: "f", Chunks: []*filer_pb.FileChunk{{FileId: "2,bb", CipherKey: key}}}, + } + if err := q.SendMessage("/dir/f", msg); err != nil { + t.Fatalf("SendMessage() error = %v", err) + } + + select { + case body := <-bodies: + if !strings.Contains(body, "1,aa") || !strings.Contains(body, "2,bb") { + t.Fatalf("webhook body lacks the chunks: %s", body) + } + if strings.Contains(body, "cipher_key") || strings.Contains(body, base64.StdEncoding.EncodeToString(key)) { + t.Fatalf("webhook body carries a cipher key: %s", body) + } + case <-time.After(5 * time.Second): + t.Fatal("webhook was not delivered") + } + if len(msg.OldEntry.Chunks[0].CipherKey) == 0 || len(msg.NewEntry.Chunks[0].CipherKey) == 0 { + t.Fatal("sending mutated the caller's event") + } +} + func TestQueueSendMessageWithFilter(t *testing.T) { tests := []struct { name string diff --git a/weed/pb/filer_pb/entry_scrub.go b/weed/pb/filer_pb/entry_scrub.go index fcdf6af6c4..db1c712bb0 100644 --- a/weed/pb/filer_pb/entry_scrub.go +++ b/weed/pb/filer_pb/entry_scrub.go @@ -9,10 +9,25 @@ func ScrubCipherKeys(entry *Entry) *Entry { return nil } clone := proto.Clone(entry).(*Entry) - for _, chunk := range clone.Chunks { + clearCipherKeys(clone) + return clone +} + +// ScrubEventCipherKeys is ScrubCipherKeys for both entries of an event. +func ScrubEventCipherKeys(event *EventNotification) *EventNotification { + if event == nil { + return nil + } + clone := proto.Clone(event).(*EventNotification) + clearCipherKeys(clone.OldEntry) + clearCipherKeys(clone.NewEntry) + return clone +} + +func clearCipherKeys(entry *Entry) { + for _, chunk := range entry.GetChunks() { if chunk != nil { chunk.CipherKey = nil } } - return clone } diff --git a/weed/pb/filer_pb/entry_scrub_test.go b/weed/pb/filer_pb/entry_scrub_test.go index ac3a305a7b..37eb8ae734 100644 --- a/weed/pb/filer_pb/entry_scrub_test.go +++ b/weed/pb/filer_pb/entry_scrub_test.go @@ -32,3 +32,33 @@ func TestScrubCipherKeys(t *testing.T) { t.Fatal("nil entry should scrub to nil") } } + +func TestScrubEventCipherKeys(t *testing.T) { + key := []byte("0123456789abcdef0123456789abcdef") + event := &EventNotification{ + OldEntry: &Entry{Name: "a", Chunks: []*FileChunk{{FileId: "1,aaa", CipherKey: key}}}, + NewEntry: &Entry{Name: "b", Chunks: []*FileChunk{{FileId: "2,bbb", CipherKey: key}}}, + NewParentPath: "/dir", + Signatures: []int32{7}, + } + + scrubbed := ScrubEventCipherKeys(event) + + for _, entry := range []*Entry{scrubbed.OldEntry, scrubbed.NewEntry} { + if len(entry.Chunks[0].CipherKey) != 0 { + t.Fatalf("%s still carries a cipher key", entry.Name) + } + } + if scrubbed.NewParentPath != "/dir" || len(scrubbed.Signatures) != 1 || scrubbed.NewEntry.Chunks[0].FileId != "2,bbb" { + t.Fatalf("scrubbing dropped event fields: %v", scrubbed) + } + if len(event.OldEntry.Chunks[0].CipherKey) != len(key) || len(event.NewEntry.Chunks[0].CipherKey) != len(key) { + t.Fatal("scrubbing mutated the original event") + } + if ScrubEventCipherKeys(nil) != nil { + t.Fatal("nil event should scrub to nil") + } + if got := ScrubEventCipherKeys(&EventNotification{}); got.OldEntry != nil || got.NewEntry != nil { + t.Fatal("event without entries gained entries") + } +}