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"
8 #include "nsIConsoleReportCollector.h"
10 constexpr auto TIME_STRING_LENGTH
= 40;
15 LazyLogModule
gCookieLog("cookie");
17 static const char* SameSiteToString(uint32_t aSameSite
) {
19 case nsICookie::SAMESITE_NONE
:
21 case nsICookie::SAMESITE_LAX
:
23 case nsICookie::SAMESITE_STRICT
:
26 MOZ_CRASH("Invalid nsICookie sameSite value");
32 void CookieLogging::LogSuccess(bool aSetCookie
, nsIURI
* aHostURI
,
33 const nsACString
& aCookieString
, Cookie
* aCookie
,
35 // if logging isn't enabled, return now to save cycles
36 if (!MOZ_LOG_TEST(gCookieLog
, LogLevel::Debug
)) {
42 aHostURI
->GetAsciiSpec(spec
);
45 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
46 ("===== %s =====\n", aSetCookie
? "COOKIE ACCEPTED" : "COOKIE SENT"));
47 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("request URL: %s\n", spec
.get()));
48 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
49 ("cookie string: %s\n", aCookieString
.BeginReading()));
51 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
52 ("replaces existing cookie: %s\n", aReplacing
? "true" : "false"));
57 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("\n"));
61 void CookieLogging::LogFailure(bool aSetCookie
, nsIURI
* aHostURI
,
62 const nsACString
& aCookieString
,
63 const char* aReason
) {
64 // if logging isn't enabled, return now to save cycles
65 if (!MOZ_LOG_TEST(gCookieLog
, LogLevel::Warning
)) {
71 aHostURI
->GetAsciiSpec(spec
);
74 MOZ_LOG(gCookieLog
, LogLevel::Warning
,
76 aSetCookie
? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT"));
77 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("request URL: %s\n", spec
.get()));
79 MOZ_LOG(gCookieLog
, LogLevel::Warning
,
80 ("cookie string: %s\n", aCookieString
.BeginReading()));
83 PRExplodedTime explodedTime
;
84 PR_ExplodeTime(PR_Now(), PR_GMTParameters
, &explodedTime
);
85 char timeString
[TIME_STRING_LENGTH
];
86 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
89 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("current time: %s", timeString
));
90 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("rejected because %s\n", aReason
));
91 MOZ_LOG(gCookieLog
, LogLevel::Warning
, ("\n"));
95 void CookieLogging::LogCookie(Cookie
* aCookie
) {
96 PRExplodedTime explodedTime
;
97 PR_ExplodeTime(PR_Now(), PR_GMTParameters
, &explodedTime
);
98 char timeString
[TIME_STRING_LENGTH
];
99 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
102 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("current time: %s", timeString
));
105 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("----------------\n"));
106 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("name: %s\n", aCookie
->Name().get()));
107 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
108 ("value: %s\n", aCookie
->Value().get()));
109 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
110 ("%s: %s\n", aCookie
->IsDomain() ? "domain" : "host",
111 aCookie
->Host().get()));
112 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("path: %s\n", aCookie
->Path().get()));
114 PR_ExplodeTime(aCookie
->Expiry() * int64_t(PR_USEC_PER_SEC
),
115 PR_GMTParameters
, &explodedTime
);
116 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
118 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
119 ("expires: %s%s", timeString
,
120 aCookie
->IsSession() ? " (at end of session)" : ""));
122 PR_ExplodeTime(aCookie
->CreationTime(), PR_GMTParameters
, &explodedTime
);
123 PR_FormatTimeUSEnglish(timeString
, TIME_STRING_LENGTH
, "%c GMT",
125 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("created: %s", timeString
));
127 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
128 ("is secure: %s\n", aCookie
->IsSecure() ? "true" : "false"));
129 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
130 ("is httpOnly: %s\n", aCookie
->IsHttpOnly() ? "true" : "false"));
131 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
132 ("sameSite: %s - rawSameSite: %s\n",
133 SameSiteToString(aCookie
->SameSite()),
134 SameSiteToString(aCookie
->RawSameSite())));
136 gCookieLog
, LogLevel::Debug
,
137 ("schemeMap %d (http: %s | https: %s | file: %s)\n",
138 aCookie
->SchemeMap(),
139 (aCookie
->SchemeMap() & nsICookie::SCHEME_HTTP
? "true" : "false"),
140 (aCookie
->SchemeMap() & nsICookie::SCHEME_HTTPS
? "true" : "false"),
141 (aCookie
->SchemeMap() & nsICookie::SCHEME_FILE
? "true" : "false")));
143 nsAutoCString suffix
;
144 aCookie
->OriginAttributesRef().CreateSuffix(suffix
);
145 MOZ_LOG(gCookieLog
, LogLevel::Debug
,
146 ("origin attributes: %s\n",
147 suffix
.IsEmpty() ? "{empty}" : suffix
.get()));
152 void CookieLogging::LogEvicted(Cookie
* aCookie
, const char* details
) {
153 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("===== COOKIE EVICTED =====\n"));
154 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("%s\n", details
));
158 MOZ_LOG(gCookieLog
, LogLevel::Debug
, ("\n"));
162 void CookieLogging::LogMessageToConsole(nsIConsoleReportCollector
* aCRC
,
163 nsIURI
* aURI
, uint32_t aErrorFlags
,
164 const nsACString
& aCategory
,
165 const nsACString
& aMsg
,
166 const nsTArray
<nsString
>& aParams
) {
173 nsresult rv
= aURI
->GetSpec(uri
);
174 if (NS_WARN_IF(NS_FAILED(rv
))) {
179 aCRC
->AddConsoleReport(aErrorFlags
, aCategory
,
180 nsContentUtils::eNECKO_PROPERTIES
, uri
, 0, 0, aMsg
,
185 } // namespace mozilla