Make /portal/sso/embed best-effort to handle Cloudflare challenges#210
Make /portal/sso/embed best-effort to handle Cloudflare challenges#210
Conversation
Some users get 403 from Cloudflare on the embed step. Since it only sets a load balancer cookie, failure shouldn't block login. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughAdds a browser-like header set Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #210 +/- ##
===========================================
+ Coverage 99.97% 100.00% +0.02%
===========================================
Files 68 68
Lines 3558 3560 +2
===========================================
+ Hits 3557 3560 +3
+ Misses 1 0 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/garth/sso.py (1)
222-227: Consider narrowing the exception handling or adding logging.The bare
except GarthException: passcatches all HTTP errors (including 500s, network failures, auth issues), not just the 403 Cloudflare challenge mentioned in the comment. While the step is intentionally best-effort, silently swallowing all exceptions reduces observability for diagnosing unexpected failures.Options to consider:
- Check for specific 403 status and only suppress that
- Add minimal logging (e.g.,
logger.debug) so failures are traceable♻️ Option 1: Narrow to 403 only
# Sets Cloudflare LB cookie for backend pinning — best-effort # (Cloudflare may challenge with 403 for bot detection) try: client.get("sso", "/portal/sso/embed") - except GarthException: - pass + except GarthHTTPError as e: + if e.error.response is not None and e.error.response.status_code == 403: + pass # Expected Cloudflare challenge + else: + raise♻️ Option 2: Log failures for observability
+import logging + +logger = logging.getLogger(__name__) + # In _complete_login: try: client.get("sso", "/portal/sso/embed") - except GarthException: - pass + except GarthException as e: + logger.debug("Cloudflare LB cookie setup failed (non-blocking): %s", e)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/garth/sso.py` around lines 222 - 227, The try/except around client.get("sso", "/portal/sso/embed") in sso.py currently swallows all GarthException instances; narrow handling to only suppress Cloudflare 403s and/or record failures: inspect the raised GarthException from client.get and if its HTTP status is 403 only, suppress it; otherwise call the module logger (e.g., logger.debug or logger.warning) with the exception details and re-raise or handle appropriately so unexpected HTTP 5xx/network/auth errors are visible; update the exception block referencing client.get and GarthException and use the existing logger variable (or create one) to emit minimal observability.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/garth/sso.py`:
- Around line 222-227: The try/except around client.get("sso",
"/portal/sso/embed") in sso.py currently swallows all GarthException instances;
narrow handling to only suppress Cloudflare 403s and/or record failures: inspect
the raised GarthException from client.get and if its HTTP status is 403 only,
suppress it; otherwise call the module logger (e.g., logger.debug or
logger.warning) with the exception details and re-raise or handle appropriately
so unexpected HTTP 5xx/network/auth errors are visible; update the exception
block referencing client.get and GarthException and use the existing logger
variable (or create one) to emit minimal observability.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 35a110fe-9c66-44d9-997e-6b644adab6d9
📒 Files selected for processing (2)
src/garth/sso.pysrc/garth/version.py
Cloudflare challenges requests that look like bots. The sign-in page and /portal/sso/embed are web pages (not APIs) and need browser-like headers (User-Agent, Sec-Fetch-*, Accept) to avoid 403 challenges. API endpoints (preauthorized, exchange) keep the GCM-iOS user agent. Also: pragma no cover on the embed except branch (only hit when Cloudflare challenges despite correct headers). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Summary
Some users get a 403 Cloudflare managed challenge ("Just a moment...") on the
/portal/sso/embedstep added in 0.7.3. This happens because Cloudflare's bot detection requires JavaScript execution, which barerequestscan't do.Making the embed step best-effort unblocks login for these users. Without the LB cookie,
preauthorizedmay occasionally hit a different backend, but that's better than login failing 100%.Diagnosed via logfire session
5ad71f1d12204642—cf-mitigated: challengein the 403 response.Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Chores