|
2 | 2 | <p align="center"> |
3 | 3 | <br><br> |
4 | 4 | <img src="https://leafphp.netlify.app/assets/img/leaf3-logo.png" height="100"/> |
5 | | - <h1 align="center">Leaf Security Module</h1> |
| 5 | + <h1 align="center">Leaf Anchor CSRF [BETA]</h1> |
6 | 6 | <br><br> |
7 | 7 | </p> |
8 | 8 |
|
9 | 9 | # Leaf PHP |
10 | 10 |
|
11 | | -[](https://packagist.org/packages/leafs/anchor) |
12 | | -[](https://packagist.org/packages/leafs/anchor) |
13 | | -[](https://packagist.org/packages/leafs/anchor) |
| 11 | +[](https://packagist.org/packages/leafs/csrf) |
| 12 | +[](https://packagist.org/packages/leafs/csrf) |
| 13 | +[](https://packagist.org/packages/leafs/csrf) |
14 | 14 |
|
15 | | -This package contains leaf's utils for deep sanitizing of data and basic security provided for your app data. |
| 15 | +> This is an experimental module. Please open an issue if you notice any bugs or malfunctions. |
| 16 | +
|
| 17 | +This package is leaf's implementation of CSRF default protection with leaf anchor. It comes separated from leaf anchor because it is not needed in every project you may build. |
16 | 18 |
|
17 | 19 | ## Installation |
18 | 20 |
|
19 | | -You can easily install Leaf using [Composer](https://getcomposer.org/). |
| 21 | +You can easily install Leaf CSRF using [Composer](https://getcomposer.org/). |
20 | 22 |
|
21 | 23 | ```bash |
22 | | -composer require leafs/anchor |
| 24 | +composer require leafs/csrf |
23 | 25 | ``` |
24 | 26 |
|
25 | 27 | ## Basic Usage |
26 | 28 |
|
27 | | -After [installing](#installation) anchor, create an _index.php_ file. |
| 29 | +After installing leaf CSRF, leaf automatically loads the CSRF package for you, so you don't need to do anything unless you want to configure the CSRF module to match your application requirements. |
| 30 | + |
| 31 | +### Using CSRF outside of leaf |
28 | 32 |
|
29 | | -### Base XSS protection |
| 33 | +Most leaf modules can be used outside of leaf. This module is one of these global modules. If you decide to use the CSRF module outside of leaf, you will need to manually initialize the package. |
30 | 34 |
|
31 | 35 | ```php |
32 | | -<?php |
33 | | -require __DIR__ . "vendor/autoload.php"; |
| 36 | +Leaf\Anchor\CSRF::init(); |
| 37 | +``` |
34 | 38 |
|
35 | | -$data = $_POST["data"]; |
36 | | -$data = Leaf\Anchor::sanitize($data); |
| 39 | +This function generates a token with a secret and a random hash and saves that in a session. If no session exists, the CSRF module will create a session for your app and save the token in that session, |
37 | 40 |
|
38 | | -echo $data; |
39 | | -``` |
| 41 | +### Config |
40 | 42 |
|
41 | | -This also works on arrays |
| 43 | +Just like every other leaf module, this module also allows you to customize it to behave in any way you want it to behave. Also, since this module is built on the Anchor module, the config object is shared with Anchor. To set any configuration, simply call the `config` method. |
42 | 44 |
|
43 | | -```php |
44 | | -<?php |
45 | | -require __DIR__ . "vendor/autoload.php"; |
| 45 | +**Available config:** |
46 | 46 |
|
47 | | -$data = Leaf\Anchor::sanitize($_POST); |
| 47 | +- **SECRET_KEY** - This is the key with which the token is saved and used in your leaf app. If this is not specified, leaf uses the name `_token` as done in other frameworks like Laravel. |
48 | 48 |
|
49 | | -echo $data["input"]; |
50 | | -``` |
| 49 | +- **SECRET** - This is the secret key used to encrypt the token. Leaf also has a default secret key set for you. Note that the secret key is attached to a set of unique numbers that not even leaf knows. |
51 | 50 |
|
52 | | -You may quickly test this using the built-in PHP server: |
| 51 | +- **EXCEPT** - This is an array of routes that you want to exclude from the CSRF protection. |
53 | 52 |
|
54 | | -```bash |
55 | | -php -S localhost:8000 |
| 53 | +- **METHODS** - This is an array of HTTP methods to apply CSRF protection to. By default, leaf uses `["POST", "PUT", "PATCH", "DELETE"]` |
| 54 | + |
| 55 | +```php |
| 56 | +use Leaf\Anchor\CSRF; |
| 57 | + |
| 58 | +CSRF::config([ |
| 59 | + "METHODS" => ["GET"], |
| 60 | + "EXCEPT" => ["/"], |
| 61 | +]); |
56 | 62 | ``` |
57 | 63 |
|
58 | 64 | Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app) |
0 commit comments