Skip to content

Commit 85b8db6

Browse files
committed
Added check to ensure stored keys do not exceed db column width
Updated documentation to reflect recent changes
1 parent 5ddf5fc commit 85b8db6

5 files changed

Lines changed: 51 additions & 22 deletions

File tree

UPGRADING.md

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ The following items have been added. Copy the properties in **src/Config/Auth.ph
3232
- `permission_denied` and `group_denied` are added to `Config\Auth::$redirects`.
3333
- `permissionDeniedRedirect()` and `groupDeniedRedirect()` are added.
3434

35-
#### Config\AuthToken
36-
37-
If you are using the HMAC authentication you need to update the encryption settings in **app/Config/AuthToken.php**.
38-
You will need to update and set the encryption key `$hmacEncryptionKey`. This should be set using .env and/or system
39-
environment variables. Instructions on how to do that can be found in the
40-
[Setting Your Encryption Key](https://codeigniter.com/user_guide/libraries/encryption.html#setting-your-encryption-key)
41-
section of the CodeIgniter 4 documentation.
42-
43-
You also may wish to adjust the default Driver `$hmacEncryptionDriver` and the default Digest `$hmacEncryptionDigest`,
44-
these currently default to `'OpenSSL'` and `'SHA512'` respectively.
45-
4635
### Fix Custom Filter If extends `AbstractAuthFilter`
4736

4837
If you have written a custom filter that extends `AbstractAuthFilter`, now you need to add and implement the `redirectToDeniedUrl()` method to your custom filter.
@@ -58,10 +47,25 @@ protected function redirectToDeniedUrl(): RedirectResponse
5847
->with('error', lang('Auth.notEnoughPrivilege'));
5948
}
6049
```
61-
### Database Migrations
6250

63-
After updating the `$hmacEncryptionKey` value, you will need to run `php spark migrate --all` in order to encrypt any
64-
existing HMAC tokens.
51+
### Fix to HMAC Secret Key Encryption
52+
53+
#### Config\AuthToken
54+
55+
If you are using the HMAC authentication you need to update the encryption settings in **app/Config/AuthToken.php**.
56+
You will need to update and set the encryption key `$hmacEncryptionKey`. This should be set using .env and/or system
57+
environment variables. Instructions on how to do that can be found in the
58+
[Setting Your Encryption Key](https://codeigniter.com/user_guide/libraries/encryption.html#setting-your-encryption-key)
59+
section of the CodeIgniter 4 documentation.
60+
61+
You also may wish to adjust the default Driver `$hmacEncryptionDriver` and the default Digest `$hmacEncryptionDigest`,
62+
these currently default to `'OpenSSL'` and `'SHA512'` respectively.
63+
64+
#### Encrypt Existing Keys
65+
66+
After updating the `$hmacEncryptionKey` value, you will need to run `php spark shield:hmac encrypt` in order to encrypt
67+
any existing HMAC tokens. This only needs to be run if you have existing unencrypted HMAC secretKeys in stored in the
68+
database.
6569

6670
## Version 1.0.0-beta.6 to 1.0.0-beta.7
6771

docs/guides/api_hmac_keys.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ section of the CodeIgniter 4 documentation.
9999
You will also be able to adjust the default Driver `$hmacEncryptionDriver` and the default Digest
100100
`$hmacEncryptionDigest`, these default to `'OpenSSL'` and `'SHA512'` respectively.
101101

102+
Depending on the set length of the Secret Key and the type of encryption used, it is possible for the encrypted value to
103+
exceed the database column character limit of 255 characters. If this happens, creation of a new HMAC identity will
104+
throw a `RuntimeException`.
102105

103106
## Protecting Routes
104107

src/Authentication/HMAC/HmacEncrypter.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace CodeIgniter\Shield\Authentication\HMAC;
66

77
use CodeIgniter\Encryption\EncrypterInterface;
8+
use CodeIgniter\Encryption\Exceptions\EncryptionException;
89
use CodeIgniter\Shield\Config\AuthToken;
10+
use CodeIgniter\Shield\Exceptions\RuntimeException;
911
use Config\Encryption;
1012
use Config\Services;
1113
use Exception;
@@ -47,36 +49,47 @@ public function __construct()
4749
/**
4850
* Decrypt
4951
*
50-
* @param string $hexString Encrypted string in Hex format
52+
* @param string $base64String Encrypted string in base64 format
5153
*
5254
* @return string Raw decrypted string
55+
*
56+
* @throws EncryptionException
5357
*/
54-
public function decrypt(string $hexString): string
58+
public function decrypt(string $base64String): string
5559
{
56-
return $this->encrypter->decrypt(hex2bin($hexString));
60+
return $this->encrypter->decrypt(base64_decode($base64String, true));
5761
}
5862

5963
/**
6064
* Encrypt
6165
*
6266
* @param string $rawString Raw string to encrypt
6367
*
64-
* @return string Encrypted string in hex format
68+
* @return string Encrypted string in base64 format
69+
*
70+
* @throws EncryptionException
71+
* @throws RuntimeException
6572
*/
6673
public function encrypt(string $rawString): string
6774
{
68-
return bin2hex($this->encrypter->encrypt($rawString));
75+
$encryptedString = base64_encode($this->encrypter->encrypt($rawString));
76+
77+
if (strlen($encryptedString) > $this->authConfig->secret2StorageLimit) {
78+
throw new RuntimeException('Encrypted key too long. Unable to store value.');
79+
}
80+
81+
return $encryptedString;
6982
}
7083

7184
/**
7285
* Generate Key
7386
*
74-
* @return string Secret Key in hexed format
87+
* @return string Secret Key in base64 format
7588
*
7689
* @throws Exception
7790
*/
7891
public function generateSecretKey(): string
7992
{
80-
return bin2hex(random_bytes($this->authConfig->hmacSecretKeyByteSize));
93+
return base64_encode(random_bytes($this->authConfig->hmacSecretKeyByteSize));
8194
}
8295
}

src/Commands/Hmac.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CodeIgniter\Shield\Authentication\HMAC\HmacEncrypter;
88
use CodeIgniter\Shield\Commands\Exceptions\BadInputException;
99
use CodeIgniter\Shield\Models\UserIdentityModel;
10+
use Exception;
1011
use ReflectionException;
1112

1213
class Hmac extends BaseCommand
@@ -82,7 +83,7 @@ public function run(array $params): int
8283
default:
8384
throw new BadInputException('Unrecognized Command');
8485
}
85-
} catch (BadInputException|ReflectionException $e) {
86+
} catch (Exception $e) {
8687
$this->write($e->getMessage(), 'red');
8788

8889
return EXIT_ERROR;

src/Config/AuthToken.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ class AuthToken extends BaseConfig
5555
*/
5656
public int $unusedTokenLifetime = YEAR;
5757

58+
/**
59+
* --------------------------------------------------------------------
60+
* Secret2 storage character limit
61+
* --------------------------------------------------------------------
62+
* Database size limit for the identities 'secret2' field.
63+
*/
64+
public int $secret2StorageLimit = 255;
65+
5866
/**
5967
* --------------------------------------------------------------------
6068
* HMAC secret key byte size

0 commit comments

Comments
 (0)