{"id":6004,"date":"2025-11-29T02:26:51","date_gmt":"2025-11-29T02:26:51","guid":{"rendered":"https:\/\/cybersecurityinfocus.com\/?p=6004"},"modified":"2025-11-29T02:26:51","modified_gmt":"2025-11-29T02:26:51","slug":"csrf-protection-with-https","status":"publish","type":"post","link":"https:\/\/cybersecurityinfocus.com\/?p=6004","title":{"rendered":"CSRF Protection with HTTPS"},"content":{"rendered":"<h2>TL;DR<\/h2>\n<p>HTTPS protects data <em>in transit<\/em>, but doesn\u2019t stop Cross-Site Request Forgery (CSRF) attacks. This guide shows how to add CSRF tokens to your web application running on HTTPS for better security.<\/p>\n<h2>What is CSRF and Why it Matters Even with HTTPS<\/h2>\n<p>Cross-Site Request Forgery lets an attacker trick a logged-in user into performing unwanted actions on a website.  HTTPS encrypts communication, but doesn\u2019t verify the <em>origin<\/em> of requests. A malicious site can still send valid requests to your server if the user is authenticated.<\/p>\n<h2>Implementing CSRF Protection<\/h2>\n<p>Generate Unique Tokens: Your server needs to create a unique, unpredictable token for each user session (or even per-form). This token will be included with sensitive forms.<\/p>\n<p>Use a cryptographically secure random number generator.<br \/>\nStore the token securely on the server, associated with the user\u2019s session.<\/p>\n<p>Include Token in Forms: Add a hidden field to every form that performs sensitive actions (e.g., changing passwords, making purchases).<br \/>\n&lt;form action=&#8221;\/change-password&#8221; method=&#8221;post&#8221;&gt;<br \/>\n  &lt;input type=&#8221;hidden&#8221; name=&#8221;csrf_token&#8221; value=&#8221;{{ session[&#8216;csrf_token&#8217;] }}&#8221;&gt;<br \/>\n  &#8230; other form fields &#8230;<br \/>\n&lt;\/form&gt;<\/p>\n<p>Validate Token on Server-Side: When the form is submitted, your server <em>must<\/em> verify that the received token matches the one stored in the user\u2019s session.<\/p>\n<p>Compare the submitted token with the session token.<br \/>\nIf they don\u2019t match, reject the request immediately.  Do not process it!<br \/>\nConsider regenerating the token after validation to prevent replay attacks.<\/p>\n<p>Example Python (Flask) Code:<br \/>\nfrom flask import Flask, render_template, session, request<br \/>\nimport secrets<\/p>\n<p>app = Flask(__name__)<br \/>\napp.secret_key = &#8216;your-secret-key&#8217; # Change this!<\/p>\n<p>@app.route(&#8216;\/login&#8217;, methods=[&#8216;POST&#8217;])<br \/>\n  # &#8230; login logic &#8230;<br \/>\n  session[&#8216;csrf_token&#8217;] = secrets.token_hex(16) # Generate token on login<br \/>\n  return render_template(&#8216;home.html&#8217;)<\/p>\n<p>@app.route(&#8216;\/change-password&#8217;, methods=[&#8216;POST&#8217;])<br \/>\n  if request.form[&#8216;csrf_token&#8217;] == session[&#8216;csrf_token&#8217;]:<br \/>\n    # Process password change<br \/>\n    session[&#8216;csrf_token&#8217;] = secrets.token_hex(16) # Regenerate token<br \/>\n    return &#8216;Password changed!&#8217;<br \/>\n  else:<br \/>\n    return &#8216;CSRF Token Invalid&#8217;, 403<\/p>\n<p>Cookie Considerations: While storing the token in a session is common, you can also store it in an HTTP-only cookie. This offers some protection against JavaScript access but requires careful handling of SameSite attributes.<\/p>\n<p>Set SameSite=Strict or SameSite=Lax on your CSRF cookie to prevent cross-site requests from including the token.<br \/>\nEnsure the cookie is marked as HTTPOnly to protect against XSS attacks.<\/p>\n<p>Double Submit Cookie Pattern: An alternative approach involves storing a random value in both a session and an HTTP-only, SameSite cookie. The server validates that both values match on form submission.<\/p>\n<p>Testing Your Implementation:<\/p>\n<p>Attempt to submit a valid form from your own website (should succeed).<br \/>\nAttempt to submit the same form from a different domain (should fail).<br \/>\nTry submitting a modified form with an incorrect token (should fail).<\/p>\n<h2>Important Notes<\/h2>\n<p>HTTPS is essential: CSRF protection works best when combined with HTTPS.<br \/>\nToken Length: Use sufficiently long and random tokens to prevent guessing. 16 bytes (32 hex characters) is a good starting point.<br \/>\nSession Security: Protect your session management system from attacks like session fixation and hijacking.<\/p>\n<p>The post <a href=\"https:\/\/blog.g5cybersecurity.com\/csrf-protection-with-https\/\">CSRF Protection with HTTPS<\/a> appeared first on <a href=\"https:\/\/blog.g5cybersecurity.com\/\">Blog | G5 Cyber Security<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>TL;DR HTTPS protects data in transit, but doesn\u2019t stop Cross-Site Request Forgery (CSRF) attacks. This guide shows how to add CSRF tokens to your web application running on HTTPS for better security. What is CSRF and Why it Matters Even with HTTPS Cross-Site Request Forgery lets an attacker trick a logged-in user into performing unwanted [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6004","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/6004"}],"collection":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6004"}],"version-history":[{"count":0,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=\/wp\/v2\/posts\/6004\/revisions"}],"wp:attachment":[{"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cybersecurityinfocus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}