fix: require RSRC_CLIENT_REQUEST_HEADERS in ConditionMethod #9976
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.
Problem
In the header_rewrite plugin, there are some cases where the METHOD condition doesn't work correctly.
The following rewriting rule is one such case.
How to reproduce
Please refer to the test added in this PR.
Cause
When header rewrite parses each rule, it keeps tabs of the resources required for evaluation. Then during evaluation, it gathers the required resources before applying each rule.
The required resource for the METHOD condition is the client request, used to extract the request method. However, the condition doesn't include the client request in its list of required resources. As a result, the condition will always fail to match because it'll compare the expected method against an empty string.
It's worth mentioning that the condition will sometimes work, though. If some other condition or operator in the same rule requires the client request, the METHOD condition can then obtain the request method. Otherwise, it'll be broken.
Here are some pointers to the relevant bits of code:
This is where the METHOD condition extracts the request method from the client request. It needs res.client_bufp and res.client_hdr_loc to be set beforehand.
trafficserver/plugins/header_rewrite/conditions.cc
Lines 106 to 121 in fc35742
This is where resources are gathered during evaluation of each rule. If the RSRC_CLIENT_REQUEST_HEADERS flag is set, then it proceeds to set res.client_bufp and res.client_hdr_loc.
trafficserver/plugins/header_rewrite/header_rewrite.cc
Line 286 in fc35742
trafficserver/plugins/header_rewrite/resources.cc
Lines 34 to 40 in fc35742
This is where the METHOD condition is initialized during the parsing phase. As we can see, it doesn't request access to the client request by setting the RSRC_CLIENT_REQUEST_HEADERS flag.
trafficserver/plugins/header_rewrite/conditions.cc
Lines 85 to 93 in fc35742
In contrast, the HEADER condition sets the RSRC_CLIENT_REQUEST_HEADERS flag, enabling access to the client request for any rule that uses the condition. So when used together with the METHOD condition, the METHOD condition would be able to obtain the request method.
trafficserver/plugins/header_rewrite/conditions.cc
Lines 201 to 214 in fc35742