Skip to content

Commit 1487d7e

Browse files
authored
fix: allow multi-tenant queries with allow_plaintext (#1240)
* fix: allow multi-tenant queries with allow_plaintext
1 parent 86406f5 commit 1487d7e

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

DynamoDbEncryption/dafny/DynamoDbEncryptionTransforms/src/QueryTransform.dfy

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module QueryTransform {
1010
import DDB = ComAmazonawsDynamodbTypes
1111
import opened AwsCryptographyDbEncryptionSdkDynamoDbTransformsTypes
1212
import EncTypes = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorTypes
13-
import Seq
13+
import EncOps = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations
1414

1515
method Input(config: Config, input: QueryInputTransformInput)
1616
returns (output: Result<QueryInputTransformOutput, Error>)
@@ -90,10 +90,19 @@ module QueryTransform {
9090
//# with the resulting decrypted [DynamoDB Item](./decrypt-item.md#dynamodb-item-1).
9191
var decryptInput := EncTypes.DecryptItemInput(encryptedItem := encryptedItems[x]);
9292
var decryptRes := tableConfig.itemEncryptor.DecryptItem(decryptInput);
93-
9493
var decrypted :- MapError(decryptRes);
95-
if keyId.KeyId? {
96-
:- Need(decrypted.parsedHeader.Some?, E("Decrypted query result has no parsed header."));
94+
95+
// If the decrypted result was plaintext, i.e. has no parsedHeader
96+
// then this is expected IFF the table config allows plaintext read
97+
assert decrypted.parsedHeader.None? ==>
98+
&& EncOps.IsPlaintextItem(encryptedItems[x])
99+
&& !tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_FORBID_PLAINTEXT_READ?
100+
&& (
101+
|| tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
102+
|| tableConfig.plaintextOverride.FORCE_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
103+
);
104+
105+
if keyId.KeyId? && decrypted.parsedHeader.Some? {
97106
:- Need(|decrypted.parsedHeader.value.encryptedDataKeys| == 1, E("Query result has more than one Encrypted Data Key"));
98107
if decrypted.parsedHeader.value.encryptedDataKeys[0].keyProviderInfo == keyIdUtf8 {
99108
decryptedItems := decryptedItems + [decrypted.plaintextItem];

DynamoDbEncryption/dafny/DynamoDbEncryptionTransforms/src/ScanTransform.dfy

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module ScanTransform {
1010
import DDB = ComAmazonawsDynamodbTypes
1111
import opened AwsCryptographyDbEncryptionSdkDynamoDbTransformsTypes
1212
import EncTypes = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorTypes
13-
import Seq
13+
import EncOps = AwsCryptographyDbEncryptionSdkDynamoDbItemEncryptorOperations
1414

1515
method Input(config: Config, input: ScanInputTransformInput)
1616
returns (output: Result<ScanInputTransformOutput, Error>)
@@ -88,10 +88,19 @@ module ScanTransform {
8888

8989
var decryptInput := EncTypes.DecryptItemInput(encryptedItem := encryptedItems[x]);
9090
var decryptRes := tableConfig.itemEncryptor.DecryptItem(decryptInput);
91-
9291
var decrypted :- MapError(decryptRes);
93-
if keyId.KeyId? {
94-
:- Need(decrypted.parsedHeader.Some?, E("Decrypted scan result has no parsed header."));
92+
93+
// If the decrypted result was plaintext, i.e. has no parsedHeader
94+
// then this is expected IFF the table config allows plaintext read
95+
assert decrypted.parsedHeader.None? ==>
96+
&& EncOps.IsPlaintextItem(encryptedItems[x])
97+
&& !tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_FORBID_PLAINTEXT_READ?
98+
&& (
99+
|| tableConfig.plaintextOverride.FORBID_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
100+
|| tableConfig.plaintextOverride.FORCE_PLAINTEXT_WRITE_ALLOW_PLAINTEXT_READ?
101+
);
102+
103+
if keyId.KeyId? && decrypted.parsedHeader.Some? {
95104
:- Need(|decrypted.parsedHeader.value.encryptedDataKeys| == 1, E("Scan result has more than one Encrypted Data Key"));
96105
if decrypted.parsedHeader.value.encryptedDataKeys[0].keyProviderInfo == keyIdUtf8 {
97106
decryptedItems := decryptedItems + [decrypted.plaintextItem];

0 commit comments

Comments
 (0)