Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Security Configuration #3836

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9ff90b9
Separate Security Config
mostafakhudair Oct 30, 2020
731f69f
Separate Security Configuration
mostafakhudair Oct 30, 2020
1466b63
Separate Security Configuration
mostafakhudair Oct 30, 2020
dcece99
Separate Security Configuration
mostafakhudair Oct 30, 2020
2ba203f
Separate Security Configuration
mostafakhudair Oct 30, 2020
538e9ea
Separate Security Configuration
mostafakhudair Oct 30, 2020
a564d92
Separate Security Configuration
mostafakhudair Oct 30, 2020
ce82b15
Separate Security Configuration
mostafakhudair Oct 30, 2020
fbfe7a1
Separate Security Configuration
mostafakhudair Oct 30, 2020
132b99b
Separate Security Configuration
mostafakhudair Oct 30, 2020
9294f29
Separate Security Configuration
mostafakhudair Oct 30, 2020
d8cadba
Separate Security Configuration
mostafakhudair Oct 30, 2020
4f1cfe1
Separate Security Configuration
mostafakhudair Oct 30, 2020
07fbb56
Separate Security Configuration
mostafakhudair Oct 30, 2020
16a56ff
Separate Security Configuration
mostafakhudair Oct 30, 2020
adc1bc2
Separate Security Configuration
mostafakhudair Oct 30, 2020
6851c67
Separate Security Configuration
mostafakhudair Oct 30, 2020
0dc56bb
Separate Security Configuration
mostafakhudair Oct 30, 2020
9e082c8
Separate Security Configuration
mostafakhudair Oct 30, 2020
c0ff8cb
Separate Security Configuration
mostafakhudair Oct 30, 2020
d363b5e
Separate Security Configuration
mostafakhudair Oct 30, 2020
6ab17ef
Separate Security Configuration
mostafakhudair Oct 30, 2020
78bfff0
Rename system/Test/MockSecurityConfig.php to system/Test/Mock/MockSec…
mostafakhudair Oct 30, 2020
3fa80e6
Update Security.php
mostafakhudair Oct 30, 2020
9df259b
Separate Security Configuration
mostafakhudair Oct 30, 2020
ab9f3f1
Merge branch 'develop' into patch-7
mostafakhudair Nov 25, 2020
2bb5089
Merge branch 'develop' into patch-7
mostafakhudair Nov 28, 2020
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
14 changes: 14 additions & 0 deletions app/Config/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ class App extends BaseConfig
*
* The token name.
*
* @deprecated Use `Config\Security` $tokenName property instead of using this property.
*
* @var string
*/
public $CSRFTokenName = 'csrf_test_name';
Expand All @@ -345,6 +347,8 @@ class App extends BaseConfig
*
* The header name.
*
* @deprecated Use `Config\Security` $headerName property instead of using this property.
*
* @var string
*/
public $CSRFHeaderName = 'X-CSRF-TOKEN';
Expand All @@ -356,6 +360,8 @@ class App extends BaseConfig
*
* The cookie name.
*
* @deprecated Use `Config\Security` $cookieName property instead of using this property.
*
* @var string
*/
public $CSRFCookieName = 'csrf_cookie_name';
Expand All @@ -367,6 +373,8 @@ class App extends BaseConfig
*
* The number in seconds the token should expire.
*
* @deprecated Use `Config\Security` $expire property instead of using this property.
*
* @var integer
*/
public $CSRFExpire = 7200;
Expand All @@ -378,6 +386,8 @@ class App extends BaseConfig
*
* Regenerate token on every submission?
*
* @deprecated Use `Config\Security` $regenerate property instead of using this property.
*
* @var boolean
*/
public $CSRFRegenerate = true;
Expand All @@ -389,6 +399,8 @@ class App extends BaseConfig
*
* Redirect to previous page with error on failure?
*
* @deprecated Use `Config\Security` $redirect property instead of using this property.
*
* @var boolean
*/
public $CSRFRedirect = true;
Expand All @@ -408,6 +420,8 @@ class App extends BaseConfig
*
* @see https://portswigger.net/web-security/csrf/samesite-cookies
*
* @deprecated Use `Config\Security` $samesite property instead of using this property.
*
* @var string
*/
public $CSRFSameSite = 'Lax';
Expand Down
92 changes: 92 additions & 0 deletions app/Config/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Config;

use CodeIgniter\Config\BaseConfig;

