fix: large carts upload all their files (Marcus's bugs of 11/11, B1 to B4)

B1: an upload start was limited to 60 per 15 minutes per session, so a
66-file cart failed with 'Muitas tentativas'; now 1000 per session and
3000 per source, and the Site waits Retry-After and retries quietly.
B2: a failed file shows its error and 'Tentar de novo' on its own card, and
payment stays blocked with 'Falta enviar N arquivos'. B3: after signing in
again, an upload id the new session does not own (403) is dropped and the
file starts again. B4: the quote is never created before every file is
sent, and an untranslatable 422 is logged for Sentry.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-11 14:49:04 -03:00
parent 07515abbd8
commit 7a24c66997
7 changed files with 180 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ from fastapi import APIRouter, Depends, HTTPException, Request
from starlette.concurrency import run_in_threadpool
from ..core import db
from ..core.auth import audit, owner, rate_limit
from ..core.auth import audit, client_ip, owner, rate_limit
from ..core.limits import upload_limit_bytes
from ..core.models import UploadStart
from ..payments import UNPAID_HOLD
@@ -20,13 +20,25 @@ from ..runtime import PART_BYTES, storage, upload_row
router = APIRouter()
# A cart of many files: 66 in Marcus's case (2026-11-11), and adding more or
# retrying must fit. The per-source bucket bounds abuse, not a real order.
UPLOAD_START_LIMIT = 1000
UPLOAD_START_IP_LIMIT = 3000
def upload_ip_limit(request: Request):
"""The public upload route's per-source bucket. The operator upload routes
reuse begin_upload directly and stay bound by their own session bucket."""
rate_limit('upload-start-ip', client_ip(request), UPLOAD_START_IP_LIMIT, 900)
@router.post('/api/uploads')
def begin_upload(body: UploadStart, session_id=Depends(owner)):
def begin_upload(body: UploadStart, session_id=Depends(owner), _source=Depends(upload_ip_limit)):
if body.size > upload_limit_bytes():
raise HTTPException(413, 'O arquivo passa do tamanho máximo aceito. Escolha um arquivo menor.')
uid = uuid4()
key = f'originals/{uid}'
rate_limit('upload-start', str(session_id), 60, 900)
rate_limit('upload-start', str(session_id), UPLOAD_START_LIMIT, 900)
with db.connect() as c:
# Serialize reservations across API processes, including changing guest IDs.
c.execute('SELECT pg_advisory_xact_lock(804208)')