|
| 1 | +from azure.storage.blob import BlobServiceClient |
| 2 | + |
| 3 | + |
| 4 | +def unsafe(): |
| 5 | + # does not set encryption_version to 2.0, default is unsafe |
| 6 | + blob_client = BlobServiceClient.get_blob_client(...) |
| 7 | + blob_client.require_encryption = True |
| 8 | + blob_client.key_encryption_key = ... |
| 9 | + with open("decryptedcontentfile.txt", "rb") as stream: |
| 10 | + blob_client.upload_blob(stream) # BAD |
| 11 | + |
| 12 | + |
| 13 | +def potentially_unsafe(use_new_version=False): |
| 14 | + blob_client = BlobServiceClient.get_blob_client(...) |
| 15 | + blob_client.require_encryption = True |
| 16 | + blob_client.key_encryption_key = ... |
| 17 | + |
| 18 | + if use_new_version: |
| 19 | + blob_client.encryption_version = '2.0' |
| 20 | + |
| 21 | + with open("decryptedcontentfile.txt", "rb") as stream: |
| 22 | + blob_client.upload_blob(stream) # BAD |
| 23 | + |
| 24 | + |
| 25 | +def safe(): |
| 26 | + blob_client = BlobServiceClient.get_blob_client(...) |
| 27 | + blob_client.require_encryption = True |
| 28 | + blob_client.key_encryption_key = ... |
| 29 | + # GOOD: Must use `encryption_version` set to `2.0` |
| 30 | + blob_client.encryption_version = '2.0' |
| 31 | + with open("decryptedcontentfile.txt", "rb") as stream: |
| 32 | + blob_client.upload_blob(stream) # OK |
| 33 | + |
| 34 | + |
| 35 | +def get_unsafe_blob_client(): |
| 36 | + blob_client = BlobServiceClient.get_blob_client(...) |
| 37 | + blob_client.require_encryption = True |
| 38 | + blob_client.key_encryption_key = ... |
| 39 | + return blob_client |
| 40 | + |
| 41 | + |
| 42 | +def unsafe_with_calls(): |
| 43 | + bc = get_unsafe_blob_client() |
| 44 | + with open("decryptedcontentfile.txt", "rb") as stream: |
| 45 | + bc.upload_blob(stream) # BAD |
| 46 | + |
| 47 | + |
| 48 | +def get_safe_blob_client(): |
| 49 | + blob_client = BlobServiceClient.get_blob_client(...) |
| 50 | + blob_client.require_encryption = True |
| 51 | + blob_client.key_encryption_key = ... |
| 52 | + blob_client.encryption_version = '2.0' |
| 53 | + return blob_client |
| 54 | + |
| 55 | + |
| 56 | +def safe_with_calls(): |
| 57 | + bc = get_safe_blob_client() |
| 58 | + with open("decryptedcontentfile.txt", "rb") as stream: |
| 59 | + bc.upload_blob(stream) # OK |
0 commit comments