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:
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -19,6 +20,15 @@ const (
|
||||
// Auth issues/verifies device bearer tokens; set once at startup (cmd/server).
|
||||
var Auth *auth.Manager
|
||||
|
||||
// EnforceDeviceExists durcit RequireAuth (FEEDBACK A15) : quand true, le
|
||||
// device_id porté par le token doit exister en base (POST /devices), sinon la
|
||||
// requête est rejetée en 401 et le rejet est loggé côté serveur.
|
||||
// Défaut false (V1) : le device reste un claim porté NON autorisant —
|
||||
// l'autorisation vient du subject user_id + existence du compte. Passer à true
|
||||
// coûte une lecture DB par requête authentifiée ; ne pas activer sans mesurer
|
||||
// sur les routes à fort trafic (snapshot/sync).
|
||||
var EnforceDeviceExists = false
|
||||
|
||||
// RequireAuth authenticates the bearer paseto token. Le subject (user_id)
|
||||
// autorise — c'est la clé de scoping de toutes les ressources ; device_id est
|
||||
// porté (non autorisant) pour l'idempotence outbox et les jobs OCR. Le compte
|
||||
@@ -26,7 +36,13 @@ var Auth *auth.Manager
|
||||
func RequireAuth(c *gin.Context) {
|
||||
|
||||
header := c.GetHeader("Authorization")
|
||||
token, found := strings.CutPrefix(header, "Bearer ")
|
||||
token := ""
|
||||
found := false
|
||||
// Préfixe Bearer insensible à la casse (cf. FEEDBACK A7).
|
||||
if len(header) > 7 && strings.EqualFold(header[:7], "Bearer ") {
|
||||
token = strings.TrimSpace(header[7:])
|
||||
found = token != ""
|
||||
}
|
||||
|
||||
if Auth == nil || !found || token == "" {
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "missing bearer token")
|
||||
@@ -57,6 +73,22 @@ func RequireAuth(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if EnforceDeviceExists && Store != nil && Store.Repository != nil {
|
||||
// Verify garantit déjà le format 32-hex : Exists reçoit une entrée saine.
|
||||
exists, err := Store.Repository.Devices.Exists(identity.DeviceID)
|
||||
if err != nil {
|
||||
api.Error(c, http.StatusInternalServerError, "INTERNAL", "could not check device")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
if !exists {
|
||||
log.Printf("auth: token d'un device inconnu rejeté (user=%s device=%s)", identity.UserID, identity.DeviceID)
|
||||
api.Error(c, http.StatusUnauthorized, "UNAUTHORIZED", "unknown device")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.Set(UserIDKey, identity.UserID)
|
||||
c.Set(DeviceIDKey, identity.DeviceID)
|
||||
c.Next()
|
||||
|
||||
Reference in New Issue
Block a user