This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GenerateUniqueFileName(productName string, originalFileName string) string {
|
||||
@@ -13,3 +14,19 @@ func GenerateUniqueFileName(productName string, originalFileName string) string
|
||||
ext := filepath.Ext(originalFileName)
|
||||
return fmt.Sprintf("%s_%s%s", productName, randomString, ext)
|
||||
}
|
||||
|
||||
// SanitizeFilePath valide qu'un chemin de fichier local reste bien dans le
|
||||
// dossier uploads/ et ne contient pas de tentative de path traversal.
|
||||
func SanitizeFilePath(path string) (string, error) {
|
||||
cleaned := filepath.Clean(path)
|
||||
|
||||
if strings.Contains(cleaned, "..") {
|
||||
return "", fmt.Errorf("path traversal détecté")
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(cleaned, "uploads/") && !strings.HasPrefix(cleaned, "uploads\\") {
|
||||
return "", fmt.Errorf("chemin invalide")
|
||||
}
|
||||
|
||||
return cleaned, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user