feat(api): captura de 401 de App Check con motivo clasificado (debug)
Browse files
main.py
CHANGED
|
@@ -123,10 +123,6 @@ app.add_middleware(
|
|
| 123 |
@app.middleware("http")
|
| 124 |
async def _stats_middleware(request, call_next):
|
| 125 |
# Mide throughput/latencia/errores por endpoint. Si call_next lanza, cuenta 500.
|
| 126 |
-
# DEBUG TEMPORAL: captura quΓ© llega en los endpoints de datos (App Check).
|
| 127 |
-
path = request.url.path
|
| 128 |
-
if path.startswith("/v1") or path.startswith("/v2"):
|
| 129 |
-
await _capture_request_debug(request)
|
| 130 |
started = time.perf_counter()
|
| 131 |
status = 500
|
| 132 |
try:
|
|
@@ -135,6 +131,10 @@ async def _stats_middleware(request, call_next):
|
|
| 135 |
return response
|
| 136 |
finally:
|
| 137 |
stats.record(request.url.path, status, time.perf_counter() - started)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
# Firebase App Check
|
|
@@ -168,57 +168,64 @@ def verify_app_check(x_firebase_appcheck: str = Header(None)):
|
|
| 168 |
|
| 169 |
|
| 170 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 171 |
-
# DEBUG TEMPORAL β App Check: quΓ©
|
| 172 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 173 |
-
#
|
| 174 |
-
#
|
| 175 |
-
#
|
| 176 |
-
#
|
| 177 |
-
#
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
| 183 |
db = history.get_db()
|
| 184 |
if db is None:
|
| 185 |
return
|
| 186 |
now = time.time()
|
| 187 |
-
if now -
|
| 188 |
return
|
| 189 |
-
|
| 190 |
|
| 191 |
token = request.headers.get("x-firebase-appcheck")
|
| 192 |
info = {
|
| 193 |
"ts": int(now),
|
| 194 |
"path": request.url.path,
|
| 195 |
-
"method": request.method,
|
| 196 |
"userAgent": request.headers.get("user-agent"),
|
| 197 |
"hasToken": token is not None,
|
| 198 |
"disableAppCheck": os.getenv("DISABLE_APP_CHECK"),
|
| 199 |
-
"expectedSub": APP_ID,
|
| 200 |
-
"expectedAud": f"projects/{PROJECT_NUMBER}",
|
| 201 |
}
|
|
|
|
| 202 |
if token:
|
| 203 |
info["tokenLen"] = len(token)
|
| 204 |
try:
|
| 205 |
claims = jwt.decode(token, options={"verify_signature": False})
|
| 206 |
info["claims"] = {k: claims.get(k) for k in ("sub", "aud", "iss", "exp", "iat")}
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
except Exception as e:
|
| 209 |
info["decodeError"] = f"{type(e).__name__}: {e}"
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
info["looksLikeUuid"] = bool(re.fullmatch(
|
| 214 |
-
r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}",
|
| 215 |
-
token,
|
| 216 |
-
))
|
| 217 |
|
|
|
|
| 218 |
try:
|
| 219 |
-
await db.collection("
|
| 220 |
except Exception as e:
|
| 221 |
-
logger.error("
|
| 222 |
|
| 223 |
|
| 224 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 123 |
@app.middleware("http")
|
| 124 |
async def _stats_middleware(request, call_next):
|
| 125 |
# Mide throughput/latencia/errores por endpoint. Si call_next lanza, cuenta 500.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
started = time.perf_counter()
|
| 127 |
status = 500
|
| 128 |
try:
|
|
|
|
| 131 |
return response
|
| 132 |
finally:
|
| 133 |
stats.record(request.url.path, status, time.perf_counter() - started)
|
| 134 |
+
# DEBUG TEMPORAL: guarda el detalle de los 401 de App Check para diagnosticarlos.
|
| 135 |
+
p = request.url.path
|
| 136 |
+
if status == 401 and (p.startswith("/v1") or p.startswith("/v2")):
|
| 137 |
+
await _capture_401(request)
|
| 138 |
|
| 139 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 140 |
# Firebase App Check
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 171 |
+
# DEBUG TEMPORAL β App Check: por quΓ© fallan los 401
|
| 172 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 173 |
+
# Ante un 401 en /v1|/v2, guarda el detalle en la colecciΓ³n rodante `appcheck_401`
|
| 174 |
+
# (30 slots que se reescriben), con el MOTIVO clasificado para ver el patrΓ³n:
|
| 175 |
+
# sin_token β la app no consiguiΓ³ token (red bloquea Google / Play Integrity)
|
| 176 |
+
# token_no_jwt β dummy/error de App Check (no obtuvo token real)
|
| 177 |
+
# token_expirado β token vencido
|
| 178 |
+
# sub_no_coincide β token de otra app
|
| 179 |
+
# firma_o_aud_iss β JWT en forma OK pero rechazado (firma/audience/issuer)
|
| 180 |
+
# Throttle 2s para no floodear Firestore. QUITAR cuando se resuelva.
|
| 181 |
+
_last_401_capture = 0.0
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
async def _capture_401(request) -> None:
|
| 185 |
+
global _last_401_capture
|
| 186 |
db = history.get_db()
|
| 187 |
if db is None:
|
| 188 |
return
|
| 189 |
now = time.time()
|
| 190 |
+
if now - _last_401_capture < 2:
|
| 191 |
return
|
| 192 |
+
_last_401_capture = now
|
| 193 |
|
| 194 |
token = request.headers.get("x-firebase-appcheck")
|
| 195 |
info = {
|
| 196 |
"ts": int(now),
|
| 197 |
"path": request.url.path,
|
|
|
|
| 198 |
"userAgent": request.headers.get("user-agent"),
|
| 199 |
"hasToken": token is not None,
|
| 200 |
"disableAppCheck": os.getenv("DISABLE_APP_CHECK"),
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
+
reason = "sin_token"
|
| 203 |
if token:
|
| 204 |
info["tokenLen"] = len(token)
|
| 205 |
try:
|
| 206 |
claims = jwt.decode(token, options={"verify_signature": False})
|
| 207 |
info["claims"] = {k: claims.get(k) for k in ("sub", "aud", "iss", "exp", "iat")}
|
| 208 |
+
sub_ok = claims.get("sub") == APP_ID
|
| 209 |
+
exp = claims.get("exp")
|
| 210 |
+
expired = bool(exp and exp < int(now))
|
| 211 |
+
info["subMatches"] = sub_ok
|
| 212 |
+
info["expired"] = expired
|
| 213 |
+
reason = (
|
| 214 |
+
"token_expirado" if expired
|
| 215 |
+
else "sub_no_coincide" if not sub_ok
|
| 216 |
+
else "firma_o_aud_iss"
|
| 217 |
+
)
|
| 218 |
except Exception as e:
|
| 219 |
info["decodeError"] = f"{type(e).__name__}: {e}"
|
| 220 |
+
info["tokenPreview"] = token[:48]
|
| 221 |
+
reason = "token_no_jwt"
|
| 222 |
+
info["reason"] = reason
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
+
slot = int(now) % 30
|
| 225 |
try:
|
| 226 |
+
await db.collection("appcheck_401").document(f"slot_{slot:02d}").set(info)
|
| 227 |
except Exception as e:
|
| 228 |
+
logger.error("captura 401 fallΓ³: %s", e)
|
| 229 |
|
| 230 |
|
| 231 |
# ββββββββββββββββββββββββββββββββββββββββββββββ
|