Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / cookie / CookieCommons.h
blobf3d05113461769db86ffad310211586e94991afb
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
9 #include <cstdint>
10 #include <functional>
11 #include "mozIThirdPartyUtil.h"
12 #include "prtime.h"
13 #include "nsString.h"
14 #include "nsICookie.h"
15 #include "mozilla/net/NeckoChannelParams.h"
17 class nsIChannel;
18 class nsICookieJarSettings;
19 class nsIEffectiveTLDService;
20 class nsIPrincipal;
21 class nsIURI;
23 namespace mozilla {
25 namespace dom {
26 class Document;
29 namespace net {
31 // these constants represent an operation being performed on cookies
32 enum CookieOperation { OPERATION_READ, OPERATION_WRITE };
34 // these constants represent a decision about a cookie based on user prefs.
35 enum CookieStatus {
36 STATUS_ACCEPTED,
37 STATUS_ACCEPT_SESSION,
38 STATUS_REJECTED,
39 // STATUS_REJECTED_WITH_ERROR indicates the cookie should be rejected because
40 // of an error (rather than something the user can control). this is used for
41 // notification purposes, since we only want to notify of rejections where
42 // the user can do something about it (e.g. whitelist the site).
43 STATUS_REJECTED_WITH_ERROR
46 class Cookie;
47 class CookieParser;
49 // pref string constants
50 static const char kPrefMaxNumberOfCookies[] = "network.cookie.maxNumber";
51 static const char kPrefMaxCookiesPerHost[] = "network.cookie.maxPerHost";
52 static const char kPrefCookieQuotaPerHost[] = "network.cookie.quotaPerHost";
53 static const char kPrefCookiePurgeAge[] = "network.cookie.purgeAge";
55 // default limits for the cookie list. these can be tuned by the
56 // network.cookie.maxNumber and network.cookie.maxPerHost prefs respectively.
57 static const uint32_t kMaxCookiesPerHost = 180;
58 static const uint32_t kCookieQuotaPerHost = 150;
59 static const uint32_t kMaxNumberOfCookies = 3000;
60 static const uint32_t kMaxBytesPerCookie = 4096;
61 static const uint32_t kMaxBytesPerPath = 1024;
63 static const int64_t kCookiePurgeAge =
64 int64_t(30 * 24 * 60 * 60) * PR_USEC_PER_SEC; // 30 days in microseconds
66 class CookieCommons final {
67 public:
68 static bool DomainMatches(Cookie* aCookie, const nsACString& aHost);
70 static bool PathMatches(Cookie* aCookie, const nsACString& aPath);
72 static bool PathMatches(const nsACString& aCookiePath,
73 const nsACString& aPath);
75 static nsresult GetBaseDomain(nsIEffectiveTLDService* aTLDService,
76 nsIURI* aHostURI, nsACString& aBaseDomain,
77 bool& aRequireHostMatch);
79 static nsresult GetBaseDomain(nsIPrincipal* aPrincipal,
80 nsACString& aBaseDomain);
82 static nsresult GetBaseDomainFromHost(nsIEffectiveTLDService* aTLDService,
83 const nsACString& aHost,
84 nsCString& aBaseDomain);
86 // This method returns true if aBaseDomain contains any colons since only
87 // IPv6 baseDomains may contain colons.
88 static bool IsIPv6BaseDomain(const nsACString& aBaseDomain);
90 static void NotifyRejected(nsIURI* aHostURI, nsIChannel* aChannel,
91 uint32_t aRejectedReason,
92 CookieOperation aOperation);
94 static bool CheckPathSize(const CookieStruct& aCookieData);
96 static bool CheckNameAndValueSize(const CookieStruct& aCookieData);
98 static bool CheckName(const CookieStruct& aCookieData);
100 static bool CheckValue(const CookieStruct& aCookieData);
102 static bool CheckCookiePermission(nsIChannel* aChannel,
103 CookieStruct& aCookieData);
105 static bool CheckCookiePermission(nsIPrincipal* aPrincipal,
106 nsICookieJarSettings* aCookieJarSettings,
107 CookieStruct& aCookieData);
109 static already_AddRefed<Cookie> CreateCookieFromDocument(
110 CookieParser& aCookieParser, dom::Document* aDocument,
111 const nsACString& aCookieString, int64_t aCurrentTimeInUsec,
112 nsIEffectiveTLDService* aTLDService, mozIThirdPartyUtil* aThirdPartyUtil,
113 nsACString& aBaseDomain, OriginAttributes& aAttrs);
115 static already_AddRefed<nsICookieJarSettings> GetCookieJarSettings(
116 nsIChannel* aChannel);
118 static bool ShouldIncludeCrossSiteCookie(Cookie* aCookie,
119 bool aPartitionForeign,
120 bool aInPrivateBrowsing,
121 bool aUsingStorageAccess,
122 bool aOn3pcbException);
124 static bool ShouldIncludeCrossSiteCookie(
125 int32_t aSameSiteAttr, bool aCookiePartitioned, bool aPartitionForeign,
126 bool aInPrivateBrowsing, bool aUsingStorageAccess, bool aOn3pcbException);
128 static bool IsFirstPartyPartitionedCookieWithoutCHIPS(
129 Cookie* aCookie, const nsACString& aBaseDomain,
130 const OriginAttributes& aOriginAttributes);
132 static bool ShouldEnforceSessionForOriginAttributes(
133 const OriginAttributes& aOriginAttributes);
135 static bool IsSchemeSupported(nsIPrincipal* aPrincipal);
136 static bool IsSchemeSupported(nsIURI* aURI);
137 static bool IsSchemeSupported(const nsACString& aScheme);
139 static nsICookie::schemeType URIToSchemeType(nsIURI* aURI);
141 static nsICookie::schemeType PrincipalToSchemeType(nsIPrincipal* aPrincipal);
143 static nsICookie::schemeType SchemeToSchemeType(const nsACString& aScheme);
145 // Returns true if the channel is a safe top-level navigation or if it's a
146 // download request
147 static bool IsSafeTopLevelNav(nsIChannel* aChannel);
149 // Returns true if the channel is a foreign with respect to the host-uri.
150 // For loads of TYPE_DOCUMENT, this function returns true if it's a cross
151 // site navigation.
152 // `aHadCrossSiteRedirects` will be true iff the channel had a cross-site
153 // redirect before the final URI.
154 static bool IsSameSiteForeign(nsIChannel* aChannel, nsIURI* aHostURI,
155 bool* aHadCrossSiteRedirects);
157 static bool ChipsLimitEnabledAndChipsCookie(
158 const Cookie& cookie, dom::BrowsingContext* aBrowsingContext);
160 static void ComposeCookieString(nsTArray<RefPtr<Cookie>>& aCookieList,
161 nsACString& aCookieString);
163 static void GetServerDateHeader(nsIChannel* aChannel,
164 nsACString& aServerDateHeader);
166 enum class SecurityChecksResult {
167 // A sandboxed context detected.
168 eSandboxedError,
169 // A security error needs to be thrown.
170 eSecurityError,
171 // This context should not see cookies without returning errors.
172 eDoNotContinue,
173 // No security issues found. Proceed to expose cookies.
174 eContinue,
177 // Runs the security checks requied by specs on the current context (Document
178 // or Worker) to see if it's allowed to set/get cookies. In case it does
179 // (eContinue), the cookie principals are returned. Use the
180 // `aCookiePartitionedPrincipal` to retrieve CHIP cookies. Use
181 // `aCookiePrincipal` to retrieve non-CHIP cookies.
182 static SecurityChecksResult CheckGlobalAndRetrieveCookiePrincipals(
183 mozilla::dom::Document* aDocument, nsIPrincipal** aCookiePrincipal,
184 nsIPrincipal** aCookiePartitionedPrincipal);
187 } // namespace net
188 } // namespace mozilla
190 #endif // mozilla_net_CookieCommons_h