Repository navigation
[6.x] Front-end forms without CSRF tokens - #20429
Conversation
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>
📚 Storybook previews@craftcms/ui — open Storybook No changed components detected in this Storybook. resources/js — open Storybook No changed components detected in this Storybook. |
|
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>
|
@riasvdv It would have. Browsers only send 82245ec adds an |
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>
[ci skip] Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984 Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-8511a552-c936-45e3-af30-788ff8ef5984 Co-authored-by: Amp <amp@ampcode.com>
…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
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
🔵 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
Originheader fallback whenSec-Fetch-Siteis 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
Amp-Thread-ID: https://ampcode.com/threads/T-fd8229b7-a0fa-4479-ab37-a4da54e2100a Co-authored-by: Amp <amp@ampcode.com>
Front-end forms don't need CSRF tokens anymore, and printing one makes the page uncacheable. Sites choose whether
csrfInput()prints one.Sec-Fetch-Site: same-origin(Laravel 13, default mode)Sec-Fetch-Site, a matchingOrigin(new, for plain-HTTP sites likehttp://my-site.test)PreventRequestForgerysubclass bound over Laravel's, until [13.x] Improve origin verification in PreventRequestForgery laravel/framework#59198 landsLegacyAwarePreventRequestForgery, which replaces that binding when the adapter is installed, now extends it, so sites with the adapter get the fallback toocsrfInput()prints nothing when the host usespreventRequestForgery(originOnly: true), as tokens are never checked then.asyncCsrfInputs.$enableCsrfValidation = falseon 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
Sec-Fetch-Sitechecking by default (Use a modern approach for cross-site request forgery protection rails/rails#56350), and no tokens rendered under it (Don't render authenticity tokens under the header_only CSRF strategy rails/rails#59370)CrossOriginProtection:Sec-Fetch-Site, thenOrigin, no tokens (write-up)Originfallback proposed in [13.x] Improve origin verification in PreventRequestForgery laravel/framework#59198Notes
asyncCsrfInputswas a stop-gap for Cloud cacheability, and contentious (Async CSRF token generation on user interaction #15427, [5.x]: asyncCsrfInput true or false negatively impacts page speed #18022, [4.x] & probs [5.x]: Multi site bug with Craft's JS fetches when using asyncCsrfInputs(true) #18006).Sec-Fetch-SitenorOrigin: token-less posts get a 419, or a 403 underoriginOnly.PreventRequestForgerydon't get theOriginfallback, unless they extend Craft's instead.