Skip to content

Commit 0074b42

Browse files
committed
refactor: add the SensitiveParameter attribute to methods dealing with sensitive info
1 parent e39c749 commit 0074b42

File tree

7 files changed

+25
-15
lines changed

7 files changed

+25
-15
lines changed

system/Encryption/EncrypterInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Encryption;
1515

1616
use CodeIgniter\Encryption\Exceptions\EncryptionException;
17+
use SensitiveParameter;
1718

1819
/**
1920
* CodeIgniter Encryption Handler
@@ -32,7 +33,7 @@ interface EncrypterInterface
3233
*
3334
* @throws EncryptionException
3435
*/
35-
public function encrypt($data, $params = null);
36+
public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $params = null);
3637

3738
/**
3839
* Decrypt - convert ciphertext into plaintext
@@ -44,5 +45,5 @@ public function encrypt($data, $params = null);
4445
*
4546
* @throws EncryptionException
4647
*/
47-
public function decrypt($data, $params = null);
48+
public function decrypt($data, #[SensitiveParameter] $params = null);
4849
}

system/Encryption/Handlers/OpenSSLHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Encryption\Handlers;
1515

1616
use CodeIgniter\Encryption\Exceptions\EncryptionException;
17+
use SensitiveParameter;
1718

1819
/**
1920
* Encryption handling for OpenSSL library
@@ -79,7 +80,7 @@ class OpenSSLHandler extends BaseHandler
7980
/**
8081
* {@inheritDoc}
8182
*/
82-
public function encrypt($data, $params = null)
83+
public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $params = null)
8384
{
8485
// Allow key override
8586
if ($params !== null) {
@@ -115,7 +116,7 @@ public function encrypt($data, $params = null)
115116
/**
116117
* {@inheritDoc}
117118
*/
118-
public function decrypt($data, $params = null)
119+
public function decrypt($data, #[SensitiveParameter] $params = null)
119120
{
120121
// Allow key override
121122
if ($params !== null) {

system/Encryption/Handlers/SodiumHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Encryption\Handlers;
1515

1616
use CodeIgniter\Encryption\Exceptions\EncryptionException;
17+
use SensitiveParameter;
1718

1819
/**
1920
* SodiumHandler uses libsodium in encryption.
@@ -40,7 +41,7 @@ class SodiumHandler extends BaseHandler
4041
/**
4142
* {@inheritDoc}
4243
*/
43-
public function encrypt($data, $params = null)
44+
public function encrypt(#[SensitiveParameter] $data, #[SensitiveParameter] $params = null)
4445
{
4546
$this->parseParams($params);
4647

@@ -71,7 +72,7 @@ public function encrypt($data, $params = null)
7172
/**
7273
* {@inheritDoc}
7374
*/
74-
public function decrypt($data, $params = null)
75+
public function decrypt($data, #[SensitiveParameter] $params = null)
7576
{
7677
$this->parseParams($params);
7778

system/HTTP/CURLRequest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Config\App;
1919
use Config\CURLRequest as ConfigCURLRequest;
2020
use CurlShareHandle;
21+
use SensitiveParameter;
2122

2223
/**
2324
* A lightweight HTTP client for sending synchronous HTTP requests via cURL.
@@ -260,13 +261,9 @@ public function put(string $url, array $options = []): ResponseInterface
260261
*
261262
* @return $this
262263
*/
263-
public function setAuth(string $username, string $password, string $type = 'basic')
264+
public function setAuth(string $username, #[SensitiveParameter] string $password, string $type = 'basic')
264265
{
265-
$this->config['auth'] = [
266-
$username,
267-
$password,
268-
$type,
269-
];
266+
$this->config['auth'] = [$username, $password, $type];
270267

271268
return $this;
272269
}

system/HTTP/URI.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\Exceptions\InvalidArgumentException;
1818
use CodeIgniter\HTTP\Exceptions\HTTPException;
1919
use Config\App;
20+
use SensitiveParameter;
2021
use Stringable;
2122

2223
/**
@@ -768,7 +769,7 @@ public function withScheme(string $scheme)
768769
*
769770
* @TODO PSR-7: Should be `withUserInfo($user, $password = null)`.
770771
*/
771-
public function setUserInfo(string $user, string $pass)
772+
public function setUserInfo(string $user, #[SensitiveParameter] string $pass)
772773
{
773774
$this->user = trim($user);
774775
$this->password = trim($pass);

system/Security/Security.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Config\Cookie as CookieConfig;
2727
use Config\Security as SecurityConfig;
2828
use ErrorException;
29+
use SensitiveParameter;
2930

3031
/**
3132
* Class Security
@@ -371,13 +372,13 @@ protected function randomize(string $hash): string
371372
*
372373
* @throws InvalidArgumentException "hex2bin(): Hexadecimal input string must have an even length"
373374
*/
374-
protected function derandomize(string $token): string
375+
protected function derandomize(#[SensitiveParameter] string $token): string
375376
{
376377
$key = substr($token, -static::CSRF_HASH_BYTES * 2);
377378
$value = substr($token, 0, static::CSRF_HASH_BYTES * 2);
378379

379380
try {
380-
return bin2hex(hex2bin($value) ^ hex2bin($key));
381+
return bin2hex((string) hex2bin($value) ^ (string) hex2bin($key));
381382
} catch (ErrorException $e) {
382383
// "hex2bin(): Hexadecimal input string must have an even length"
383384
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ Interface Changes
4848
Method Signature Changes
4949
========================
5050

51+
- Added the ``SensitiveParameter`` attribute to various methods to conceal sensitive information from stack traces. Affected methods are:
52+
- ``CodeIgniter\Encryption\EncrypterInterface::encrypt()``
53+
- ``CodeIgniter\Encryption\Handlers\OpenSSLHandler::encrypt()``
54+
- ``CodeIgniter\Encryption\Handlers\SodiumHandler::encrypt()``
55+
- ``CodeIgniter\HTTP\CURLRequest::setAuth()``
56+
- ``CodeIgniter\HTTP\URI::setUserInfo()``
57+
- ``CodeIgniter\Security\Security::derandomize()``
58+
5159
Removed Deprecated Items
5260
========================
5361

0 commit comments

Comments
 (0)