Repository navigation
Expand file tree
/
Copy pathEncryptedCastTest.php
More file actions
153 lines (118 loc) · 5.04 KB
/
Copy pathEncryptedCastTest.php
File metadata and controls
153 lines (118 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\DataCaster;
use CodeIgniter\Config\Factories;
use CodeIgniter\DataCaster\Exceptions\CastException;
use CodeIgniter\DataConverter\DataConverter;
use CodeIgniter\Encryption\Exceptions\EncryptionException;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Encryption as EncryptionConfig;
use Config\Services;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
/**
* @internal
*/
#[Group('Others')]
#[RequiresPhpExtension('openssl')]
final class EncryptedCastTest extends CIUnitTestCase
{
private const CURRENT_KEY = 'current-encrypted-cast-key';
private const OLD_KEY = 'old-encrypted-cast-key';
protected function setUp(): void
{
parent::setUp();
$this->useEncryptionKey(self::CURRENT_KEY);
}
public function testSetEncryptsStringAsEncodedText(): void
{
$dataCaster = new DataCaster(types: ['secret' => 'encrypted']);
$encrypted = $dataCaster->castAs('plain-secret', 'secret', 'set');
$this->assertIsString($encrypted);
$this->assertNotSame('plain-secret', $encrypted);
$this->assertNotFalse(base64_decode($encrypted, true));
$this->assertSame('plain-secret', $dataCaster->castAs($encrypted, 'secret'));
}
public function testEncryptedCastSupportsNullableValues(): void
{
$dataCaster = new DataCaster(types: ['secret' => '?encrypted']);
$this->assertNull($dataCaster->castAs(null, 'secret', 'set'));
$this->assertNull($dataCaster->castAs(null, 'secret'));
}
public function testEncryptedCastRejectsInvalidPlainValueWithoutLeakingIt(): void
{
$dataCaster = new DataCaster(types: ['secret' => 'encrypted']);
try {
$dataCaster->castAs(['token' => 'sensitive-value'], 'secret', 'set');
} catch (CastException $e) {
$this->assertSame('Type casting "encrypted" expects a string or null value.', $e->getMessage());
$this->assertStringNotContainsString('token', $e->getMessage());
$this->assertStringNotContainsString('sensitive-value', $e->getMessage());
return;
}
$this->fail('Expected encrypted casting to reject non-string values.');
}
public function testEncryptedCastRejectsMalformedPayload(): void
{
$this->expectException(CastException::class);
$this->expectExceptionMessage('Type casting "encrypted" expects a valid encrypted value.');
$dataCaster = new DataCaster(types: ['secret' => 'encrypted']);
$dataCaster->castAs('@@not-base64@@', 'secret');
}
public function testEncryptedCastBubblesAuthenticationFailures(): void
{
$this->expectException(EncryptionException::class);
$dataCaster = new DataCaster(types: ['secret' => 'encrypted']);
$dataCaster->castAs(base64_encode('not-encrypted'), 'secret');
}
public function testEncryptedCastCanDecryptPreviousKeyValues(): void
{
$this->useEncryptionKey(self::OLD_KEY);
$oldEncryptedValue = base64_encode(Services::encrypter()->encrypt('old-secret'));
$this->useEncryptionKey(self::CURRENT_KEY, [self::OLD_KEY]);
$dataCaster = new DataCaster(types: ['secret' => 'encrypted']);
$this->assertSame('old-secret', $dataCaster->castAs($oldEncryptedValue, 'secret'));
}
public function testDataConverterConvertsEncryptedFieldToAndFromDataSource(): void
{
$converter = new DataConverter(['secret' => 'encrypted']);
$dataSourceData = $converter->toDataSource(['secret' => 'plain-secret']);
$this->assertIsString($dataSourceData['secret']);
$this->assertNotSame('plain-secret', $dataSourceData['secret']);
$this->assertSame(['secret' => 'plain-secret'], $converter->fromDataSource($dataSourceData));
}
public function testEntityStoresEncryptedRawValueAndReturnsPlaintext(): void
{
$entity = new class () extends Entity {
protected $casts = [
'secret' => 'encrypted',
];
};
$entity->secret = 'plain-secret';
$raw = $entity->toRawArray();
$this->assertIsString($raw['secret']);
$this->assertNotSame('plain-secret', $raw['secret']);
$this->assertSame('plain-secret', $entity->secret);
$this->assertSame(['secret' => 'plain-secret'], $entity->toArray());
}
/**
* @param list<string> $previousKeys
*/
private function useEncryptionKey(string $key, array $previousKeys = []): void
{
$config = new EncryptionConfig();
$config->driver = 'OpenSSL';
$config->key = $key;
$config->previousKeys = $previousKeys;
Factories::injectMock('config', EncryptionConfig::class, $config);
}
}