Skip to content

Commit df7e83a

Browse files
mutantkeyboardAntonio Nesic
andauthored
Added Partitioned flag for cookies (#2230)
Co-authored-by: Antonio Nesic <[email protected]>
1 parent 99e8162 commit df7e83a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

lib/inc/drogon/Cookie.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ class DROGON_EXPORT Cookie
156156
sameSite_ = sameSite;
157157
}
158158

159+
/**
160+
* @brief Set the partitioned status of the cookie
161+
*/
162+
void setPartitioned(bool partitioned)
163+
{
164+
partitioned_ = partitioned;
165+
if (partitioned)
166+
{
167+
setSecure(true);
168+
}
169+
}
170+
159171
/**
160172
* @brief Get the string value of the cookie
161173
*/
@@ -282,6 +294,17 @@ class DROGON_EXPORT Cookie
282294
return secure_;
283295
}
284296

297+
/**
298+
* @brief Check if the cookie is partitioned.
299+
*
300+
* @return true means the cookie is partitioned.
301+
* @return false means the cookie is not partitioned.
302+
*/
303+
bool isPartitioned() const
304+
{
305+
return partitioned_;
306+
}
307+
285308
/**
286309
* @brief Get the max-age of the cookie
287310
*/
@@ -394,6 +417,7 @@ class DROGON_EXPORT Cookie
394417
trantor::Date expiresDate_{(std::numeric_limits<int64_t>::max)()};
395418
bool httpOnly_{true};
396419
bool secure_{false};
420+
bool partitioned_{false};
397421
std::string domain_;
398422
std::string path_;
399423
std::string key_;

lib/src/Cookie.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,18 @@ std::string Cookie::cookieString() const
6969
ret.append("SameSite=Lax; ");
7070
}
7171
}
72-
if (secure_ && sameSite_ != SameSite::kNone)
72+
if ((secure_ && sameSite_ != SameSite::kNone) || partitioned_)
7373
{
7474
ret.append("Secure; ");
7575
}
7676
if (httpOnly_)
7777
{
7878
ret.append("HttpOnly; ");
7979
}
80+
if (partitioned_)
81+
{
82+
ret.append("Partitioned; ");
83+
}
8084
ret.resize(ret.length() - 2); // delete last semicolon
8185
ret.append("\r\n");
8286
return ret;

lib/tests/unittests/CookieTest.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,29 @@ DROGON_TEST(CookieTest)
3535
drogon::Cookie::convertString2SameSite("Strict"));
3636
CHECK(drogon::Cookie::SameSite::kNone ==
3737
drogon::Cookie::convertString2SameSite("None"));
38+
39+
// Test for Partitioned attribute
40+
drogon::Cookie cookie6("test", "6");
41+
cookie6.setPartitioned(true);
42+
CHECK(cookie6.cookieString() ==
43+
"Set-Cookie: test=6; Secure; HttpOnly; Partitioned\r\n");
44+
// Test that partitioned attribute automatically sets secure
45+
drogon::Cookie cookie7("test", "7");
46+
cookie7.setPartitioned(true);
47+
CHECK(cookie7.isSecure() == true);
48+
// Test other attributes
49+
drogon::Cookie cookie8("test", "8");
50+
cookie8.setPartitioned(true);
51+
cookie8.setDomain("drogon.org");
52+
cookie8.setMaxAge(3600);
53+
CHECK(cookie8.cookieString() ==
54+
"Set-Cookie: test=8; Max-Age=3600; Domain=drogon.org; Secure; "
55+
"HttpOnly; Partitioned\r\n");
56+
// Teset Partitioned and SameSite can coexist
57+
drogon::Cookie cookie9("test", "9");
58+
cookie9.setPartitioned(true);
59+
cookie9.setSameSite(drogon::Cookie::SameSite::kLax);
60+
CHECK(
61+
cookie9.cookieString() ==
62+
"Set-Cookie: test=9; SameSite=Lax; Secure; HttpOnly; Partitioned\r\n");
3863
}

0 commit comments

Comments
 (0)