From e842aada8f1ac2e1c763a212c6117ba15c6ed1ee Mon Sep 17 00:00:00 2001 From: Reginald Alfret Date: Fri, 25 Sep 2026 10:46:44 +0530 Subject: [PATCH] docs: clarify GetObject stream ContentLength is not plaintext length Fixes #164 Clarify in the README, Sphinx documentation (docs/index.rst), and API docstrings (S3EncryptionClient.get_object, on_get_object_after_call, and GetEncryptedObjectPipeline.decrypt) that the ContentLength in the response dictionary returned by get_object corresponds to the ciphertext stream length stored in S3 (which includes the cryptographic authentication tag or CBC padding), rather than the decrypted plaintext length. Callers must read the entire stream (e.g. via response['Body'].read()) to ensure complete decryption and authentication verification. --- README.md | 5 +++++ docs/index.rst | 13 +++++++++++++ src/s3_encryption/__init__.py | 10 ++++++++++ src/s3_encryption/pipelines.py | 5 ++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 70866688..00dfa160 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,11 @@ response = s3ec.get_object(Bucket="my-bucket", Key="my-object") plaintext = response["Body"].read() ``` +> [!NOTE] +> **Stream Length vs. Plaintext Length:** +> The `ContentLength` in the response returned by `get_object` reflects the size of the encrypted ciphertext stream stored in S3, which includes the authentication tag (e.g. 16 bytes for AES-GCM) or cipher padding (for CBC mode). Consequently, `response["ContentLength"]` will be larger than the decrypted plaintext length. You should always read the entire stream (`response["Body"].read()`) to complete decryption and authentication verification rather than limiting reads to the expected plaintext length. + + ## Development ### Prerequisites diff --git a/docs/index.rst b/docs/index.rst index 84dd359a..ff5e66ba 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -34,6 +34,19 @@ Getting Started response = s3ec.get_object(Bucket="my-bucket", Key="my-object") plaintext = response["Body"].read() +.. note:: + + **Stream Length vs. Plaintext Length** + + The ``ContentLength`` field in the response dictionary returned by ``get_object`` + reflects the length of the ciphertext stream stored in S3, which includes + the cryptographic authentication tag (or padding in the case of CBC). Consequently, + the stream's ``ContentLength`` is greater than the decrypted plaintext length. + Callers must read the entire stream (e.g. ``response["Body"].read()``) to complete + decryption and authentication verification rather than relying on ``ContentLength`` + as the plaintext length. + + Indices and tables ------------------ diff --git a/src/s3_encryption/__init__.py b/src/s3_encryption/__init__.py index ea6f1dc8..4cd73001 100644 --- a/src/s3_encryption/__init__.py +++ b/src/s3_encryption/__init__.py @@ -224,6 +224,8 @@ def on_get_object_after_call(self, parsed, **kwargs): """Event handler for after-call.s3.GetObject. This handler decrypts the body after the response is received from S3. + Note that the response's ContentLength remains the length of the encrypted + ciphertext in S3, which includes the authentication tag or cipher padding. Args: parsed: Dictionary containing the parsed response @@ -524,6 +526,14 @@ def get_object(self, **kwargs): The response from the S3 client's get_object method with the Body replaced with a StreamingBody containing the decrypted data. + Note: + The ``ContentLength`` in the response represents the length of the + encrypted ciphertext stream in S3, which includes the cryptographic + authentication tag (or padding for CBC). Consequently, ``ContentLength`` + is greater than the decrypted plaintext length. Callers must read the + entire stream (e.g., ``response["Body"].read()``) to complete decryption + and authentication verification. + Raises: S3EncryptionClientError: If decryption fails or the object is not properly encrypted. """ diff --git a/src/s3_encryption/pipelines.py b/src/s3_encryption/pipelines.py index 22324e50..c02378d7 100644 --- a/src/s3_encryption/pipelines.py +++ b/src/s3_encryption/pipelines.py @@ -376,7 +376,10 @@ def decrypt( key (str, optional): S3 object key (required for instruction file) Returns: - A botocore.response.StreamingBody of plain-text + A botocore.response.StreamingBody of plain-text. Note that the + ``ContentLength`` in the response reflects the ciphertext stream + length (including the authentication tag or cipher padding), which + is greater than the decrypted plaintext length. """ # Convert the metadata dictionary to an ObjectMetadata instance streaming_body: StreamingBody = response.get("Body")