Skip to content

Commit 0d7b1ef

Browse files
breken-aiclaude
authored andcommitted
fix: serve attachments with uppercase extensions
UploadPostAttachment stores an upload with a lowercased extension (Report.PDF is saved as files/post/<hash>.pdf) but keeps the original name in the download link. The download route rebuilt the local name from the link's extension as typed, so on a case-sensitive filesystem it looked for <hash>.PDF, did not find it, and redirected to /404. Lowercase the extension when resolving the local path. Generated-by: Claude Opus 5.5 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ea68794 commit 0d7b1ef

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎internal/router/static_router.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func (a *StaticRouter) RegisterStaticRouter(r *gin.RouterGroup) {
6868
}
6969

7070
func attachmentFileLocalPath(uploadPath, requestPath, originalFilename string) (string, bool) {
71-
realFilename := strings.TrimSuffix(requestPath, "/"+originalFilename) + filepath.Ext(originalFilename)
71+
// The uploader saves attachments with a lowercased extension, so match it here.
72+
realFilename := strings.TrimSuffix(requestPath, "/"+originalFilename) + strings.ToLower(filepath.Ext(originalFilename))
7273
attachmentRoot := filepath.Join(uploadPath, constant.FilesPostSubPath)
7374
fileLocalPath := filepath.Join(attachmentRoot, realFilename)
7475
relPath, err := filepath.Rel(attachmentRoot, fileLocalPath)

‎internal/router/static_router_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
package router
2121

2222
import (
23+
"net/http"
24+
"net/http/httptest"
25+
"os"
2326
"path/filepath"
2427
"testing"
2528

2629
"github.com/apache/answer/internal/base/constant"
30+
"github.com/apache/answer/internal/service/service_config"
31+
"github.com/gin-gonic/gin"
2732
)
2833

2934
func TestAttachmentFileLocalPathRejectsTraversal(t *testing.T) {
@@ -44,3 +49,43 @@ func TestAttachmentFileLocalPathRejectsTraversal(t *testing.T) {
4449
}
4550
}
4651
}
52+
53+
// The uploader stores attachments under a lowercased extension (Report.PDF is
54+
// saved as hash.pdf), so the download route must look the file up the same way.
55+
func TestAttachmentFileLocalPathLowercasesExtension(t *testing.T) {
56+
uploadPath := t.TempDir()
57+
58+
filePath, ok := attachmentFileLocalPath(uploadPath, "/hash/Report.PDF", "Report.PDF")
59+
if !ok {
60+
t.Fatal("valid attachment path was rejected")
61+
}
62+
want := filepath.Join(uploadPath, constant.FilesPostSubPath, "hash.pdf")
63+
if filePath != want {
64+
t.Fatalf("attachment path = %q, want %q", filePath, want)
65+
}
66+
}
67+
68+
func TestAttachmentDownloadWithUppercaseExtension(t *testing.T) {
69+
gin.SetMode(gin.TestMode)
70+
uploadPath := t.TempDir()
71+
attachmentDir := filepath.Join(uploadPath, constant.FilesPostSubPath)
72+
if err := os.MkdirAll(attachmentDir, 0o755); err != nil {
73+
t.Fatal(err)
74+
}
75+
// This is the name UploadPostAttachment gives an upload called Report.PDF.
76+
if err := os.WriteFile(filepath.Join(attachmentDir, "hash.pdf"), []byte("%PDF-1.4"), 0o644); err != nil {
77+
t.Fatal(err)
78+
}
79+
80+
r := gin.New()
81+
NewStaticRouter(&service_config.ServiceConfig{UploadPath: uploadPath}).RegisterStaticRouter(&r.RouterGroup)
82+
83+
w := httptest.NewRecorder()
84+
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/uploads/files/post/hash/Report.PDF", nil))
85+
if w.Code != http.StatusOK {
86+
t.Fatalf("status = %d (location %q), want %d", w.Code, w.Header().Get("Location"), http.StatusOK)
87+
}
88+
if got := w.Body.String(); got != "%PDF-1.4" {
89+
t.Fatalf("body = %q, want the stored attachment", got)
90+
}
91+
}

0 commit comments

Comments
 (0)