Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redirects additional details button target URL on crashes page (1.10.x). #5815

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "brave/common/brave_features.h"
#include "brave/common/brave_switches.h"
#include "brave/common/network_constants.h"
Expand All @@ -24,6 +26,8 @@

namespace brave {

namespace {

// Update server checks happen from the profile context for admin policy
// installed extensions. Update server checks happen from the system context for
// normal update operations.
Expand All @@ -45,6 +49,32 @@ bool IsUpdaterURL(const GURL& gurl) {
[&gurl](URLPattern pattern) { return pattern.MatchesURL(gurl); });
}

bool RewriteBugReportingURL(const GURL& request_url, GURL* new_url) {
GURL url("https://github.com/brave/brave-browser/issues/new");
std::string query = "title=Crash%20Report&labels=crash";
// We are expecting 3 query keys: comment, template, and labels
base::StringPairs pairs;
if (!base::SplitStringIntoKeyValuePairs(request_url.query(), '=', '&',
&pairs) || pairs.size() != 3) {
return false;
}
for (const auto& pair : pairs) {
if (pair.first == "comment") {
query += "&body=" + pair.second;
base::ReplaceSubstringsAfterOffset(&query, 0, "Chrome", "Brave");
} else if (pair.first != "template" && pair.first != "labels") {
return false;
}
}

GURL::Replacements replacements;
replacements.SetQueryStr(query);
*new_url = url.ReplaceComponents(replacements);
return true;
}

} // namespace

int OnBeforeURLRequest_CommonStaticRedirectWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
Expand All @@ -67,6 +97,9 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS, kChromeCastPrefix);
static URLPattern clients4_pattern(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS, kClients4Prefix);
static URLPattern bugsChromium_pattern(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS,
"*://bugs.chromium.org/p/chromium/issues/entry?*");

if (IsUpdaterURL(request_url)) {
replacements.SetQueryStr(request_url.query_piece());
Expand Down Expand Up @@ -97,6 +130,11 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
return net::OK;
}

if (bugsChromium_pattern.MatchesURL(request_url)) {
if (RewriteBugReportingURL(request_url, new_url))
return net::OK;
}

return net::OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,44 @@ TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
EXPECT_EQ(redirect.path(), url.path());
EXPECT_EQ(rc, net::OK);
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
RedirectBugsChromium) {
// Check when we will redirect.
const GURL url(
"https://bugs.chromium.org/p/chromium/issues/"
"entry?template=Crash%20Report&comment=IMPORTANT%20Chrome&labels="
"Restrict-View-"
"EditIssue%2CStability-Crash%2CUser-Submitted");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);

int rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
const GURL redirect = GURL(request_info->new_url_spec);
EXPECT_EQ(redirect.host(), "github.com");
EXPECT_TRUE(redirect.SchemeIs(url::kHttpsScheme));
EXPECT_EQ(redirect.path(), "/brave/brave-browser/issues/new");
EXPECT_EQ(redirect.query(),
"title=Crash%20Report&labels=crash&body=IMPORTANT%20Brave");
EXPECT_EQ(rc, net::OK);

// Check when we should not redirect: wrong query keys count
request_info.reset();
const GURL url_fewer_keys(
"https://bugs.chromium.org/p/chromium/issues/entry?template=A");
request_info = std::make_shared<brave::BraveRequestInfo>(url_fewer_keys);
rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);

// Check when we should not redirect: wrong query keys
request_info.reset();
const GURL url_wrong_keys(
"https://bugs.chromium.org/p/chromium/issues/entry?t=A&l=B&c=C");
request_info = std::make_shared<brave::BraveRequestInfo>(url_wrong_keys);
rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);
}
10 changes: 10 additions & 0 deletions chromium_src/chrome/browser/ui/webui/crashes_ui.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/browser/version_info.h"

#define GetVersionNumber GetBraveVersionNumberForDisplay
#include "../../../../../../../chrome/browser/ui/webui/crashes_ui.cc"
#undef GetVersionNumber