Commit 2a6ea295 authored by jan.koester's avatar jan.koester
Browse files

HttpCookie::parse: an empty Cookie header crashed the server

A "Cookie:" header without a value has no Values; parse() read
hc->getfirstValue()->getvalue() into an unused string and dereferenced
null: one request (curl -H 'Cookie;', or Node's fetch with Cookie: '')
took down any server that parses cookies -- found with mammut's admin
interface, which segfaulted on it over HTTP/1.1 and HTTP/2 alike.

The unused line is gone and the loop runs over hc's values (none: no
cookies). http_cookie_headers_test sends "Cookie: ", "Cookie:",
"Cookie: ;" and "Cookie: a=1; ; b=2" to HttpCookie::parse (11/11; the
process crashes on the first of them without the fix).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent fe550d25
Loading
Loading
Loading
Loading
+4 −3
Changes for src/http.cpp: 4 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -5785,8 +5785,6 @@ void libhttppp::HttpCookie::parse(libhttppp::HttpRequest &curreq){
        throw httpexception;
    }

    std::string raw_cookie_string = hc->getfirstValue()->getvalue();

    // Helper to trim leading/trailing whitespace
    auto trim = [](std::string& s) {
        s.erase(0, s.find_first_not_of(" \t"));
@@ -5794,7 +5792,10 @@ void libhttppp::HttpCookie::parse(libhttppp::HttpRequest &curreq){
    };

    // Loop through the raw string, splitting by semicolon (';')
    for(HttpHeader::HeaderData::Values *curval=curreq.getHeaderData("cookie")->getfirstValue(); curval; curval=curval->nextvalue()){
    // an empty "Cookie:" header has no values at all (getfirstValue() is
    // null): it used to be dereferenced here, a segfault any client could
    // trigger with one request
    for(HttpHeader::HeaderData::Values *curval=hc->getfirstValue(); curval; curval=curval->nextvalue()){
        std::string cpair = curval->getvalue();
        trim(cpair); // Trim whitespace around the entire pair

+46 −0
Changes for test/http_cookie_headers_test.cpp: 46 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -108,6 +108,27 @@ public:
            return;
        }

        if (url == "/cookie-parse") {
            // HttpCookie::parse(), as servers use it for their session cookies
            std::string seen;
            size_t count = 0;
            try {
                HttpCookie cookie;
                cookie.parse(req);
                for (auto *cd = cookie.getfirstCookieData(); cd; cd = cd->nextCookieData()) {
                    if (count++) seen += "|";
                    seen += cd->getKey() + "=" + cd->getValue();
                }
            } catch (HTTPException &) {
                seen = "no-cookie";
            }
            HttpResponse res;
            res.setHeaderData("x-cookie-count")->push_back(std::to_string(count));
            res.setHeaderData("x-cookie-echo")->push_back(seen);
            res.send(req, std::string("ok"), 2);
            return;
        }

        if (url == "/set-two-cookies") {
            HttpResponse res;
            auto *sc = res.setHeaderData("set-cookie");
@@ -223,6 +244,30 @@ void testDuplicatePhysicalCookieLinesAppend(int port) {
          "duplicate Cookie: lines -- both exact values preserved, in order");
}

// An empty "Cookie:" header (no value at all) went to HttpCookie::parse() as a
// header without values, which it dereferenced: one request crashed the
// whole server (SIGSEGV). Now: no cookies, and the server keeps serving.
void testEmptyCookieHeaderParse(int port) {
    for (const char *line : {"Cookie: \r\n", "Cookie:\r\n", "Cookie: ;\r\n", "Cookie: a=1; ; b=2\r\n"}) {
        auto sock = std::make_unique<netplus::tcp>();
        rawtcp::connectLocal(*sock, port);
        std::ostringstream req;
        req << "GET /cookie-parse HTTP/1.1\r\n"
            << "Host: 127.0.0.1:" << port << "\r\n"
            << line
            << "Connection: close\r\n"
            << "\r\n";
        rawtcp::sendAllOrThrow(*sock, req.str());
        HttpResponse res;
        rawReadHeadersAndParse(*sock, res);
        sock->close();
        std::string shown(line, std::string(line).size() - 2);
        std::string want = std::string(line).find("a=1") != std::string::npos ? "a=1|b=2" : "";
        check(headerValue(res, "x-cookie-echo") == want || (want.empty() && headerValue(res, "x-cookie-echo") == "no-cookie"),
              "HttpCookie::parse with \"" + shown + "\" answers (got \"" + headerValue(res, "x-cookie-echo") + "\")");
    }
}

// HttpRequest::printHeader() must serialize a Cookie header holding multiple
// Values as ONE "; "-joined line, not one physical line per value -- exercises
// the printHeader() fix directly by inspecting the exact bytes put on the wire.
@@ -318,6 +363,7 @@ int main() {
        std::cout << "Server listening on 127.0.0.1:" << port << std::endl;

        testDuplicatePhysicalCookieLinesAppend(port);
        testEmptyCookieHeaderParse(port);
        testPrintHeaderJoinsCookieOntoOneLine();
        testResponseSetCookiePreserved(port);