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 == "" {
|
||||
|
||||
@@ -10,6 +10,7 @@ func clearEnv(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"PORT", "DATABASE_URL", "UPLOAD_DIR", "MAX_FILE_SIZE_MB",
|
||||
"OCR_LANG", "OCR_WORKERS", "AUTH_SECRET", "ADMIN_USERNAME", "ADMIN_PASSWORD",
|
||||
"APP_ENV", "ENV",
|
||||
} {
|
||||
t.Setenv(name, "")
|
||||
}
|
||||
@@ -40,9 +41,12 @@ func TestLoadApplicationDefaults(t *testing.T) {
|
||||
if cfg.OcrWorkers != 2 {
|
||||
t.Errorf("OcrWorkers = %d, attendu 2", cfg.OcrWorkers)
|
||||
}
|
||||
if cfg.AuthSecret != "dev-secret-change-me" {
|
||||
if cfg.AuthSecret != defaultAuthSecret {
|
||||
t.Errorf("AuthSecret défaut inattendu: %q", cfg.AuthSecret)
|
||||
}
|
||||
if len(cfg.AuthSecret) < minAuthSecretLen {
|
||||
t.Errorf("AuthSecret défaut trop court pour le minimum %d: %q", minAuthSecretLen, cfg.AuthSecret)
|
||||
}
|
||||
if cfg.AdminUsername != "" || cfg.AdminPassword != "" {
|
||||
t.Errorf("identifiants admin doivent être vides par défaut: %+v", cfg)
|
||||
}
|
||||
@@ -56,7 +60,7 @@ func TestLoadApplicationEnvOverrides(t *testing.T) {
|
||||
t.Setenv("UPLOAD_DIR", "/tmp/up")
|
||||
t.Setenv("OCR_LANG", "eng")
|
||||
t.Setenv("OCR_WORKERS", "4")
|
||||
t.Setenv("AUTH_SECRET", "super-secret")
|
||||
t.Setenv("AUTH_SECRET", "super-secret-0123456789abcdef01234567")
|
||||
t.Setenv("ADMIN_USERNAME", "root")
|
||||
t.Setenv("ADMIN_PASSWORD", "toor")
|
||||
|
||||
@@ -65,12 +69,46 @@ func TestLoadApplicationEnvOverrides(t *testing.T) {
|
||||
t.Fatalf("LoadApplicationConfig: %v", err)
|
||||
}
|
||||
if cfg.Port != 9090 || cfg.MaxFileSizeMB != 120 || cfg.DatabaseURL != "postgres://u:p@host:5433/db?sslmode=disable" ||
|
||||
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.OcrWorkers != 4 || cfg.AuthSecret != "super-secret" ||
|
||||
cfg.UploadDir != "/tmp/up" || cfg.OcrLang != "eng" || cfg.OcrWorkers != 4 || cfg.AuthSecret != "super-secret-0123456789abcdef01234567" ||
|
||||
cfg.AdminUsername != "root" || cfg.AdminPassword != "toor" {
|
||||
t.Errorf("overrides non appliqués: %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadApplicationRejectsShortSecret(t *testing.T) {
|
||||
clearEnv(t)
|
||||
t.Setenv("AUTH_SECRET", "too-short")
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Error("AUTH_SECRET court : attendu une erreur (min 32)")
|
||||
}
|
||||
|
||||
// Le défaut dev passe en dev mais est refusé en production.
|
||||
clearEnv(t)
|
||||
if _, err := LoadApplicationConfig(); err != nil {
|
||||
t.Errorf("défaut dev en dev : inattendu %v", err)
|
||||
}
|
||||
for _, env := range []string{"prod", "production", "PROD"} {
|
||||
clearEnv(t)
|
||||
t.Setenv("APP_ENV", env)
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Errorf("défaut dev avec APP_ENV=%q : attendu une erreur", env)
|
||||
}
|
||||
clearEnv(t)
|
||||
t.Setenv("ENV", env)
|
||||
if _, err := LoadApplicationConfig(); err == nil {
|
||||
t.Errorf("défaut dev avec ENV=%q : attendu une erreur", env)
|
||||
}
|
||||
}
|
||||
|
||||
// Un secret long et explicite passe en production.
|
||||
clearEnv(t)
|
||||
t.Setenv("APP_ENV", "prod")
|
||||
t.Setenv("AUTH_SECRET", "prod-secret-0123456789abcdef01234567")
|
||||
if _, err := LoadApplicationConfig(); err != nil {
|
||||
t.Errorf("secret prod valide : inattendu %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadApplicationRejectsInvalidInt(t *testing.T) {
|
||||
clearEnv(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user