Skip to content

Commit 5da7ab2

Browse files
authored
Merge pull request #4019 from mostafakhudair/security-class-interface
Add Interface to Security Component
2 parents b475229 + 81919b7 commit 5da7ab2

2 files changed

Lines changed: 98 additions & 5 deletions

File tree

system/Security/Security.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@
1515
use CodeIgniter\HTTP\RequestInterface;
1616
use CodeIgniter\Security\Exceptions\SecurityException;
1717
use Config\App;
18-
use Exception;
1918

2019
/**
2120
* Class Security
2221
*
2322
* Provides methods that help protect your site against
2423
* Cross-Site Request Forgery attacks.
2524
*/
26-
class Security
25+
class Security implements SecurityInterface
2726
{
2827
/**
2928
* CSRF Hash
@@ -41,7 +40,7 @@ class Security
4140
*
4241
* @var string
4342
*/
44-
protected $tokenName = 'CSRFToken';
43+
protected $tokenName = 'csrf_token_name';
4544

4645
/**
4746
* CSRF Header Name
@@ -50,7 +49,7 @@ class Security
5049
*
5150
* @var string
5251
*/
53-
protected $headerName = 'CSRFToken';
52+
protected $headerName = 'X-CSRF-TOKEN';
5453

5554
/**
5655
* CSRF Cookie Name
@@ -59,7 +58,7 @@ class Security
5958
*
6059
* @var string
6160
*/
62-
protected $cookieName = 'CSRFToken';
61+
protected $cookieName = 'csrf_cookie_name';
6362

6463
/**
6564
* CSRF Expires
@@ -148,6 +147,7 @@ public function __construct($config)
148147
* @param RequestInterface $request
149148
*
150149
* @return $this|false
150+
*
151151
* @throws SecurityException
152152
*
153153
* @deprecated Use `CodeIgniter\Security\Security::verify()` instead of using this method.
@@ -193,6 +193,7 @@ public function getCSRFTokenName(): string
193193
* @param RequestInterface $request
194194
*
195195
* @return $this|false
196+
*
196197
* @throws SecurityException
197198
*/
198199
public function verify(RequestInterface $request)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Security;
13+
14+
use CodeIgniter\HTTP\RequestInterface;
15+
use CodeIgniter\Security\Exceptions\SecurityException;
16+
17+
/**
18+
* Expected behavior of a Security.
19+
*/
20+
interface SecurityInterface
21+
{
22+
/**
23+
* CSRF Verify
24+
*
25+
* @param RequestInterface $request
26+
*
27+
* @return $this|false
28+
*
29+
* @throws SecurityException
30+
*/
31+
public function verify(RequestInterface $request);
32+
33+
/**
34+
* Returns the CSRF Hash.
35+
*
36+
* @return string|null
37+
*/
38+
public function getHash(): ?string;
39+
40+
/**
41+
* Returns the CSRF Token Name.
42+
*
43+
* @return string
44+
*/
45+
public function getTokenName(): string;
46+
47+
/**
48+
* Returns the CSRF Header Name.
49+
*
50+
* @return string
51+
*/
52+
public function getHeaderName(): string;
53+
54+
/**
55+
* Returns the CSRF Cookie Name.
56+
*
57+
* @return string
58+
*/
59+
public function getCookieName(): string;
60+
61+
/**
62+
* Check if CSRF cookie is expired.
63+
*
64+
* @return boolean
65+
*/
66+
public function isExpired(): bool;
67+
68+
/**
69+
* Check if request should be redirect on failure.
70+
*
71+
* @return boolean
72+
*/
73+
public function shouldRedirect(): bool;
74+
75+
/**
76+
* Sanitize Filename
77+
*
78+
* Tries to sanitize filenames in order to prevent directory traversal attempts
79+
* and other security threats, which is particularly useful for files that
80+
* were supplied via user input.
81+
*
82+
* If it is acceptable for the user input to include relative paths,
83+
* e.g. file/in/some/approved/folder.txt, you can set the second optional
84+
* parameter, $relative_path to TRUE.
85+
*
86+
* @param string $str Input file name
87+
* @param boolean $relativePath Whether to preserve paths
88+
*
89+
* @return string
90+
*/
91+
public function sanitizeFilename(string $str, bool $relativePath = false): string;
92+
}

0 commit comments

Comments
 (0)