CookieLogging.cpp (6829B)
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/. */ 5 6 #include "CookieLogging.h" 7 #include "Cookie.h" 8 #include "nsIConsoleReportCollector.h" 9 10 constexpr auto TIME_STRING_LENGTH = 40; 11 12 namespace mozilla { 13 namespace net { 14 15 LazyLogModule gCookieLog("cookie"); 16 17 static const char* SameSiteToString(uint32_t aSameSite) { 18 switch (aSameSite) { 19 case nsICookie::SAMESITE_NONE: 20 return "none"; 21 case nsICookie::SAMESITE_LAX: 22 return "lax"; 23 case nsICookie::SAMESITE_STRICT: 24 return "strict"; 25 case nsICookie::SAMESITE_UNSET: 26 return "unset"; 27 default: 28 MOZ_CRASH("Invalid nsICookie sameSite value"); 29 return ""; 30 } 31 } 32 33 // static 34 void CookieLogging::LogSuccess(bool aSetCookie, nsIURI* aHostURI, 35 const nsACString& aCookieString, Cookie* aCookie, 36 bool aReplacing) { 37 // if logging isn't enabled, return now to save cycles 38 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Debug)) { 39 return; 40 } 41 42 nsAutoCString spec; 43 if (aHostURI) { 44 aHostURI->GetAsciiSpec(spec); 45 } 46 47 MOZ_LOG(gCookieLog, LogLevel::Debug, 48 ("===== %s =====\n", aSetCookie ? "COOKIE ACCEPTED" : "COOKIE SENT")); 49 MOZ_LOG(gCookieLog, LogLevel::Debug, ("request URL: %s\n", spec.get())); 50 MOZ_LOG(gCookieLog, LogLevel::Debug, 51 ("cookie string: %s\n", aCookieString.BeginReading())); 52 if (aSetCookie) { 53 MOZ_LOG(gCookieLog, LogLevel::Debug, 54 ("replaces existing cookie: %s\n", aReplacing ? "true" : "false")); 55 } 56 57 LogCookie(aCookie); 58 59 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n")); 60 } 61 62 // static 63 void CookieLogging::LogFailure(bool aSetCookie, nsIURI* aHostURI, 64 const nsACString& aCookieString, 65 const char* aReason) { 66 // if logging isn't enabled, return now to save cycles 67 if (!MOZ_LOG_TEST(gCookieLog, LogLevel::Warning)) { 68 return; 69 } 70 71 nsAutoCString spec; 72 if (aHostURI) { 73 aHostURI->GetAsciiSpec(spec); 74 } 75 76 MOZ_LOG(gCookieLog, LogLevel::Warning, 77 ("===== %s =====\n", 78 aSetCookie ? "COOKIE NOT ACCEPTED" : "COOKIE NOT SENT")); 79 MOZ_LOG(gCookieLog, LogLevel::Warning, ("request URL: %s\n", spec.get())); 80 if (aSetCookie) { 81 MOZ_LOG(gCookieLog, LogLevel::Warning, 82 ("cookie string: %s\n", aCookieString.BeginReading())); 83 } 84 85 PRExplodedTime explodedTime; 86 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime); 87 char timeString[TIME_STRING_LENGTH]; 88 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", 89 &explodedTime); 90 91 MOZ_LOG(gCookieLog, LogLevel::Warning, ("current time: %s", timeString)); 92 MOZ_LOG(gCookieLog, LogLevel::Warning, ("rejected because %s\n", aReason)); 93 MOZ_LOG(gCookieLog, LogLevel::Warning, ("\n")); 94 } 95 96 // static 97 void CookieLogging::LogCookie(Cookie* aCookie) { 98 PRExplodedTime explodedTime; 99 PR_ExplodeTime(PR_Now(), PR_GMTParameters, &explodedTime); 100 char timeString[TIME_STRING_LENGTH]; 101 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", 102 &explodedTime); 103 104 MOZ_LOG(gCookieLog, LogLevel::Debug, ("current time: %s", timeString)); 105 106 if (aCookie) { 107 MOZ_LOG(gCookieLog, LogLevel::Debug, ("----------------\n")); 108 MOZ_LOG(gCookieLog, LogLevel::Debug, ("name: %s\n", aCookie->Name().get())); 109 MOZ_LOG(gCookieLog, LogLevel::Debug, 110 ("value: %s\n", aCookie->Value().get())); 111 MOZ_LOG(gCookieLog, LogLevel::Debug, 112 ("%s: %s\n", aCookie->IsDomain() ? "domain" : "host", 113 aCookie->Host().get())); 114 MOZ_LOG(gCookieLog, LogLevel::Debug, ("path: %s\n", aCookie->Path().get())); 115 116 PR_ExplodeTime(aCookie->ExpiryInMSec() * int64_t(PR_USEC_PER_MSEC), 117 PR_GMTParameters, &explodedTime); 118 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", 119 &explodedTime); 120 MOZ_LOG(gCookieLog, LogLevel::Debug, 121 ("expires: %s%s", timeString, 122 aCookie->IsSession() ? " (at end of session)" : "")); 123 124 PR_ExplodeTime(aCookie->CreationTimeInUSec(), PR_GMTParameters, 125 &explodedTime); 126 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", 127 &explodedTime); 128 MOZ_LOG(gCookieLog, LogLevel::Debug, ("created: %s", timeString)); 129 130 PR_ExplodeTime(aCookie->UpdateTimeInUSec(), PR_GMTParameters, 131 &explodedTime); 132 PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", 133 &explodedTime); 134 MOZ_LOG(gCookieLog, LogLevel::Debug, ("created: %s", timeString)); 135 136 MOZ_LOG(gCookieLog, LogLevel::Debug, 137 ("is secure: %s\n", aCookie->IsSecure() ? "true" : "false")); 138 MOZ_LOG(gCookieLog, LogLevel::Debug, 139 ("is httpOnly: %s\n", aCookie->IsHttpOnly() ? "true" : "false")); 140 MOZ_LOG(gCookieLog, LogLevel::Debug, 141 ("sameSite: %s\n", SameSiteToString(aCookie->SameSite()))); 142 MOZ_LOG( 143 gCookieLog, LogLevel::Debug, 144 ("schemeMap %d (http: %s | https: %s | file: %s)\n", 145 aCookie->SchemeMap(), 146 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTP ? "true" : "false"), 147 (aCookie->SchemeMap() & nsICookie::SCHEME_HTTPS ? "true" : "false"), 148 (aCookie->SchemeMap() & nsICookie::SCHEME_FILE ? "true" : "false"))); 149 150 nsAutoCString suffix; 151 aCookie->OriginAttributesRef().CreateSuffix(suffix); 152 MOZ_LOG(gCookieLog, LogLevel::Debug, 153 ("origin attributes: %s\n", 154 suffix.IsEmpty() ? "{empty}" : suffix.get())); 155 } 156 } 157 158 // static 159 void CookieLogging::LogEvicted(Cookie* aCookie, const char* details) { 160 MOZ_LOG(gCookieLog, LogLevel::Debug, ("===== COOKIE EVICTED =====\n")); 161 MOZ_LOG(gCookieLog, LogLevel::Debug, ("%s\n", details)); 162 163 LogCookie(aCookie); 164 165 MOZ_LOG(gCookieLog, LogLevel::Debug, ("\n")); 166 } 167 168 // static 169 void CookieLogging::LogMessageToConsole(nsIConsoleReportCollector* aCRC, 170 nsIURI* aURI, uint32_t aErrorFlags, 171 const nsACString& aCategory, 172 const nsACString& aMsg, 173 const nsTArray<nsString>& aParams) { 174 if (!aCRC) { 175 return; 176 } 177 178 nsAutoCString uri; 179 if (aURI) { 180 nsresult rv = aURI->GetSpec(uri); 181 if (NS_WARN_IF(NS_FAILED(rv))) { 182 return; 183 } 184 } 185 186 aCRC->AddConsoleReport(aErrorFlags, aCategory, 187 nsContentUtils::eNECKO_PROPERTIES, uri, 0, 0, aMsg, 188 aParams); 189 } 190 191 } // namespace net 192 } // namespace mozilla