Skip to content

hcm: adding a knob for setting scheme - #17070

Merged
alyssawilk merged 12 commits into
envoyproxy:mainfrom
alyssawilk:hcm_scheme
Jul 7, 2021
Merged

alyssawilk merged 12 commits into
envoyproxy:mainfrom
alyssawilk:hcm_scheme

Conversation

@alyssawilk

@alyssawilk alyssawilk commented Jun 21, 2021 •

Copy link
Copy Markdown
Contributor

Adding the option to
Risk Level: low (config guarded code)
Testing: unit testing
Docs Changes: n/a
Release Notes: inline
Part of #14587
Fixes #17105

@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to api/envoy/.
envoyproxy/api-shepherds assignee is @mattklein123
CC @envoyproxy/api-watchers: FYI only for changes made to api/envoy/.

🐱

Caused by: #17070 was opened by alyssawilk.

see: more, trace.

Signed-off-by: Alyssa Wilk <[email protected]>

@snowp snowp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, just a few comments

// Overwrite any Scheme header with the contents of scheme
OVERWRITE_SCHEME = 0;

// If no Scheme header is present, append Scheme scheme

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since we'd always be appending to an empty string should this be called ADD_SCHEME_IF_ABSENT instead?

OVERWRITE_SCHEME = 0;

// If no Scheme header is present, append Scheme scheme
// If a Scheme header is present, pass it through.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For my understanding: when would the scheme not be present?

[(validate.rules).enum = {defined_only: true}];

// Defines the action to be applied to the Scheme header on the request path
// if scheme is set in the config. Defaults to OVERWRITE_SCHEME.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should this reference the scheme parameter with :ref:?

[(validate.rules).enum = {defined_only: true}];

// An optional override. If set, the connection manager will set the request
// scheme to this value based on scheme_header_transformation. Valid values are 'http' and

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here, :ref: even if they are next to each other :)

config.schemeHeaderTransformation());
}

TEST_F(HttpConnectionManagerConfigTest, SchemeINvalid) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SchemeINvalid -> SchemeInvalid

@snowp snowp added the waiting label Jun 23, 2021
Signed-off-by: Alyssa Wilk <[email protected]>
Signed-off-by: Alyssa Wilk <[email protected]>

enum SchemeHeaderTransformation {
// Overwrite any Scheme header with the contents of scheme
OVERWRITE_SCHEME = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The config is still confusing to me. Can we define it as "oneof"

oneof {
// the scheme to override
string  override_scheme = 11;

// preserve downstream one, if absent set it with upstream security level
protobuf.Empty preserve_downstream = 12;

// always set it as upstream security level
protobuf.Empty by_upstream_security_level = 13
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is consistent with other header overrides - will let API shephards make that call through :-)

@alyssawilk

Copy link
Copy Markdown
Contributor Author

@phlax added the new config here and confirmed it no longer redirects

@phlax

phlax commented Jun 26, 2021

Copy link
Copy Markdown
Member

@alyssawilk i picked the changes in this pr, built envoy with it and tested with the default config

not sure if this pr is still wip, but using the built envoy i got three different results

  • segfault - i think in Envoy::Http::HeaderString::setCopy()
  • 400 bad response and some wierdly mangled headers 'x-forwarded-proto', '�oV��o'
  • worked perfectly

i have log traces from each of the outcomes if its helpful

worth mentioning is that whichever of the outcomes occurred the server continued with that until being restarted - ie if it worked once it continued working perfectly. It never gets the redirects problem. This is unlike envoy main in which you start the server and sometimes it works and sometimes it redirects - restarting has no effect

fwiw i tested out updating the default config to point to google and that also got spun out on redirects, so its not just the envoyproxy site i think

im wondering more generally - if this used to work without having to set anything extra - and that was the "default" demo config - can we make it work that way again - at least initially ?

@alyssawilk

Copy link
Copy Markdown
Contributor Author

so the default demo config had a listener which was listening in the clear, and then forwarding that content over TLS. Envoy proxy expects TLS traffic and now can detect (via x-forwarded-proto) that the request was sent in cleartext and is so not actually secure. The new default behavior is closing a fairly serious security loophole. We could address by either having Envoy proxy allow http traffic, or making sure our example uses TLS rather than sending https urls in the clear :-)

Signed-off-by: Alyssa Wilk <[email protected]>

@mattklein123 mattklein123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for helping to sort this out. Starting out with some API comments.

/wait

Signed-off-by: Alyssa Wilk <[email protected]>
Signed-off-by: Alyssa Wilk <[email protected]>
Signed-off-by: Alyssa Wilk <[email protected]>

@mattklein123 mattklein123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for fixing this. Looks great. Some small comments.

/wait

Comment thread api/envoy/config/core/v3/protocol.proto Resolved
For HTTP/2, and HTTP/3, incoming :scheme headers are trusted and propogated through upstream.
For HTTP/1, the :scheme header will be set
1) From the absolute URL if present and valid. An invalid (not "http" or "https") scheme, or an https scheme over an unencrypted connection will result in Envoy rejecting the request.
2) From the value of the :ref:`config_http_conn_man_headers_x-forwarded-proto` header after sanitization.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Option 3 is to set from whether the connection is TLS or not? Clarify?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Here I propose to have another scheme header setting policy: always set it according to the upstream security level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm pretty sure that'll break mesh networks pretty fast, if some hops are secure and some are insecure.
Generally if scheme is set it should be preserved. If it's not set, it is set according to upstream security level as you suggest.

@qiwzhang qiwzhang Jun 30, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I proposed to add an option in scheme_header_transformation to allow setting ":scheme" according to the upstream security level. I totally agreed with you that scheme should be preserved by the proxy if all proxy in the path works the same way. But reality is: some are not. In GCP, there are GFE, Cloud-run app-server, one of them may set scheme wrong. If we add this option, envoy will have a way to set it correctly. This is just an option which will not be enabled by default.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can add that if necessary but I'd prefer not to unless we actually need to, because that's not what :scheme is for. As commented, it's a part of the URI and I'm not yet clear on why we'd want to change the URI to one thing for one cluster and another for another. If you have a use case where a global override doesn't work and we need a per cluster override I can of course look into that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I do also have concerns about consistency if we go the cluster route. Previously, scheme was only set for outbound traffic so filters didn't see the value. If we add a "set based on cluster" knob we can best effort set on ingress based on expected cluster, but the expected cluster can change over the course of the filter chain so IMO is much less stable and adds complexity to an already complex process.

That said there was previously support for that, so if we need to again we'll just have to discuss how to make it as clear and consistent as possible.

Comment on lines +361 to +362
If *xff_num_trusted_hops* is zero, downstream *x-forwarded-proto* headers will be set to http
or https based on if the downstream connection is TLS or not.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Per above, for H1 scheme will also be set in this case, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, though I was trying to talk to scheme updates in the scheme section and XFP in the XFP section. Given the complexity do you think redundancy is a better plan?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah I would be pretty redundant here and over share, given how important this is to understand.

Comment on lines +169 to +170
request_headers.setScheme(config.schemeToSet().value());
request_headers.setForwardedProto(config.schemeToSet().value());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technically if we control the values here (enum, etc.) I think we could use reference variants here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shouldn't x-forwarded-proto be the old one, not the new one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think in the case we force-overwrite we should force-overwrite both, which is what we have in the API and docs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, I am not sure. I thought XFP should be the one from original client, force-overwrite should not overwrite it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Scheme should also be from the original client. :-)
If we're using this knob it's cause we want to set a scheme which doesn't match underlying encryption.

I'm going for the principle of least surprise here. Because XFP is the way to communicate http/https for HTTP/1.1, if we have several Envoy hops of mixed HTTP/1 and HTTP/2 we want to force something to look like it's https scheme, you must overwrite both scheme and XFP to have it consistently look like it's https scheme through both sets of hops. If folks want scheme and XFP to not match they can always change XFP back in request_headers_to_add, but I assume they're usually going to want to have them match.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

My understanding is: scheme should always matches underlying connection security level. I could be wrong then. In that case, I am fine overwriting both scheme and XFP

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah, :scheme actually indicates a fundamental part of the URI
https://datatracker.ietf.org/doc/html/rfc7540#section-8.1.2.3
It's a fundamental part of the resource being requested and is not supposed to change from hop to hop based on connection security level.

Signed-off-by: Alyssa Wilk <[email protected]>

@alyssawilk alyssawilk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the thoughtful review!

For HTTP/2, and HTTP/3, incoming :scheme headers are trusted and propogated through upstream.
For HTTP/1, the :scheme header will be set
1) From the absolute URL if present and valid. An invalid (not "http" or "https") scheme, or an https scheme over an unencrypted connection will result in Envoy rejecting the request.
2) From the value of the :ref:`config_http_conn_man_headers_x-forwarded-proto` header after sanitization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +361 to +362
If *xff_num_trusted_hops* is zero, downstream *x-forwarded-proto* headers will be set to http
or https based on if the downstream connection is TLS or not.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, though I was trying to talk to scheme updates in the scheme section and XFP in the XFP section. Given the complexity do you think redundancy is a better plan?

Comment on lines +169 to +170
request_headers.setScheme(config.schemeToSet().value());
request_headers.setForwardedProto(config.schemeToSet().value());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think in the case we force-overwrite we should force-overwrite both, which is what we have in the API and docs.

Signed-off-by: Alyssa Wilk <[email protected]>

@mattklein123 mattklein123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks LGTM with a few small comments.

/wait

HttpConnectionManagerProto::ServerHeaderTransformation server_transformation_{
HttpConnectionManagerProto::OVERWRITE};
std::string server_name_;
absl::optional<std::string> scheme_;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit

Suggested change
absl::optional<std::string> scheme_;
absl::optional<std::string> scheme_to_set_;

(Or scheme_to_overwrite_ which might be more clear, up to you)

Comment on lines +909 to +912
HttpConnectionManagerConfig config(parseHttpConnectionManagerFromYaml(yaml_string), context_,
date_provider_, route_config_provider_manager_,
scoped_routes_config_provider_manager_, http_tracer_manager_,
filter_config_provider_manager_);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't really test anything. Can you query the value from the config at least?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

whoops - lost the actual test on merge :-P

Signed-off-by: Alyssa Wilk <[email protected]>

@mattklein123 mattklein123 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM modulo any remaining @qiwzhang comments. Thank you!

@repokitteh-read-only repokitteh-read-only Bot removed the api label Jul 1, 2021
@phlax

phlax commented Jul 1, 2021

Copy link
Copy Markdown
Member

ill test this with #17200 now...

@phlax

phlax commented Jul 1, 2021

Copy link
Copy Markdown
Member

worked perfectly every time! great, thanks

@phlax phlax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@alyssawilk please could you fix the inline literals in the docs

For HTTP/2, and HTTP/3, incoming *:scheme* headers are trusted and propogated through upstream.
For HTTP/1, the *:scheme* header will be set
1) From the absolute URL if present and valid. An invalid (not "http" or "https") scheme, or an https scheme over an unencrypted connection will result in Envoy rejecting the request. This is the only scheme validation Envoy performs as it avoids a HTTP/1.1-specific privledge escalation attack for edge Envoys [1]_ which doesn't have a comparable vector for HTTP/2 and above [2]_.
2) From the value of the :ref:`config_http_conn_man_headers_x-forwarded-proto` header after sanitization (to valid *x-forwarded-proto* from trusted downstreams, otherwise based on downstream encryption level).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

please use double-backtick literal for ``x-forward-proto``

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I'd prefer a follow-up sweep to tthe file at large. I think all other headers in this file are *d rather than ``d and I'd prefer consistency
x-b3-traceid x-b3-spanid x-b3-parentspanid x-b3-sampled x-datadog-trace-id x-envoy-external-address etc

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

k, thanks

of the connection terminated by front/edge Envoy. *x-forwarded-proto* contains this information. It
will be set to either *http* or *https*.

Downstream *x-forwarded-proto* headers will only be trusted if *xff_num_trusted_hops* is non-zero.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

literals

@@ -341,6 +360,14 @@ It is a common case where a service wants to know what the originating protocol
of the connection terminated by front/edge Envoy. *x-forwarded-proto* contains this information. It
will be set to either *http* or *https*.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

literals

@alyssawilk

Copy link
Copy Markdown
Contributor Author

@phlax I'm going to merge as-is - lmk if you want a follow up with general docs clean up

@alyssawilk
alyssawilk merged commit fe8f224 into envoyproxy:main Jul 7, 2021
leyao-daily pushed a commit to leyao-daily/envoy that referenced this pull request Sep 30, 2021
Adding the option to override scheme
Risk Level: low (config guarded code)
Testing: unit testing
Docs Changes: n/a
Release Notes: inline
Part of envoyproxy#14587
Fixes envoyproxy#17105

Signed-off-by: Alyssa Wilk <[email protected]>
@alyssawilk
alyssawilk deleted the hcm_scheme branch February 28, 2022 21:25
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.

Default Envoy config no longer works

5 participants