Skip to content

fix: return a decode error for malformed material description - #85

Open
lucasmcdonald3 wants to merge 6 commits into
mainfrom
fix/material-description-decoding-robustness
Open

fix: return a decode error for malformed material description#85
lucasmcdonald3 wants to merge 6 commits into
mainfrom
fix/material-description-decoding-robustness

Conversation

@lucasmcdonald3

@lucasmcdonald3 lucasmcdonald3 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Issue #, if available:

Description of changes:

Fix customS3Decoder panic when reading a tampered, invalid material description

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Check any applicable:

  • Were any files moved? Moving files changes their URL, which breaks all hyperlinks to the files.

customS3Decoder consumed a high-bit (>127) byte together with the
following byte without confirming that a following byte exists. An
incomplete multi-byte sequence at the end of the (mime-decoded) material
description could therefore read past the end of the string.

Bound-check before reading the pair and return a clean decode error for
an incomplete sequence. The decrypt middleware already surfaces decode
errors, so a malformed material description becomes a normal error
return. Applied identically to the v3 and v4 modules.

Adds regression tests to both modules covering a trailing high byte, a
single high byte, and RFC 2047 mime-encoded variants, plus valid-JSON
negative controls.
@lucasmcdonald3
lucasmcdonald3 force-pushed the fix/material-description-decoding-robustness branch from c0f28b5 to c667124 Compare July 27, 2026 16:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens customS3Decoder in both v3 and v4 clients by preventing an out-of-bounds read (panic) when the decoded material description ends with an unmatched high-bit byte, returning a decode error instead.

Changes:

  • Add a bounds check in customS3Decoder to reject material descriptions that end mid “byte-pair”.
  • Add new unit tests in both v3 and v4 to ensure malformed/truncated inputs return an error (and don’t panic).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
v4/client/decrypt_middleware.go Adds a guard to avoid out-of-bounds access when decoding malformed material descriptions.
v4/client/decrypt_middleware_matdesc_test.go Adds tests asserting malformed material descriptions produce decode errors.
v3/client/decrypt_middleware.go Mirrors the v4 decoder hardening in the v3 client.
v3/client/decrypt_middleware_matdesc_test.go Mirrors the v4 decoder tests for v3.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v4/client/decrypt_middleware_matdesc_test.go
Comment thread v4/client/decrypt_middleware_matdesc_test.go
Comment thread v3/client/decrypt_middleware_matdesc_test.go
Comment thread v3/client/decrypt_middleware_matdesc_test.go
lucasmcdonald3 and others added 2 commits July 27, 2026 09:45
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Drop two cases that don't exercise a distinct code path:
- "single 0x80 byte" duplicates the "single high byte" (0xFF) guard path
- "mime-encoded abc+ff" decodes to the same bytes as "trailing high byte"

Remaining cases cover the guard firing on the first byte, after a
completed pair (3-byte rune), and on mime.DecodeHeader output.
lucasmcdonald3 pushed a commit to aws/amazon-s3-encryption-client-python that referenced this pull request Jul 27, 2026
…erify

Go: aws/amazon-s3-encryption-client-go#85 (aea828bfeffe4bde5afbbfebf8e08ef644a649ad). PHP: fork PR#1 (411d948378d7412f94d8c4fb23f64442b9a90f0f). Ruby: fork PR#1 (1139087e071c971308d107352cc41219710886c9).
lucasmcdonald3 pushed a commit to aws/amazon-s3-encryption-client-python that referenced this pull request Jul 27, 2026
…erify

Go: aws/amazon-s3-encryption-client-go#85 (aea828bfeffe4bde5afbbfebf8e08ef644a649ad). PHP: fork PR#1 (411d948378d7412f94d8c4fb23f64442b9a90f0f). Ruby: fork PR#1 (1139087e071c971308d107352cc41219710886c9).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants