Repository navigation
fix: Handle malformed Content-Length in size metrics - #405
Open
Sam-Razavi wants to merge 1 commit into
Open
Sam-Razavi wants to merge 1 commit into
Sam-Razavi wants to merge 1 commit into
Conversation
int() raises ValueError on a Content-Length value that is not a valid integer. The exception escaped the instrumentation callback and the size was not recorded. Observe 0 instead, the same as a missing header. Fixes trallnag#404
Author
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this do?
Adds a small
_parse_content_lengthhelper and uses it wherever the size metrics convert aContent-Lengthheader. A value that isn't a valid integer is now observed as0, the same as a missing header. This coversrequest_size,response_size,combined_sizeanddefault, on both the request and response side.Why do we need it?
int()raisesValueErroron a malformed value. The exception escapes the instrumentation callback and the size isn't recorded.The impact is narrower than #404 describes. I tested
Content-Length: abcagainst Uvicorn (h11 and httptools), Hypercorn, Daphne and Granian, and all of them return 400 before the app sees the request. Calling the app directly at the ASGI level, the client still gets its normal 200, and theValueErrorpropagates to the server afterwards. So I'd treat this as hardening for servers or adapters that pass the header through, rather than a way for clients to force a 500.Linked issues
Fixes #404
Reviewer notes
Content-Length: -5doesn't raise, so I left it out of scope, though it would be observed as a negative size. Happy to handle it here if you'd prefer.test_instrumentation.pyfail with or without this change becauseprocess_cpu_seconds_totalisn't available there.