CSRF Prevention
See original GitHub issueHi,
Are we able to describe how CSRF is prevented?
When we deliver actions via POST in the Dash interface, I understand that preflight requests are made to the server from the browser for non-standard headers like application/json
, indicated in the OWASP guide https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet under “Content-Type Header Validation” and Mozilla https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
Is this how Dash and DPD are preventing CSRF by default? I have spent a while looking at both Dash and DPD but am having a difficult time decoding whether we should be implementing our own CSRF token methods.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (10 by maintainers)
Top Results From Across the Web
Cross-Site Request Forgery Prevention Cheat Sheet
A CSRF attack works because browser requests automatically include all cookies including session cookies. Therefore, if the user is authenticated to the site, ......
Read more >CSRF Attacks: Anatomy, Prevention, and XSRF Tokens
Cross-site Request Forgery, also known as CSRF, Sea Surf, or XSRF, is an attack whereby an attacker tricks a victim into performing actions...
Read more >Protecting Your Users Against CSRF - Hacksplaining
Protecting against CSRF (commonly pronounced “sea-surf”) requires two things: ensuring that GET requests are side-effect free, and ensuring that non-GET ...
Read more >6 CSRF Mitigation Techniques You Must Know - Bright Security
1. Token Synchronization ... CSRF tokens help prevent CSRF attacks because attackers cannot make requests to the backend without valid tokens.
Read more >What is cross-site request forgery? - Invicti
How to Prevent Cross-Site Request Forgery Attacks ... An attacker can launch a CSRF attack when he knows which parameters and value combination ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As far as I can tell from my first cut I have this working as per your thoughts.
Django’s
get_token
method is called from the initiallayout
method and the token is assigned as a replacement value for a component in the layout using thewalk_tree_and_replace
method in WrappedDash. Django’s CsrfViewMiddleware is extended to search for a component with ‘csrfmiddlewaretoken’ in the body if its a POST request and no csrf token is otherwise found.I have a few couple of thoughts as to how this can be improved e.g.
I will revisit the code again this week and send a pull request.
From this I’m wondering if something along the lines of the following might work:
django.middleware.csrf.CsrfViewMiddleware
(if needed) with an enhanced version that is the Django middleware for all non-Dash views, and instead picks out the CSRF token from the hidden component when in useThis would have the advantage of keeping things fairly close to (and consistent with) the main Django CSRF approach.