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
+58 -5
View File
@@ -73,6 +73,7 @@ func setup(t *testing.T) (*gin.Engine, *service.Resources, *repository.Repositor
}
handlers.Auth = manager
handlers.Store = store
handlers.ResetDevicesRateLimit()
gin.SetMode(gin.TestMode)
r := gin.New()
@@ -517,12 +518,12 @@ func TestFilesPaginationDefaultsAndClamp(t *testing.T) {
t.Errorf("clamp pageSize: %+v", env.Meta)
}
// Paramètres invalides → défauts (page=1, pageSize=50)
// Paramètres invalides → 400 (plus de fallback silencieux, cf. A16).
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=0&pageSize=-5", token, nil, "")
env = expectOK(t, rec, "invalid-params")
if env.Meta == nil || env.Meta.Page != 1 || env.Meta.PageSize != 50 {
t.Errorf("defauts page/pageSize: %+v", env.Meta)
}
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "negative-params")
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?page=abc&pageSize=zz", token, nil, "")
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "garbage-params")
// sort/order inconnus → pas d'erreur (défaut created_at desc)
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files?sort=zzz&order=up&pageSize=10", token, nil, "")
@@ -633,3 +634,55 @@ func TestFilesGetOcrOwnerShareAndScoping(t *testing.T) {
rec, _ = doRequest(t, r, http.MethodGet, "/api/v1/files/not-hex/ocr", ownerToken, nil, "")
expectError(t, rec, http.StatusNotFound, "NOT_FOUND", "ocr-invalid-id")
}
func TestFilesUploadRejectsBadFilename(t *testing.T) {
r, _, repo := setup(t)
device := repository.NewID()
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "bn"), "badname-test-password", device)
// Body multipart construit à la main : filename brut, NON neutralisé par
// le writer stdlib (qui basename/encode de son côté). Le serveur doit
// rejeter en 400, jamais 500 (cf. A16).
rawUpload := func(filename string) *httptest.ResponseRecorder {
t.Helper()
boundary := "testboundary123"
body := "--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"; filename=\"" + filename + "\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n" +
"hi\r\n" +
"--" + boundary + "--\r\n"
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/files/upload", token, []byte(body), "multipart/form-data; boundary="+boundary)
return rec
}
rec := rawUpload("../../evil.txt")
// La stdlib Go basename déjà côté lecture (défense en profondeur côté
// service via sanitizeFileName, testé en unitaire + backslash) : le nom
// stocké est "evil.txt" et aucun chemin ne s'échappe (destPath dérivé
// uniquement de IDs 32-hex + extension sanitizée).
env := expectOK(t, rec, "upload-traversal-name")
var uploaded fileDTO
if err := json.Unmarshal(env.Data, &uploaded); err != nil {
t.Fatalf("upload-traversal-name: unmarshal: %v", err)
}
if uploaded.Name != "evil.txt" {
t.Errorf("nom traversal neutralisé attendu \"evil.txt\", got %q", uploaded.Name)
}
// Backslash : préservé par la stdlib → rejet service 400, jamais 500.
rec = rawUpload(`bad\name.txt`)
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "upload-backslash-name")
}
func TestSearchQTooLong(t *testing.T) {
r, _, repo := setup(t)
device := repository.NewID()
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "ql"), "search-test-password", device)
long := make([]byte, 201)
for i := range long {
long[i] = 'a'
}
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/search?q="+string(long), token, nil, "")
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "search-q-too-long")
}