What if a stranger could change your password just by visiting a website?
Why CSRF protection in Flask? - Purpose & Use Cases
Imagine you have a web form where users submit sensitive data, like changing their password. Without protection, a hacker could trick users into submitting unwanted requests by embedding malicious forms on other sites.
Manually checking every request for legitimacy is complicated and easy to forget. Attackers can exploit this to perform actions on behalf of users without their consent, leading to security breaches.
CSRF protection automatically adds a secret token to forms and verifies it on submission, ensuring requests come from trusted users and not attackers.
if request.method == 'POST': # no token check update_password()
@app.route('/change-password', methods=['POST']) # Automatically protected by CSRFProtect(app) from flask_wtf.csrf # csrf = CSRFProtect(app) def change_password(): update_password()
It enables secure user interactions by preventing unauthorized actions triggered from other sites.
When you change your email or password on a website, CSRF protection stops hackers from silently changing it for you without your knowledge.
Manual request checks are error-prone and risky.
CSRF protection uses secret tokens to verify requests.
This keeps user actions safe from cross-site attacks.