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