1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_net_CookieCommons_h
7 #define mozilla_net_CookieCommons_h
13 #include "nsICookie.h"
16 class nsICookieJarSettings
;
17 class nsIEffectiveTLDService
;
29 // these constants represent an operation being performed on cookies
30 enum CookieOperation
{ OPERATION_READ
, OPERATION_WRITE
};
32 // these constants represent a decision about a cookie based on user prefs.
35 STATUS_ACCEPT_SESSION
,
37 // STATUS_REJECTED_WITH_ERROR indicates the cookie should be rejected because
38 // of an error (rather than something the user can control). this is used for
39 // notification purposes, since we only want to notify of rejections where
40 // the user can do something about it (e.g. whitelist the site).
41 STATUS_REJECTED_WITH_ERROR
46 // pref string constants
47 static const char kPrefMaxNumberOfCookies
[] = "network.cookie.maxNumber";
48 static const char kPrefMaxCookiesPerHost
[] = "network.cookie.maxPerHost";
49 static const char kPrefCookieQuotaPerHost
[] = "network.cookie.quotaPerHost";
50 static const char kPrefCookiePurgeAge
[] = "network.cookie.purgeAge";
52 // default limits for the cookie list. these can be tuned by the
53 // network.cookie.maxNumber and network.cookie.maxPerHost prefs respectively.
54 static const uint32_t kMaxCookiesPerHost
= 180;
55 static const uint32_t kCookieQuotaPerHost
= 150;
56 static const uint32_t kMaxNumberOfCookies
= 3000;
57 static const uint32_t kMaxBytesPerCookie
= 4096;
58 static const uint32_t kMaxBytesPerPath
= 1024;
60 static const int64_t kCookiePurgeAge
=
61 int64_t(30 * 24 * 60 * 60) * PR_USEC_PER_SEC
; // 30 days in microseconds
63 class CookieCommons final
{
65 static bool DomainMatches(Cookie
* aCookie
, const nsACString
& aHost
);
67 static bool PathMatches(Cookie
* aCookie
, const nsACString
& aPath
);
69 static nsresult
GetBaseDomain(nsIEffectiveTLDService
* aTLDService
,
70 nsIURI
* aHostURI
, nsACString
& aBaseDomain
,
71 bool& aRequireHostMatch
);
73 static nsresult
GetBaseDomain(nsIPrincipal
* aPrincipal
,
74 nsACString
& aBaseDomain
);
76 static nsresult
GetBaseDomainFromHost(nsIEffectiveTLDService
* aTLDService
,
77 const nsACString
& aHost
,
78 nsCString
& aBaseDomain
);
80 static void NotifyRejected(nsIURI
* aHostURI
, nsIChannel
* aChannel
,
81 uint32_t aRejectedReason
,
82 CookieOperation aOperation
);
84 static bool CheckPathSize(const CookieStruct
& aCookieData
);
86 static bool CheckNameAndValueSize(const CookieStruct
& aCookieData
);
88 static bool CheckName(const CookieStruct
& aCookieData
);
90 static bool CheckHttpValue(const CookieStruct
& aCookieData
);
92 static bool CheckCookiePermission(nsIChannel
* aChannel
,
93 CookieStruct
& aCookieData
);
95 static bool CheckCookiePermission(nsIPrincipal
* aPrincipal
,
96 nsICookieJarSettings
* aCookieJarSettings
,
97 CookieStruct
& aCookieData
);
99 static already_AddRefed
<Cookie
> CreateCookieFromDocument(
100 dom::Document
* aDocument
, const nsACString
& aCookieString
,
101 int64_t aCurrentTimeInUsec
, nsIEffectiveTLDService
* aTLDService
,
102 mozIThirdPartyUtil
* aThirdPartyUtil
,
103 std::function
<bool(const nsACString
&, const OriginAttributes
&)>&&
104 aHasExistingCookiesLambda
,
105 nsIURI
** aDocumentURI
, nsACString
& aBaseDomain
, OriginAttributes
& aAttrs
);
107 static already_AddRefed
<nsICookieJarSettings
> GetCookieJarSettings(
108 nsIChannel
* aChannel
);
110 static bool ShouldIncludeCrossSiteCookieForDocument(Cookie
* aCookie
);
112 static bool MaybeCompareSchemeWithLogging(nsIConsoleReportCollector
* aCRC
,
113 nsIURI
* aHostURI
, Cookie
* aCookie
,
114 nsICookie::schemeType aSchemeType
);
116 static bool MaybeCompareScheme(Cookie
* aCookie
,
117 nsICookie::schemeType aSchemeType
);
119 static bool IsSchemeSupported(nsIPrincipal
* aPrincipal
);
120 static bool IsSchemeSupported(nsIURI
* aURI
);
121 static bool IsSchemeSupported(const nsACString
& aScheme
);
123 static nsICookie::schemeType
URIToSchemeType(nsIURI
* aURI
);
125 static nsICookie::schemeType
PrincipalToSchemeType(nsIPrincipal
* aPrincipal
);
127 static nsICookie::schemeType
SchemeToSchemeType(const nsACString
& aScheme
);
129 // Returns true if the channel is a safe top-level navigation or if it's a
131 static bool IsSafeTopLevelNav(nsIChannel
* aChannel
);
133 // Returns true if the channel is a foreign with respect to the host-uri.
134 // For loads of TYPE_DOCUMENT, this function returns true if it's a cross
135 // origin navigation.
136 static bool IsSameSiteForeign(nsIChannel
* aChannel
, nsIURI
* aHostURI
);
140 } // namespace mozilla
142 #endif // mozilla_net_CookieCommons_h