fix(boto3): Fix botocore SigV4 failures caused by post-sign trace propagation - #7050
fix(boto3): Fix botocore SigV4 failures caused by post-sign trace propagation#7050pabloDeputter wants to merge 38 commits into
Conversation
- merge Sentry baggage with existing vendor (e.g. Datadog) baggage in botocore's`before-sign` hook; avoiding post-sign header tampering that invalidates the SigV4 signature. - Skip propagation for presigned requests Fixes: #7031 & PY-2667
This comment was marked as outdated.
This comment was marked as outdated.
… SigV4 headers Refs: #7031 & PY-2667
|
I haven't forgot about this, it's just complex so I'll likely only re-review fully at the start of next week. |
…` + support for SigV4 query/presigned authentication Refs: #7031 & PY-2667
…ssues Refs: #7031 & PY-2667
…` since it's not used anymore
…ion` string instead of all headers
- Record existing and signed headers in `putheader()` so trace propagation can avoid reparsing `_buffer` on every request. Refs: #7031 & PY-2667
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 3ce7587. Configure here.
alexander-alderman-webb
left a comment
There was a problem hiding this comment.
Next round 😃.
I've also pushed directly to your branch to limit the behavior to the affected HTTPConnection subclasses. I remove the changes to aiohttp as well, they can be in a separate PR when we get around to it (this one is big enough).
| ) | ||
|
|
||
|
|
||
| def _install_httplib() -> None: |
There was a problem hiding this comment.
AWS connection wrapper bypasses stdlib putrequest instrumentation
_patch_aws_connection() runs before HTTPConnection.putrequest is replaced. Because the AWS connection classes inherit putrequest, their wrapper captures the original uninstrumented method and then shadows the later base-class patch. As a result, botocore requests using only StdlibIntegration do not execute the stdlib instrumentation or set _sentrysdk_trace_url, so the deferred endheaders() path never injects Sentry propagation headers. Requests with Boto3Integration may still receive headers from its before-sign handler, but lose stdlib-level instrumentation.
Evidence
_install_httplib()calls_patch_aws_connection()before assigning the instrumentedHTTPConnection.putrequest._patch_aws_connection()assigns an AWS-classputrequestwrapper that captures the then-current inherited, uninstrumented method.- The AWS-class method shadows the later
HTTPConnection.putrequestassignment, so AWS requests skip the stdlib span setup and never set_sentrysdk_trace_url. _get_wrapped_endheaders()injects propagation only when_sentrysdk_trace_urlis present; therefore the Stdlib-only botocore path omits Sentry headers, as exercised bytest_botocore_without_boto3_integration_preserves_signed_baggage.
Identified by Warden · code-review, find-bugs · 8UW-Y37
There was a problem hiding this comment.
no they call the superclass
There was a problem hiding this comment.
From looking at the botocore code, I think this is legit. Here's a sample script of what it's describing. If you reverse the order of the Sub/Base patches, you'll see that when Sub is called that it also calls the patch on Base as well (right now it doesn't).
Edit: the solution would be to move the _patch_aws_connection call to below the HTTPConnection.* patches
ericapisani
left a comment
There was a problem hiding this comment.
Still working through the changes but have some initial comments
| return | ||
|
|
||
| def _replace_header(request: "AWSRequest", key: str, value: str) -> None: | ||
| # HTTPHeaders appends on assignment, so delete existing values first. |
There was a problem hiding this comment.
Because there's subtly different ways that headers can be handled when multiple values are set on the same header key, I think we should add a bit more context to this comment.
It'd be worth clarifying what "append" (ideally with a concrete example) means as it could be interpreted as:
- adding to a list (e.g: [foo, bar, baz])
- appending to a comma separated string (e.g. foo, bar, baz)
And I think it may also be worth pointing out that this is coming from email.message.Message from the standard library as this is what the botocore package is using under the hood.
There was a problem hiding this comment.
Updated the comment to clarify that it creates another header field rather than extending, @alexander-alderman-webb was also confused about this 😄
| "build": sys.version, | ||
| } | ||
|
|
||
| _SENTRY_HEADER_NAMES = frozenset((BAGGAGE_HEADER_NAME, "sentry-trace")) |
There was a problem hiding this comment.
We also have a constant for sentry-trace that you can use here
| _SENTRY_HEADER_NAMES = frozenset((BAGGAGE_HEADER_NAME, "sentry-trace")) | |
| _SENTRY_HEADER_NAMES = frozenset((BAGGAGE_HEADER_NAME, SENTRY_TRACE_HEADER_NAME)) |
There was a problem hiding this comment.
Yup good idea, didn't know this existed
| original_putheader: "Callable[..., Any]", | ||
| ) -> "Callable[..., Any]": | ||
| """ | ||
| Responsible for tracking request and signed headers. |
There was a problem hiding this comment.
Similar to the other comment, I think we should point future devs reading this to the specific "signed headers" that are being referred to here (in this case botocore's SignedHeaders).
| Responsible for tracking request and signed headers. | |
| Responsible for tracking request and `SignedHeaders`. |
There was a problem hiding this comment.
Yup, changed it to "Responsible for tracking which sentry headers are present and whether they are listed in AWS SigV4 SignedHeaders."
| ) | ||
|
|
||
|
|
||
| def _get_aws_sigv4_signed_headers( |
There was a problem hiding this comment.
Assuming I'm understanding this correctly, we've got 2 distinct code paths here:
- searching for the signed headers in the Authorization header (lines 1705-1716)
- searching for the signed headers in the URL's query string (lines 1718-1734)
If this were a public API and we wanted to optimize for user convenience, I can see the value of supporting the passing in both authorization and url and then searching in the correct place based on if the value is present or not, but because this is strictly for internal use, I think it'd be cleaner to have two distinct helper functions for each path:
_get_aws_sigv4_signed_headers_from_authorization_header_get_aws_sigv4_signed_headers_from_url_query_string
since at the calling sites, we already know where these headers are.
There was a problem hiding this comment.
agreed, I left it like that because originally we didn't read from the query string. I split it into 2 separate functions.
| if authorization is not None: | ||
| # only AWS SigV4 authorization has the SignedHeaders parameter. | ||
| value = authorization.lstrip() | ||
| if value.startswith(("AWS4-HMAC-SHA256", "AWS4-ECDSA-P256-SHA256")): |
There was a problem hiding this comment.
Although these aren't used very widely in the broader SDK codebase, what are your thoughts on doing something similar to the _SENTRY_HEADER_NAMES constant with the SigV4 algorithms?
_AWS_SIGV4_SIGNING_ALGORITHMS = frozenset(("AWS4-HMAC-SHA256", "AWS4-ECDSA-P256-SHA256"))
There was a problem hiding this comment.
I think that's okay. I'm doing it in stdlib.py with _SENTRY_HEADER_NAMES as well. Not sure whether we have a separate file for such constants, but I put it at the top of utils.py.
| with capture_internal_exceptions(): | ||
| authorization = values[0] | ||
| if isinstance(authorization, bytes): | ||
| authorization = authorization.decode("ascii", "ignore") |
There was a problem hiding this comment.
To mirror the codecs used in the putheader method - it looks like we want latin-1 decoding instead of ascii here.
| authorization = authorization.decode("ascii", "ignore") | |
| authorization = authorization.decode("latin-1", "ignore") |
ericapisani
left a comment
There was a problem hiding this comment.
I'm going to hold off on reviewing the tests for now until we've addressed all the questions around the core logic.
Overall lots of great stuff here - I particularly appreciate the docstrings/comments explaining what's happening.
Don't hesitate to reach out if there's any notes that I've left here that you want to chat about! 😄
| """ | ||
|
|
||
| def endheaders(self: "HTTPConnection", *args: "Any", **kwargs: "Any") -> "Any": | ||
| real_url = getattr(self, "_sentrysdk_trace_url", None) |
There was a problem hiding this comment.
Does real_url represent the original URL in the request? Or should this instead be called trace_url?
There was a problem hiding this comment.
yes, it represents the whole URL, but its mainly used for trace propagation. _sentrysdk_real_url is better though.
| self, "_sentrysdk_request_headers", {} | ||
| ) | ||
| if request_headers is not None: |
There was a problem hiding this comment.
If _sentrysdk_request_headers is not set on self and the empty dict default is returned, the if request_headers is not None conditional will still pass.
If this is the intent, it'd be clearer to have the default value for getattr be None as the way that this is currently written, this looks like a potential bug 😅
| for ( | ||
| signed_header | ||
| ) in _get_aws_sigv4_signed_headers_from_url_query_string( | ||
| real_url | ||
| ): | ||
| if signed_header in _SENTRY_HEADER_NAMES: | ||
| is_present, _ = request_headers.get( | ||
| signed_header, (False, False) | ||
| ) | ||
| request_headers[signed_header] = (is_present, True) |
There was a problem hiding this comment.
I'm finding this block of code (and the related one further up that deals with the authorization header) a bit confusing.
In what circumstances would the signed_header not be found in request_headers but is present on the query string, such that is_present would be False (lines 160-62)?
Related to that - do we need the request_headers.get() part that sets is_present? If the signed header is in the query string, would that not mean that, at this point in the code, is_present is always true?
| normalized_header, (False, False) | ||
| ) | ||
| if is_signed or ( | ||
| is_present and normalized_header != BAGGAGE_HEADER_NAME |
There was a problem hiding this comment.
If we're looking to avoid a duplicate sentry-trace (the comment on line 172), should BAGGAGE_HEADER_NAME instead be SENTRY_TRACE_HEADER_NAME? Do we potentially need to avoid duplicating both of these headers?
| # track which propagation headers are present and signed e.g. {"sentry-trace": (is_present, is_signed)} | ||
| request_headers: "Optional[Dict[str, Tuple[bool, bool]]]" = {} | ||
| self._sentrysdk_request_headers = request_headers # type: ignore[attr-defined] |
There was a problem hiding this comment.
Was there code that was previously here that used request_headers? This looks like we could remove request_headers and assign self._sentrysdk_request_headers to an empty dict instead
| # track which propagation headers are present and signed e.g. {"sentry-trace": (is_present, is_signed)} | |
| request_headers: "Optional[Dict[str, Tuple[bool, bool]]]" = {} | |
| self._sentrysdk_request_headers = request_headers # type: ignore[attr-defined] | |
| self._sentrysdk_request_headers = {} |
There was a problem hiding this comment.
I was trying to fix a mypy error, but just did type: ignore[attr-defined] in the end and forgot to remove that part.

Description
Summary of issue
baggagewas not included inSignedHeaders. Any later modifications to the value did not invalidate the request.before-signevent. It addsbaggage, ... andx-datadog-*before signing. Any later modifications to the value DO invalidate the request, thus later HTTP-client injection is suppressed to avoid duplicate headers.before-signhandler writes the baggage to the AWS requestbaggagein the SigV4 signaturebaggagevalue403 ForbiddenorSignatureDoesNotMatch.Changes
before-signhandler, so finalbaggageandsentry-tracevalues are created before SigV4 signing.http.clientpropagation is delayed untilendheaders(), when the complete request headers and SigV4SignedHeadersare available. Existingbaggageheader is never mutated after it already was signed.Issues
Resolves: #7031 & #7031
Related issues in dd-trace-py: #19477 & #19358
Reminders
uv run ruff.feat:,fix:,ref:,meta:)