Repository navigation
fix(csrf): recover from an expired CSRF token without a page reload - #43655
Conversation
Code Review Agent Run #80ef48Actionable Suggestions - 0Additional Suggestions - 1
Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #43655 +/- ##
==========================================
- Coverage 80.14% 80.13% -0.01%
==========================================
Files 2923 2924 +1
Lines 172121 172253 +132
Branches 39952 39986 +34
==========================================
+ Hits 137945 138041 +96
- Misses 31599 31625 +26
- Partials 2577 2587 +10
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:
|
3562966 to
92df00d
Compare
Code Review Agent Run #81ca4fActionable Suggestions - 0Additional Suggestions - 4
Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
|
Hey @saurabhdamle11, this looks solid, the CSRF_ERROR type plus the shared refresh promise and single-retry cap match what's in #43550, and the tests cover the concurrent-refresh and give-up-after-one-replay cases well. GitHub shows conflicts with master though, so it needs a rebase before it can merge. Holler once that's sorted and I'll take another look. |
Signed-off-by: saurabhdamle11 <[email protected]>
92df00d to
8968c62
Compare
|
@rusackas rebased onto master and force-pushed, conflicts are resolved. Whenever you get a chance, pls take a look. Thanks! |
Code Review Agent Run #2ce1eaActionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
✅ Deploy Preview for superset-docs-preview ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Code Review Agent Run #ba3cc5Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
CI's messages.pot drift check was failing on this PR (and on master generally) because apache#43655's CSRF-recovery strings ("Reload the page and try again to continue.", "Security token error", "The CSRF token could not be validated.") were never synced into messages.pot or the .po catalogs. Regenerated with the pinned Babel/jinja2 toolchain via scripts/translations/babel_update.sh; verified no existing translations regressed (translated/fuzzy counts unchanged across all 28 catalogs, only the 3 new strings land untranslated). Co-Authored-By: Evan Rusackas <[email protected]> Co-Authored-By: Claude Sonnet 5 <[email protected]>
CI's messages.pot drift check was failing on this PR (and on master generally) because apache#43655's CSRF-recovery strings ("Reload the page and try again to continue.", "Security token error", "The CSRF token could not be validated.") were never synced into messages.pot or the .po catalogs. Regenerated with the pinned Babel/jinja2 toolchain via scripts/translations/babel_update.sh; verified no existing translations regressed (translated/fuzzy counts unchanged across all 28 catalogs, only the 3 new strings land untranslated). Co-Authored-By: Evan Rusackas <[email protected]> Co-Authored-By: Claude Sonnet 5 <[email protected]>
…43655) Signed-off-by: saurabhdamle11 <[email protected]> (cherry picked from commit 61a5d0e)
SUMMARY
Fixes #43550.
When a cached CSRF token expired, JSON API mutations failed with an HTTP 400
GENERIC_BACKEND_ERROR(issue code 1011).SupersetClienthandled HTTP 401 butnot CSRF-related 400s, so it neither refreshed the token nor retried the request.
The user saw a generic error and had to reload the page before the action (e.g.
saving a dataset) would succeed.
This change lets Superset recover from an expired CSRF token in JSON requests
without a page reload:
views/error_handling.pynow returns a dedicated, machine-readableCSRF_ERRORtype for JSON requests instead of a generic backend error. Theexisting non-JSON login redirect (from feat: redirect to /login when CSRF expired #14675) is unchanged.
CSRF_ERRORisadded to
errors.pyand toSAFE_ERROR_TYPESinerror_sanitization.pysothe type survives guest-user sanitization.
SupersetClientClassdetects that error type, fetches a fresh token from/api/v1/security/csrf_token/, and retries the original request once.concurrently, only one token refresh is issued rather than one per request.
Retry is capped at a single attempt, so a request that fails again cannot
trigger a refresh/retry loop.
CsrfErrorMessagecomponent, registered insetupErrorMessages, forthe case where recovery is not possible — the replay was rejected too, most
often because the session itself is gone.
Why a new
CSRF_ERRORrather than the existingFRONTEND_CSRF_ERROR:FRONTEND_CSRF_ERRORis a client-originated marker and is never emitted by thebackend. This is a server-emitted rejection from Flask-WTF, so it needs its own
type; reusing the frontend one would conflate two different origins.
Security note: this does not bypass or weaken CSRF validation. The refresh
only runs after Flask-WTF has already rejected the request, and only when the
server explicitly identifies the failure as
CSRF_ERROR. The new token isissued against the existing authenticated session, the refresh runs in the
same-origin application context (a cross-site attacker cannot execute it or read
the response), and the single-retry cap means a genuinely forged request fails
after one replay rather than looping.
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A. The recovery is transparent; the visible change is the absence of an
error. The one new visual surface is
CsrfErrorMessage, shown only when thetoken refresh itself fails.
TESTING INSTRUCTIONS
superset_config.py:request to /api/v1/security/csrf_token/ is issued and the original save
request is retried once and succeeds.
token refresh request is issued.
pytest tests/unit_tests/views/test_error_handling.py
cd superset-frontend
npm run test -- packages/superset-ui-core/test/connection/SupersetClientCsrfRecovery.test.ts
npm run test -- src/components/ErrorMessage/CsrfErrorMessage.test.tsx
ADDITIONAL INFORMATION
Note for reviewers: this adds a public refreshCSRFToken() method on
SupersetClientClass and a new CSRF_ERROR member to the exported
SupersetApiErrorType / ErrorTypeEnum in @superset-ui/core. Both are
additive and backwards-compatible, but they are consumable surface for plugins.