Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------

Expand Down
10 changes: 10 additions & 0 deletions src/s3_encryption/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand Down
5 changes: 4 additions & 1 deletion src/s3_encryption/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down