ported from mozilla: Bug 1264792 - Update request'referrer policy when redirect.r=bkelly,dragana. (763507de3e)

This commit is contained in:
roytam1 2024-10-03 07:35:53 +08:00
commit 97fb5d7f9b
16 changed files with 244 additions and 91 deletions

View file

@ -1316,7 +1316,7 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer,
if(NS_FAILED(rv)) {
return rv;
}
mReferrerPolicy = REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE;
mReferrerPolicy = referrerPolicy;
if (!referrer) {
return NS_OK;
@ -1324,7 +1324,6 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer,
// Don't send referrer at all when the meta referrer setting is "no-referrer"
if (referrerPolicy == REFERRER_POLICY_NO_REFERRER) {
mReferrerPolicy = REFERRER_POLICY_NO_REFERRER;
return NS_OK;
}
@ -1597,7 +1596,6 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer,
if (NS_FAILED(rv)) return rv;
mReferrer = clone;
mReferrerPolicy = referrerPolicy;
return NS_OK;
}

View file

@ -1681,8 +1681,13 @@ NS_IMETHODIMP
HttpChannelChild::OnRedirectVerifyCallback(nsresult result)
{
LOG(("HttpChannelChild::OnRedirectVerifyCallback [this=%p]\n", this));
OptionalURIParams redirectURI;
nsresult rv;
OptionalURIParams redirectURI;
uint32_t referrerPolicy = REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE;
OptionalURIParams referrerURI;
SerializeURI(nullptr, referrerURI);
nsCOMPtr<nsIHttpChannel> newHttpChannel =
do_QueryInterface(mRedirectChannelChild);
@ -1700,6 +1705,12 @@ HttpChannelChild::OnRedirectVerifyCallback(nsresult result)
if (newHttpChannel) {
// Must not be called until after redirect observers called.
newHttpChannel->SetOriginalURI(mOriginalURI);
newHttpChannel->GetReferrerPolicy(&referrerPolicy);
nsCOMPtr<nsIURI> newChannelReferrerURI;
newHttpChannel->GetReferrer(getter_AddRefs(newChannelReferrerURI));
SerializeURI(newChannelReferrerURI, referrerURI);
}
if (mRedirectingForSubsequentSynthesizedResponse) {
@ -1770,8 +1781,9 @@ HttpChannelChild::OnRedirectVerifyCallback(nsresult result)
}
if (mIPCOpen)
SendRedirect2Verify(result, *headerTuples, loadFlags, redirectURI,
corsPreflightArgs, chooseAppcache);
SendRedirect2Verify(result, *headerTuples, loadFlags, referrerPolicy,
referrerURI, redirectURI, corsPreflightArgs,
chooseAppcache);
return NS_OK;
}
@ -2147,6 +2159,25 @@ HttpChannelChild::ContinueAsyncOpen()
// HttpChannelChild::nsIHttpChannel
//-----------------------------------------------------------------------------
NS_IMETHODIMP
HttpChannelChild::SetReferrerWithPolicy(nsIURI *referrer,
uint32_t referrerPolicy)
{
ENSURE_CALLED_BEFORE_CONNECT();
// remove old referrer if any, loop backwards
for (int i = mClientSetRequestHeaders.Length() - 1; i >= 0; --i) {
if (NS_LITERAL_CSTRING("Referer").Equals(mClientSetRequestHeaders[i].mHeader)) {
mClientSetRequestHeaders.RemoveElementAt(i);
}
}
nsresult rv = HttpBaseChannel::SetReferrerWithPolicy(referrer, referrerPolicy);
if (NS_FAILED(rv))
return rv;
return NS_OK;
}
NS_IMETHODIMP
HttpChannelChild::SetRequestHeader(const nsACString& aHeader,
const nsACString& aValue,

View file

@ -77,6 +77,7 @@ public:
NS_IMETHOD AsyncOpen2(nsIStreamListener *aListener) override;
// HttpBaseChannel::nsIHttpChannel
NS_IMETHOD SetReferrerWithPolicy(nsIURI *referrer, uint32_t referrerPolicy) override;
NS_IMETHOD SetRequestHeader(const nsACString& aHeader,
const nsACString& aValue,
bool aMerge) override;

View file

@ -733,6 +733,8 @@ bool
HttpChannelParent::RecvRedirect2Verify(const nsresult& result,
const RequestHeaderTuples& changedHeaders,
const uint32_t& loadFlags,
const uint32_t& referrerPolicy,
const OptionalURIParams& aReferrerURI,
const OptionalURIParams& aAPIRedirectURI,
const OptionalCorsPreflightArgs& aCorsPreflightArgs,
const bool& aChooseAppcache)
@ -773,6 +775,9 @@ HttpChannelParent::RecvRedirect2Verify(const nsresult& result,
newInternalChannel->SetCorsPreflightParameters(args.unsafeHeaders());
}
nsCOMPtr<nsIURI> referrerUri = DeserializeURI(aReferrerURI);
newHttpChannel->SetReferrerWithPolicy(referrerUri, referrerPolicy);
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel =
do_QueryInterface(newHttpChannel);
if (appCacheChannel) {

View file

@ -159,6 +159,8 @@ protected:
virtual bool RecvRedirect2Verify(const nsresult& result,
const RequestHeaderTuples& changedHeaders,
const uint32_t& loadFlags,
const uint32_t& referrerPolicy,
const OptionalURIParams& aReferrerURI,
const OptionalURIParams& apiRedirectUri,
const OptionalCorsPreflightArgs& aCorsPreflightArgs,
const bool& aChooseAppcache) override;

View file

@ -44,7 +44,9 @@ parent:
// Reports approval/veto of redirect by child process redirect observers
async Redirect2Verify(nsresult result, RequestHeaderTuples changedHeaders,
uint32_t loadFlags, OptionalURIParams apiRedirectTo,
uint32_t loadFlags, uint32_t referrerPolicy,
OptionalURIParams referrerUri,
OptionalURIParams apiRedirectTo,
OptionalCorsPreflightArgs corsPreflightArgs,
bool chooseAppcache);