Skip to content

fix(security): auth + size cap + rate limit on OCR extract endpoints (#182)#229

Merged
Jose-Gael-Cruz-Lopez merged 1 commit into
mainfrom
security/182-ocr-auth-bounds-ratelimit
Jun 14, 2026
Merged

fix(security): auth + size cap + rate limit on OCR extract endpoints (#182)#229
Jose-Gael-Cruz-Lopez merged 1 commit into
mainfrom
security/182-ocr-auth-bounds-ratelimit

Conversation

@Jose-Gael-Cruz-Lopez

Copy link
Copy Markdown
Member

Closes #182. P2.

Vulnerability

/api/extract/pdf and /api/extract/image (routes/extract.py) took no Request and no auth guard, ran CPU-heavy Docling/tesseract OCR (up to 200 pages) for any anonymous caller, with no size cap and no rate limit. An attacker could drive unbounded OCR load/cost.

Fix

Tests — tests/test_extract_auth_bounds.py

Unauthenticated PDF/image → 401; oversize → 413; over-threshold → 429. All fail on pre-fix code (no auth/bounds/limiter).

Follow-ups (noted, out of scope)

  • flashcard_import_service.check_rate_limit is a duplicate of the now-shared limiter and can migrate to request_limits (would make it truly "shared" per the issue). Left untouched to avoid scope creep.
  • main.py's global HTTPException handler drops exc.headers, so the 429 conveys the retry budget in the detail string rather than a Retry-After header. Forwarding headers is a cross-cutting change worth its own PR.

⚠️ Auth-boundary change — do not merge, opened for your review.

…dpoints (#182)

/api/extract/pdf and /api/extract/image took no Request and no auth guard, ran
multi-second Docling/tesseract OCR for any anonymous caller, with no size cap
and no rate limit — an attacker could drive unbounded OCR load/cost.

- Require an authenticated session via get_session_user_id (401).
- Cap upload size at 20 MB via a bounded read (413), reusing the #220
  size-bound pattern factored into a shared services/request_limits.py.
- Per-user sliding-window rate limit (429), same limiter shape as the
  flashcard import service, now in the shared module.

Tests: unauthenticated PDF/image -> 401; oversize -> 413; >threshold -> 429.
All fail on pre-fix code (no auth/bounds/limiter).

Follow-ups (noted, out of scope): flashcard_import_service has a duplicate
limiter that can migrate to request_limits; main.py's HTTPException handler
drops exc.headers, so 429 conveys retry in the detail string, not Retry-After.
@coderabbitai

coderabbitai Bot commented Jun 13, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Jose-Gael-Cruz-Lopez, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 50 minutes and 18 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more credits in the billing tab to continue.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 18424445-85d9-49fb-b3e6-fa425ef167ab

📥 Commits

Reviewing files that changed from the base of the PR and between a27b688 and ff1d670.

📒 Files selected for processing (3)
  • backend/routes/extract.py
  • backend/services/request_limits.py
  • backend/tests/test_extract_auth_bounds.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch security/182-ocr-auth-bounds-ratelimit

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
frontend ff1d670 Commit Preview URL

Branch Preview URL
Jun 13 2026, 06:13 AM

@Jose-Gael-Cruz-Lopez Jose-Gael-Cruz-Lopez merged commit e8376d8 into main Jun 14, 2026
6 checks passed
@Jose-Gael-Cruz-Lopez Jose-Gael-Cruz-Lopez deleted the security/182-ocr-auth-bounds-ratelimit branch June 14, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/api/extract/pdf and /api/extract/image are unauthenticated, unbounded OCR endpoints with no rate limit

1 participant