web: include offending value in Unsafe header ValueError message#3679
Draft
HrachShah wants to merge 1 commit into
Draft
web: include offending value in Unsafe header ValueError message#3679HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
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.
_convert_header_valueraisedValueError("Unsafe header value %r", retval)— a two-argument form thatstr(e)rendered as the tuple repr('Unsafe header value %r', 'foo\r\nbar')with the actual offending value tucked intoe.args[1]. Callers that logged the exception got the literal"Unsafe header value %r"with the bad value nowhere to be seen, which defeats the purpose of the message.The fix is the obvious one: format the value into the message so
str(e)actually contains it. The two existing integration tests (test_header_injection,test_set_header) still pass because they only assert the substring"Unsafe header value"is instr(e), which is true in both forms.tornado/web.py: changeraise ValueError("Unsafe header value %r", retval)toraise ValueError("Unsafe header value %r" % retval).tornado/test/web_test.py: add a newConvertHeaderValueTestclass with 5 unit tests that exercise the str, bytes (latin1), int, empty-string, and unsafe-value code paths directly without going through a full handler. The unsafe-value test asserts that the bad value appears in the message, thatlen(e.args) == 1, and that"Unsafe header value"is instr(e)— so any future regression to a two-arg raise (or to a different format placeholder) would be caught.Verification: pre-fix the new
test_unsafe_value_message_includes_valuefails withAssertionError: 2 != 1; post-fix all 5 new tests pass. Existingtest_header_injection,test_set_header, and the 3 Accept-Language tests inAcceptLanguageTest(which also touch_convert_header_valueindirectly) all still pass.Drafted with Claude.