• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 20589198684

30 Dec 2025 04:55AM UTC coverage: 84.503% (-0.03%) from 84.53%
20589198684

Pull #9853

github

web-flow
Merge f1d8312ec into e2fc5243b
Pull Request #9853: feat(encryption): Add previous keys fallback feature

65 of 77 new or added lines in 5 files covered. (84.42%)

38 existing lines in 3 files now uncovered.

21572 of 25528 relevant lines covered (84.5%)

203.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.87
/system/Encryption/Handlers/BaseHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Encryption\Handlers;
15

16
use CodeIgniter\Encryption\EncrypterInterface;
17
use CodeIgniter\Encryption\Exceptions\EncryptionException;
18
use Config\Encryption;
19
use SensitiveParameter;
20

21
/**
22
 * Base class for encryption handling
23
 */
24
abstract class BaseHandler implements EncrypterInterface
25
{
26
    /**
27
     * List of previous keys for fallback decryption.
28
     *
29
     * @var list<string>
30
     */
31
    protected array $previousKeys = [];
32

33
    /**
34
     * Constructor
35
     */
36
    public function __construct(?Encryption $config = null)
37
    {
38
        $config ??= config(Encryption::class);
19✔
39

40
        // make the parameters conveniently accessible
41
        foreach (get_object_vars($config) as $key => $value) {
19✔
42
            if (property_exists($this, $key)) {
19✔
43
                $this->{$key} = $value;
19✔
44
            }
45
        }
46
    }
47

48
    /**
49
     * Byte-safe substr()
50
     *
51
     * @param string $str
52
     * @param int    $start
53
     * @param int    $length
54
     *
55
     * @return string
56
     */
57
    protected static function substr($str, $start, $length = null)
58
    {
59
        return mb_substr($str, $start, $length, '8bit');
10✔
60
    }
61

62
    /**
63
     * Attempts to decrypt using the provided callback, and if it fails,
64
     * tries again with any previous keys we may have.
65
     *
66
     * @param string                                                      $data            Data to decrypt
67
     * @param array<string, string>|string|null                           $params          Decryption parameters
68
     * @param callable(string, array<string, string>|string|null): string $decryptCallback Callback that performs decryption
69
     *
70
     * @return string Decrypted data
71
     *
72
     * @throws EncryptionException
73
     */
74
    protected function tryDecryptWithFallback($data, #[SensitiveParameter] array|string|null $params, callable $decryptCallback)
75
    {
76
        try {
77
            return $decryptCallback($data, $params);
11✔
78
        } catch (EncryptionException $e) {
4✔
79
            if ($this->previousKeys === []) {
4✔
NEW
80
                throw $e;
×
81
            }
82

83
            if (is_string($params) || (is_array($params) && isset($params['key']))) {
4✔
84
                throw $e;
4✔
85
            }
86

NEW
87
            foreach ($this->previousKeys as $previousKey) {
×
88
                try {
NEW
89
                    $previousParams = is_array($params)
×
NEW
90
                        ? array_merge($params, ['key' => $previousKey])
×
NEW
91
                        : $previousKey;
×
92

NEW
93
                    return $decryptCallback($data, $previousParams);
×
NEW
94
                } catch (EncryptionException) {
×
NEW
95
                    continue;
×
96
                }
97
            }
98

NEW
99
            throw $e;
×
100
        }
101
    }
102

103
    /**
104
     * __get() magic, providing readonly access to some of our properties
105
     *
106
     * @param string $key Property name
107
     *
108
     * @return array|bool|int|string|null
109
     */
110
    public function __get($key)
111
    {
112
        if ($this->__isset($key)) {
4✔
113
            return $this->{$key};
4✔
114
        }
115

116
        return null;
1✔
117
    }
118

119
    /**
120
     * __isset() magic, providing checking for some of our properties
121
     *
122
     * @param string $key Property name
123
     */
124
    public function __isset($key): bool
125
    {
126
        return property_exists($this, $key);
4✔
127
    }
128
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc