Repository navigation
Description
Title: Session cookies are still routing the traffic after the ttl's expiry
Description:
The HTTP requests with cookies are still being routed to the same backend after the expiry of the cookies set in the "TTL" field (https://www.envoyproxy.io/docs/envoy/latest/api-v3/type/http/v3/cookie.proto). I presume we use "Max-Age" in the "Set-Cookie:" field to view the expiry of the cookie.
Repro steps:
Listener configuration similar to the one below but using the go-control plane.
an HTTP request (using curl) to the envoy is routing the request to the same backend after the cookie's expiry
Admin and Stats Output:
"route": {
"weighted_clusters": {
"clusters": [
{
"name": "some_cluster",
"weight": 100
}
],
"total_weight": 100
},
"host_rewrite_literal": "",
"timeout": "30s",
"hash_policy": [
{
"cookie": {
"name": "session-cookie",
"ttl": "25s"
}
}
],
"idle_timeout": "60s"
}
Config: Below is an example, but I am using a control-plane similar to the one in the go-control plane.
static_resources:
listeners:
- name: listener_0
address:
socket_address:
address: 0.0.0.0
port_value: 5000
filter_chains:
filters:
name: envoy.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: ingress_http
drain_timeout: 2s
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains:
- "*"
routes:
- match:
prefix: "/"
route:
cluster: example_proxy_cluster
http_filters:
- name: envoy.filters.http.stateful_session
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.stateful_session.v3.StatefulSession
session_state:
name: envoy.http.stateful_session.cookie
typed_config:
"@type": type.googleapis.com/envoy.extensions.http.stateful_session.cookie.v3.CookieBasedSessionState
cookie:
name: session-cookie
ttl: 25stransport_socket:
name: envoy.transport_sockets.starttls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.starttls.v3.StartTlsConfig
tls_socket_config:
common_tls_context: {}clusters:
- name: some_cluster
transport_socket:
name: envoy.transport_sockets.starttls
typed_config:
"@type": type.googleapis.com/envoy.extensions.transport_sockets.starttls.v3.UpstreamStartTlsConfig
tls_socket_config:
common_tls_context: {}
Logs:
Envoy-logs - Did not see any cookie-related information apart from the cookies mentioned in the requests
Access-Logs - Cookies are present under the route-action.
Activity
[-]Session cookies are still routing the traffic after the ttl's expiry[/-][+]cookies are still routing the traffic after the ttl's expiry[/+]lizan commentedon Mar 27, 2023
@wbpcode @cpakulski
cpakulski commentedon Mar 27, 2023
I can look into this issues.
pramodrj07 commentedon Mar 31, 2023
hi @cpakulski - Did you get a chance to confirm if this is a bug? Would like to validate if it's just me finding this.
cpakulski commentedon Apr 3, 2023
I will verify it this week.
cpakulski commentedon Apr 4, 2023
@pramodrj07 You are right. After Envoy issues cookie to a client, Envoy does not check ttl. As long as the client keeps sending the same cookie, Envoy will route to the originally assigned endpoint, regardless of ttl value. Working on the fix.
wbpcode commentedon Apr 5, 2023
I think the responsibility of cookies management is belong to the client or browser. The TTL is used to tell the client when will the cookies are expired and the client shouldn't send request with expired cookies.
How can envoy check the TTL of cookies in the downstream request? @cpakulski
pramodrj07 commentedon Apr 5, 2023
@wbpcode - Agree the cookie management does belong to the client. However, how does the lifecycle of cookies (specifically hash value assigned per cookie) look from Envoy's perspective? Disregarding the client's side for a moment, does envoy hold on to the cookie value without ever recycling(deleting) it or consider it as expired (considering envoy doesn't care about ttl)?
cpakulski commentedon Apr 5, 2023
From Envoy's perspective cookies are stateless. It means that when a cookie is created and sent downstream to a client, Envoy does not store any context associated with that cookie. Currently the cookie is base-64 encoded IP address of upstream host. When Envoy receives a cookie, it does base-64 decoding, reads the address and asks upstream cluster to select that host. Envoy does not check if it issued that cookie in the past.
@wbpcode To solve the problem with TTL, I would like to add expiration time when base-64 encoding happens. When StatefulSession filter receives a cookie, it would check the expiration date and if it is expired, it will behave the same way as in requests without the cookie - it would ask upstream LB to select a new host. That host would be used to generate a new cookie.
wbpcode commentedon Apr 6, 2023
SGTM to add additional field to the base-64 encoded cookie values. Then we can do more control. But please take the backward compatibility into account when doing the design. Thanks. 😸
wbpcode commentedon Apr 6, 2023
/assign @cpakulski
pramodrj07 commentedon Apr 10, 2023
I see. Thanks for the clarification.
ramaraochavali commentedon Apr 11, 2023
This is very important. Please consider supporting both during the design.
cpakulski commentedon Apr 11, 2023
@ramaraochavali Would you mind explaining why backward compatibility is important for your use case? I thought that Envoy may issue a cookie in any format and HTTP clients just needs to attach that cookie to further requests without caring about the format, length, etc of the cookie.
ramaraochavali commentedon Apr 11, 2023
What you said is generally correct. Our use case is mainly for http stateful session header where we are trying to get the first request also to an ip that we want instead of a random ip and then session getting maintained from there. So in a some rare cases, we use the same logic as envoy to encode the first ip.
Not sure if some one uses it like this for cookie also.
cpakulski commentedon Apr 11, 2023
Understood. Thanks for explaining. For your use case statefullness based on a header should be used (as you are using it now). Documentation clearly states that the content of the header should be base64 encoded IP:port of the preferred upstream host.
However, for cookies, docs do not say anything about encoded content of the cookie. I will provide the backward compatibility but will also generate logs to indicate that this behaviour will change and "old" format of the cookie will be rejected in the future.
ramaraochavali commentedon Apr 12, 2023
Sure. Makes sense. Thank you
pramodrj07 commentedon May 10, 2023
Thank you @cpakulski @wbpcode for your help. Really appreciate it.