Skip to content

Commit

Permalink
Merge pull request #11081 from brave/maxk-fix-tor-reset-connection-1.…
Browse files Browse the repository at this point in the history
…33.x

Fixes serialization of ProxyServer for mojo (1.33.x).
  • Loading branch information
kjozwiak authored Nov 14, 2021
2 parents 5abbaca + 320e97f commit 998bcd0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 19 deletions.
14 changes: 14 additions & 0 deletions chromium_src/net/base/BUILD.gn
Original file line number Diff line number Diff line change
@@ -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",
]
}
65 changes: 65 additions & 0 deletions chromium_src/net/base/brave_proxy_string_util_unittest.cc
Original file line number Diff line number Diff line change
@@ -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
63 changes: 44 additions & 19 deletions chromium_src/net/base/proxy_string_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(), \
Expand All @@ -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
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,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",
Expand Down

0 comments on commit 998bcd0

Please sign in to comment.