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:
@@ -0,0 +1,126 @@
|
||||
package handlers_test
|
||||
|
||||
// Tests contrat LOT 6 : ordre statique/param (§1), validation 32-hex et
|
||||
// curseur `after` stricts (A7), validation POST /devices.
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/kazier/backend/handlers"
|
||||
"github.com/kazier/backend/repository"
|
||||
)
|
||||
|
||||
// TestFoldersRouteServesListNotParam (§1) : GET /files/folders via le routeur
|
||||
// atteint FoldersList (200), jamais FilesGet avec id="folders".
|
||||
func TestFoldersRouteServesListNotParam(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6f"), "lot6-test-password", device)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/files/folders", token, nil, "")
|
||||
expectOK(t, rec, "folders-static-wins")
|
||||
}
|
||||
|
||||
// TestFilesGetRejectsFoldersLiteral (§1) : si le routeur tombait un jour sur
|
||||
// FilesGet avec id="folders", le garde 32-hex répond 404 (jamais 200, jamais
|
||||
// 500). Appel direct du handler pour forcer le cas paramétré.
|
||||
func TestFilesGetRejectsFoldersLiteral(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/files/folders", nil)
|
||||
c.Params = gin.Params{{Key: "id", Value: "folders"}}
|
||||
c.Set(handlers.UserIDKey, "00000000000000000000000000000001")
|
||||
|
||||
handlers.FilesGet(c)
|
||||
|
||||
expectError(t, w, http.StatusNotFound, "NOT_FOUND", "files-get-folders-literal")
|
||||
}
|
||||
|
||||
// TestShareLinkMalformedToken400 (A7) : token non 32-hex → 400
|
||||
// INVALID_REQUEST (pas 404).
|
||||
func TestShareLinkMalformedToken400(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
for _, path := range []string{
|
||||
"/api/v1/shares/links/NOT-HEX",
|
||||
"/api/v1/shares/links/xyz",
|
||||
"/api/v1/shares/links/ABCDEF0123456789ABCDEF0123456789", // majuscules rejetées
|
||||
} {
|
||||
rec, _ := doRequest(t, r, http.MethodGet, path, "", nil, "")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "share-malformed-token")
|
||||
}
|
||||
}
|
||||
|
||||
// TestSyncPermissionsAfterValidation (A7) : garbage/négatif → 400, epoch-ms
|
||||
// 64-bit accepté.
|
||||
func TestSyncPermissionsAfterValidation(t *testing.T) {
|
||||
r, _, repo := setup(t)
|
||||
device := repository.NewID()
|
||||
token, _ := registerAndLogin(t, r, repo, testUserUsername(device, "l6a"), "lot6-test-password", device)
|
||||
|
||||
for _, path := range []string{
|
||||
"/api/v1/sync/permissions?after=garbage",
|
||||
"/api/v1/sync/permissions?after=-5",
|
||||
"/api/v1/sync/permissions?after=12.5",
|
||||
"/api/v1/sync/permissions?after=",
|
||||
} {
|
||||
// "?after=" vide = curseur absent → 200 (seul le dernier cas).
|
||||
rec, _ := doRequest(t, r, http.MethodGet, path, token, nil, "")
|
||||
if path == "/api/v1/sync/permissions?after=" {
|
||||
expectOK(t, rec, "after-empty-ok")
|
||||
} else {
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "after-invalid:"+path)
|
||||
}
|
||||
}
|
||||
|
||||
// Epoch-ms au-delà de 2^31 (64-bit) → 200, pas de troncation/rejet.
|
||||
rec, _ := doRequest(t, r, http.MethodGet, "/api/v1/sync/permissions?after=9999999999999", token, nil, "")
|
||||
expectOK(t, rec, "after-64bit-ok")
|
||||
}
|
||||
|
||||
// TestDevicesRegisterValidation : 32-hex malformé/absent → 400, valide → 200.
|
||||
func TestDevicesRegisterValidation(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"NOT-HEX"}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-malformed")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_REQUEST", "devices-missing")
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
expectOK(t, rec, "devices-valid")
|
||||
}
|
||||
|
||||
// TestDevicesRegisterRateLimited : flood → 429 RATE_LIMITED (budget 300/min/IP,
|
||||
// cf. handlers/devices.go). Les requêtes invalides ne consomment pas le budget.
|
||||
func TestDevicesRegisterRateLimited(t *testing.T) {
|
||||
r, _, _ := setup(t)
|
||||
|
||||
hookCalls := 0
|
||||
handlers.OnDevicesRateLimited = func(ip string) { hookCalls++ }
|
||||
t.Cleanup(func() { handlers.OnDevicesRateLimited = nil })
|
||||
|
||||
// Les requêtes invalides (avant le compteur) ne doivent pas déclencher.
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":"BAD"}`), "application/json")
|
||||
expectError(t, rec, http.StatusBadRequest, "INVALID_DEVICE_ID", "devices-ratelimit-invalid")
|
||||
|
||||
for i := 0; i < 300; i++ {
|
||||
rec, _ := doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("enregistrement %d: attendu 200, got %d body=%s", i, rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
rec, _ = doRequest(t, r, http.MethodPost, "/api/v1/devices", "", []byte(`{"deviceId":`+`"`+repository.NewID()+`"}`), "application/json")
|
||||
expectError(t, rec, http.StatusTooManyRequests, "RATE_LIMITED", "devices-ratelimited")
|
||||
if hookCalls != 1 {
|
||||
t.Errorf("OnDevicesRateLimited appelé %d fois, attendu 1", hookCalls)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user