Skip to content
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

context: harden empty header cases #9862

Merged
merged 3 commits into from
Jan 29, 2020

Conversation

kyessenov
Copy link
Contributor

@kyessenov kyessenov commented Jan 28, 2020

Signed-off-by: Kuat Yessenov kuat@google.com

Description: envoy passes nullptr whenever either headers or trailers are not available. This change hardens the context to handle those cases.
Risk Level: low
Testing: added unit tests
Docs Changes: none
Release Notes: none
CC: @douglas-reid

Signed-off-by: Kuat Yessenov <kuat@google.com>
@kyessenov kyessenov requested a review from lizan as a code owner January 28, 2020 22:50
if (optional_status.has_value()) {
return optional_status;
if (trailers) {
auto status = Grpc::Common::getGrpcStatus(*trailers, allow_user_defined);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor perf optimizations to avoid unnecessary computation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any case outside of test passing nullptr to this? Did I miss anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's passed from context.cc which itself is used by Wasm context.cc. Wasm sets trailers to nullptr (a separate issue which we are debugging).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC right now we assume that headers are always passed to these functions, and static empty headers are passed in cases where there are none, which keeps the logic more simple. I would recommend you do the same from the WASM code unless there is a compelling reason to change this. If we do change it we should be consistent everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasm captures local variables into object fields here, so it's natural to use nullptr as a default value (for performance reasons I assume to avoid de-allocating an optional). Generally speaking, I am not sure it's always right to replace missing headers with empty headers.

If there is a strong desire to use HeaderMap&, I can adapt the code to do that. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Originally I think the code was written this way to avoid having the conditional checks in a bunch of places. My point mainly is that I think we should be consistent here so I would take a look at all the callers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted the changes outside of the context to keep the nullptr business confined to that unit. Using header map singleton consistent with other instances (e.g. access_log_base.cc).

Signed-off-by: Kuat Yessenov <kuat@google.com>
@@ -141,15 +143,18 @@ absl::optional<CelValue> ResponseWrapper::operator[](CelValue key) const {
} else if (value == Flags) {
return CelValue::CreateInt64(info_.responseFlags());
} else if (value == GrpcStatus) {
auto const& optional_status =
Grpc::Common::getGrpcStatus(*(trailers_.value_), *(headers_.value_), info_);
ConstSingleton<Http::HeaderMapImpl> empty_headers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need even to instantiate ConstSingleton here. Just use ConstSingleton<Http::HeaderMapImpl>::get().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah, it's a static member. Done.

Signed-off-by: Kuat Yessenov <kuat@google.com>
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mattklein123 mattklein123 merged commit d7b6474 into envoyproxy:master Jan 29, 2020
kyessenov added a commit to kyessenov/envoy that referenced this pull request Jan 30, 2020
Signed-off-by: Kuat Yessenov <kuat@google.com>
kyessenov added a commit to istio/envoy that referenced this pull request Jan 30, 2020
kyessenov added a commit to istio/envoy that referenced this pull request Feb 3, 2020
Signed-off-by: Kuat Yessenov <kuat@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants