Files
moloapp/src/clients/cookies.cpp
T
retoor 3c4334cec9 feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
2026-10-23 18:20:04 +02:00

174 lines
5.7 KiB
C++

#include "clients/cookies.h"
#include <cctype>
#include "clients/caps.h"
namespace molo::clients {
namespace {
std::string trim(std::string s) {
while (!s.empty() && (s.front() == ' ' || s.front() == '\t')) s.erase(s.begin());
while (!s.empty() && (s.back() == ' ' || s.back() == '\t')) s.pop_back();
return s;
}
std::string to_lower(std::string s) {
for (auto& c : s) c = static_cast<char>(std::tolower((unsigned char)c));
return s;
}
bool domain_match(const std::string& host, const std::string& cookie_domain) {
if (host == cookie_domain) return true;
if (host.size() > cookie_domain.size() + 1 &&
host.compare(host.size() - cookie_domain.size(), cookie_domain.size(),
cookie_domain) == 0 &&
host[host.size() - cookie_domain.size() - 1] == '.')
return true;
return false;
}
bool path_match(const std::string& request_path, const std::string& cookie_path) {
if (cookie_path.empty() || cookie_path == "/") return true;
if (request_path.compare(0, cookie_path.size(), cookie_path) != 0)
return false;
return request_path.size() == cookie_path.size() ||
request_path[cookie_path.size()] == '/';
}
} // namespace
void CookieJar::clear() {
std::lock_guard<std::mutex> lock(mutex_);
cookies_.clear();
}
std::size_t CookieJar::size() const {
std::lock_guard<std::mutex> lock(mutex_);
return cookies_.size();
}
std::optional<Cookie> CookieJar::parse_set_cookie(const Url& url,
const std::string& text) {
if (text.size() > Caps::kCookieMaxBytes) return std::nullopt;
auto semi = text.find(';');
std::string pair = trim(semi == std::string::npos ? text : text.substr(0, semi));
std::string attrs = semi == std::string::npos ? "" : text.substr(semi + 1);
auto eq = pair.find('=');
if (eq == std::string::npos || eq == 0) return std::nullopt;
Cookie cookie;
cookie.name = trim(pair.substr(0, eq));
cookie.value = trim(pair.substr(eq + 1));
cookie.domain = to_lower(url.host());
cookie.path = "/";
// Default path: request path up to the last slash.
auto slash = url.path().rfind('/');
if (slash != std::string::npos && slash > 0)
cookie.path = url.path().substr(0, slash);
while (!attrs.empty()) {
auto next = attrs.find(';');
std::string attr =
trim(next == std::string::npos ? attrs : attrs.substr(0, next));
attrs = next == std::string::npos ? "" : attrs.substr(next + 1);
auto aeq = attr.find('=');
std::string aname = to_lower(trim(aeq == std::string::npos
? attr
: attr.substr(0, aeq)));
std::string avalue =
aeq == std::string::npos ? "" : trim(attr.substr(aeq + 1));
if (aname == "domain") {
std::string d = to_lower(avalue);
if (!d.empty() && d.front() == '.') d.erase(d.begin());
if (d.empty() || !domain_match(to_lower(url.host()), d)) return std::nullopt;
cookie.domain = d;
} else if (aname == "path") {
if (!avalue.empty() && avalue.front() == '/') cookie.path = avalue;
} else if (aname == "max-age") {
try {
long secs = std::stol(avalue);
if (secs <= 0)
cookie.expires_at = std::chrono::steady_clock::now();
else
cookie.expires_at =
std::chrono::steady_clock::now() + std::chrono::seconds(secs);
} catch (...) {
return std::nullopt;
}
} else if (aname == "secure") {
cookie.secure = true;
} else if (aname == "httponly") {
cookie.http_only = true;
} else if (aname == "samesite") {
cookie.same_site = avalue.empty() ? "Lax" : avalue;
}
}
return cookie;
}
void CookieJar::store_from_response(const Url& url,
const ClientResponse& response) {
std::lock_guard<std::mutex> lock(mutex_);
auto now = std::chrono::steady_clock::now();
for (const auto& raw : response.headers().set_cookies()) {
auto parsed = parse_set_cookie(url, raw);
if (!parsed) continue;
if (parsed->expires_at && *parsed->expires_at <= now) {
// Expired: delete matching cookie.
for (auto it = cookies_.begin(); it != cookies_.end();) {
if (it->name == parsed->name && it->domain == parsed->domain &&
it->path == parsed->path)
it = cookies_.erase(it);
else
++it;
}
continue;
}
bool replaced = false;
for (auto& existing : cookies_) {
if (existing.name == parsed->name && existing.domain == parsed->domain &&
existing.path == parsed->path) {
existing = *parsed;
replaced = true;
break;
}
}
if (!replaced) {
if (cookies_.size() >= Caps::kCookieJarMax) continue; // Loud skip: full.
cookies_.push_back(*parsed);
}
}
}
std::string CookieJar::header_for(const Url& url) {
std::lock_guard<std::mutex> lock(mutex_);
auto now = std::chrono::steady_clock::now();
std::string host = to_lower(url.host());
std::string out;
for (auto it = cookies_.begin(); it != cookies_.end();) {
if (it->expires_at && *it->expires_at <= now) {
it = cookies_.erase(it);
continue;
}
if (it->secure && !url.is_https()) {
++it;
continue;
}
if (!domain_match(host, it->domain) || !path_match(url.path(), it->path)) {
++it;
continue;
}
// SameSite=Strict cookies are same-site only: host must equal the cookie
// domain exactly (this client never navigates cross-site top-level).
if (to_lower(it->same_site) == "strict" && host != it->domain) {
++it;
continue;
}
if (!out.empty()) out += "; ";
out += it->name + "=" + it->value;
++it;
}
return out;
}
} // namespace molo::clients