Commit dd2307b8 authored by Steven Bingler's avatar Steven Bingler Committed by Commit Bot

Clean up dns_utils.

Add all members to chrome_browser_net namespace.
Change IsValidDohTemplate() name to be DoH specific.

Change-Id: I68cf5d2ce3a987b2e0f000ebd125a4ef490d7237
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1764899
Commit-Queue: Steven Bingler <bingler@chromium.org>
Reviewed-by: default avatarEric Orth <ericorth@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690874}
parent 9aea3a28
...@@ -14,7 +14,9 @@ ...@@ -14,7 +14,9 @@
#include "base/enterprise_util.h" #include "base/enterprise_util.h"
#endif #endif
bool IsValidDoHTemplate(const std::string& server_template, namespace chrome_browser_net {
bool IsValidDohTemplate(const std::string& server_template,
std::string* server_method) { std::string* server_method) {
std::string url_string; std::string url_string;
std::string test_query = "this_is_a_test_query"; std::string test_query = "this_is_a_test_query";
...@@ -54,3 +56,4 @@ bool ShouldDisableDohForManaged() { ...@@ -54,3 +56,4 @@ bool ShouldDisableDohForManaged() {
#endif #endif
return false; return false;
} }
} // namespace chrome_browser_net
...@@ -7,11 +7,13 @@ ...@@ -7,11 +7,13 @@
#include <string> #include <string>
namespace chrome_browser_net {
// Returns true if the URI template is acceptable for sending requests. If so, // Returns true if the URI template is acceptable for sending requests. If so,
// the |server_method| is set to "GET" if the template contains a "dns" variable // the |server_method| is set to "GET" if the template contains a "dns" variable
// and to "POST" otherwise. Any "dns" variable may not be part of the hostname, // and to "POST" otherwise. Any "dns" variable may not be part of the hostname,
// and the expanded template must parse to a valid HTTPS URL. // and the expanded template must parse to a valid HTTPS URL.
bool IsValidDoHTemplate(const std::string& server_template, bool IsValidDohTemplate(const std::string& server_template,
std::string* server_method); std::string* server_method);
// Returns true if there are any active machine level policies or if the machine // Returns true if there are any active machine level policies or if the machine
...@@ -25,4 +27,6 @@ const char kDnsOverHttpsModeOff[] = "off"; ...@@ -25,4 +27,6 @@ const char kDnsOverHttpsModeOff[] = "off";
const char kDnsOverHttpsModeAutomatic[] = "automatic"; const char kDnsOverHttpsModeAutomatic[] = "automatic";
const char kDnsOverHttpsModeSecure[] = "secure"; const char kDnsOverHttpsModeSecure[] = "secure";
} // namespace chrome_browser_net
#endif // CHROME_BROWSER_NET_DNS_UTIL_H_ #endif // CHROME_BROWSER_NET_DNS_UTIL_H_
...@@ -6,43 +6,44 @@ ...@@ -6,43 +6,44 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
TEST(NetDnsUtilTest, IsValidDoHTemplate) { TEST(NetDnsUtilTest, IsValidDohTemplate) {
std::string server_method; std::string server_method;
EXPECT_TRUE(IsValidDoHTemplate( EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
"https://dnsserver.example.net/dns-query{?dns}", &server_method)); "https://dnsserver.example.net/dns-query{?dns}", &server_method));
EXPECT_EQ("GET", server_method); EXPECT_EQ("GET", server_method);
EXPECT_TRUE(IsValidDoHTemplate( EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
"https://dnsserver.example.net/dns-query{?dns,extra}", &server_method)); "https://dnsserver.example.net/dns-query{?dns,extra}", &server_method));
EXPECT_EQ("GET", server_method); EXPECT_EQ("GET", server_method);
EXPECT_TRUE(IsValidDoHTemplate( EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
"https://dnsserver.example.net/dns-query{?query}", &server_method)); "https://dnsserver.example.net/dns-query{?query}", &server_method));
EXPECT_EQ("POST", server_method); EXPECT_EQ("POST", server_method);
EXPECT_TRUE(IsValidDoHTemplate("https://dnsserver.example.net/dns-query", EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
&server_method)); "https://dnsserver.example.net/dns-query", &server_method));
EXPECT_EQ("POST", server_method); EXPECT_EQ("POST", server_method);
EXPECT_TRUE(IsValidDoHTemplate("https://query:{dns}@dnsserver.example.net", EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
&server_method)); "https://query:{dns}@dnsserver.example.net", &server_method));
EXPECT_EQ("GET", server_method); EXPECT_EQ("GET", server_method);
EXPECT_TRUE(IsValidDoHTemplate("https://dnsserver.example.net{/dns}", EXPECT_TRUE(chrome_browser_net::IsValidDohTemplate(
&server_method)); "https://dnsserver.example.net{/dns}", &server_method));
EXPECT_EQ("GET", server_method); EXPECT_EQ("GET", server_method);
// Invalid template format // Invalid template format
EXPECT_FALSE(IsValidDoHTemplate( EXPECT_FALSE(chrome_browser_net::IsValidDohTemplate(
"https://dnsserver.example.net/dns-query{{?dns}}", &server_method)); "https://dnsserver.example.net/dns-query{{?dns}}", &server_method));
// Must be HTTPS // Must be HTTPS
EXPECT_FALSE(IsValidDoHTemplate("http://dnsserver.example.net/dns-query", EXPECT_FALSE(chrome_browser_net::IsValidDohTemplate(
&server_method)); "http://dnsserver.example.net/dns-query", &server_method));
EXPECT_FALSE(IsValidDoHTemplate( EXPECT_FALSE(chrome_browser_net::IsValidDohTemplate(
"http://dnsserver.example.net/dns-query{?dns}", &server_method)); "http://dnsserver.example.net/dns-query{?dns}", &server_method));
// Template must expand to a valid URL // Template must expand to a valid URL
EXPECT_FALSE(IsValidDoHTemplate("https://{?dns}", &server_method));
// The hostname must not contain the dns variable
EXPECT_FALSE( EXPECT_FALSE(
IsValidDoHTemplate("https://{dns}.dnsserver.net", &server_method)); chrome_browser_net::IsValidDohTemplate("https://{?dns}", &server_method));
// The hostname must not contain the dns variable
EXPECT_FALSE(chrome_browser_net::IsValidDohTemplate(
"https://{dns}.dnsserver.net", &server_method));
} }
...@@ -35,11 +35,11 @@ bool SecureDnsPolicyHandler::CheckPolicySettings(const PolicyMap& policies, ...@@ -35,11 +35,11 @@ bool SecureDnsPolicyHandler::CheckPolicySettings(const PolicyMap& policies,
} else if (mode_str.size() == 0) { } else if (mode_str.size() == 0) {
errors->AddError(key::kDnsOverHttpsMode, IDS_POLICY_NOT_SPECIFIED_ERROR); errors->AddError(key::kDnsOverHttpsMode, IDS_POLICY_NOT_SPECIFIED_ERROR);
return false; return false;
} else if (mode_str == kDnsOverHttpsModeSecure) { } else if (mode_str == chrome_browser_net::kDnsOverHttpsModeSecure) {
errors->AddError(key::kDnsOverHttpsMode, errors->AddError(key::kDnsOverHttpsMode,
IDS_POLICY_SECURE_DNS_MODE_NOT_SUPPORTED_ERROR); IDS_POLICY_SECURE_DNS_MODE_NOT_SUPPORTED_ERROR);
} else if (mode_str != kDnsOverHttpsModeOff && } else if (mode_str != chrome_browser_net::kDnsOverHttpsModeOff &&
mode_str != kDnsOverHttpsModeAutomatic) { mode_str != chrome_browser_net::kDnsOverHttpsModeAutomatic) {
errors->AddError(key::kDnsOverHttpsMode, errors->AddError(key::kDnsOverHttpsMode,
IDS_POLICY_INVALID_SECURE_DNS_MODE_ERROR); IDS_POLICY_INVALID_SECURE_DNS_MODE_ERROR);
return false; return false;
...@@ -56,11 +56,12 @@ void SecureDnsPolicyHandler::ApplyPolicySettings(const PolicyMap& policies, ...@@ -56,11 +56,12 @@ void SecureDnsPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
std::string mode_str = mode->GetString(); std::string mode_str = mode->GetString();
// TODO(http://crbug.com/955454): Include secure in conditional when // TODO(http://crbug.com/955454): Include secure in conditional when
// support is implemented. // support is implemented.
if (mode_str == kDnsOverHttpsModeAutomatic) { if (mode_str == chrome_browser_net::kDnsOverHttpsModeAutomatic) {
prefs->SetString(prefs::kDnsOverHttpsMode, mode_str); prefs->SetString(prefs::kDnsOverHttpsMode, mode_str);
} else { } else {
// Captures "off" and "secure". // Captures "off" and "secure".
prefs->SetString(prefs::kDnsOverHttpsMode, kDnsOverHttpsModeOff); prefs->SetString(prefs::kDnsOverHttpsMode,
chrome_browser_net::kDnsOverHttpsModeOff);
} }
} }
......
...@@ -127,7 +127,8 @@ TEST_F(SecureDnsPolicyHandlerTest, PolicyValueSecureShouldError) { ...@@ -127,7 +127,8 @@ TEST_F(SecureDnsPolicyHandlerTest, PolicyValueSecureShouldError) {
// Secure will eventually be a valid option, but for the moment it should // Secure will eventually be a valid option, but for the moment it should
// error. // error.
SetPolicyValue(key::kDnsOverHttpsMode, SetPolicyValue(key::kDnsOverHttpsMode,
std::make_unique<base::Value>(kDnsOverHttpsModeSecure)); std::make_unique<base::Value>(
chrome_browser_net::kDnsOverHttpsModeSecure));
CheckAndApplyPolicySettings(); CheckAndApplyPolicySettings();
...@@ -140,11 +141,12 @@ TEST_F(SecureDnsPolicyHandlerTest, PolicyValueSecureShouldError) { ...@@ -140,11 +141,12 @@ TEST_F(SecureDnsPolicyHandlerTest, PolicyValueSecureShouldError) {
std::string mode; std::string mode;
EXPECT_TRUE(prefs().GetString(prefs::kDnsOverHttpsMode, &mode)); EXPECT_TRUE(prefs().GetString(prefs::kDnsOverHttpsMode, &mode));
// Pref should have changed to "off." // Pref should have changed to "off."
EXPECT_EQ(mode, kDnsOverHttpsModeOff); EXPECT_EQ(mode, chrome_browser_net::kDnsOverHttpsModeOff);
} }
TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueOff) { TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueOff) {
const std::string test_policy_value = kDnsOverHttpsModeOff; const std::string test_policy_value =
chrome_browser_net::kDnsOverHttpsModeOff;
SetPolicyValue(key::kDnsOverHttpsMode, SetPolicyValue(key::kDnsOverHttpsMode,
std::make_unique<base::Value>(test_policy_value)); std::make_unique<base::Value>(test_policy_value));
...@@ -161,7 +163,8 @@ TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueOff) { ...@@ -161,7 +163,8 @@ TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueOff) {
} }
TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueAutomatic) { TEST_F(SecureDnsPolicyHandlerTest, ValidPolicyValueAutomatic) {
const std::string test_policy_value = kDnsOverHttpsModeAutomatic; const std::string test_policy_value =
chrome_browser_net::kDnsOverHttpsModeAutomatic;
SetPolicyValue(key::kDnsOverHttpsMode, SetPolicyValue(key::kDnsOverHttpsMode,
std::make_unique<base::Value>(test_policy_value)); std::make_unique<base::Value>(test_policy_value));
......
...@@ -96,14 +96,14 @@ void GetStubResolverConfig( ...@@ -96,14 +96,14 @@ void GetStubResolverConfig(
std::string doh_mode; std::string doh_mode;
if (!local_state->FindPreference(prefs::kDnsOverHttpsMode)->IsManaged() && if (!local_state->FindPreference(prefs::kDnsOverHttpsMode)->IsManaged() &&
ShouldDisableDohForManaged()) chrome_browser_net::ShouldDisableDohForManaged())
doh_mode = kDnsOverHttpsModeOff; doh_mode = chrome_browser_net::kDnsOverHttpsModeOff;
else else
doh_mode = local_state->GetString(prefs::kDnsOverHttpsMode); doh_mode = local_state->GetString(prefs::kDnsOverHttpsMode);
if (doh_mode == kDnsOverHttpsModeSecure) if (doh_mode == chrome_browser_net::kDnsOverHttpsModeSecure)
*secure_dns_mode = net::DnsConfig::SecureDnsMode::SECURE; *secure_dns_mode = net::DnsConfig::SecureDnsMode::SECURE;
else if (doh_mode == kDnsOverHttpsModeAutomatic) else if (doh_mode == chrome_browser_net::kDnsOverHttpsModeAutomatic)
*secure_dns_mode = net::DnsConfig::SecureDnsMode::AUTOMATIC; *secure_dns_mode = net::DnsConfig::SecureDnsMode::AUTOMATIC;
else else
*secure_dns_mode = net::DnsConfig::SecureDnsMode::OFF; *secure_dns_mode = net::DnsConfig::SecureDnsMode::OFF;
...@@ -116,7 +116,8 @@ void GetStubResolverConfig( ...@@ -116,7 +116,8 @@ void GetStubResolverConfig(
for (const std::string& server_template : for (const std::string& server_template :
SplitString(doh_templates, " ", base::TRIM_WHITESPACE, SplitString(doh_templates, " ", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY)) { base::SPLIT_WANT_NONEMPTY)) {
if (!IsValidDoHTemplate(server_template, &server_method)) { if (!chrome_browser_net::IsValidDohTemplate(server_template,
&server_method)) {
continue; continue;
} }
...@@ -379,13 +380,13 @@ SystemNetworkContextManager::SystemNetworkContextManager( ...@@ -379,13 +380,13 @@ SystemNetworkContextManager::SystemNetworkContextManager(
// features before registering change callbacks for these preferences. // features before registering change callbacks for these preferences.
local_state_->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled, local_state_->SetDefaultPrefValue(prefs::kBuiltInDnsClientEnabled,
base::Value(ShouldEnableAsyncDns())); base::Value(ShouldEnableAsyncDns()));
std::string default_doh_mode = kDnsOverHttpsModeOff; std::string default_doh_mode = chrome_browser_net::kDnsOverHttpsModeOff;
std::string default_doh_templates = ""; std::string default_doh_templates = "";
if (base::FeatureList::IsEnabled(features::kDnsOverHttps)) { if (base::FeatureList::IsEnabled(features::kDnsOverHttps)) {
if (features::kDnsOverHttpsFallbackParam.Get()) { if (features::kDnsOverHttpsFallbackParam.Get()) {
default_doh_mode = kDnsOverHttpsModeAutomatic; default_doh_mode = chrome_browser_net::kDnsOverHttpsModeAutomatic;
} else { } else {
default_doh_mode = kDnsOverHttpsModeSecure; default_doh_mode = chrome_browser_net::kDnsOverHttpsModeSecure;
} }
default_doh_templates = features::kDnsOverHttpsTemplatesParam.Get(); default_doh_templates = features::kDnsOverHttpsTemplatesParam.Get();
} }
...@@ -404,10 +405,11 @@ SystemNetworkContextManager::SystemNetworkContextManager( ...@@ -404,10 +405,11 @@ SystemNetworkContextManager::SystemNetworkContextManager(
if (entries.count("dns-over-https@1")) { if (entries.count("dns-over-https@1")) {
// The user has "Enabled" selected. // The user has "Enabled" selected.
local_state_->SetString(prefs::kDnsOverHttpsMode, local_state_->SetString(prefs::kDnsOverHttpsMode,
kDnsOverHttpsModeAutomatic); chrome_browser_net::kDnsOverHttpsModeAutomatic);
} else if (entries.count("dns-over-https@2")) { } else if (entries.count("dns-over-https@2")) {
// The user has "Disabled" selected. // The user has "Disabled" selected.
local_state_->SetString(prefs::kDnsOverHttpsMode, kDnsOverHttpsModeOff); local_state_->SetString(prefs::kDnsOverHttpsMode,
chrome_browser_net::kDnsOverHttpsModeOff);
} else { } else {
// The user has "Default" selected. // The user has "Default" selected.
local_state_->ClearPref(prefs::kDnsOverHttpsMode); local_state_->ClearPref(prefs::kDnsOverHttpsMode);
......
...@@ -66,7 +66,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) { ...@@ -66,7 +66,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) {
" " + good_get_template + " " + good_post_template + " "; " " + good_get_template + " " + good_post_template + " ";
PrefService* local_state = g_browser_process->local_state(); PrefService* local_state = g_browser_process->local_state();
local_state->SetString(prefs::kDnsOverHttpsMode, kDnsOverHttpsModeSecure); local_state->SetString(prefs::kDnsOverHttpsMode,
chrome_browser_net::kDnsOverHttpsModeSecure);
local_state->SetString(prefs::kDnsOverHttpsTemplates, bad_template); local_state->SetString(prefs::kDnsOverHttpsTemplates, bad_template);
GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode, GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode,
&dns_over_https_servers); &dns_over_https_servers);
...@@ -84,7 +85,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) { ...@@ -84,7 +85,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) {
EXPECT_EQ(good_post_template, dns_over_https_servers->at(0)->server_template); EXPECT_EQ(good_post_template, dns_over_https_servers->at(0)->server_template);
EXPECT_EQ(true, dns_over_https_servers->at(0)->use_post); EXPECT_EQ(true, dns_over_https_servers->at(0)->use_post);
local_state->SetString(prefs::kDnsOverHttpsMode, kDnsOverHttpsModeAutomatic); local_state->SetString(prefs::kDnsOverHttpsMode,
chrome_browser_net::kDnsOverHttpsModeAutomatic);
local_state->SetString(prefs::kDnsOverHttpsTemplates, bad_template); local_state->SetString(prefs::kDnsOverHttpsTemplates, bad_template);
GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode, GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode,
&dns_over_https_servers); &dns_over_https_servers);
...@@ -125,7 +127,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) { ...@@ -125,7 +127,8 @@ void RunStubResolverConfigTests(bool async_dns_feature_enabled) {
EXPECT_EQ(good_post_template, dns_over_https_servers->at(1)->server_template); EXPECT_EQ(good_post_template, dns_over_https_servers->at(1)->server_template);
EXPECT_TRUE(dns_over_https_servers->at(1)->use_post); EXPECT_TRUE(dns_over_https_servers->at(1)->use_post);
local_state->SetString(prefs::kDnsOverHttpsMode, kDnsOverHttpsModeOff); local_state->SetString(prefs::kDnsOverHttpsMode,
chrome_browser_net::kDnsOverHttpsModeOff);
local_state->SetString(prefs::kDnsOverHttpsTemplates, good_get_template); local_state->SetString(prefs::kDnsOverHttpsTemplates, good_get_template);
GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode, GetStubResolverConfig(&insecure_stub_resolver_enabled, &secure_dns_mode,
&dns_over_https_servers); &dns_over_https_servers);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment