Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docs/incoming/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ class Filters extends BaseConfig

You can apply filters to all requests of a certain HTTP method, like `POST`, `GET`, `PUT`, etc. Its value would be an array of filters to run:

When a POST request uses [method spoofing](methodspoofing.md), `$methods` uses the spoofed method. For example, a POST form with `_method=PUT` runs the filters configured for `PUT`, not those configured for `POST`.

```php
<?php

Expand All @@ -327,8 +329,11 @@ class Filters extends BaseConfig
* @var array<string, list<string>>
*/
public array $methods = [
'POST' => ['invalidchars', 'csrf'],
'GET' => ['csrf'],
'GET' => ['csrf'],
'POST' => ['invalidchars', 'csrf'],
'PUT' => ['invalidchars', 'csrf'],
'PATCH' => ['invalidchars', 'csrf'],
'DELETE' => ['invalidchars', 'csrf'],
];

// ...
Expand Down
2 changes: 1 addition & 1 deletion docs/incoming/methodspoofing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To spoof the method, a hidden input is added to the form with the name of `_meth
</form>
```

This form is converted into a PUT request and is a true PUT request as far as the routing and the IncomingRequest class are concerned.
This form is converted into a PUT request and is a true PUT request as far as routing, the IncomingRequest class, and [method-specific filters](filters.md#methods) are concerned. For example, a POST form with `_method=PUT` runs filters configured for `PUT`, not filters configured for `POST`. If you configure CSRF or other security filters by method, include every method you need to protect.

The form that you are using must be a POST request. GET requests cannot be spoofed.

Expand Down
11 changes: 8 additions & 3 deletions docs/libraries/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class Filters extends BaseConfig
}
```

It is also possible to enable the CSRF filter only for specific methods:
It is also possible to enable the CSRF filter only for specific methods. Include every method that needs CSRF protection, including methods reached through [method spoofing](../incoming/methodspoofing.md):

```php
<?php
Expand All @@ -268,14 +268,19 @@ class Filters extends BaseConfig
* @var array<string, list<string>>
*/
public array $methods = [
'GET' => ['csrf'],
'POST' => ['csrf'],
'GET' => ['csrf'],
'POST' => ['csrf'],
'PUT' => ['csrf'],
'PATCH' => ['csrf'],
'DELETE' => ['csrf'],
];

// ...
}
```

The `GET` entry lets `form_open()` add a CSRF field to forms displayed on GET pages. The built-in CSRF filter verifies only POST, PUT, PATCH, and DELETE requests.

!!! warning "Warning"
If you use `$methods` filters, you should [disable Auto Routing (Legacy)](../incoming/routing.md#use-defined-routes-only) because [Auto Routing (Legacy)](../incoming/routing.md#auto-routing-legacy) permits any HTTP method to access a controller. Accessing the controller with a method you don't expect could bypass the filter.

Expand Down