Skip to content

Commit

Permalink
Bug 1641459 - Do not expose sameSite=lax/strict cookies to cross-site…
Browse files Browse the repository at this point in the history
… documents - part 1 - implementation, r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D77208
  • Loading branch information
bakulf committed May 29, 2020
1 parent 7e07333 commit e9c61f5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions netwerk/cookie/CookieCommons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,15 @@ already_AddRefed<nsICookieJarSettings> CookieCommons::GetCookieJarSettings(
return cookieJarSettings.forget();
}

// static
bool CookieCommons::ShouldIncludeCrossSiteCookieForDocument(Cookie* aCookie) {
MOZ_ASSERT(aCookie);

int32_t sameSiteAttr = 0;
aCookie->GetSameSite(&sameSiteAttr);

return sameSiteAttr == nsICookie::SAMESITE_NONE;
}

} // namespace net
} // namespace mozilla
3 changes: 3 additions & 0 deletions netwerk/cookie/CookieCommons.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <functional>
#include "prtime.h"
#include "nsString.h"
#include "nsICookie.h"

class nsIChannel;
class nsICookieJarSettings;
Expand Down Expand Up @@ -105,6 +106,8 @@ class CookieCommons final {

static already_AddRefed<nsICookieJarSettings> GetCookieJarSettings(
nsIChannel* aChannel);

static bool ShouldIncludeCrossSiteCookieForDocument(Cookie* aCookie);
};

} // namespace net
Expand Down
13 changes: 13 additions & 0 deletions netwerk/cookie/CookieService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ CookieService::GetCookieStringFromDocument(Document* aDocument,
// if it isn't, then we can't send a secure cookie over the connection.
bool potentiallyTurstworthy = principal->GetIsOriginPotentiallyTrustworthy();

nsPIDOMWindowInner* innerWindow = aDocument->GetInnerWindow();
if (NS_WARN_IF(!innerWindow)) {
return NS_OK;
}

bool thirdParty = nsContentUtils::IsThirdPartyWindowOrChannel(
innerWindow, nullptr, nullptr);

bool stale = false;
nsTArray<Cookie*> cookieList;

Expand All @@ -333,6 +341,11 @@ CookieService::GetCookieStringFromDocument(Document* aDocument,
continue;
}

if (thirdParty &&
!CookieCommons::ShouldIncludeCrossSiteCookieForDocument(cookie)) {
continue;
}

// if the cookie is secure and the host scheme isn't, we can't send it
if (cookie->IsSecure() && !potentiallyTurstworthy) {
continue;
Expand Down
13 changes: 13 additions & 0 deletions netwerk/cookie/CookieServiceChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ CookieServiceChild::GetCookieStringFromDocument(Document* aDocument,
nsAutoCString pathFromURI;
principal->GetFilePath(pathFromURI);

nsPIDOMWindowInner* innerWindow = aDocument->GetInnerWindow();
if (NS_WARN_IF(!innerWindow)) {
return NS_OK;
}

bool thirdParty = nsContentUtils::IsThirdPartyWindowOrChannel(
innerWindow, nullptr, nullptr);

bool isPotentiallyTrustworthy =
principal->GetIsOriginPotentiallyTrustworthy();
int64_t currentTimeInUsec = PR_Now();
Expand All @@ -369,6 +377,11 @@ CookieServiceChild::GetCookieStringFromDocument(Document* aDocument,
continue;
}

if (thirdParty &&
!CookieCommons::ShouldIncludeCrossSiteCookieForDocument(cookie)) {
continue;
}

// if the cookie is secure and the host scheme isn't, we can't send it
if (cookie->IsSecure() && !isPotentiallyTrustworthy) {
continue;
Expand Down

0 comments on commit e9c61f5

Please sign in to comment.