Skip to content

Commit b7f5096

Browse files
ramaraochavaliphlax
authored andcommitted
http: fix cookie attributes (#34885)
--------- Signed-off-by: Rama Chavali <[email protected]>
1 parent 22c5a3c commit b7f5096

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

changelogs/current.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ minor_behavior_changes:
88

99
bug_fixes:
1010
# *Changes expected to improve the state of the world and are unlikely to have negative effects*
11+
- area: http
12+
change: |
13+
Fixed a bug where additional :ref:`cookie attributes <envoy_v3_api_msg_config.route.v3.RouteAction.HashPolicy.cookie>`
14+
are not sent properly to clients.
1115
1216
removed_config_or_runtime:
1317
# *Normally occurs at the end of the* :ref:`deprecation period <deprecated>`

source/common/http/hash_policy.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ class CookieHashMethod : public HashMethodImplBase {
8282
CookieHashMethod(const std::string& key, const std::string& path,
8383
const absl::optional<std::chrono::seconds>& ttl, bool terminal,
8484
const CookieAttributeRefVector attributes)
85-
: HashMethodImplBase(terminal), key_(key), path_(path), ttl_(ttl), attributes_(attributes) {}
85+
: HashMethodImplBase(terminal), key_(key), path_(path), ttl_(ttl) {
86+
for (const auto& attribute : attributes) {
87+
attributes_.push_back(attribute);
88+
}
89+
}
8690

8791
absl::optional<uint64_t> evaluate(const Network::Address::Instance*,
8892
const RequestHeaderMap& headers,
@@ -91,7 +95,11 @@ class CookieHashMethod : public HashMethodImplBase {
9195
absl::optional<uint64_t> hash;
9296
std::string value = Utility::parseCookieValue(headers, key_);
9397
if (value.empty() && ttl_.has_value()) {
94-
value = add_cookie(key_, path_, ttl_.value(), attributes_);
98+
CookieAttributeRefVector attributes;
99+
for (const auto& attribute : attributes_) {
100+
attributes.push_back(attribute);
101+
}
102+
value = add_cookie(key_, path_, ttl_.value(), attributes);
95103
hash = HashUtil::xxHash64(value);
96104

97105
} else if (!value.empty()) {
@@ -104,7 +112,7 @@ class CookieHashMethod : public HashMethodImplBase {
104112
const std::string key_;
105113
const std::string path_;
106114
const absl::optional<std::chrono::seconds> ttl_;
107-
const CookieAttributeRefVector attributes_;
115+
std::vector<CookieAttribute> attributes_;
108116
};
109117

110118
class IpHashMethod : public HashMethodImplBase {

test/integration/multiplexed_integration_test.cc

+39
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,45 @@ TEST_P(MultiplexedRingHashIntegrationTest, CookieRoutingNoCookieWithNonzeroTtlSe
17771777
EXPECT_EQ(set_cookies.size(), 1);
17781778
}
17791779

1780+
TEST_P(MultiplexedRingHashIntegrationTest,
1781+
CookieRoutingNoCookieWithNonzeroTtlSetAndWithAttributes) {
1782+
config_helper_.addConfigModifier(
1783+
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&
1784+
hcm) -> void {
1785+
auto* hash_policy = hcm.mutable_route_config()
1786+
->mutable_virtual_hosts(0)
1787+
->mutable_routes(0)
1788+
->mutable_route()
1789+
->add_hash_policy();
1790+
auto* cookie = hash_policy->mutable_cookie();
1791+
cookie->set_name("foo");
1792+
cookie->mutable_ttl()->set_seconds(15);
1793+
auto* attribute_1 = cookie->mutable_attributes()->Add();
1794+
attribute_1->set_name("test1");
1795+
attribute_1->set_value("value1");
1796+
auto* attribute_2 = cookie->mutable_attributes()->Add();
1797+
attribute_2->set_name("test2");
1798+
attribute_2->set_value("value2");
1799+
});
1800+
1801+
std::set<std::string> set_cookies;
1802+
sendMultipleRequests(
1803+
1024,
1804+
Http::TestRequestHeaderMapImpl{{":method", "POST"},
1805+
{":path", "/test/long/url"},
1806+
{":scheme", "http"},
1807+
{":authority", "host"}},
1808+
[&](IntegrationStreamDecoder& response) {
1809+
EXPECT_EQ("200", response.headers().getStatusValue());
1810+
std::string value(
1811+
response.headers().get(Http::Headers::get().SetCookie)[0]->value().getStringView());
1812+
set_cookies.insert(value);
1813+
EXPECT_THAT(value,
1814+
MatchesRegex("foo=.*; Max-Age=15; test1=value1; test2=value2; HttpOnly"));
1815+
});
1816+
EXPECT_EQ(set_cookies.size(), 1);
1817+
}
1818+
17801819
TEST_P(MultiplexedRingHashIntegrationTest, CookieRoutingNoCookieWithZeroTtlSet) {
17811820
config_helper_.addConfigModifier(
17821821
[&](envoy::extensions::filters::network::http_connection_manager::v3::HttpConnectionManager&

0 commit comments

Comments
 (0)