You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-11Lines changed: 29 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -142,21 +142,26 @@ or render input element basis.
142
142
```
143
143
### CSRF Protections
144
144
145
-
CSRF protection is **opt-in**. A form that uses `SetAntiCsrfTrait` is wired
146
-
with an `AntiCsrfInterface`, but the token is only verified when the
147
-
validated method is annotated with `#[CsrfProtection]`. Methods without
148
-
`#[CsrfProtection]` perform no CSRF check even if the form supports it.
145
+
CSRF protection is **opt-in** and can be enabled through either of two
146
+
independent paths:
147
+
148
+
-**Per-form**: add `use SetAntiCsrfTrait;` to the form. `AntiCsrfInterface`
149
+
is injected by Ray.Di through the trait's `#[Inject]` setter, the token
150
+
field is added in `postConstruct()`, and every `apply()` call verifies
151
+
the token.
152
+
-**Per-action**: annotate the validated controller method with
153
+
`#[CsrfProtection]`. `AuraInputInterceptor` then injects
154
+
`AntiCsrfInterface` into the form before `apply()` runs.
155
+
156
+
Either path causes `AbstractForm::apply()` to throw `CsrfViolationException`
157
+
on token mismatch. Without either path, no CSRF check is performed. Combining
158
+
both paths is harmless but redundant — pick whichever fits your use case.
159
+
160
+
Per-action — declare CSRF on the controller method:
149
161
150
162
```php
151
-
use Ray\WebFormModule\AbstractAuraForm;
152
163
use Ray\WebFormModule\Annotation\CsrfProtection;
153
164
use Ray\WebFormModule\Annotation\FormValidation;
154
-
use Ray\WebFormModule\SetAntiCsrfTrait;
155
-
156
-
class MyForm extends AbstractAuraForm
157
-
{
158
-
use SetAntiCsrfTrait;
159
-
}
160
165
161
166
class MyController
162
167
{
@@ -167,6 +172,19 @@ class MyController
167
172
}
168
173
}
169
174
```
175
+
176
+
Per-form — declare CSRF on the form itself:
177
+
178
+
```php
179
+
use Ray\WebFormModule\AbstractForm;
180
+
use Ray\WebFormModule\SetAntiCsrfTrait;
181
+
182
+
class MyForm extends AbstractForm
183
+
{
184
+
use SetAntiCsrfTrait;
185
+
}
186
+
```
187
+
170
188
You can provide your custom `AntiCsrf` class. See more detail at [Aura.Input](https://github.com/auraphp/Aura.Input#applying-csrf-protections)
0 commit comments