Skip to content

Commit

Permalink
Bug 1475073: Pass individual CORS errors as categories to web console…
Browse files Browse the repository at this point in the history
… error messages. r=baku
  • Loading branch information
Christoph Kerschbaumer committed Jul 20, 2018
1 parent 9bce7ad commit 109ba1c
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 27 deletions.
10 changes: 6 additions & 4 deletions netwerk/protocol/http/HttpChannelChild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3974,20 +3974,22 @@ HttpChannelChild::ActorDestroy(ActorDestroyReason aWhy)
}

mozilla::ipc::IPCResult
HttpChannelChild::RecvLogBlockedCORSRequest(const nsString& aMessage)
HttpChannelChild::RecvLogBlockedCORSRequest(const nsString& aMessage,
const nsCString& aCategory)
{
Unused << LogBlockedCORSRequest(aMessage);
Unused << LogBlockedCORSRequest(aMessage, aCategory);
return IPC_OK();
}

NS_IMETHODIMP
HttpChannelChild::LogBlockedCORSRequest(const nsAString & aMessage)
HttpChannelChild::LogBlockedCORSRequest(const nsAString & aMessage,
const nsACString& aCategory)
{
if (mLoadInfo) {
uint64_t innerWindowID = mLoadInfo->GetInnerWindowID();
bool privateBrowsing = !!mLoadInfo->GetOriginAttributes().mPrivateBrowsingId;
nsCORSListenerProxy::LogBlockedCORSRequest(innerWindowID, privateBrowsing,
aMessage);
aMessage, aCategory);
}
return NS_OK;
}
Expand Down
4 changes: 2 additions & 2 deletions netwerk/protocol/http/HttpChannelChild.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ class HttpChannelChild final : public PHttpChannelChild
// Get event target for processing network events.
already_AddRefed<nsIEventTarget> GetNeckoTarget() override;

virtual mozilla::ipc::IPCResult RecvLogBlockedCORSRequest(const nsString& aMessage) override;
NS_IMETHOD LogBlockedCORSRequest(const nsAString & aMessage) override;
virtual mozilla::ipc::IPCResult RecvLogBlockedCORSRequest(const nsString& aMessage, const nsCString& aCategory) override;
NS_IMETHOD LogBlockedCORSRequest(const nsAString & aMessage, const nsACString& aCategory) override;

private:
// this section is for main-thread-only object
Expand Down
6 changes: 4 additions & 2 deletions netwerk/protocol/http/HttpChannelParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2354,10 +2354,12 @@ HttpChannelParent::DoSendSetPriority(int16_t aValue)
}

nsresult
HttpChannelParent::LogBlockedCORSRequest(const nsAString& aMessage)
HttpChannelParent::LogBlockedCORSRequest(const nsAString& aMessage,
const nsACString& aCategory)
{
if (mIPCClosed ||
NS_WARN_IF(!SendLogBlockedCORSRequest(nsString(aMessage)))) {
NS_WARN_IF(!SendLogBlockedCORSRequest(nsString(aMessage),
nsCString(aCategory)))) {
return NS_ERROR_UNEXPECTED;
}
return NS_OK;
Expand Down
2 changes: 1 addition & 1 deletion netwerk/protocol/http/HttpChannelParent.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class HttpChannelParent final : public nsIInterfaceRequestor
MOZ_MUST_USE nsresult
ReportSecurityMessage(const nsAString& aMessageTag,
const nsAString& aMessageCategory) override;
nsresult LogBlockedCORSRequest(const nsAString& aMessage) override;
nsresult LogBlockedCORSRequest(const nsAString& aMessage, const nsACString& aCategory) override;

// Calls SendDeleteSelf and sets mIPCClosed to true because we should not
// send any more messages after that. Bug 1274886
Expand Down
3 changes: 2 additions & 1 deletion netwerk/protocol/http/InterceptedHttpChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,8 @@ InterceptedHttpChannel::AsyncOpen2(nsIStreamListener* aListener)
}

NS_IMETHODIMP
InterceptedHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage)
InterceptedHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage,
const nsACString& aCategory)
{
// Synthetic responses should not trigger CORS blocking.
return NS_ERROR_NOT_IMPLEMENTED;
Expand Down
2 changes: 1 addition & 1 deletion netwerk/protocol/http/InterceptedHttpChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class InterceptedHttpChannel final : public HttpBaseChannel
AsyncOpen2(nsIStreamListener *aListener) override;

NS_IMETHOD
LogBlockedCORSRequest(const nsAString & aMessage) override;
LogBlockedCORSRequest(const nsAString & aMessage, const nsACString& aCategory) override;

NS_IMETHOD
SetupFallbackChannel(const char * aFallbackKey) override;
Expand Down
3 changes: 2 additions & 1 deletion netwerk/protocol/http/NullHttpChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,8 @@ NullHttpChannel::SetIsMainDocumentChannel(bool aValue)
}

NS_IMETHODIMP
NullHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage)
NullHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage,
const nsACString& aCategory)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
Expand Down
2 changes: 1 addition & 1 deletion netwerk/protocol/http/PHttpChannel.ipdl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ child:
// When CORS blocks the request in the parent process, it doesn't have the
// correct window ID, so send the message to the child for logging to the web
// console.
async LogBlockedCORSRequest(nsString message);
async LogBlockedCORSRequest(nsString message, nsCString category);

async AttachStreamFilter(Endpoint<PStreamFilterParent> aEndpoint);

Expand Down
16 changes: 10 additions & 6 deletions netwerk/protocol/http/nsCORSListenerProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ LogBlockedRequest(nsIRequest* aRequest,
}

