mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-11-29 08:55:57 +00:00
Keep attachment files in their temporary directory
An attachment's filename was joined to its temporary directory as given, so a name with path separators or ".." pointed somewhere else. Reject such names before creating the file. Also remove the temporary files when the JSON-RPC client is missing, and the directory when the file cannot be created; both returns left them behind. Assisted-by: Claude Opus 5.5 (cherry picked from commit abfbf6ec85e23f966d089133860b31832f8d7de9)
This commit is contained in:
1 parent
044282717d
commit
893be5e3fc
3 files changed
+74
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user