feat: make CSRF cookie and header names configurable#3745
Draft
yosofbadr wants to merge 2 commits intoplotly:devfrom
Draft
feat: make CSRF cookie and header names configurable#3745yosofbadr wants to merge 2 commits intoplotly:devfrom
yosofbadr wants to merge 2 commits intoplotly:devfrom
Conversation
Add `csrf_token_name` and `csrf_header_name` config options to the Dash
constructor, allowing users to match the CSRF cookie/header names used
by their server framework (e.g. Django uses 'csrftoken').
The values flow through Dash's standard config pipeline: Python
`app.config` -> HTML `_dash-config` JSON -> frontend Redux store.
`getCSRFHeader()` now reads these from config with backward-compatible
defaults ('_csrf_token' / 'X-CSRFToken'). When the configured cookie
is absent, no CSRF header is sent (previously sent undefined value).
Fixes plotly#729
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.
Summary
csrf_token_nameandcsrf_header_nameconfig options to theDashconstructorcsrftokeninstead of_csrf_token)app.config-> HTML_dash-configJSON -> frontend Redux storegetCSRFHeader()now reads cookie/header names from config with backward-compatible defaultsundefinedvalue)Usage
Motivation
Dash's frontend hardcodes the CSRF cookie name as
_csrf_token, which conflicts with Django's defaultcsrftoken. Users embedding Dash in Django apps with CSRF middleware enabled cannot use Dash without project-wide Django config changes. See #729 for full history.This follows the architecture recommended by @chriddyp in #729: config variables should flow through Dash's standard config pipeline, not through hooks.
Prior art
PR #990 attempted this via the hooks system but was closed without review. This PR instead uses the config pipeline as maintainers requested.
Files changed
dash/dash.pydash-renderer/src/actions/index.jsgetCSRFHeader(config)with dynamic cookie/header namesdash-renderer/src/actions/api.jsdash-renderer/src/actions/callbacks.tsgetCSRFHeaderdash-renderer/src/config.tsDashConfigtypetests/unit/test_configs.pyCHANGELOG.mdTest plan
CsrfViewMiddlewareactiveFixes #729