nsAutoString msg(blockedMessage.get());
nsDependentCString category(aProperty);

if (XRE_IsParentProcess()) {
if (aCreatingChannel) {
rv = aCreatingChannel->LogBlockedCORSRequest(msg);
rv = aCreatingChannel->LogBlockedCORSRequest(msg, category);
if (NS_SUCCEEDED(rv)) {
return;
}
Expand All @@ -105,10 +106,11 @@ LogBlockedRequest(nsIRequest* aRequest,
privateBrowsing = nsContentUtils::IsInPrivateBrowsing(loadGroup);
}

// log message ourselves
// we are passing aProperty as the category so we can link to the
// appropriate MDN docs depending on the specific error.
uint64_t innerWindowID = nsContentUtils::GetInnerWindowID(aRequest);
nsCORSListenerProxy::LogBlockedCORSRequest(innerWindowID, privateBrowsing,
msg);
msg, category);
}

//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1578,7 +1580,8 @@ nsCORSListenerProxy::StartCORSPreflight(nsIChannel* aRequestChannel,
void
nsCORSListenerProxy::LogBlockedCORSRequest(uint64_t aInnerWindowID,
bool aPrivateBrowsing,
const nsAString& aMessage)
const nsAString& aMessage,
const nsACString& aCategory)
{
nsresult rv = NS_OK;

Expand All @@ -1605,17 +1608,18 @@ nsCORSListenerProxy::LogBlockedCORSRequest(uint64_t aInnerWindowID,
0, // lineNumber
0, // columnNumber
nsIScriptError::warningFlag,
"CORS",
aCategory,
aInnerWindowID);
}
else {
nsCString category = PromiseFlatCString(aCategory);
rv = scriptError->Init(aMessage,
EmptyString(), // sourceName
EmptyString(), // sourceLine
0, // lineNumber
0, // columnNumber
nsIScriptError::warningFlag,
"CORS",
category.get(),
aPrivateBrowsing);
}
if (NS_FAILED(rv)) {
Expand Down
3 changes: 2 additions & 1 deletion netwerk/protocol/http/nsCORSListenerProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class nsCORSListenerProxy final : public nsIStreamListener,
// browser console if no valid inner window ID is found.
static void LogBlockedCORSRequest(uint64_t aInnerWindowID,
bool aPrivateBrowsing,
const nsAString& aMessage);
const nsAString& aMessage,
const nsACString& aCategory);
private:
// Only HttpChannelParent can call RemoveFromCorsPreflightCache
friend class mozilla::net::HttpChannelParent;
Expand Down
4 changes: 2 additions & 2 deletions netwerk/protocol/http/nsHttpChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ nsHttpChannel::AddSecurityMessage(const nsAString& aMessageTag,
}

NS_IMETHODIMP
nsHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage)
nsHttpChannel::LogBlockedCORSRequest(const nsAString& aMessage, const nsACString& aCategory)
{
if (mWarningReporter) {
return mWarningReporter->LogBlockedCORSRequest(aMessage);
return mWarningReporter->LogBlockedCORSRequest(aMessage, aCategory);
}
return NS_ERROR_UNEXPECTED;
}
Expand Down
4 changes: 2 additions & 2 deletions netwerk/protocol/http/nsHttpChannel.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class HttpChannelSecurityWarningReporter : public nsISupports
virtual MOZ_MUST_USE nsresult
ReportSecurityMessage(const nsAString& aMessageTag,
const nsAString& aMessageCategory) = 0;
virtual nsresult LogBlockedCORSRequest(const nsAString& aMessage) = 0;
virtual nsresult LogBlockedCORSRequest(const nsAString& aMessage, const nsACString& aCategory) = 0;
};

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -190,7 +190,7 @@ class nsHttpChannel final : public HttpBaseChannel
MOZ_MUST_USE nsresult
AddSecurityMessage(const nsAString& aMessageTag,
const nsAString& aMessageCategory) override;
NS_IMETHOD LogBlockedCORSRequest(const nsAString& aMessage) override;
NS_IMETHOD LogBlockedCORSRequest(const nsAString& aMessage, const nsACString& aCategory) override;

void SetWarningReporter(HttpChannelSecurityWarningReporter *aReporter);
HttpChannelSecurityWarningReporter* GetWarningReporter();
Expand Down
5 changes: 4 additions & 1 deletion netwerk/protocol/http/nsIHttpChannel.idl
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,9 @@ interface nsIHttpChannel : nsIChannel
*
* @param aMessage
* The message to print in the console.
*
* @param aCategory
* The category under which the message should be displayed.
*/
void logBlockedCORSRequest(in AString aMessage);
void logBlockedCORSRequest(in AString aMessage, in ACString aCategory);
};
5 changes: 3 additions & 2 deletions netwerk/protocol/viewsource/nsViewSourceChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,11 +1134,12 @@ nsViewSourceChannel::SetAltDataForChild(bool aIsForChild)
}

NS_IMETHODIMP
nsViewSourceChannel::LogBlockedCORSRequest(const nsAString& aMessage)
nsViewSourceChannel::LogBlockedCORSRequest(const nsAString& aMessage,
const nsACString& aCategory)
{
if (!mHttpChannel) {
NS_WARNING("nsViewSourceChannel::LogBlockedCORSRequest mHttpChannel is null");
return NS_ERROR_UNEXPECTED;
}
return mHttpChannel->LogBlockedCORSRequest(aMessage);
return mHttpChannel->LogBlockedCORSRequest(aMessage, aCategory);
}

0 comments on commit 109ba1c

Please sign in to comment.