From 79d2b9e89a2a8333503141edc79a3bc8b8683720 Mon Sep 17 00:00:00 2001 From: Max <41635752+mkarolin@users.noreply.github.com> Date: Sat, 13 Nov 2021 18:09:20 -0500 Subject: [PATCH] Merge pull request #11028 from brave/maxk-fix-tor-reset-connection Fixes serialization of ProxyServer for mojo. --- chromium_src/net/base/BUILD.gn | 14 ++++ .../base/brave_proxy_string_util_unittest.cc | 65 +++++++++++++++++++ chromium_src/net/base/proxy_string_util.cc | 63 ++++++++++++------ test/BUILD.gn | 1 + 4 files changed, 124 insertions(+), 19 deletions(-) create mode 100644 chromium_src/net/base/BUILD.gn create mode 100644 chromium_src/net/base/brave_proxy_string_util_unittest.cc diff --git a/chromium_src/net/base/BUILD.gn b/chromium_src/net/base/BUILD.gn new file mode 100644 index 000000000000..a4483dd11ca7 --- /dev/null +++ b/chromium_src/net/base/BUILD.gn @@ -0,0 +1,14 @@ +# Copyright 2021 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/. + +source_set("unit_tests") { + testonly = true + sources = [ "brave_proxy_string_util_unittest.cc" ] + + deps = [ + "//net", + "//testing/gtest:gtest", + ] +} diff --git a/chromium_src/net/base/brave_proxy_string_util_unittest.cc b/chromium_src/net/base/brave_proxy_string_util_unittest.cc new file mode 100644 index 000000000000..2d8594c5ff92 --- /dev/null +++ b/chromium_src/net/base/brave_proxy_string_util_unittest.cc @@ -0,0 +1,65 @@ +/* Copyright (c) 2021 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 "net/base/proxy_string_util.h" + +#include "net/base/proxy_server.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace net { + +TEST(BraveProxySpecificationUtilTest, ProxyUriWithAuthToProxyServer) { + const struct { + const char* const input_uri; + const char* const expected_uri; + ProxyServer::Scheme expected_scheme; + const char* const expected_host; + int expected_port; + const char* const expected_username; + const char* const expected_password; + const char* const expected_pac_string; + } tests[] = { + {"socks5://foo:bar@foopy", // No port. + "socks5://foo:bar@foopy:1080", ProxyServer::SCHEME_SOCKS5, "foopy", 1080, + "foo", "bar", "SOCKS5 foo:bar@foopy:1080"}, + {"socks5://baz:qux@foopy:10", "socks5://baz:qux@foopy:10", + ProxyServer::SCHEME_SOCKS5, "foopy", 10, "baz", "qux", + "SOCKS5 baz:qux@foopy:10"}, + }; + + for (const auto& test : tests) { + ProxyServer uri = + ProxyUriToProxyServer(test.input_uri, ProxyServer::SCHEME_HTTP); + EXPECT_TRUE(uri.is_valid()); + EXPECT_FALSE(uri.is_direct()); + EXPECT_EQ(test.expected_uri, ProxyServerToProxyUri(uri)); + EXPECT_EQ(test.expected_scheme, uri.scheme()); + EXPECT_EQ(test.expected_host, uri.host_port_pair().host()); + EXPECT_EQ(test.expected_port, uri.host_port_pair().port()); + EXPECT_EQ(test.expected_username, uri.host_port_pair().username()); + EXPECT_EQ(test.expected_password, uri.host_port_pair().password()); + EXPECT_EQ(test.expected_pac_string, ProxyServerToPacResultElement(uri)); + } +} + +TEST(BraveProxySpecificationUtilTest, PacResultElementWithAuthToProxyServer) { + const struct { + const char* const input_pac; + const char* const expected_uri; + } tests[] = { + { + "PROXY foo:bar@foopy:10", + "foo:bar@foopy:10", + }, + }; + + for (const auto& test : tests) { + ProxyServer uri = PacResultElementToProxyServer(test.input_pac); + EXPECT_TRUE(uri.is_valid()); + EXPECT_EQ(test.expected_uri, ProxyServerToProxyUri(uri)); + } +} + +} // namespace net diff --git a/chromium_src/net/base/proxy_string_util.cc b/chromium_src/net/base/proxy_string_util.cc index e5ef036d684d..58cac3d15127 100644 --- a/chromium_src/net/base/proxy_string_util.cc +++ b/chromium_src/net/base/proxy_string_util.cc @@ -45,41 +45,31 @@ ProxyServer CreateProxyServerWithAuthInfo( host_and_port.substr(password_component.begin, password_component.len)}); } - auth_hostname = auth_str + "@" + auth_hostname; + auth_hostname = base::StrCat({auth_str, "@", auth_hostname}); } return ProxyServer::FromSchemeHostAndPort(scheme, auth_hostname, port); } -} // namespace - -std::string ProxyServerToProxyUri(const ProxyServer& proxy_server) { - std::string scheme_prefix; - std::string proxy_uri = ProxyServerToProxyUri_ChromiumImpl(proxy_server); - size_t colon = proxy_uri.find(':'); - if (colon != std::string::npos && proxy_uri.size() - colon >= 3 && - proxy_uri[colon + 1] == '/' && proxy_uri[colon + 2] == '/') { - scheme_prefix = proxy_uri.substr(0, colon + 3); - proxy_uri = proxy_uri.substr(colon + 3); // Skip past the "://" - } - +std::string GetProxyServerAuthString(const ProxyServer& proxy_server) { std::string auth_string; const HostPortPair& host_port_pair = proxy_server.host_port_pair(); if (!host_port_pair.username().empty()) { - auth_string += host_port_pair.username(); + auth_string = host_port_pair.username(); if (!host_port_pair.password().empty()) { - auth_string += ':' + host_port_pair.password(); + base::StrAppend(&auth_string, {":", host_port_pair.password()}); } - auth_string += '@'; + base::StrAppend(&auth_string, {"@"}); } - - std::string result = scheme_prefix + auth_string + proxy_uri; - return result; + return auth_string; } +} // namespace + } // namespace net #define ProxyServerToProxyUri ProxyServerToProxyUri_ChromiumImpl +#define ProxyServerToPacResultElement ProxyServerToPacResultElement_ChromiumImpl #define ParseAuthority(HOST_AND_PORT, AUTH, USER, PASS, HOST, PORT) \ ParseAuthority(host_and_port.data(), \ @@ -94,4 +84,39 @@ std::string ProxyServerToProxyUri(const ProxyServer& proxy_server) { #include "../../../../net/base/proxy_string_util.cc" #undef ParseAuthority +#undef ProxyServerToPacResultElement #undef ProxyServerToProxyUri + +namespace net { + +std::string ProxyServerToProxyUri(const ProxyServer& proxy_server) { + std::string scheme_prefix; + std::string proxy_uri = ProxyServerToProxyUri_ChromiumImpl(proxy_server); + size_t colon = proxy_uri.find(':'); + if (colon != std::string::npos && proxy_uri.size() - colon >= 3 && + proxy_uri[colon + 1] == '/' && proxy_uri[colon + 2] == '/') { + scheme_prefix = proxy_uri.substr(0, colon + 3); + proxy_uri = proxy_uri.substr(colon + 3); // Skip past the "://" + } + + std::string result = base::StrCat( + {scheme_prefix, GetProxyServerAuthString(proxy_server), proxy_uri}); + return result; +} + +std::string ProxyServerToPacResultElement(const ProxyServer& proxy_server) { + std::string scheme_prefix; + std::string proxy_pac = + ProxyServerToPacResultElement_ChromiumImpl(proxy_server); + size_t space = proxy_pac.find(' '); + if (space != std::string::npos && proxy_pac.size() - space >= 1) { + scheme_prefix = proxy_pac.substr(0, space + 1); + proxy_pac = proxy_pac.substr(space + 1); // Skip past the space + } + + std::string result = base::StrCat( + {scheme_prefix, GetProxyServerAuthString(proxy_server), proxy_pac}); + return result; +} + +} // namespace net diff --git a/test/BUILD.gn b/test/BUILD.gn index 3f3c613103db..cc4755e04473 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -178,6 +178,7 @@ test("brave_unit_tests") { "//brave/browser/widevine:unittest", "//brave/chromium_src/chrome/browser/privacy_sandbox:unit_tests", "//brave/chromium_src/components/autofill_assistant/browser:unit_tests", + "//brave/chromium_src/net/base:unit_tests", "//brave/common:network_constants", "//brave/common:pref_names", "//brave/components/adblock_rust_ffi",