Repository navigation
Validate trailers when assigning headers with Headers#[]=. - #126
Open
samuel-williams-shopify wants to merge 7 commits into
Open
samuel-williams-shopify wants to merge 7 commits into
samuel-williams-shopify wants to merge 7 commits into
Conversation
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.
Protocol::HTTP::Headers#[]=did not perform trailer validation, so forbidden headers such ascontent-length,transfer-encodingandauthorizationcould be appended to the trailer section aftertrailer!had been called and subsequently written to the wire. This change routes[]=through the same validation asadd, so it now raisesInvalidTrailerErrorin that case, and makessetvalidate before it deletes the existing header.Problem
Once
trailer!has been called,addchecks the header policy'strailer?and raisesInvalidTrailerErrorfor fields which must not appear in trailers (framing, hop-by-hop, routing and credential headers). However,[]=appended directly to@fieldswithout that check, so:merge!was already safe, since it delegates toadd.setalso delegates toadd, so it did raise, but only after callingdelete(key), so a rejectedset("content-length", "2")in trailer mode destroyed the existingcontent-lengthheader and then raised.Separately,
InvalidTrailerError#keywas alwaysnil, becauseinitializenever assigned@key.Solution
validate_trailer!(key, policy)helper, shared byadd,setand[]=.[]=andset, validate whentrailer?is true, before the existingdelete(key)call, so that a rejected assignment leaves the headers (including any existing header of the same name) unchanged.[]=passes the key as given tovalidate_trailer!(the downcased key is still used for the policy lookup), so its error message andInvalidTrailerError#keymatch the caller's spelling, consistent withadd.InvalidTrailerError#initializenow assigns@key.[]=with forbidden fields (content-length,transfer-encoding,authorization) aftertrailer!raises with the matchingkeyand leaves the trailer empty;[]=with a permitted field (etag) appears intrailer; a rejected[]=orsetdoes not delete an existing header;[]=reports the key as given;[]=withcontent-lengthbeforetrailer!still works; and regression guards forset/merge!in trailer mode.add,set,[]=andmerge!validate before modifying the headers and raiseInvalidTrailerError.context/headers.mdis the generated copy of that guide and has been updated to match.This deliberately does not touch
delete/extractindex handling, which is being addressed separately.Not addressed here
In trailer mode,
[]=orseton a key which already exists in the header section deletes it from the header section and appends the new value to the trailer section. With #125 (which adjusts@tailondelete), the appended value becomes a real trailer; however, replacing a header which has conceptually already been sent is arguably meaningless and should probably raise instead. That is left for a follow-up discussion.