1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsHTTPSOnlyStreamListener.h"
#include "NSSErrorsService.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/WindowGlobalParent.h"
#include "mozilla/glean/DomSecurityMetrics.h"
#include "mozpkix/pkixnss.h"
#include "nsCOMPtr.h"
#include "nsHTTPSOnlyUtils.h"
#include "nsIChannel.h"
#include "nsIRequest.h"
#include "nsITransportSecurityInfo.h"
#include "nsIURI.h"
#include "nsIWebProgressListener.h"
#include "nsPrintfCString.h"
#include "secerr.h"
#include "sslerr.h"
NS_IMPL_ISUPPORTS(nsHTTPSOnlyStreamListener, nsIStreamListener,
nsIRequestObserver)
nsHTTPSOnlyStreamListener::nsHTTPSOnlyStreamListener(
nsIStreamListener* aListener, nsILoadInfo* aLoadInfo)
: mListener(aListener), mCreationStart(mozilla::TimeStamp::Now()) {
RefPtr<mozilla::dom::WindowGlobalParent> wgp =
mozilla::dom::WindowGlobalParent::GetByInnerWindowId(
aLoadInfo->GetInnerWindowID());
// For Top-level document loads (which don't have a requesting window-context)
// we compute these flags once we create the Document in nsSecureBrowserUI.
if (wgp) {
wgp->TopWindowContext()->AddSecurityState(
nsIWebProgressListener::STATE_HTTPS_ONLY_MODE_UPGRADED);
}
}
NS_IMETHODIMP
nsHTTPSOnlyStreamListener::OnDataAvailable(nsIRequest* aRequest,
nsIInputStream* aInputStream,
uint64_t aOffset, uint32_t aCount) {
return mListener->OnDataAvailable(aRequest, aInputStream, aOffset, aCount);
}
NS_IMETHODIMP
nsHTTPSOnlyStreamListener::OnStartRequest(nsIRequest* request) {
return mListener->OnStartRequest(request);
}
NS_IMETHODIMP
nsHTTPSOnlyStreamListener::OnStopRequest(nsIRequest* request,
nsresult aStatus) {
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
// Note: CouldBeHttpsOnlyError also returns true if there was no error
if (nsHTTPSOnlyUtils::CouldBeHttpsOnlyError(channel, aStatus)) {
RecordUpgradeTelemetry(request, aStatus);
LogUpgradeFailure(request, aStatus);
// If the request failed and there is a requesting window-context, set
// HTTPS-Only state flag to indicate a failed upgrade.
// For Top-level document loads (which don't have a requesting
// window-context) we simply check in the UI code whether we landed on the
// HTTPS-Only error page.
if (NS_FAILED(aStatus)) {
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
RefPtr<mozilla::dom::WindowGlobalParent> wgp =
mozilla::dom::WindowGlobalParent::GetByInnerWindowId(
loadInfo->GetInnerWindowID());
if (wgp) {
wgp->TopWindowContext()->AddSecurityState(
nsIWebProgressListener::STATE_HTTPS_ONLY_MODE_UPGRADE_FAILED);
}
}
}
return mListener->OnStopRequest(request, aStatus);
}
void nsHTTPSOnlyStreamListener::RecordUpgradeTelemetry(nsIRequest* request,
nsresult aStatus) {
// 1. Get time between now and when the initial upgrade request started
mozilla::TimeDuration duration = mozilla::TimeStamp::Now() - mCreationStart;
// 2. Assemble the category string
// [!] All strings have to be present in Histograms.json
nsresult rv;
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
if (NS_FAILED(rv)) {
return;
}
nsAutoCString category;
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
nsContentPolicyType internalType = loadInfo->InternalContentPolicyType();
if (internalType == nsIContentPolicy::TYPE_DOCUMENT) {
category.AppendLiteral("top_");
} else {
category.AppendLiteral("sub_");
}
if (NS_SUCCEEDED(aStatus)) {
category.AppendLiteral("successful");
} else {
int32_t code = -1 * NS_ERROR_GET_CODE(aStatus);
if (aStatus == NS_ERROR_REDIRECT_LOOP) {
category.AppendLiteral("f_redirectloop");
} else if (aStatus == NS_ERROR_NET_TIMEOUT ||
aStatus == NS_ERROR_NET_TIMEOUT_EXTERNAL) {
category.AppendLiteral("f_timeout");
} else if (aStatus == NS_BINDING_ABORTED) {
category.AppendLiteral("f_aborted");
} else if (aStatus == NS_ERROR_CONNECTION_REFUSED) {
category.AppendLiteral("f_cxnrefused");
} else if (mozilla::psm::IsNSSErrorCode(code)) {
switch (code) {
case mozilla::pkix::MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT:
category.AppendLiteral("f_ssl_selfsignd");
break;
case SSL_ERROR_BAD_CERT_DOMAIN:
category.AppendLiteral("f_ssl_badcertdm");
break;
case SEC_ERROR_UNKNOWN_ISSUER:
category.AppendLiteral("f_ssl_unkwnissr");
break;
default:
category.AppendLiteral("f_ssl_other");
break;
}
} else {
category.AppendLiteral("f_other");
}
}
mozilla::glean::security::https_only_mode_upgrade_time.Get(category)
.AccumulateRawDuration(duration);
bool success = NS_SUCCEEDED(aStatus);
ExtContentPolicyType externalType = loadInfo->GetExternalContentPolicyType();
auto typeKey = nsAutoCString("unknown");
if (externalType == ExtContentPolicy::TYPE_MEDIA) {
switch (internalType) {
case nsIContentPolicy::TYPE_INTERNAL_AUDIO:
case nsIContentPolicy::TYPE_INTERNAL_TRACK:
typeKey = "audio"_ns;
break;
case nsIContentPolicy::TYPE_INTERNAL_VIDEO:
typeKey = "video"_ns;
break;
default:
MOZ_ASSERT_UNREACHABLE();
break;
}
} else {
switch (externalType) {
case ExtContentPolicy::TYPE_SCRIPT:
typeKey = "script"_ns;
break;
case ExtContentPolicy::TYPE_OBJECT:
typeKey = "object"_ns;
break;
case ExtContentPolicy::TYPE_DOCUMENT:
typeKey = "document"_ns;
break;
case ExtContentPolicy::TYPE_SUBDOCUMENT:
typeKey = "subdocument"_ns;
break;
case ExtContentPolicy::TYPE_XMLHTTPREQUEST:
typeKey = "xmlhttprequest"_ns;
break;
case ExtContentPolicy::TYPE_IMAGE:
case ExtContentPolicy::TYPE_IMAGESET:
typeKey = "image"_ns;
break;
case ExtContentPolicy::TYPE_DTD:
typeKey = "dtd"_ns;
break;
case ExtContentPolicy::TYPE_FONT:
case ExtContentPolicy::TYPE_UA_FONT:
typeKey = "font"_ns;
break;
case ExtContentPolicy::TYPE_FETCH:
typeKey = "fetch"_ns;
break;
case ExtContentPolicy::TYPE_WEBSOCKET:
typeKey = "websocket"_ns;
break;
case ExtContentPolicy::TYPE_STYLESHEET:
typeKey = "stylesheet"_ns;
break;
case ExtContentPolicy::TYPE_CSP_REPORT:
typeKey = "cspreport"_ns;
break;
case ExtContentPolicy::TYPE_WEB_MANIFEST:
typeKey = "webmanifest"_ns;
break;
case ExtContentPolicy::TYPE_PING:
typeKey = "ping"_ns;
break;
case ExtContentPolicy::TYPE_XSLT:
typeKey = "xslt"_ns;
break;
case ExtContentPolicy::TYPE_PROXIED_WEBRTC_MEDIA:
typeKey = "proxied-webrtc"_ns;
break;
case ExtContentPolicy::TYPE_INVALID:
case ExtContentPolicy::TYPE_OTHER:
case ExtContentPolicy::TYPE_MEDIA: // already handled above
case ExtContentPolicy::TYPE_BEACON:
case ExtContentPolicy::TYPE_SAVEAS_DOWNLOAD:
case ExtContentPolicy::TYPE_SPECULATIVE:
case ExtContentPolicy::TYPE_WEB_TRANSPORT:
case ExtContentPolicy::TYPE_WEB_IDENTITY:
case ExtContentPolicy::TYPE_JSON:
break;
// Do not add default: so that compilers can catch the missing case.
}
}
// Needs bug 1657470 (New Metric Type: "Keyed Categorical") before
// this can be migrated to Glean.
mozilla::glean::security::https_only_mode_upgrade_type
.Get(typeKey, success ? "true"_ns : "false"_ns)
.Add();
}
void nsHTTPSOnlyStreamListener::LogUpgradeFailure(nsIRequest* request,
nsresult aStatus) {
// If the request failed we'll log it to the console with the error-code
if (NS_SUCCEEDED(aStatus)) {
return;
}
nsresult rv;
// Try to query for the channel-object
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request, &rv);
if (NS_FAILED(rv)) {
return;
}
nsCOMPtr<nsIURI> uri;
rv = channel->GetURI(getter_AddRefs(uri));
if (NS_FAILED(rv)) {
return;
}
// Logging URI as well as Module- and Error-Code
AutoTArray<nsString, 2> params = {
NS_ConvertUTF8toUTF16(uri->GetSpecOrDefault()),
NS_ConvertUTF8toUTF16(nsPrintfCString("M%u-C%u",
NS_ERROR_GET_MODULE(aStatus),
NS_ERROR_GET_CODE(aStatus)))};
nsCOMPtr<nsILoadInfo> loadInfo = channel->LoadInfo();
nsHTTPSOnlyUtils::LogLocalizedString("HTTPSOnlyFailedRequest", params,
nsIScriptError::errorFlag, loadInfo,
uri);
}
|