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 #include "CookieLogging.h"
9 constexpr auto TIME_STRING_LENGTH
= 40;
14 LazyLogModule
gCookieLog("cookie");
16 static const char* SameSiteToString(uint32_t aSameSite
) {
18 case nsICookie::SAMESITE_NONE
:
20 case nsICookie::SAMESITE_LAX
:
22 case nsICookie::SAMESITE_STRICT
:
25 MOZ_CRASH("Invalid nsICookie sameSite value");
31 void CookieLogging::LogSuccess(bool aSetCookie
, nsIURI
* aHostURI
,
32 const nsACString
& aCookieString
, Cookie
* aCookie
,
34 // if logging isn't enabled, return now to save cycles
35 if (!MOZ_LOG_TEST(gCookieLog
, LogLevel::Debug
)) {
41 aHostURI
->GetAsciiSpec(spec
);
44 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
45 ("===== %s =====\n", aSetCookie
? "COOKIE ACCEPTED" : "COOKIE SENT"));
46 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("request URL: %s\n", spec
.get()));
47 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
48 ("cookie string: %s\n", aCookieString
.BeginReading()));
50 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
51 ("replaces existing cookie: %s\n", aReplacing
? "true" : "false"));
56 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("\n"));
60 void CookieLogging::LogFailure(bool aSetCookie
, nsIURI
* aHostURI
,
61 const nsACString
& aCookieString
,
62 const char* aReason
) {
63 // if logging isn't enabled, return now to save cycles
64 if (!MOZ_LOG_TEST(gCookieLog
, LogLevel::Warning
)) {
70 aHostURI
->GetAsciiSpec(spec
);
73 MOZ_LOG(gCookieLog
, LogLevel::Warning
,
75 aSetCookie
? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT"));
76 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("request URL: %s\n", spec
.get()));
78 MOZ_LOG(gCookieLog
, LogLevel::Warning
,
79 ("cookie string: %s\n", aCookieString
.BeginReading()));
82 PRExplodedTime explodedTime
;
83 PR_ExplodeTime(PR_Now(), PR_GMTParameters
, &explodedTime
);
84 char timeString
[TIME_STRING_LENGTH
];
85 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
88 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("current time: %s", timeString
));
89 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("rejected because %s\n", aReason
));
90 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("\n"));
94 void CookieLogging::LogCookie(Cookie
* aCookie
) {
95 PRExplodedTime explodedTime
;
96 PR_ExplodeTime(PR_Now(), PR_GMTParameters
, &explodedTime
);
97 char timeString
[TIME_STRING_LENGTH
];
98 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
101 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("current time: %s", timeString
));
104 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("----------------\n"));
105 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("name: %s\n", aCookie
->Name().get()));
106 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
107 ("value: %s\n", aCookie
->Value().get()));
108 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
109 ("%s: %s\n", aCookie
->IsDomain() ? "domain" : "host",
110 aCookie
->Host().get()));
111 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("path: %s\n", aCookie
->Path().get()));
113 PR_ExplodeTime(aCookie
->Expiry() * int64_t(PR_USEC_PER_SEC
),
114 PR_GMTParameters
, &explodedTime
);
115 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
117 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
118 ("expires: %s%s", timeString
,
119 aCookie
->IsSession() ? " (at end of session)" : ""));
121 PR_ExplodeTime(aCookie
->CreationTime(), PR_GMTParameters
, &explodedTime
);
122 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
124 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("created: %s", timeString
));
126 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
127 ("is secure: %s\n", aCookie
->IsSecure() ? "true" : "false"));
128 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
129 ("is httpOnly: %s\n", aCookie
->IsHttpOnly() ? "true" : "false"));
130 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
131 ("sameSite: %s - rawSameSite: %s\n",
132 SameSiteToString(aCookie
->SameSite()),
133 SameSiteToString(aCookie
->RawSameSite())));
135 gCookieLog
, LogLevel::Debug
,
136 ("schemeMap %d (http: %s | https: %s | file: %s)\n",
137 aCookie
->SchemeMap(),
138 (aCookie
->SchemeMap() & nsICookie::SCHEME_HTTP
? "true" : "false"),
139 (aCookie
->SchemeMap() & nsICookie::SCHEME_HTTPS
? "true" : "false"),
140 (aCookie
->SchemeMap() & nsICookie::SCHEME_FILE
? "true" : "false")));
142 nsAutoCString suffix
;
143 aCookie
->OriginAttributesRef().CreateSuffix(suffix
);
144 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
145 ("origin attributes: %s\n",
146 suffix
.IsEmpty() ? "{empty}" : suffix
.get()));
151 void CookieLogging::LogEvicted(Cookie
* aCookie
, const char* details
) {
152 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("===== COOKIE EVICTED =====\n"));
153 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("%s\n", details
));
157 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("\n"));
161 void CookieLogging::LogMessageToConsole(nsIConsoleReportCollector
* aCRC
,
162 nsIURI
* aURI
, uint32_t aErrorFlags
,
163 const nsACString
& aCategory
,
164 const nsACString
& aMsg
,
165 const nsTArray
<nsString
>& aParams
) {
172 nsresult rv
= aURI
->GetSpec(uri
);
173 if (NS_WARN_IF(NS_FAILED(rv
))) {
178 aCRC
->AddConsoleReport(aErrorFlags
, aCategory
,
179 nsContentUtils::eNECKO_PROPERTIES
, uri
, 0, 0, aMsg
,
184 } // namespace mozilla