Skip to content

Commit

Permalink
Bug 1779993 - Reject cookies with no name and a __Secure- or __Host- …
Browse files Browse the repository at this point in the history
…prefix r=necko-reviewers,kershaw a=RyanVM

Differential Revision: https://phabricator.services.mozilla.com/D156554
  • Loading branch information
valenting committed Sep 7, 2022
1 parent 7a4366e commit 5fbf64d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
33 changes: 32 additions & 1 deletion netwerk/cookie/CookieService.cpp
Expand Up @@ -1207,6 +1207,18 @@ bool CookieService::CanSetCookie(
return newCookie;
}

if (!CheckHiddenPrefix(aCookieData)) {
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader,
"failed the CheckHiddenPrefix tests");
CookieLogging::LogMessageToConsole(
aCRC, aHostURI, nsIScriptError::warningFlag, CONSOLE_REJECTION_CATEGORY,
"CookieRejectedInvalidPrefix"_ns,
AutoTArray<nsString, 1>{
NS_ConvertUTF8toUTF16(aCookieData.name()),
});
return newCookie;
}

// magic prefix checks. MUST be run after CheckDomain() and CheckPath()
if (!CheckPrefixes(aCookieData, potentiallyTurstworthy)) {
COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader,
Expand Down Expand Up @@ -1837,6 +1849,25 @@ bool CookieService::CheckDomain(CookieStruct& aCookieData, nsIURI* aHostURI,
return true;
}

// static
bool CookieService::CheckHiddenPrefix(CookieStruct& aCookieData) {
// If a cookie is nameless, then its value must not start with
// `__Host-` or `__Secure-`
if (!aCookieData.name().IsEmpty()) {
return true;
}

if (StringBeginsWith(aCookieData.value(), "__Host-"_ns)) {
return false;
}

if (StringBeginsWith(aCookieData.value(), "__Secure-"_ns)) {
return false;
}

return true;
}

namespace {
nsAutoCString GetPathFromURI(nsIURI* aHostURI) {
// strip down everything after the last slash to get the path,
Expand Down Expand Up @@ -1913,7 +1944,7 @@ bool CookieService::CheckPath(CookieStruct& aCookieData,
// CheckPrefixes
//
// Reject cookies whose name starts with the magic prefixes from
// https://tools.ietf.org/html/draft-ietf-httpbis-cookie-prefixes-00
// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis
// if they do not meet the criteria required by the prefix.
//
// Must not be called until after CheckDomain() and CheckPath() have
Expand Down
1 change: 1 addition & 0 deletions netwerk/cookie/CookieService.h
Expand Up @@ -121,6 +121,7 @@ class CookieService final : public nsICookieService,
static bool CheckDomain(CookieStruct& aCookieData, nsIURI* aHostURI,
const nsACString& aBaseDomain,
bool aRequireHostMatch);
static bool CheckHiddenPrefix(CookieStruct& aCookieData);
static bool CheckPath(CookieStruct& aCookieData,
nsIConsoleReportCollector* aCRC, nsIURI* aHostURI);
static bool CheckPrefixes(CookieStruct& aCookieData, bool aSecureRequest);
Expand Down
26 changes: 26 additions & 0 deletions netwerk/test/gtest/TestCookie.cpp
Expand Up @@ -1057,3 +1057,29 @@ TEST(TestCookie, OnionSite)
GetACookieNoHttp(cookieService, "http://123456789abcdef.onion/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_EQUAL, "test=onion-security4"));
}

TEST(TestCookie, HiddenPrefix)
{
nsresult rv;
nsCString cookie;

nsCOMPtr<nsICookieService> cookieService =
do_GetService(kCookieServiceCID, &rv);
ASSERT_TRUE(NS_SUCCEEDED(rv));

SetACookie(cookieService, "http://hiddenprefix.test/", "=__Host-test=a");
GetACookie(cookieService, "http://hiddenprefix.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));

SetACookie(cookieService, "http://hiddenprefix.test/", "=__Secure-test=a");
GetACookie(cookieService, "http://hiddenprefix.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));

SetACookie(cookieService, "http://hiddenprefix.test/", "=__Host-check");
GetACookie(cookieService, "http://hiddenprefix.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));

SetACookie(cookieService, "http://hiddenprefix.test/", "=__Secure-check");
GetACookie(cookieService, "http://hiddenprefix.test/", cookie);
EXPECT_TRUE(CheckResult(cookie.get(), MUST_BE_NULL));
}

0 comments on commit 5fbf64d

Please sign in to comment.