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
View File
@@ -94,6 +94,76 @@ func TestOcrPhysicalPath(t *testing.T) {
}
}
func TestOcrPhysicalPathRejectsNonHexIDs(t *testing.T) {
o := NewOcr(nil, t.TempDir(), "fra+eng", nil)
for _, tc := range [][2]string{
{"../evil", "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
{"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "../../etc/passwd.png"},
{"", ""},
} {
if _, err := o.physicalPath(tc[0], tc[1]); err == nil {
t.Errorf("ids %q/%q : attendu rejet non 32-hex", tc[0], tc[1])
}
}
}
func TestOcrPhysicalPathRejectsSymlinkEscape(t *testing.T) {
uploadDir := t.TempDir()
userID := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
fileID := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
outside := t.TempDir()
secret := filepath.Join(outside, "secret.png")
if err := os.WriteFile(secret, []byte("secret"), 0o644); err != nil {
t.Fatalf("write secret: %v", err)
}
userDir := filepath.Join(uploadDir, userID)
if err := os.MkdirAll(userDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
// Lien symbolique <fileID>.png → hors UPLOAD_DIR : doit être ignoré.
if err := os.Symlink(secret, filepath.Join(userDir, fileID+".png")); err != nil {
t.Fatalf("symlink: %v", err)
}
o := NewOcr(nil, uploadDir, "fra+eng", nil)
if _, err := o.physicalPath(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
t.Errorf("symlink hors UPLOAD_DIR : attendu ErrNotFound, got %v", err)
}
}
func TestOcrPhysicalPathRejectsDirectory(t *testing.T) {
uploadDir := t.TempDir()
userID := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
fileID := "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
userDir := filepath.Join(uploadDir, userID)
if err := os.MkdirAll(filepath.Join(userDir, fileID+".png"), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
o := NewOcr(nil, uploadDir, "fra+eng", nil)
if _, err := o.physicalPath(userID, fileID); !errors.Is(err, repository.ErrNotFound) {
t.Errorf("dossier à la place du fichier : attendu ErrNotFound, got %v", err)
}
}
func TestNormalizeOcrLang(t *testing.T) {
for _, lang := range []string{"fra", "eng", "fra+eng"} {
if got := normalizeOcrLang(lang); got != lang {
t.Errorf("langue %q : attendue conservée, got %q", lang, got)
}
}
for _, lang := range []string{"", "e", "fra;id", "fra eng", "FRA", "--psm 6 fra"} {
if got := normalizeOcrLang(lang); got != DefaultOcrLang {
t.Errorf("langue %q : attendu repli %q, got %q", lang, DefaultOcrLang, got)
}
}
o := NewOcr(nil, t.TempDir(), "fra;evil", nil)
if o.Lang != DefaultOcrLang {
t.Errorf("NewOcr langue hostile : attendu %q, got %q", DefaultOcrLang, o.Lang)
}
}
func TestOcrJobFailsWhenPhysicalFileMissing(t *testing.T) {
o, userID, deviceID, _ := newTestOcr(t, t.TempDir(), stubEngine{text: "x"})