feat(device-flow): redirect SSO-complete failures to a dedicated device error view#38185
Draft
GareArc wants to merge 3 commits into
Draft
feat(device-flow): redirect SSO-complete failures to a dedicated device error view#38185GareArc wants to merge 3 commits into
GareArc wants to merge 3 commits into
Conversation
All failure paths in sso_complete now return 302 redirects to /device?sso_error=... instead of raw werkzeug error pages. Adds _device_error_redirect helper that clamps codes to the two allowed values, an outer catch-all try/except that logs + redirects on unhandled exceptions, and relays any inbound sso_error from Go enterprise straight through. user_code is now extracted before the nonce is consumed so replay redirects can still carry it.
| params: dict[str, str] = {"sso_error": safe_code} | ||
| if user_code: | ||
| params["user_code"] = user_code | ||
| return redirect(f"/device?{urlencode(params)}", code=302) |
Contributor
Pyrefly Type Coverage
|
Contributor
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-06-30 03:16:18.460885769 +0000
+++ /tmp/pyrefly_pr.txt 2026-06-30 03:16:03.777897455 +0000
@@ -2297,9 +2297,9 @@
ERROR Object of class `NoneType` has no attribute `total_tokens` [missing-attribute]
--> tests/unit_tests/controllers/openapi/test_models.py:13:12
ERROR Argument `test_verify_approval_grant_raises_on_missing_field._FakeKeyset` is not assignable to parameter `keyset` with type `KeySet` in function `libs.jws.sign` [bad-argument-type]
- --> tests/unit_tests/controllers/openapi/test_oauth_sso_claims.py:67:9
+ --> tests/unit_tests/controllers/openapi/test_oauth_sso_claims.py:69:9
ERROR Argument `test_verify_approval_grant_raises_on_missing_field._FakeKeyset` is not assignable to parameter `keyset` with type `KeySet` in function `libs.device_flow_security.verify_approval_grant` [bad-argument-type]
- --> tests/unit_tests/controllers/openapi/test_oauth_sso_claims.py:73:52
+ --> tests/unit_tests/controllers/openapi/test_oauth_sso_claims.py:75:52
ERROR Object of class `FunctionType` has no attribute `view_class` [missing-attribute]
--> tests/unit_tests/controllers/openapi/test_workspaces.py:38:12
ERROR `in` is not supported between `Literal['GET']` and `None` [not-iterable]
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #38185 +/- ##
==========================================
- Coverage 85.34% 85.33% -0.02%
==========================================
Files 4967 4967
Lines 258448 258548 +100
Branches 49042 49064 +22
==========================================
+ Hits 220582 220626 +44
- Misses 33573 33630 +57
+ Partials 4293 4292 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…assert Validate user_code against a [A-Z0-9-] charset before placing it in the /device redirect, closing the CodeQL url-redirection finding (the value can no longer carry path/scheme separators). Split the compound assert flagged by ruff PT018.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When an external SSO login fails during the OAuth device flow,
sso-completeraisedBadRequest/Conflict→ a raw werkzeug error page, and the/devicepage only showed an inline banner on the code-entry screen (never scrubbed from the URL). Most failures showed a raw page; the CLI received no signal and polled until expiry.What this does (Python + web half)
api) —sso-completenow returns a 302 redirect on every failure to a relative/device?sso_error=...&user_code=.... Adds_device_error_redirect(clamps the code to{sso_failed, email_belongs_to_dify_account}, relative location, urlencoded) and a catch-alltry/exceptthat logs the full exception server-side and redirects generic — so no unforeseen raise can leak a raw page.user_codeis extracted from claims before the nonce is consumed so a replay redirect still carries it. Email special case keeps its audit emit; success path unchanged.web) — dedicated full-screenerror_ssoview (replaces the inline banner), scrubssso_error+user_codefrom the URL on mount, and a single "← Back to login options" button that re-checks the code (valid → chooser, dead → expired, none → code entry). Raw error code is never rendered — copy comes from the existingssoErrorCopytable.Cross-repo seam
Go enterprise relays a failure to
<idp_callback_url>?sso_error=sso_failed&user_code=<code>; Python owns the single relative/devicemapping. Companion PR: langgenius/dify-enterprisefix/device-flow-sso-error-ux.Security / invariants
/deviceredirects stay relative (same-origin proxy convention).user_codestays PENDING (error paths are read-only on device state).Tests
pytest tests/unit_tests/controllers/openapi/test_device_sso.py15/15; web vitestapp/device12/12. Two pre-existing claim tests updated 400→302 (now also assert the redirect target — stricter).Follow-up (not blocking)