Skip to content

Commit

Permalink
Bug 1754975 - Introduce a new error code for blocking a non-local con…
Browse files Browse the repository at this point in the history
…nection, r=necko-reviewers,dragana

Differential Revision: https://phabricator.services.mozilla.com/D146281
  • Loading branch information
KershawChang committed May 18, 2022
1 parent 4e94318 commit 509741d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion netwerk/base/nsSocketTransport2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ nsresult nsSocketTransport::InitiateSocket() {
"Browser services should be disabled or redirected to a local "
"server.\n",
mHost.get(), ipaddr.get());
MOZ_CRASH("Attempting to connect to non-local address!");
return NS_ERROR_NON_LOCAL_CONNECTION_REFUSED;
}
}

Expand Down
2 changes: 2 additions & 0 deletions netwerk/protocol/http/HttpLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace mozilla {
namespace net {
Maybe<nsCString> CallingScriptLocationString();
void LogCallingScriptLocation(void* instance);
void LogCallingScriptLocation(void* instance,
const Maybe<nsCString>& aLogLocation);
extern LazyLogModule gHttpLog;
} // namespace net
} // namespace mozilla
Expand Down
11 changes: 8 additions & 3 deletions netwerk/protocol/http/nsHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ ParsedHeaderValueListList::ParsedHeaderValueListList(
}

Maybe<nsCString> CallingScriptLocationString() {
if (!LOG4_ENABLED()) {
if (!LOG4_ENABLED() && !xpc::IsInAutomation()) {
return Nothing();
}

Expand All @@ -817,13 +817,18 @@ Maybe<nsCString> CallingScriptLocationString() {

void LogCallingScriptLocation(void* instance) {
Maybe<nsCString> logLocation = CallingScriptLocationString();
if (logLocation.isNothing()) {
LogCallingScriptLocation(instance, logLocation);
}

void LogCallingScriptLocation(void* instance,
const Maybe<nsCString>& aLogLocation) {
if (aLogLocation.isNothing()) {
return;
}

nsCString logString;
logString.AppendPrintf("%p called from script: ", instance);
logString.AppendPrintf("%s", logLocation->get());
logString.AppendPrintf("%s", aLogLocation->get());
LOG(("%s", logString.get()));
}

Expand Down
14 changes: 13 additions & 1 deletion netwerk/protocol/http/nsHttpChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5756,7 +5756,8 @@ nsHttpChannel::AsyncOpen(nsIStreamListener* aListener) {
"security flags in loadInfo but doContentSecurityCheck() not called");

LOG(("nsHttpChannel::AsyncOpen [this=%p]\n", this));
LogCallingScriptLocation(this);
mOpenerCallingScriptLocation = CallingScriptLocationString();
LogCallingScriptLocation(this, mOpenerCallingScriptLocation);
NS_CompareLoadInfoAndLoadContext(this);

#ifdef DEBUG
Expand Down Expand Up @@ -6779,6 +6780,17 @@ nsHttpChannel::OnStartRequest(nsIRequest* request) {
mStatus = status;
}

if (mStatus == NS_ERROR_NON_LOCAL_CONNECTION_REFUSED) {
MOZ_CRASH_UNSAFE(nsPrintfCString("Attempting to connect to non-local "
"address! opener is [%s], uri is "
"[%s]",
mOpenerCallingScriptLocation
? mOpenerCallingScriptLocation->get()
: "unknown",
mURI->GetSpecOrDefault().get())
.get());
}

LOG(("nsHttpChannel::OnStartRequest [this=%p request=%p status=%" PRIx32
"]\n",
this, request, static_cast<uint32_t>(static_cast<nsresult>(mStatus))));
Expand Down
1 change: 1 addition & 0 deletions netwerk/protocol/http/nsHttpChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ class nsHttpChannel final : public HttpBaseChannel,
bool mDidReval{false};

RefPtr<nsIEarlyHintObserver> mEarlyHintObserver;
Maybe<nsCString> mOpenerCallingScriptLocation;
};

NS_DEFINE_STATIC_IID_ACCESSOR(nsHttpChannel, NS_HTTPCHANNEL_IID)
Expand Down
3 changes: 3 additions & 0 deletions xpcom/base/ErrorList.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ def SUCCESS(code):
errors["NS_ERROR_HTTPS_ONLY"] = FAILURE(86)
# A WebSocket connection is failed.
errors["NS_ERROR_WEBSOCKET_CONNECTION_REFUSED"] = FAILURE(87)
# A connection to a non local address is refused because
# xpc::AreNonLocalConnectionsDisabled() returns true.
errors["NS_ERROR_NON_LOCAL_CONNECTION_REFUSED"] = FAILURE(88)

# XXX really need to better rationalize these error codes. are consumers of
# necko really expected to know how to discern the meaning of these??
Expand Down

0 comments on commit 509741d

Please sign in to comment.