class Security extends BaseConfig
{
/**
* --------------------------------------------------------------------------
* CSRF Token Name
* --------------------------------------------------------------------------
*
* Token name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
public $tokenName = 'csrf_test_name';

/**
* --------------------------------------------------------------------------
* CSRF Header Name
* --------------------------------------------------------------------------
*
* Token name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
public $headerName = 'X-CSRF-TOKEN';

/**
* --------------------------------------------------------------------------
* CSRF Cookie Name
* --------------------------------------------------------------------------
*
* Cookie name for Cross Site Request Forgery protection cookie.
*
* @var string
*/
public $cookieName = 'csrf_cookie_name';

/**
* --------------------------------------------------------------------------
* CSRF Expires
* --------------------------------------------------------------------------
*
* Expiration time for Cross Site Request Forgery protection cookie.
*
* Defaults to two hours (in seconds).
*
* @var integer
*/
public $expires = 7200;

/**
* --------------------------------------------------------------------------
* CSRF Regenerate
* --------------------------------------------------------------------------
*
* Regenerate CSRF Token on every request.
*
* @var boolean
*/
public $regenerate = true;

/**
* --------------------------------------------------------------------------
* CSRF Redirect
* --------------------------------------------------------------------------
*
* Redirect to previous page with error on failure.
*
* @var boolean
*/
public $redirect = true;

/**
* --------------------------------------------------------------------------
* CSRF SameSite
* --------------------------------------------------------------------------
*
* Setting for CSRF SameSite cookie token.
*
* Allowed values are: None - Lax - Strict - ''.
*
* Defaults to `Lax` as recommended in this link:
* @see https://portswigger.net/web-security/csrf/samesite-cookies
*
* @var string
*/
public $samesite = 'Lax';
}
12 changes: 3 additions & 9 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@ function config(string $name, bool $getShared = true)
*/
function csrf_token(): string
{
$config = config(App::class);

return $config->CSRFTokenName;
return Services::security()->getTokenName();
}
}

Expand All @@ -250,9 +248,7 @@ function csrf_token(): string
*/
function csrf_header(): string
{
$config = config(App::class);

return $config->CSRFHeaderName;
return Services::security()->getHeaderName();
}
}

Expand All @@ -267,9 +263,7 @@ function csrf_header(): string
*/
function csrf_hash(): string
{
$security = Services::security(null, true);

return $security->getCSRFHash();
return Services::security()->getHash();
}
}

Expand Down
2 changes: 1 addition & 1 deletion system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ public static function security(App $config = null, bool $getShared = true)
return static::getSharedInstance('security', $config);
}

$config = $config ?? config('App');
$config = $config ?? config('App') ?? config('Security');

return new Security($config);
}
Expand Down
2 changes: 1 addition & 1 deletion system/Debug/Toolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ public function respond()
]);
$format = explode('/', $format)[1];

$file = sanitize_filename('debugbar_' . $request->getGet('debugbar_time'));
$file = Services::security()->sanitizeFilename('debugbar_' . $request->getGet('debugbar_time'));
$filename = WRITEPATH . 'debugbar/' . $file . '.json';

// Show the toolbar
Expand Down
4 changes: 2 additions & 2 deletions system/Filters/CSRF.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public function before(RequestInterface $request, $arguments = null)

try
{
$security->CSRFVerify($request);
$security->verify($request);
}
catch (SecurityException $e)
{
if (config('App')->CSRFRedirect && ! $request->isAJAX())
if ($security->shouldRedirect() && ! $request->isAJAX())
{
return redirect()->back()->with('error', $e->getMessage());
}
Expand Down
16 changes: 16 additions & 0 deletions system/Language/en/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

// Security language settings
return [
'disallowedAction' => 'The action you requested is not allowed.',
'invalidSameSite' => 'The SameSite value must be None, Lax, Strict, or a blank string. Given: {0}',
];
8 changes: 4 additions & 4 deletions system/Security/Exceptions/SecurityException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class SecurityException extends FrameworkException
{
public static function forDisallowedAction()
{
return new static(lang('HTTP.disallowedAction'), 403);
return new static(lang('Security.disallowedAction'), 403);
}

public static function forInvalidSameSiteSetting(string $samesite)
public static function forInvalidSameSite(string $samesite)
{
return new static(lang('HTTP.invalidSameSiteSetting', [$samesite]));
return new static(lang('Security.invalidSameSite', [$samesite]));
}
}
Loading