Skip to content

Commit

Permalink
cookie session: support 0 ttl
Browse files Browse the repository at this point in the history
Signed-off-by: Kuat Yessenov <[email protected]>
  • Loading branch information
kyessenov committed Jan 29, 2024
1 parent 5c821fe commit f8306f0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
9 changes: 6 additions & 3 deletions source/extensions/http/stateful_session/cookie/cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ void CookieBasedSessionStateFactory::SessionStateImpl::onUpdate(
if (!upstream_address_.has_value() || host_address != upstream_address_.value()) {
if (Runtime::runtimeFeatureEnabled(
"envoy.reloadable_features.stateful_session_encode_ttl_in_cookie")) {
auto expiry_time = std::chrono::duration_cast<std::chrono::seconds>(
(time_source_.monotonicTime() + std::chrono::seconds(factory_.ttl_)).time_since_epoch());
// Build proto message
envoy::Cookie cookie;
cookie.set_address(std::string(host_address));
cookie.set_expires(expiry_time.count());
if (factory_.ttl_ != std::chrono::seconds::zero()) {
auto expiry_time = std::chrono::duration_cast<std::chrono::seconds>(
(time_source_.monotonicTime() + std::chrono::seconds(factory_.ttl_))
.time_since_epoch());
cookie.set_expires(expiry_time.count());
}
std::string proto_string;
cookie.SerializeToString(&proto_string);

Expand Down
18 changes: 10 additions & 8 deletions source/extensions/http/stateful_session/cookie/cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ class CookieBasedSessionStateFactory : public Envoy::Http::SessionStateFactory {
envoy::Cookie cookie;
if (cookie.ParseFromString(decoded_value)) {
address = cookie.address();
if (address.empty() || (cookie.expires() == 0)) {
if (address.empty()) {
return absl::nullopt;
}

std::chrono::seconds expiry_time(cookie.expires());
auto now = std::chrono::duration_cast<std::chrono::seconds>(
(time_source_.monotonicTime()).time_since_epoch());
if (now > expiry_time) {
// Ignore the address extracted from the cookie. This will cause
// upstream cluster to select a new host and new cookie will be generated.
return absl::nullopt;
if (cookie.expires() != 0) {
std::chrono::seconds expiry_time(cookie.expires());
auto now = std::chrono::duration_cast<std::chrono::seconds>(
(time_source_.monotonicTime()).time_since_epoch());
if (now > expiry_time) {
// Ignore the address extracted from the cookie. This will cause
// upstream cluster to select a new host and new cookie will be generated.
return absl::nullopt;
}
}
} else {
ENVOY_LOG_ONCE_MISC(
Expand Down
5 changes: 2 additions & 3 deletions test/extensions/http/stateful_session/cookie/cookie_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ TEST(CookieBasedSessionStateFactoryTest, SessionStateTest) {
if (use_proto) {
envoy::Cookie cookie;
cookie.set_address("1.2.3.4:80");
cookie.set_expires(1000);
cookie.SerializeToString(&cookie_content);
} else {
cookie_content = "1.2.3.4:80";
Expand Down Expand Up @@ -176,13 +175,13 @@ TEST(CookieBasedSessionStateFactoryTest, SessionStateProtoCookie) {
EXPECT_EQ(absl::nullopt, session_state->upstreamAddress());

// PROTO format - no "expired field"
cookie.set_expires(0);
cookie.clear_expires();
cookie.SerializeToString(&cookie_content);
request_headers = {{":path", "/path"},
{"cookie", "override_host=" + Envoy::Base64::encode(cookie_content.c_str(),
cookie_content.length())}};
session_state = factory.create(request_headers);
EXPECT_EQ(absl::nullopt, session_state->upstreamAddress());
EXPECT_EQ("2.3.4.5:80", session_state->upstreamAddress().value());

// PROTO format - pass incorrect format.
// The content should be treated as "old" style encoding.
Expand Down

0 comments on commit f8306f0

Please sign in to comment.