Skip to content

[6.x] Front-end forms without CSRF tokens - #20429

Merged
brandonkelly merged 12 commits into
6.xfrom
csrf-without-tokens
Nov 17, 2026
Merged

brandonkelly merged 12 commits into
6.xfrom
csrf-without-tokens

Conversation

@timkelty

@timkelty timkelty commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Front-end forms don't need CSRF tokens anymore, and printing one makes the page uncacheable. Sites choose whether csrfInput() prints one.

  • Same-origin posts pass without a token:
    • Sec-Fetch-Site: same-origin (Laravel 13, default mode)
    • or, without Sec-Fetch-Site, a matching Origin (new, for plain-HTTP sites like http://my-site.test)
  • csrfInput() prints nothing when the host uses preventRequestForgery(originOnly: true), as tokens are never checked then.
  • Removes asyncCsrfInputs.
    • kept in the Yii adapter as a deprecated no-op, so existing configs don't throw
  • In origin-only mode, the Yii adapter still honors $enableCsrfValidation = false on legacy controllers reached through site URL rules, such as plugin webhooks. Laravel's origin check rejects those requests before tokens are checked, so the adapter checks the opt-out there too.

Alternative: keep csrfInput() always printing and deprecate it. Plugin-rendered forms would keep pages uncacheable until each plugin drops the call.

Prior art

Notes

timkelty and others added 2 commits November 6, 2026 18:22
Laravel accepts unsafe requests with Sec-Fetch-Site: same-origin without
a token, and a page that prints one can't be stored by shared caches.

[ci skip]

Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984
Co-authored-by: Amp <amp@ampcode.com>
Front-end forms don't need CSRF inputs, so there's no reason to fill
them in with JavaScript. csrfInput() now always prints csrf_field().

The Yii adapter's GeneralConfig keeps asyncCsrfInputs() as a deprecated
no-op, so existing configs don't throw.

Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984
Co-authored-by: Amp <amp@ampcode.com>
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

📚 Storybook previews

@craftcms/ui — open Storybook

No changed components detected in this Storybook.

resources/js — open Storybook

No changed components detected in this Storybook.

@timkelty
timkelty added this pull request to stack #20442 November 6, 2026 20:25
@riasvdv

riasvdv commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Can we make sure this doesn't break local dev (for example http://my-site.test on Laravel Valet or a http ddev site)? I can vaguely remember removing CSRF still breaking this.

…Site

Browsers only send Sec-Fetch-Site to HTTPS and localhost origins, so
token-less forms on plain-HTTP dev sites like http://my-site.test got a
419. Requests without Sec-Fetch-Site are now accepted when Origin
matches the request's scheme and host, as proposed in
laravel/framework#59198.

Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984
Co-authored-by: Amp <amp@ampcode.com>

timkelty commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@riasvdv It would have. Browsers only send Sec-Fetch-Site to HTTPS and localhost origins. In Chrome, a POST on http://my-site.test sends Origin but no Sec-Fetch-Site, so a form without a token got a 419.

82245ec adds an Origin fallback: when Sec-Fetch-Site is missing, the request is accepted if Origin matches the request's scheme and host. Go's net/http.CrossOriginProtection works the same way, and laravel/framework#59198 proposes the same fallback for Laravel. Only clients that send neither header still need a token.

timkelty and others added 2 commits November 6, 2026 21:36
Under PreventRequestForgery::useOriginOnly() tokens are never checked,
so csrfInput() prints nothing, and plugin-rendered forms no longer keep
their pages from being cached. Rails ties token rendering to its
verification strategy the same way (rails/rails#59370).

Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984
Co-authored-by: Amp <amp@ampcode.com>
Base automatically changed from lazy-site-sessions to 6.x November 9, 2026 13:45
@i-just
i-just deleted the branch 6.x November 16, 2026 15:32
Base automatically changed from 6.x to 5.x November 16, 2026 15:32
@timkelty
timkelty removed this pull request from stack #20442 November 17, 2026 07:29
@timkelty
timkelty changed the base branch from 5.x to 6.x November 17, 2026 07:29
…nly opt-outs

LegacyAwarePreventRequestForgery replaces Craft's binding when the adapter
is installed, so it now extends Craft's PreventRequestForgery. In
origin-only mode, the origin check throws before tokensMatch() runs, so
the legacy site-route CSRF opt-out is also checked in hasValidOrigin().

Amp-Thread-ID: https://ampcode.com/threads/T-01a1e862-60c1-7788-81fa-33b14aae4da5
@timkelty
timkelty marked this pull request as ready for review November 17, 2026 08:10
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T08:13:53.961248Z 029f541 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Global CSRF trust-boundary changes and token suppression require security-focused browser and trusted-proxy integration validation that could not be performed because dependencies were unavailable.

No open issues 🎉

What changed in this PR

This PR lets Craft’s front-end forms use same-origin request checks without CSRF tokens. In origin-only mode, csrfInput() emits nothing, avoiding token-related cache restrictions and asynchronous token requests.

Changes:

  • Adds an Origin header fallback when Sec-Fetch-Site is absent.
  • Makes CSRF input rendering follow origin-only settings and removes asynchronous inputs, retaining the legacy setting as a deprecated no-op.
  • Adds coverage for request validation and preserves legacy controller CSRF opt-outs.
File Description
yii2-adapter/​tests-laravel/​Integration/​LegacySiteRouteCsrfTest.php Tests legacy opt-outs and matching origins in both modes.
yii2-adapter/​src/​Http/​LegacyAwarePreventRequestForgery.php Preserves legacy opt-outs under origin-only checks.
yii2-adapter/​legacy/​helpers/​Html.php Delegates CSRF rendering without legacy options.
yii2-adapter/​legacy/​config/​GeneralConfig.php Retains asyncCsrfInputs as a deprecated no-op.
tests/​Feature/​Http/​Middleware/​PreventRequestForgeryTest.php Tests origin validation and conditional token rendering.
src/​Support/​Html.php Omits CSRF inputs in origin-only mode.
src/​Route/​RouteServiceProvider.php Binds Laravel’s middleware to Craft’s subclass.
src/​Http/​Middleware/​PreventRequestForgery.php Adds matching-origin fallback and exposes origin-only state.
src/​Config/​GeneralConfig.php Removes the core asynchronous CSRF setting.
resources/​templates/​_special/​async-csrf-input.twig Deletes asynchronous token-fetching markup.
CHANGELOG-WIP.md Documents token behavior, caching, and deprecations.

Review effort: Balanced

@brandonkelly
brandonkelly merged commit c49f424 into 6.x Nov 17, 2026
22 checks passed
@brandonkelly
brandonkelly deleted the csrf-without-tokens branch November 17, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants