Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Partitioned flag for cookies #2230

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/inc/drogon/Cookie.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,18 @@ class DROGON_EXPORT Cookie
sameSite_ = sameSite;
}

/**
* @brief Set the partitioned status of the cookie
*/
void setPartitioned(bool partitioned)
{
partitioned_ = partitioned;
if (partitioned)
{
setSecure(true);
}
}

/**
* @brief Get the string value of the cookie
*/
Expand Down Expand Up @@ -282,6 +294,17 @@ class DROGON_EXPORT Cookie
return secure_;
}

/**
* @brief Check if the cookie is partitioned.
*
* @return true means the cookie is partitioned.
* @return false means the cookie is not partitioned.
*/
bool isPartitioned() const
{
return partitioned_;
}

/**
* @brief Get the max-age of the cookie
*/
Expand Down Expand Up @@ -394,6 +417,7 @@ class DROGON_EXPORT Cookie
trantor::Date expiresDate_{(std::numeric_limits<int64_t>::max)()};
bool httpOnly_{true};
bool secure_{false};
bool partitioned_{false};
std::string domain_;
std::string path_;
std::string key_;
Expand Down
6 changes: 5 additions & 1 deletion lib/src/Cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ std::string Cookie::cookieString() const
ret.append("SameSite=Lax; ");
}
}
if (secure_ && sameSite_ != SameSite::kNone)
if ((secure_ && sameSite_ != SameSite::kNone) || partitioned_)
{
ret.append("Secure; ");
}
if (httpOnly_)
{
ret.append("HttpOnly; ");
}
if (partitioned_)
{
ret.append("Partitioned; ");
}
ret.resize(ret.length() - 2); // delete last semicolon
ret.append("\r\n");
return ret;
Expand Down
25 changes: 25 additions & 0 deletions lib/tests/unittests/CookieTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,29 @@ DROGON_TEST(CookieTest)
drogon::Cookie::convertString2SameSite("Strict"));
CHECK(drogon::Cookie::SameSite::kNone ==
drogon::Cookie::convertString2SameSite("None"));

// Test for Partitioned attribute
drogon::Cookie cookie6("test", "6");
cookie6.setPartitioned(true);
CHECK(cookie6.cookieString() ==
"Set-Cookie: test=6; Secure; HttpOnly; Partitioned\r\n");
// Test that partitioned attribute automatically sets secure
drogon::Cookie cookie7("test", "7");
cookie7.setPartitioned(true);
CHECK(cookie7.isSecure() == true);
// Test other attributes
drogon::Cookie cookie8("test", "8");
cookie8.setPartitioned(true);
cookie8.setDomain("drogon.org");
cookie8.setMaxAge(3600);
CHECK(cookie8.cookieString() ==
"Set-Cookie: test=8; Max-Age=3600; Domain=drogon.org; Secure; "
"HttpOnly; Partitioned\r\n");
// Teset Partitioned and SameSite can coexist
drogon::Cookie cookie9("test", "9");
cookie9.setPartitioned(true);
cookie9.setSameSite(drogon::Cookie::SameSite::kLax);
CHECK(
cookie9.cookieString() ==
"Set-Cookie: test=9; SameSite=Lax; Secure; HttpOnly; Partitioned\r\n");
}
Loading