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

This commit is contained in:
m
2026-09-21 19:59:52 +02:00
parent df7dc76405
commit a6b7f2002b
60 changed files with 3414 additions and 412 deletions
+70 -3
View File
@@ -42,6 +42,8 @@ func setTestStore(t *testing.T) *repository.Repository {
func setTestAuth(t *testing.T) {
t.Helper()
Auth, _ = auth.NewManager("test-secret")
ResetLoginRateLimit()
ResetDevicesRateLimit()
t.Cleanup(func() { Auth = nil })
}
@@ -202,6 +204,71 @@ func TestAuthLoginWrongCredentials(t *testing.T) {
}
}
// TestAuthLoginUnknownUserRegisteredDevice — régression A1 : un username
// inconnu ne doit JAMAIS déréférencer user (panic → 500 via Gin recovery).
// Le device est enregistré pour isoler la branche « user inconnu » : on attend
// 401 UNAUTHORIZED, corps identique au cas « mauvais mot de passe ».
func TestAuthLoginUnknownUserRegisteredDevice(t *testing.T) {
setTestAuth(t)
repo := setTestStore(t)
createAdmin(t, repo, "admin", authTestPassword)
r := newTestRouterForAuth()
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
badPassword := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
if badPassword.Code != 401 {
t.Fatalf("mauvais mot de passe: status %d body %s", badPassword.Code, badPassword.Body.String())
}
ghost := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"ghost","password":"wrong-password","device_id":"`+authTestDevice+`"}`)
if ghost.Code != 401 {
t.Fatalf("username inconnu: status %d body %s (attendu 401, jamais 500)", ghost.Code, ghost.Body.String())
}
if !strings.Contains(ghost.Body.String(), "UNAUTHORIZED") {
t.Errorf("code attendu UNAUTHORIZED, got %s", ghost.Body.String())
}
if ghost.Body.String() != badPassword.Body.String() {
t.Errorf("réponses distinguables (sécurité):\n%q\n%q", ghost.Body.String(), badPassword.Body.String())
}
}
func TestAuthLoginRateLimited(t *testing.T) {
setTestAuth(t)
repo := setTestStore(t)
createAdmin(t, repo, "admin", authTestPassword)
r := newTestRouterForAuth()
doAuth(t, r, http.MethodPost, "/api/v1/devices", "", `{"deviceId":"`+authTestDevice+`"}`)
oldLimit := loginRateLimitPerMinute
loginRateLimitPerMinute = 2
hookCalls := 0
oldHook := OnLoginRateLimited
OnLoginRateLimited = func(ip string) { hookCalls++ }
t.Cleanup(func() {
loginRateLimitPerMinute = oldLimit
OnLoginRateLimited = oldHook
ResetLoginRateLimit()
})
badBody := `{"username":"admin","password":"wrong-password","device_id":"` + authTestDevice + `"}`
for i := 0; i < 2; i++ {
if rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody); rec.Code != 401 {
t.Fatalf("tentative %d: status %d, attendu 401", i+1, rec.Code)
}
}
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", badBody)
if rec.Code != 429 {
t.Fatalf("quota dépassé: status %d body %s, attendu 429", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "RATE_LIMITED") {
t.Errorf("code attendu RATE_LIMITED, got %s", rec.Body.String())
}
if hookCalls != 1 {
t.Errorf("OnLoginRateLimited appelé %d fois, attendu 1", hookCalls)
}
}
func TestAuthLoginUnregisteredDevice(t *testing.T) {
setTestAuth(t)
repo := setTestStore(t)
@@ -209,11 +276,11 @@ func TestAuthLoginUnregisteredDevice(t *testing.T) {
r := newTestRouterForAuth()
rec := doAuth(t, r, http.MethodPost, "/api/v1/auth/login", "", `{"username":"admin","password":"`+authTestPassword+`","device_id":"`+authTestDevice+`"}`)
if rec.Code != 400 {
if rec.Code != 401 {
t.Fatalf("device non enregistré: status %d body %s", rec.Code, rec.Body.String())
}
if !strings.Contains(rec.Body.String(), "INVALID_DEVICE_ID") {
t.Errorf("code attendu INVALID_DEVICE_ID, got %s", rec.Body.String())
if !strings.Contains(rec.Body.String(), "UNAUTHORIZED") {
t.Errorf("code attendu UNAUTHORIZED (anti-oracle), got %s", rec.Body.String())
}
}