From c55748ac0b03f7240594adfad8ed7a28ea84aeaa Mon Sep 17 00:00:00 2001 From: Peter Van der Beken Date: Wed, 15 Jun 2022 11:32:16 +0000 Subject: [PATCH] Bug 1773192 - Referrer header missing after calling history.replaceState and clicking back button. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D149176 --- docshell/base/nsDocShell.cpp | 17 ++++-- docshell/base/nsDocShell.h | 8 +-- docshell/shistory/SessionHistoryEntry.h | 5 ++ .../test/mochitest/file_bug1773192_1.html | 13 ++++ .../test/mochitest/file_bug1773192_2.html | 13 ++++ docshell/test/mochitest/file_bug1773192_3.sjs | 3 + docshell/test/mochitest/mochitest.ini | 5 ++ docshell/test/mochitest/test_bug1773192.html | 61 +++++++++++++++++++ 8 files changed, 116 insertions(+), 9 deletions(-) create mode 100644 docshell/test/mochitest/file_bug1773192_1.html create mode 100644 docshell/test/mochitest/file_bug1773192_2.html create mode 100644 docshell/test/mochitest/file_bug1773192_3.sjs create mode 100644 docshell/test/mochitest/test_bug1773192.html diff --git a/docshell/base/nsDocShell.cpp b/docshell/base/nsDocShell.cpp index b37f148310bd..c42f9450ce1a 100644 --- a/docshell/base/nsDocShell.cpp +++ b/docshell/base/nsDocShell.cpp @@ -11466,6 +11466,7 @@ nsresult nsDocShell::UpdateURLAndHistory(Document* aDocument, nsIURI* aNewURI, UpdateActiveEntry(false, /* aPreviousScrollPos = */ Some(scrollPos), aNewURI, /* aOriginalURI = */ nullptr, + /* aReferrerInfo = */ nullptr, /* aTriggeringPrincipal = */ aDocument->NodePrincipal(), csp, title, scrollRestorationIsManual, aData, uriWasModified); @@ -11508,12 +11509,17 @@ nsresult nsDocShell::UpdateURLAndHistory(Document* aDocument, nsIURI* aNewURI, // in our case. We could also set it to aNewURI, with the same result. // We don't use aTitle here, see bug 544535. nsString title; + nsCOMPtr referrerInfo; if (mActiveEntry) { title = mActiveEntry->GetTitle(); + referrerInfo = mActiveEntry->GetReferrerInfo(); + } else { + referrerInfo = nullptr; } UpdateActiveEntry( true, /* aPreviousScrollPos = */ Nothing(), aNewURI, aNewURI, - aDocument->NodePrincipal(), aDocument->GetCsp(), title, + /* aReferrerInfo = */ referrerInfo, aDocument->NodePrincipal(), + aDocument->GetCsp(), title, mActiveEntry && mActiveEntry->GetScrollRestorationIsManual(), aData, uriWasModified); } else { @@ -11933,10 +11939,10 @@ nsresult nsDocShell::AddToSessionHistory( void nsDocShell::UpdateActiveEntry( bool aReplace, const Maybe& aPreviousScrollPos, nsIURI* aURI, - nsIURI* aOriginalURI, nsIPrincipal* aTriggeringPrincipal, - nsIContentSecurityPolicy* aCsp, const nsAString& aTitle, - bool aScrollRestorationIsManual, nsIStructuredCloneContainer* aData, - bool aURIWasModified) { + nsIURI* aOriginalURI, nsIReferrerInfo* aReferrerInfo, + nsIPrincipal* aTriggeringPrincipal, nsIContentSecurityPolicy* aCsp, + const nsAString& aTitle, bool aScrollRestorationIsManual, + nsIStructuredCloneContainer* aData, bool aURIWasModified) { MOZ_ASSERT(mozilla::SessionHistoryInParent()); MOZ_ASSERT(aURI, "uri is null"); MOZ_ASSERT(mLoadType == LOAD_PUSHSTATE, @@ -11965,6 +11971,7 @@ void nsDocShell::UpdateActiveEntry( aURI, aTriggeringPrincipal, nullptr, nullptr, aCsp, mContentTypeHint); } mActiveEntry->SetOriginalURI(aOriginalURI); + mActiveEntry->SetReferrerInfo(aReferrerInfo); mActiveEntry->SetTitle(aTitle); mActiveEntry->SetStateData(static_cast(aData)); mActiveEntry->SetURIWasModified(aURIWasModified); diff --git a/docshell/base/nsDocShell.h b/docshell/base/nsDocShell.h index 09f84b25ae9b..79b2af0c0f58 100644 --- a/docshell/base/nsDocShell.h +++ b/docshell/base/nsDocShell.h @@ -640,10 +640,10 @@ class nsDocShell final : public nsDocLoader, void UpdateActiveEntry( bool aReplace, const mozilla::Maybe& aPreviousScrollPos, - nsIURI* aURI, nsIURI* aOriginalURI, nsIPrincipal* aTriggeringPrincipal, - nsIContentSecurityPolicy* aCsp, const nsAString& aTitle, - bool aScrollRestorationIsManual, nsIStructuredCloneContainer* aData, - bool aURIWasModified); + nsIURI* aURI, nsIURI* aOriginalURI, nsIReferrerInfo* aReferrerInfo, + nsIPrincipal* aTriggeringPrincipal, nsIContentSecurityPolicy* aCsp, + const nsAString& aTitle, bool aScrollRestorationIsManual, + nsIStructuredCloneContainer* aData, bool aURIWasModified); nsresult AddChildSHEntry(nsISHEntry* aCloneRef, nsISHEntry* aNewEntry, int32_t aChildOffset, uint32_t aLoadType, diff --git a/docshell/shistory/SessionHistoryEntry.h b/docshell/shistory/SessionHistoryEntry.h index 791d48877bae..174e4ef47318 100644 --- a/docshell/shistory/SessionHistoryEntry.h +++ b/docshell/shistory/SessionHistoryEntry.h @@ -71,6 +71,11 @@ class SessionHistoryInfo { mResultPrincipalURI = aResultPrincipalURI; } + nsIReferrerInfo* GetReferrerInfo() { return mReferrerInfo; } + void SetReferrerInfo(nsIReferrerInfo* aReferrerInfo) { + mReferrerInfo = aReferrerInfo; + } + bool HasPostData() const { return mPostData; } already_AddRefed GetPostData() const; void SetPostData(nsIInputStream* aPostData); diff --git a/docshell/test/mochitest/file_bug1773192_1.html b/docshell/test/mochitest/file_bug1773192_1.html new file mode 100644 index 000000000000..42d5b3fceded --- /dev/null +++ b/docshell/test/mochitest/file_bug1773192_1.html @@ -0,0 +1,13 @@ + diff --git a/docshell/test/mochitest/file_bug1773192_2.html b/docshell/test/mochitest/file_bug1773192_2.html new file mode 100644 index 000000000000..3b1e5bcb28d3 --- /dev/null +++ b/docshell/test/mochitest/file_bug1773192_2.html @@ -0,0 +1,13 @@ + + + + + + +
+ + + diff --git a/docshell/test/mochitest/file_bug1773192_3.sjs b/docshell/test/mochitest/file_bug1773192_3.sjs new file mode 100644 index 000000000000..ce889c7035ec --- /dev/null +++ b/docshell/test/mochitest/file_bug1773192_3.sjs @@ -0,0 +1,3 @@ +function handleRequest(request, response) { + response.write(""); +} diff --git a/docshell/test/mochitest/mochitest.ini b/docshell/test/mochitest/mochitest.ini index 07c9ae556665..63b0cbe06b09 100644 --- a/docshell/test/mochitest/mochitest.ini +++ b/docshell/test/mochitest/mochitest.ini @@ -140,6 +140,11 @@ support-files = [test_bug1747033.html] support-files = file_bug1747033.sjs +[test_bug1773192.html] +support-files = + file_bug1773192_1.html + file_bug1773192_2.html + file_bug1773192_3.sjs [test_close_onpagehide_by_history_back.html] [test_close_onpagehide_by_window_close.html] [test_compressed_multipart.html] diff --git a/docshell/test/mochitest/test_bug1773192.html b/docshell/test/mochitest/test_bug1773192.html new file mode 100644 index 000000000000..d4c42dc1a778 --- /dev/null +++ b/docshell/test/mochitest/test_bug1773192.html @@ -0,0 +1,61 @@ + + + + + Test referrer with going back + + + + + +

+ +

+
+