summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/browser/renderer_context_menu/render_view_context_menu.cc18
-rw-r--r--chrome/browser/ui/android/context_menu_helper.cc17
-rw-r--r--chrome/browser/ui/android/context_menu_helper.h3
-rw-r--r--net/url_request/url_request.cc15
-rw-r--r--url/gurl.cc12
-rw-r--r--url/gurl.h5
-rw-r--r--url/gurl_unittest.cc19
7 files changed, 46 insertions, 43 deletions
diff --git a/chrome/browser/renderer_context_menu/render_view_context_menu.cc b/chrome/browser/renderer_context_menu/render_view_context_menu.cc
index 8257468..63c9fe5 100644
--- a/chrome/browser/renderer_context_menu/render_view_context_menu.cc
+++ b/chrome/browser/renderer_context_menu/render_view_context_menu.cc
@@ -1995,24 +1995,14 @@ base::string16 RenderViewContextMenu::PrintableSelectionText() {
// Controller functions --------------------------------------------------------
void RenderViewContextMenu::OpenURL(
- const GURL& url, const GURL& referrer, int64 frame_id,
+ const GURL& url, const GURL& referring_url, int64 frame_id,
WindowOpenDisposition disposition,
content::PageTransition transition) {
- // Ensure that URL fragment, username and password fields are not sent
- // in the referrer.
- GURL sanitized_referrer(referrer);
- if (sanitized_referrer.is_valid() && (sanitized_referrer.has_ref() ||
- sanitized_referrer.has_username() || sanitized_referrer.has_password())) {
- GURL::Replacements referrer_mods;
- referrer_mods.ClearRef();
- referrer_mods.ClearUsername();
- referrer_mods.ClearPassword();
- sanitized_referrer = sanitized_referrer.ReplaceComponents(referrer_mods);
- }
+ content::Referrer referrer(referring_url.GetAsReferrer(),
+ params_.referrer_policy);
WebContents* new_contents = source_web_contents_->OpenURL(OpenURLParams(
- url, content::Referrer(sanitized_referrer, params_.referrer_policy),
- disposition, transition, false));
+ url, referrer, disposition, transition, false));
if (!new_contents)
return;
diff --git a/chrome/browser/ui/android/context_menu_helper.cc b/chrome/browser/ui/android/context_menu_helper.cc
index ef9dbe6..a5db636 100644
--- a/chrome/browser/ui/android/context_menu_helper.cc
+++ b/chrome/browser/ui/android/context_menu_helper.cc
@@ -66,8 +66,8 @@ void ContextMenuHelper::SetPopulator(jobject jpopulator) {
base::android::ScopedJavaLocalRef<jobject>
ContextMenuHelper::CreateJavaContextMenuParams(
const content::ContextMenuParams& params) {
- GURL sanitizedReferrer = SanitizeReferrer(
- params.frame_url.is_empty() ? params.page_url : params.frame_url);
+ GURL sanitizedReferrer = (params.frame_url.is_empty() ?
+ params.page_url : params.frame_url).GetAsReferrer();
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> jmenu_info =
@@ -95,19 +95,6 @@ ContextMenuHelper::CreateJavaContextMenuParams(
return jmenu_info;
}
-GURL ContextMenuHelper::SanitizeReferrer(const GURL& referring_url) {
- // This mirrors sanitization done on Desktop in RenderViewContextMenu.
- if (referring_url.is_valid() && (referring_url.has_ref() ||
- referring_url.has_username() || referring_url.has_password())) {
- GURL::Replacements referrer_mods;
- referrer_mods.ClearRef();
- referrer_mods.ClearUsername();
- referrer_mods.ClearPassword();
- return referring_url.ReplaceComponents(referrer_mods);
- }
- return referring_url;
-}
-
void ContextMenuHelper::OnCustomItemSelected(JNIEnv* env,
jobject obj,
jint action) {
diff --git a/chrome/browser/ui/android/context_menu_helper.h b/chrome/browser/ui/android/context_menu_helper.h
index 5f20520..cf01818 100644
--- a/chrome/browser/ui/android/context_menu_helper.h
+++ b/chrome/browser/ui/android/context_menu_helper.h
@@ -41,9 +41,6 @@ class ContextMenuHelper
static base::android::ScopedJavaLocalRef<jobject> CreateJavaContextMenuParams(
const content::ContextMenuParams& params);
- // Strips the referring url of the username, password and ref fields.
- static GURL SanitizeReferrer(const GURL& referrer);
-
base::android::ScopedJavaGlobalRef<jobject> java_obj_;
content::WebContents* web_contents_;
diff --git a/net/url_request/url_request.cc b/net/url_request/url_request.cc
index 53d0043..dbff821 100644
--- a/net/url_request/url_request.cc
+++ b/net/url_request/url_request.cc
@@ -608,20 +608,13 @@ std::string URLRequest::ComputeMethodForRedirect(
void URLRequest::SetReferrer(const std::string& referrer) {
DCHECK(!is_pending_);
- referrer_ = referrer;
- // Ensure that we do not send URL fragment, username and password
- // fields in the referrer.
GURL referrer_url(referrer);
UMA_HISTOGRAM_BOOLEAN("Net.URLRequest_SetReferrer_IsEmptyOrValid",
referrer_url.is_empty() || referrer_url.is_valid());
- if (referrer_url.is_valid() && (referrer_url.has_ref() ||
- referrer_url.has_username() || referrer_url.has_password())) {
- GURL::Replacements referrer_mods;
- referrer_mods.ClearRef();
- referrer_mods.ClearUsername();
- referrer_mods.ClearPassword();
- referrer_url = referrer_url.ReplaceComponents(referrer_mods);
- referrer_ = referrer_url.spec();
+ if (referrer_url.is_valid()) {
+ referrer_ = referrer_url.GetAsReferrer().spec();
+ } else {
+ referrer_ = referrer;
}
}
diff --git a/url/gurl.cc b/url/gurl.cc
index 8eb2d61..77b8def2 100644
--- a/url/gurl.cc
+++ b/url/gurl.cc
@@ -320,6 +320,18 @@ GURL GURL::GetOrigin() const {
return ReplaceComponents(replacements);
}
+GURL GURL::GetAsReferrer() const {
+ if (!is_valid_ ||
+ (!has_ref() && !has_username() && !has_password()))
+ return GURL(*this);
+
+ url_canon::Replacements<char> replacements;
+ replacements.ClearRef();
+ replacements.ClearUsername();
+ replacements.ClearPassword();
+ return ReplaceComponents(replacements);
+}
+
GURL GURL::GetWithEmptyPath() const {
// This doesn't make sense for invalid or nonstandard URLs, so return
// the empty URL.
diff --git a/url/gurl.h b/url/gurl.h
index dc88fec25..28897d1 100644
--- a/url/gurl.h
+++ b/url/gurl.h
@@ -195,6 +195,11 @@ class URL_EXPORT GURL {
// will be the empty URL.
GURL GetOrigin() const;
+ // A helper function to return a GURL stripped from the elements that are not
+ // supposed to be sent as HTTP referrer: username, password and ref fragment.
+ // For invalid URLs the original URL will be returned.
+ GURL GetAsReferrer() const;
+
// Returns true if the scheme for the current URL is a known "standard"
// scheme. Standard schemes have an authority and a path section. This
// includes file: and filesystem:, which some callers may want to filter out
diff --git a/url/gurl_unittest.cc b/url/gurl_unittest.cc
index c2d86e9..738f2cc 100644
--- a/url/gurl_unittest.cc
+++ b/url/gurl_unittest.cc
@@ -287,6 +287,25 @@ TEST(GURLTest, GetOrigin) {
}
}
+TEST(GURLTest, GetAsReferrer) {
+ struct TestCase {
+ const char* input;
+ const char* expected;
+ } cases[] = {
+ {"http://www.google.com", "http://www.google.com/"},
+ {"http://user:pass@www.google.com:21/blah#baz", "http://www.google.com:21/blah"},
+ {"http://user@www.google.com", "http://www.google.com/"},
+ {"http://:pass@www.google.com", "http://www.google.com/"},
+ {"http://:@www.google.com", "http://www.google.com/"},
+ {"http://www.google.com/temp/foo?q#b", "http://www.google.com/temp/foo?q"},
+ };
+ for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
+ GURL url(cases[i].input);
+ GURL origin = url.GetAsReferrer();
+ EXPECT_EQ(cases[i].expected, origin.spec());
+ }
+}
+
TEST(GURLTest, GetWithEmptyPath) {
struct TestCase {
const char* input;