mirror of
https://github.com/knadh/listmonk.git
synced 2025-11-10 01:31:24 +08:00
Sanitize media upload filenames. Closes #397.
This commit is contained in:
parent
fc84082c87
commit
5988ea36cb
3 changed files with 6 additions and 12 deletions
|
|
@ -51,7 +51,7 @@ func handleUploadMedia(c echo.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate filename
|
// Generate filename
|
||||||
fName := generateFileName(file.Filename)
|
fName := makeFilename(file.Filename)
|
||||||
|
|
||||||
// Read file contents in memory
|
// Read file contents in memory
|
||||||
src, err := file.Open()
|
src, err := file.Open()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -25,13 +26,13 @@ func inArray(val string, vals []string) (ok bool) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateFileName appends the incoming file's name with a small random hash.
|
// makeFilename sanitizes a filename (user supplied upload filenames).
|
||||||
func generateFileName(fName string) string {
|
func makeFilename(fName string) string {
|
||||||
name := strings.TrimSpace(fName)
|
name := strings.TrimSpace(fName)
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name, _ = generateRandomString(10)
|
name, _ = generateRandomString(10)
|
||||||
}
|
}
|
||||||
return name
|
return filepath.Base(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given an error, pqErrMsg will try to return pq error details
|
// Given an error, pqErrMsg will try to return pq error details
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/knadh/listmonk/internal/media"
|
"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.
|
// 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) {
|
func (c *Client) Put(filename string, cType string, src io.ReadSeeker) (string, error) {
|
||||||
var out *os.File
|
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
|
// Get the directory path
|
||||||
dir := getDir(c.opts.UploadPath)
|
dir := getDir(c.opts.UploadPath)
|
||||||
filename = assertUniqueFilename(dir, filename)
|
filename = assertUniqueFilename(dir, filename)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue