Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit 61e1cbd

Browse files
committed
Added ability to decrypt primitive type values using the same path as the encrypted value
1 parent 9caa1d3 commit 61e1cbd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Developer/Encryption/FieldLevelEncryption.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ private static function decryptPayloadPath($payloadJsonObject, $jsonPathIn, $jso
170170
self::addDecryptedDataToPayload($payloadJsonObject, $jsonPathOut, $outJsonObject, $decryptedValue);
171171

172172
// Remove the input if now empty
173-
if (empty((array)$inJsonObject) && '$' !== $jsonPathIn) {
173+
$inJsonElement = self::readJsonElement($payloadJsonObject, $jsonPathIn);
174+
if (empty((array)$inJsonElement) && '$' !== $jsonPathIn) {
174175
JsonPath::delete($payloadJsonObject, $jsonPathIn);
175176
}
176177
}

tests/Developer/Encryption/FieldLevelEncryptionTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ public function testDecryptPayload_ShouldKeepInputObject_WhenContainsAdditionalF
10381038
$this->assertEquals('fieldValue', $payloadObject->encryptedData->field);
10391039
}
10401040

1041-
public function testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath() {
1041+
public function testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath_ObjectData() {
10421042

10431043
// GIVEN
10441044
$encryptedPayload = '{
@@ -1063,6 +1063,31 @@ public function testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsI
10631063
$this->assertEquals('field2Value', $payloadObject->encryptedData->field2);
10641064
}
10651065

1066+
public function testDecryptPayload_ShouldOverwriteInputObject_WhenOutPathSameAsInPath_PrimitiveTypeData() {
1067+
1068+
// GIVEN
1069+
$encryptedPayload = '{
1070+
"data": {
1071+
"encryptedValue": "e2d6a3a76ea6e605e55b400e5a4eba11",
1072+
"iv": "3ce861359fa1630c7a794901ee14bf41",
1073+
"encryptedKey": "02bb8d5c7d113ef271f199c09f0d76db2b6d5d2d209ad1a20dbc4dd0d04576a92ceb917eea5f403ccf64c3c39dda564046909af96c82fad62f89c3cbbec880ea3105a0a171af904cd3b86ea68991202a2795dca07050ca58252701b7ecea06055fd43e96f4beee48b6275e86af93c88c21994ff46f0610171bd388a2c0a1f518ffc8346f7f513f3283feae5b102c8596ddcb2aea5e62ceb17222e646c599f258463405d28ac012bfd4cc431f94111ee07d79e660948485e38c13cdb8bba8e1df3f7dba0f4c77696f71930533c955f3a430658edaa03b0b0c393934d60f5ac3ea5c06ed64bf969fc01942eac432b8e0c56f7538659a72859d445d150c169ae690",
1074+
"encryptionCertificateFingerprint": "80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279",
1075+
"encryptionKeyFingerprint": "761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79",
1076+
"oaepHashingAlgorithm": "SHA256"
1077+
}
1078+
}';
1079+
$config = TestUtils::getTestFieldLevelEncryptionConfigBuilder()
1080+
->withDecryptionPath('$.data', '$.data')
1081+
->build();
1082+
1083+
// WHEN
1084+
$payload = FieldLevelEncryption::decryptPayload($encryptedPayload, $config);
1085+
1086+
// THEN
1087+
$payloadObject = json_decode($payload);
1088+
$this->assertEquals('string', $payloadObject->data);
1089+
}
1090+
10661091
public function testDecryptPayload_ShouldSupportRootAsInputPath() {
10671092

10681093
// GIVEN

0 commit comments

Comments
 (0)