Skip to content

MOD: include zero content-length header if present #3127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vjoke
Copy link

@vjoke vjoke commented Jul 2, 2025

In Brief

This change addresses an issue in the SigV4 signing process where an explicit Content-Length: 0 header would be omitted from the set of signed headers.

Description of Problem

The v4 signer is responsible for building a canonical request, which includes a list of signed headers. The current implementation has two relevant behaviors:
It automatically adds the Content-Length header to the signed headers only if the request body's length is greater than zero (length > 0).
It iterates through the request's existing headers but contains a condition to always skip a header named Content-Length, to prevent it from being added twice.
The combination of these two behaviors means that if a request has a zero-length body and an explicit Content-Length: 0 header, the header is never included in the signature. This can lead to signature mismatch errors for AWS services that require Content-Length to be signed, even for empty bodies (e.g., S3 PutObject).

The Fix

This PR modifies the condition that skips existing Content-Length headers. Instead of unconditionally skipping it, the condition is now strings.EqualFold(k, contentLengthHeader) && length > 0.
This ensures that the pre-existing Content-Length header is only skipped if the signer has already handled it (because length > 0). If length is 0, the signer will now correctly process and sign an explicit Content-Length: 0 header found on the request.

How to Test

This can be verified with a unit test that creates a request with a zero-byte body, adds an explicit Content-Length: 0 header, and asserts that content-length is present in the canonical headers string generated by the signer.

@vjoke vjoke requested a review from a team as a code owner July 2, 2025 01:47
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.

1 participant