fix backend audit findings (auth, upload, sync, data, ocr, contract)
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
CI / lint-backend (push) Failing after 1m39s
CI / lint-mobile (push) Canceled after 11m8s
CI / test-backend (push) Canceled after 0s
CI / test-mobile (push) Canceled after 0s
CI / build-backend (push) Canceled after 0s
CI / docker (push) Canceled after 0s
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
@@ -19,6 +21,21 @@ type ApplicationConfig struct {
|
||||
AdminPassword string
|
||||
}
|
||||
|
||||
// Sécurité AUTH_SECRET (FEEDBACK #4) :
|
||||
// - valeur par défaut DEV UNIQUEMENT (jamais en production : refusée au boot
|
||||
// si APP_ENV/ENV vaut "prod"/"production") ;
|
||||
// - longueur minimale 32 caractères partout (dev comme prod) : le secret ne
|
||||
// sert qu'à dériver la clé paseto v4-local (SHA256), un secret court ou
|
||||
// deviné = tokens forgeables.
|
||||
//
|
||||
// NOTE inter-lots (LOT 6 « secret-doc ») : aligner backend/.env.example —
|
||||
// documenter « ≥ 32 caractères, ex. `openssl rand -hex 32` » et ne plus
|
||||
// suggérer une valeur copiable telle quelle.
|
||||
const (
|
||||
defaultAuthSecret = "dev-secret-change-me-please-set-in-prod"
|
||||
minAuthSecretLen = 32
|
||||
)
|
||||
|
||||
func LoadApplicationConfig() (*ApplicationConfig, error) {
|
||||
|
||||
// .env optionnel — les défauts suffisent pour le dev local.
|
||||
@@ -28,16 +45,33 @@ func LoadApplicationConfig() (*ApplicationConfig, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if port < 1 || port > 65535 {
|
||||
return nil, fmt.Errorf("PORT hors plage 1-65535: %d", port)
|
||||
}
|
||||
|
||||
maxSize, err := getInt("MAX_FILE_SIZE_MB", 50)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if maxSize < 1 || maxSize > 1024 {
|
||||
return nil, fmt.Errorf("MAX_FILE_SIZE_MB hors plage 1-1024: %d", maxSize)
|
||||
}
|
||||
|
||||
ocrWorkers, err := getInt("OCR_WORKERS", 2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if ocrWorkers < 1 || ocrWorkers > 16 {
|
||||
return nil, fmt.Errorf("OCR_WORKERS hors plage 1-16: %d", ocrWorkers)
|
||||
}
|
||||
|
||||
authSecret := get("AUTH_SECRET", defaultAuthSecret)
|
||||
if isProduction() && (authSecret == "" || authSecret == defaultAuthSecret) {
|
||||
return nil, fmt.Errorf("AUTH_SECRET doit être défini en production (pas de défaut)")
|
||||
}
|
||||
if len(authSecret) < minAuthSecretLen {
|
||||
return nil, fmt.Errorf("AUTH_SECRET trop court (min %d caractères)", minAuthSecretLen)
|
||||
}
|
||||
|
||||
return &ApplicationConfig{
|
||||
Port: port,
|
||||
@@ -46,13 +80,21 @@ func LoadApplicationConfig() (*ApplicationConfig, error) {
|
||||
MaxFileSizeMB: int64(maxSize),
|
||||
OcrLang: get("OCR_LANG", "fra+eng"),
|
||||
OcrWorkers: ocrWorkers,
|
||||
AuthSecret: get("AUTH_SECRET", "dev-secret-change-me"),
|
||||
AuthSecret: authSecret,
|
||||
AdminUsername: get("ADMIN_USERNAME", ""),
|
||||
AdminPassword: get("ADMIN_PASSWORD", ""),
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func isProduction() bool {
|
||||
env := strings.ToLower(strings.TrimSpace(os.Getenv("APP_ENV")))
|
||||
if env == "" {
|
||||
env = strings.ToLower(strings.TrimSpace(os.Getenv("ENV")))
|
||||
}
|
||||
return env == "production" || env == "prod"
|
||||
}
|
||||
|
||||
func get(name string, defaultValue string) string {
|
||||
value := os.Getenv(name)
|
||||
if value == "" {
|
||||
|
||||
Reference in New Issue
Block a user