Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/commands/attachment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"path/filepath"
"regexp"
"strconv"

Expand Down Expand Up @@ -112,7 +113,8 @@ Use 'fizzy card attachments show CARD_NUMBER' to see available attachments and t
// Download the files
var results []map[string]interface{}
for _, attachment := range toDownload {
outputPath := attachment.Filename
// Sanitize filename to prevent path traversal attacks
outputPath := filepath.Base(attachment.Filename)
// If downloading single file with custom output name
if len(toDownload) == 1 && attachmentDownloadOutput != "" {
outputPath = attachmentDownloadOutput
Expand Down
81 changes: 81 additions & 0 deletions internal/commands/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,84 @@ func stringContains(s, substr string) bool {
}
return false
}

func TestAttachmentDownloadSanitizesFilename(t *testing.T) {
tests := []struct {
name string
maliciousFilename string
expectedOutputPath string
}{
{
name: "path traversal with ../",
maliciousFilename: "../../../etc/passwd",
expectedOutputPath: "passwd",
},
{
name: "path traversal with subdirectory",
maliciousFilename: "../../.bashrc",
expectedOutputPath: ".bashrc",
},
{
name: "absolute path attempt",
maliciousFilename: "/etc/shadow",
expectedOutputPath: "shadow",
},
{
name: "normal filename unchanged",
maliciousFilename: "safe-file.png",
expectedOutputPath: "safe-file.png",
},
{
name: "filename with spaces",
maliciousFilename: "my document.pdf",
expectedOutputPath: "my document.pdf",
},
{
name: "deeply nested traversal",
maliciousFilename: "../../../../../../../../tmp/malware.sh",
expectedOutputPath: "malware.sh",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cardData := map[string]interface{}{
"id": "card-id",
"number": 241,
"description_html": `<action-text-attachment sgid="test-sgid" content-type="image/png" filename="` + tt.maliciousFilename + `" filesize="1000">
<a href="/blobs/blob/file?disposition=attachment">Download</a>
</action-text-attachment>`,
}

mock := NewMockClient().WithGetData(cardData)
result := SetTestMode(mock)
SetTestConfig("test-token", "test-account", "https://api.test.com")
defer ResetTestMode()

rootCmd.SetArgs([]string{"card", "attachments", "download", "241", "1"})

RunTestCommand(func() {
_ = rootCmd.Execute()
})

if result.Response == nil {
t.Fatal("expected response, got nil")
}

if !result.Response.Success {
t.Errorf("expected success, got error: %v", result.Response)
return
}

// Verify the output path was sanitized
if len(mock.DownloadFileCalls) != 1 {
t.Fatalf("expected 1 download call, got %d", len(mock.DownloadFileCalls))
}

actualOutputPath := mock.DownloadFileCalls[0].DestPath
if actualOutputPath != tt.expectedOutputPath {
t.Errorf("expected sanitized output path %q, got %q", tt.expectedOutputPath, actualOutputPath)
}
})
}
}