-
Notifications
You must be signed in to change notification settings - Fork 5.3k
ext_proc: Check validity of the :status header #17596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ namespace ExternalProcessing { | |
| using Http::Headers; | ||
| using Http::LowerCaseString; | ||
|
|
||
| using envoy::config::core::v3::HeaderValueOption; | ||
| using envoy::service::ext_proc::v3alpha::BodyMutation; | ||
| using envoy::service::ext_proc::v3alpha::BodyResponse; | ||
| using envoy::service::ext_proc::v3alpha::CommonResponse; | ||
|
|
@@ -56,19 +57,26 @@ void MutationUtils::applyHeaderMutations(const HeaderMutation& mutation, Http::H | |
| if (!sh.has_header()) { | ||
| continue; | ||
| } | ||
| if (isSettableHeader(sh.header().key(), replacing_message)) { | ||
| if (!isSettableHeader(sh, replacing_message)) { | ||
| // Log the failure to set the header here, but don't log the value in case it's | ||
| // something sensitive like the Authorization header. | ||
| ENVOY_LOG(debug, "Ignorning improper attempt to set header {}", sh.header().key()); | ||
| } else { | ||
| // Make "false" the default. This is logical and matches the ext_authz | ||
| // filter. However, the router handles this same protobuf and uses "true" | ||
| // as the default instead. | ||
| const bool append = PROTOBUF_GET_WRAPPED_OR_DEFAULT(sh, append, false); | ||
| ENVOY_LOG(trace, "Setting header {} append = {}", sh.header().key(), append); | ||
| if (append) { | ||
| headers.addCopy(LowerCaseString(sh.header().key()), sh.header().value()); | ||
| const LowerCaseString lcKey(sh.header().key()); | ||
| if (append && !headers.get(lcKey).empty() && !isAppendableHeader(lcKey)) { | ||
| ENVOY_LOG(debug, "Ignoring duplicate value for header {}", sh.header().key()); | ||
| } else { | ||
| headers.setCopy(LowerCaseString(sh.header().key()), sh.header().value()); | ||
| ENVOY_LOG(trace, "Setting header {} append = {}", sh.header().key(), append); | ||
| if (append) { | ||
| headers.addCopy(lcKey, sh.header().value()); | ||
| } else { | ||
| headers.setCopy(lcKey, sh.header().value()); | ||
| } | ||
| } | ||
| } else { | ||
| ENVOY_LOG(debug, "Header {} is not settable", sh.header().key()); | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -112,16 +120,40 @@ void MutationUtils::applyBodyMutations(const BodyMutation& mutation, Buffer::Ins | |
| } | ||
| } | ||
|
|
||
| bool MutationUtils::isValidHttpStatus(int code) { return (code >= 200); } | ||
|
|
||
| // Ignore attempts to set certain sensitive headers that can break later processing. | ||
| // We may re-enable some of these after further testing. This logic is specific | ||
| // to the ext_proc filter so it is not shared with HeaderUtils. | ||
| bool MutationUtils::isSettableHeader(absl::string_view key, bool replacing_message) { | ||
| bool MutationUtils::isSettableHeader(const HeaderValueOption& header, bool replacing_message) { | ||
| const auto& key = header.header().key(); | ||
| const auto& headers = Headers::get(); | ||
| return !absl::EqualsIgnoreCase(key, headers.HostLegacy.get()) && | ||
| !absl::EqualsIgnoreCase(key, headers.Host.get()) && | ||
| (!absl::EqualsIgnoreCase(key, headers.Method.get()) || replacing_message) && | ||
| !absl::EqualsIgnoreCase(key, headers.Scheme.get()) && | ||
| !absl::StartsWithIgnoreCase(key, headers.prefix()); | ||
| if (absl::EqualsIgnoreCase(key, headers.HostLegacy.get()) || | ||
| absl::EqualsIgnoreCase(key, headers.Host.get()) || | ||
| (absl::EqualsIgnoreCase(key, headers.Method.get()) && !replacing_message) || | ||
| absl::EqualsIgnoreCase(key, headers.Scheme.get()) || | ||
| absl::StartsWithIgnoreCase(key, headers.prefix())) { | ||
| return false; | ||
| } | ||
| if (absl::EqualsIgnoreCase(key, headers.Status.get())) { | ||
| const auto& value = header.header().value(); | ||
| uint32_t status; | ||
| if (!absl::SimpleAtoi(value, &status)) { | ||
| ENVOY_LOG(debug, "Invalid value {} for HTTP status code", value); | ||
| return false; | ||
| } | ||
| if (!isValidHttpStatus(status)) { | ||
| ENVOY_LOG(debug, "Invalid HTTP status code {}", status); | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // Ignore attempts to append a second value to any system header, as in general those | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these are usually referred to as psuedo headers, not system headers |
||
| // were never designed to support multiple values. | ||
| bool MutationUtils::isAppendableHeader(absl::string_view key) { | ||
| return !key.empty() && key[0] != ':'; | ||
| } | ||
|
|
||
| } // namespace ExternalProcessing | ||
|
|
||
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to signal an error to the processing server if we get back an invalid status code in the response? Might make it easier for operators to understand that something is misconfigured
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now we don't have a mechanism to do that. That applies to attempts to set invalid headers as well as attempts to set an invalid status. I'm not sure what would be a good way -- for instance, we could send a list of error messages on the next message that we send to the processor, or we could introduce a third set of messages (request, response, and response status) to the protocol -- but I feel like either of these would make things more complicated and make it harder to write a processor.
One thing we should probably do is introduce another statistic so that there is a way to count the number of invalid responses by looking at a stat instead of requiring that people turn on debug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I think a statistic would be good, enabling debug logging is often not an option when debugging production systems due the log volume
Maybe follow up with that in another PR?