Skip to content
FunnelGuard

Fix guide by FunnelsGuard

CSRF on forms: how to find and fix missing form protection

OPS-180Medium impactOther toolBy FunnelGuardPublished Updated
On this page

What this issue is

Browsers automatically attach your cookies to any request sent to your site, even when the request starts on a different website. If your form handler trusts the cookie alone, an attacker's page can submit a hidden form to your site and your server will treat it as a real action by the logged-in user.

A CSRF token is a random value your server puts in the form and checks when the form comes back. A third-party page cannot read it, so it cannot forge a valid request.

Why it costs you leads or sales

A forged request can change a customer's email, address or settings, place or alter orders, or let an attacker act as a logged-in admin. That damages trust, creates refund and support work, and can trigger data-protection duties if personal data is changed or exposed.

It can also pollute your lead data. Forged submissions to unprotected forms fill your CRM and inbox with junk, and you waste time sorting real leads from fake ones.

How a CSRF attack unfolds
  1. Customer logged inSession cookie stored in the browser
  2. Visits a bad pageHidden form auto-submits to your site
  3. Your server accepts itNo token or origin check
  4. Account changedEmail, address or order altered
  5. Trust and revenue lostSupport load, refunds, complaints

The break is at the server: it accepts the request because the cookie is valid, without checking where it came from.

How to check if your site has it

Only test sites you own or have written permission to test. Start with a quick look at the form markup, then try a controlled replay.

Check a form for CSRF protection
  1. Inspect the form HTML

    Open the page, right-click the form, choose Inspect, and look for a hidden input named like csrf_token, _token, authenticity_token, __RequestVerificationToken or _wpnonce. No hidden token on a logged-in, state-changing form is a warning sign.

  2. Check the cookies

    In browser DevTools, open Application (or Storage) and Cookies. Look at the SameSite attribute on your session cookie. Missing or None raises risk.

  3. Replay the request without the token

    In the Network tab, copy the form submission as cURL, remove the token field, and send it from a test account on a staging copy. If the server still accepts it, the form is not protected.

  4. Try a wrong Origin

    Send the same request with an Origin header set to another domain. A protected server rejects it with 403.

  5. Run a security scan

    Use a web application security scanner or a penetration tester to cover every form and endpoint, not just the ones you thought of.

How to fix it

Use layers. The token is the main defence. SameSite cookies and Origin checks add protection if one layer is missed. Never use GET requests for actions that change data.

Add CSRF protection by platform
  1. Use nonces in custom forms

    Add wp_nonce_field('my_action','my_nonce') inside the form and check it with check_admin_referer or wp_verify_nonce before processing.

  2. Update form plugins

    Keep your form plugin and theme updated. Well-maintained plugins already use nonces. Replace plugins that are abandoned.

Form with a CSRF token (generic example)html
<form method="post" action="/account/update">
  <input type="hidden" name="csrf_token" value="{{ csrf_token }}">
  <input type="email" name="email" required>
  <button type="submit">Save</button>
</form>
WordPress nonce: add and verifyphp
<?php
// In the form
wp_nonce_field( 'save_profile', 'profile_nonce' );

// In the handler
if ( ! isset( $_POST['profile_nonce'] ) || ! wp_verify_nonce( $_POST['profile_nonce'], 'save_profile' ) ) {
    wp_die( 'Invalid request', 403 );
}
Session cookie header with SameSitetext
Set-Cookie: session=abc123; Secure; HttpOnly; SameSite=Lax; Path=/
Form handling before and after

Unprotected

  • Server trusts the session cookie only
  • No hidden token in the form
  • Cookie has no SameSite attribute
  • Actions allowed over GET

Protected

  • Per-session token checked on every POST
  • SameSite=Lax cookies with Secure and HttpOnly
  • Origin header must match your domain
  • State changes only over POST

How to confirm it's fixed

Verify the fix
  1. Submit normally

    Use the form as a real user and confirm it still works and the token is in the markup.

  2. Remove the token

    Replay the request without the token. The server should return 403 or a validation error, and nothing should change.

  3. Use a wrong token and wrong Origin

    Both should be rejected.

  4. Check cookie flags

    In DevTools confirm SameSite, Secure and HttpOnly on the session cookie.

  5. Test cached pages

    Make sure caching or a CDN does not serve one visitor's token to others, and that tokens do not expire so fast that real users see errors.

How to prevent it happening again

Ongoing CSRF hygiene
  • Keep CSRF middleware enabled by default and review any exemptions
  • Add a CSRF test to code review for every new form or endpoint
  • Update CMS, plugins and themes promptly
  • Use POST, PUT or DELETE for any change, never GET
  • Set SameSite, Secure and HttpOnly on all session cookies
  • Run a security scan or penetration test after major releases
  • Exclude token-bearing pages from shared caching

How FunnelGuard handles this

CSRF is outside what FunnelGuard checks. It needs a web application security tool, such as a vulnerability scanner or a penetration test, that probes your forms and server responses for missing tokens and origin checks.

What FunnelGuard does do is run your real lead forms and checkouts on a schedule and confirm they still work after a fix. After you add tokens, a robot visitor submitting the form and checking that the test lead reaches your inbox and CRM can show that the change did not break the journey. That is a check on the form working, not on whether it is secure.

Questions people ask

What is CSRF and how does it work?

CSRF (cross-site request forgery) is an attack where a malicious page makes a visitor's browser send a request to your site. The browser adds the visitor's login cookie automatically, so your server may treat the request as genuine. A secret token in each form stops this.

How do I fix CSRF on my forms?

Add a random, per-session token to each state-changing form and verify it on the server before acting. Also set SameSite=Lax on session cookies and check the Origin header. Most frameworks and CMSs have built-in support, so turn it on rather than building your own.

Does a contact form need CSRF protection?

A public contact form with no login has little to forge, so risk is lower. Protection is still useful against forced junk submissions and is essential on any form tied to a logged-in account, checkout or admin screen.

Does SameSite=Lax stop CSRF?

It blocks cookies on most cross-site POST requests, which helps a lot, but it is not a full fix. It has exceptions, browser behaviour differs, and same-site attacks still work. Use it together with CSRF tokens.

Why do I get an invalid nonce or token expired error on my form?

The token is missing, expired or doesn't match the session. Common causes are full-page caching that serves an old token, a session that timed out, or a form left open too long. Exclude the page from caching or load the token with a small request on page load.

How do I test my site for CSRF?

On a site you own, copy a form submission from DevTools, remove the token, and replay it from a logged-in test account on staging. If it succeeds, the form is vulnerable. A security scanner can check all forms at scale.

Can FunnelGuard detect CSRF vulnerabilities?

No. CSRF testing needs a web application security scanner or penetration test. FunnelGuard checks that your funnels, tracking and lead delivery work, which is useful for confirming a fix did not break your forms.

Do WordPress and Shopify forms have CSRF protection by default?

Core WordPress features use nonces and Shopify's form tag adds an authenticity token. Custom forms, outdated plugins or hand-built endpoints may not, so check any form you added yourself.

Does FunnelGuard catch this?

Other tool

Needs a different kind of tool.

Stop finding out from your customers

FunnelGuard runs your real forms and checkouts on a schedule, checks the tracking and the leads behind them, and alerts you once, with a screenshot. Try any paid plan free for 3 days.