diff --git a/src/client/attachment.go b/src/client/attachment.go index 5688506..a75e737 100644 --- a/src/client/attachment.go +++ b/src/client/attachment.go @@ -86,6 +86,13 @@ func (attachmentEntry *AttachmentEntry) storeBase64AsTemporaryFile() error { attachmentEntry.FileName = fileNameUuid.String() + mimeType.Extension() } + // The filename comes from the caller and is joined to a directory below, + // and cleanUp removes the result, so it must name one entry in that directory. + name := attachmentEntry.FileName + if name == "." || name == ".." || strings.ContainsAny(name, "/\\\x00") { + return errors.New("The attachment filename must be a single file name.") + } + dirNameUuid, err := uuid.NewV4() if err != nil { return err @@ -101,6 +108,7 @@ func (attachmentEntry *AttachmentEntry) storeBase64AsTemporaryFile() error { f, err := os.Create(attachmentEntry.FilePath) if err != nil { + attachmentEntry.cleanUp() return err } defer f.Close() diff --git a/src/client/attachment_test.go b/src/client/attachment_test.go index 1482ad2..cc80970 100644 --- a/src/client/attachment_test.go +++ b/src/client/attachment_test.go @@ -3,8 +3,11 @@ package client import ( "flag" "os" + "path/filepath" "strings" "testing" + + ds "github.com/bbernhard/signal-cli-rest-api/datastructs" ) func Test_Attachment_ExtractMetadata_ShouldPrepareDataFor_toDataForSignal(t *testing.T) { @@ -108,3 +111,65 @@ func Test_Attachment_ExtractMetadata_ShouldPrepareDataFor_toDataForSignal(t *tes }) } } + +func Test_Attachment_StoreBase64AsTemporaryFile_RejectsFileNamesWithPaths(t *testing.T) { + for _, name := range []string{".", "..", "../../outside", "/outside", "dir/file", `dir\file`, "file\x00name"} { + t.Run(name, func(t *testing.T) { + parent := t.TempDir() + tmpDir := filepath.Join(parent, "tmp") + string(os.PathSeparator) + if err := os.Mkdir(tmpDir, 0o700); err != nil { + t.Fatal(err) + } + outside := filepath.Join(parent, "outside") + if err := os.WriteFile(outside, []byte("original"), 0o600); err != nil { + t.Fatal(err) + } + + attachmentEntry := &AttachmentEntry{Base64: "aGVsbG8=", FileName: name, attachmentTmpDir: tmpDir} + if err := attachmentEntry.storeBase64AsTemporaryFile(); err == nil { + t.Errorf("storeBase64AsTemporaryFile accepted filename %q", name) + } + attachmentEntry.cleanUp() + + entries, err := os.ReadDir(tmpDir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Errorf("attachment tmp dir has entries %v", entries) + } + content, err := os.ReadFile(outside) + if err != nil || string(content) != "original" { + t.Errorf("file next to the tmp dir got %q, %v; want \"original\"", content, err) + } + }) + } +} + +func Test_Send_RemovesAttachmentsWhenJsonRpcClientIsMissing(t *testing.T) { + tmpDir := t.TempDir() + string(os.PathSeparator) + signalClient := &SignalClient{ + attachmentTmpDir: tmpDir, + signalCliMode: JsonRpc, + jsonRpc2Clients: map[string]*JsonRpc2Client{}, + } + + _, err := signalClient.send(ds.SignalCliSendRequest{ + Number: "+4915100000000", + Recipients: []string{"+4915100000001"}, + RecipientType: ds.Number, + Message: "hello", + Base64Attachments: []string{"data:text/plain;filename=hello.txt;base64,aGVsbG8="}, + }) + if err == nil { + t.Fatal("send succeeded without a JSON-RPC client") + } + + entries, err := os.ReadDir(tmpDir) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Errorf("attachment tmp dir has entries %v after the failed send", entries) + } +} diff --git a/src/client/client.go b/src/client/client.go index 03f69f8..f97bea9 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -536,6 +536,7 @@ func (s *SignalClient) send(signalCliSendRequest ds.SignalCliSendRequest) (*ds.S if s.signalCliMode == JsonRpc { jsonRpc2Client, err := s.getJsonRpc2Client() if err != nil { + cleanupAttachmentEntries(attachmentEntries, linkPreviewAttachmentEntry) return nil, err }