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
+37 -4
View File
@@ -2,6 +2,7 @@ package handlers
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
@@ -9,10 +10,37 @@ import (
"github.com/kazier/backend/service"
)
// parseAfterMs parses the `after` delta cursor of GET /sync/permissions :
// epoch-millis, 64-bit (les curseurs dépassent 2^31). Chaîne vide → 0 (sync
// complet). Garbage ou négatif → ok=false : le handler répond 400
// INVALID_REQUEST, jamais de repli silencieux vers 0 (un curseur malformé
// pris pour « sync complet » masquerait une désynchro client).
// NOTE : intParam (files.go, LOT 2) garde son repli-défaut historique pour la
// pagination ; la validation stricte 400 des pages relève du LOT 2
// (inter-lots).
func parseAfterMs(raw string) (int64, bool) {
if raw == "" {
return 0, true
}
ms, err := strconv.ParseInt(raw, 10, 64)
if err != nil || ms < 0 {
return 0, false
}
return ms, true
}
type syncOpsRequest struct {
Operations []service.SyncOperation `json:"operations"`
}
// Bornes anti-DoS de POST /sync/ops (FEEDBACK §moyenne) : le client envoie des
// batchs de 20 ; au-delà de 100 ops ou 1 MiB de body → rejet explicite, jamais
// d'OOM sur batch géant.
const (
maxSyncOpsBodyBytes = 1 << 20
maxSyncOpsPerBatch = 100
)
func SyncOpsPush(c *gin.Context) {
if Store == nil {
api.Error(c, http.StatusServiceUnavailable, "SERVICE_UNAVAILABLE", "backend not initialized")
@@ -20,11 +48,16 @@ func SyncOpsPush(c *gin.Context) {
}
userID := c.GetString(UserIDKey)
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxSyncOpsBodyBytes)
var req syncOpsRequest
if err := c.ShouldBindJSON(&req); err != nil {
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid JSON body")
return
}
if len(req.Operations) > maxSyncOpsPerBatch {
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "too many operations in batch (max 100)")
return
}
result, err := Store.ApplyBatch(userID, c.GetString(DeviceIDKey), req.Operations)
if err != nil {
@@ -40,10 +73,10 @@ func SyncPermissionsGet(c *gin.Context) {
return
}
userID := c.GetString(UserIDKey)
after := c.Query("after")
var afterMs int64
if after != "" {
afterMs = int64(intParam(after, 0))
afterMs, ok := parseAfterMs(c.Query("after"))
if !ok {
api.Error(c, http.StatusBadRequest, "INVALID_REQUEST", "invalid `after` cursor, must be non-negative epoch-millis")
return
}
perms, err := Store.Snapshot(userID, afterMs)
if err != nil {