Skip to content

fix(streaming): reject negative SEEK_SET offset and clamp negative relative seek - #8887

Open
utkarshpatwa393-sys wants to merge 1 commit into
aws-powertools:developfrom
utkarshpatwa393-sys:fix/s3object-seek-negative-position
Open

utkarshpatwa393-sys wants to merge 1 commit into
aws-powertools:developfrom
utkarshpatwa393-sys:fix/s3object-seek-negative-position

Conversation

@utkarshpatwa393-sys

Copy link
Copy Markdown

Summary of changes

Fixes #8885

Description

S3Object and its underlying _S3SeekableIO wrapper are documented as seekable, file-like objects. Previously, seek() accepted arbitrary negative positions:

  • Negative SEEK_SET offsets were accepted, resulting in negative positions (e.g. seek(-5) resulted in position -5) and causing subsequent read() operations to issue malformed HTTP Range headers such as Range: bytes=-5-.
  • Negative relative seeks (SEEK_CUR and SEEK_END) that landed before the start of the object resulted in negative positions rather than clamping to 0.

This PR aligns _S3SeekableIO.seek() with standard Python file-like semantics (io.BytesIO, io.IOBase):

  1. Raises ValueError: negative seek value <offset> when whence == io.SEEK_SET and offset < 0.
  2. Clamps the target position to 0 for io.SEEK_CUR and io.SEEK_END (max(0, ...)).

Checklist

…lative seek

Closes aws-powertools#8885

S3Object is documented as a seekable, file-like object, but seek() previously
accepted negative offsets for io.SEEK_SET, storing a negative position and
causing subsequent read() calls to issue a malformed HTTP Range header (e.g.
'bytes=-5-'). In addition, negative relative seeks (io.SEEK_CUR and io.SEEK_END)
that landed before the beginning of the object were not clamped.

Following Python file-like semantics (io.BytesIO):
- Raises ValueError for negative seek values when whence is io.SEEK_SET.
- Clamps position to 0 for io.SEEK_CUR and io.SEEK_END.

Signed-off-by: Utkarsh patwa <utkarshpatwa393@gmail.com>
@boring-cyborg

boring-cyborg Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

No acknowledgement section found. Please make sure you used the template to open a PR and didn't remove the acknowledgment section. Check the template at .github/PULL_REQUEST_TEMPLATE.md#acknowledgment

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Nov 23, 2026
@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files. streaming tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: S3Object.seek() accepts negative positions and sends a malformed Range header

1 participant