Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Sanitize media upload filenames. Closes #397.

Kailash Nadh5988ea36cbdc
diff --git a/cmd/media.go b/cmd/media.go
index cf603c5d..b1c61bc0 100644
--- a/cmd/media.go
+++ b/cmd/media.go
@@ -51,7 +51,7 @@ func handleUploadMedia(c echo.Context) error {
}
// Generate filename
- fName := generateFileName(file.Filename)
+ fName := makeFilename(file.Filename)
// Read file contents in memory
src, err := file.Open()
diff --git a/cmd/utils.go b/cmd/utils.go
index b187f75d..7ded53df 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rand"
"fmt"
+ "path/filepath"
"regexp"
"strconv"
"strings"
@@ -25,13 +26,13 @@ func inArray(val string, vals []string) (ok bool) {
return false
}
-// generateFileName appends the incoming file's name with a small random hash.
-func generateFileName(fName string) string {
+// makeFilename sanitizes a filename (user supplied upload filenames).
+func makeFilename(fName string) string {
name := strings.TrimSpace(fName)
if name == "" {
name, _ = generateRandomString(10)
}
- return name
+ return filepath.Base(name)
}
// Given an error, pqErrMsg will try to return pq error details
diff --git a/internal/media/providers/filesystem/filesystem.go b/internal/media/providers/filesystem/filesystem.go
index 686894dc..939f0c8e 100644
--- a/internal/media/providers/filesystem/filesystem.go
+++ b/internal/media/providers/filesystem/filesystem.go
@@ -8,7 +8,6 @@ import (
"path/filepath"
"regexp"
"strconv"
- "strings"
"github.com/knadh/listmonk/internal/media"
)
@@ -43,13 +42,7 @@ func NewDiskStore(opts Opts) (media.Store, error) {
// Put accepts the filename, the content type and file object itself and stores the file in disk.
func (c *Client) Put(filename string, cType string, src io.ReadSeeker) (string, error) {
var out *os.File
- // There's no explicit name. Use the one posted in the HTTP request.
- if filename == "" {
- filename = strings.TrimSpace(filename)
- if filename == "" {
- filename, _ = generateRandomString(10)
- }
- }
+
// Get the directory path
dir := getDir(c.opts.UploadPath)
filename = assertUniqueFilename(dir, filename)