Skip to content

Commit d632fa1

Browse files
authored
Merge pull request #384 from datamweb/update-shield-setup-command
feat: update `shield:setup` for Security Setup
2 parents 7b9892f + 1a2bd93 commit d632fa1

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

docs/install.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ These instructions assume that you have already [installed the CodeIgniter 4 app
1515
> **Note**
1616
> CodeIgniter Shield requires Codeigniter v4.2.3 or later.
1717
18-
> **Note**
19-
> You must set ``Config\Security::$csrfProtection`` to `'session'` (or set `security.csrfProtection = session` in your `.env` file) for security reasons, if you use Session Authenticator.
20-
2118
Installation is done through [Composer](https://getcomposer.org). The example assumes you have it installed globally.
2219
If you have it installed as a phar, or othewise you will need to adjust the way you call composer itself.
2320

@@ -89,7 +86,7 @@ If you get `Specified key was too long` error:
8986

9087
### Command Setup
9188

92-
1. Run the following command. This command handles steps 1-3 of *Manual Setup* and runs the migrations.
89+
1. Run the following command. This command handles steps 1-4 of *Manual Setup* and runs the migrations.
9390

9491
```
9592
> php spark shield:setup
@@ -137,6 +134,8 @@ This requires that all of your controllers extend the `BaseController`, but that
137134
service('auth')->routes($routes);
138135
```
139136

137+
4. **Security Setup** Set `Config\Security::$csrfProtection` to `'session'` (or set `security.csrfProtection = session` in your `.env` file) for security reasons, if you use Session Authenticator.
138+
140139
## Controller Filters
141140

142141
Shield provides 4 [Controller Filters](https://codeigniter.com/user_guide/incoming/filters.html) you can

src/Commands/Setup.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ private function publishConfig(): void
8787
$this->setupHelper();
8888
$this->setupRoutes();
8989

90+
$this->setSecurityCSRF();
91+
9092
$this->runMigrations();
9193
}
9294

@@ -258,6 +260,42 @@ private function setupRoutes(): void
258260
$this->add($file, $check, $pattern, $replace);
259261
}
260262

263+
/**
264+
* @see https://github.com/codeigniter4/shield/security/advisories/GHSA-5hm8-vh6r-2cjq
265+
*/
266+
private function setSecurityCSRF(): void
267+
{
268+
$file = 'Config/Security.php';
269+
$replaces = [
270+
'public $csrfProtection = \'cookie\';' => 'public $csrfProtection = \'session\';',
271+
];
272+
273+
$path = $this->distPath . $file;
274+
$cleanPath = clean_path($path);
275+
276+
if (! is_file($path)) {
277+
CLI::error(" Not found file '{$cleanPath}'.");
278+
279+
return;
280+
}
281+
282+
$content = file_get_contents($path);
283+
$output = $this->replacer->replace($content, $replaces);
284+
285+
// check $csrfProtection = 'session'
286+
if ($output === $content) {
287+
CLI::write(CLI::color(' Security Setup: ', 'green') . 'Everything is fine.');
288+
289+
return;
290+
}
291+
292+
if (write_file($path, $output)) {
293+
CLI::write(CLI::color(' Updated: ', 'green') . "We have updated file '{$cleanPath}' for security reasons.");
294+
} else {
295+
CLI::error(" Error updating file '{$cleanPath}'.");
296+
}
297+
}
298+
261299
private function runMigrations(): void
262300
{
263301
if (

tests/Commands/SetupTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ public function testRun(): void
6060
$routes = file_get_contents($appFolder . 'Config/Routes.php');
6161
$this->assertStringContainsString('service(\'auth\')->routes($routes);', $routes);
6262

63+
$security = file_get_contents($appFolder . 'Config/Security.php');
64+
$this->assertStringContainsString('public $csrfProtection = \'session\';', $security);
65+
6366
$result = str_replace(["\033[0;32m", "\033[0m"], '', CITestStreamFilter::$buffer);
6467

6568
$this->assertStringContainsString(
6669
' Created: vfs://root/Config/Auth.php
6770
Created: vfs://root/Config/AuthGroups.php
6871
Updated: vfs://root/Controllers/BaseController.php
69-
Updated: vfs://root/Config/Routes.php',
72+
Updated: vfs://root/Config/Routes.php
73+
Updated: We have updated file \'vfs://root/Config/Security.php\' for security reasons.',
7074
$result
7175
);
7276
$this->assertStringContainsString(

0 commit comments

Comments
 (0)