Bug 1779993 - Reject cookies with no name and a __Secure- or __Host- prefix r=necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D156554
This commit is contained in:
Valentin Gosu 2022-09-07 19:04:22 +00:00
Родитель 58f63166ec
Коммит 226adb5a86
3 изменённых файлов: 59 добавлений и 1 удалений

Просмотреть файл

@ -1237,6 +1237,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,
@ -1867,6 +1879,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,
@ -1943,7 +1974,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

Просмотреть файл

@ -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);

Просмотреть файл

@ -1060,3 +1060,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));
}