fix(security): harden outbound requests and file previews

This commit is contained in:
wizardchen
2026-07-02 18:17:51 +08:00
committed by lyingbug
parent 7099a7e181
commit 663d13eb58
16 changed files with 546 additions and 110 deletions
+10 -39
View File
@@ -1465,28 +1465,12 @@ func newFileServeHandler(globalFileService interfaces.FileService) gin.HandlerFu
}
defer reader.Close()
ext := filepath.Ext(filePath)
contentType := "application/octet-stream"
switch strings.ToLower(ext) {
case ".png":
contentType = "image/png"
case ".jpg", ".jpeg":
contentType = "image/jpeg"
case ".gif":
contentType = "image/gif"
case ".webp":
contentType = "image/webp"
case ".bmp":
contentType = "image/bmp"
case ".svg":
contentType = "image/svg+xml"
case ".pdf":
contentType = "application/pdf"
case ".csv":
contentType = "text/csv; charset=utf-8"
}
contentType, inline := secutils.SafeContentTypeByFilename(filePath)
c.Header("Content-Type", contentType)
c.Header("X-Content-Type-Options", "nosniff")
if !inline {
c.Header("Content-Disposition", "attachment")
}
c.Header("Cache-Control", "public, max-age=86400")
c.Status(http.StatusOK)
if _, err := io.Copy(c.Writer, reader); err != nil {
@@ -1587,24 +1571,7 @@ func presignedFileHandler(tenantService interfaces.TenantService, absDir string)
return
}
ext := filepath.Ext(filePath)
contentType := "application/octet-stream"
switch strings.ToLower(ext) {
case ".png":
contentType = "image/png"
case ".jpg", ".jpeg":
contentType = "image/jpeg"
case ".gif":
contentType = "image/gif"
case ".webp":
contentType = "image/webp"
case ".bmp":
contentType = "image/bmp"
case ".svg":
contentType = "image/svg+xml"
case ".pdf":
contentType = "application/pdf"
}
contentType, inline := secutils.SafeContentTypeByFilename(filePath)
// HEAD short-circuits the body read. We still need to confirm the
// object exists, but we use a 0-byte content length and skip io.Copy.
@@ -1621,6 +1588,10 @@ func presignedFileHandler(tenantService interfaces.TenantService, absDir string)
defer reader.Close()
c.Header("Content-Type", contentType)
c.Header("X-Content-Type-Options", "nosniff")
if !inline {
c.Header("Content-Disposition", "attachment")
}
c.Header("Cache-Control", "public, max-age=86400")
if c.Request.Method == http.MethodHead {
c.Status(http.StatusOK)
+32
View File
@@ -152,3 +152,35 @@ func TestServeFilesRejectsPathWithoutTenantSegment(t *testing.T) {
t.Fatalf("status = %d, want %d", got, want)
}
}
func TestServeFilesForcesActiveContentDownload(t *testing.T) {
gin.SetMode(gin.TestMode)
t.Setenv("STORAGE_TYPE", "local")
engine := gin.New()
serveFiles(engine, &stubFileService{
getFile: func(_ context.Context, _ string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader(`<svg onload="alert(1)"></svg>`)), nil
},
})
filePath := "local://42/docs/payload.svg"
req := httptest.NewRequest(http.MethodGet, "/files?file_path="+url.QueryEscape(filePath), nil)
req = req.WithContext(context.WithValue(req.Context(), types.TenantInfoContextKey, &types.Tenant{ID: 42}))
recorder := httptest.NewRecorder()
engine.ServeHTTP(recorder, req)
if got, want := recorder.Code, http.StatusOK; got != want {
t.Fatalf("status = %d, want %d", got, want)
}
if got := recorder.Header().Get("Content-Type"); got != "application/octet-stream" {
t.Fatalf("Content-Type = %q, want application/octet-stream", got)
}
if got := recorder.Header().Get("Content-Disposition"); got != "attachment" {
t.Fatalf("Content-Disposition = %q, want attachment", got)
}
if got := recorder.Header().Get("X-Content-Type-Options"); got != "nosniff" {
t.Fatalf("X-Content-Type-Options = %q, want nosniff", got)
}
}
+22
View File
@@ -170,6 +170,28 @@ func TestPresignedFile_GET_ReturnsContent(t *testing.T) {
}
}
func TestPresignedFile_ForcesActiveContentDownload(t *testing.T) {
engine, baseDir, signURL := setupPresignedTestServer(t)
storagePath := writeTestFile(t, baseDir, "1/payload.svg", `<svg onload="alert(1)"></svg>`)
req := httptest.NewRequest(http.MethodGet, signURL(storagePath, 1, time.Hour), nil)
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
if got, want := w.Code, http.StatusOK; got != want {
t.Fatalf("status = %d, want %d", got, want)
}
if got := w.Header().Get("Content-Type"); got != "application/octet-stream" {
t.Fatalf("Content-Type = %q, want application/octet-stream", got)
}
if got := w.Header().Get("Content-Disposition"); got != "attachment" {
t.Fatalf("Content-Disposition = %q, want attachment", got)
}
if got := w.Header().Get("X-Content-Type-Options"); got != "nosniff" {
t.Fatalf("X-Content-Type-Options = %q, want nosniff", got)
}
}
func TestPresignedFile_InvalidSig_403(t *testing.T) {
engine, _, _ := setupPresignedTestServer(t)