blob: 42261d69b1bc6569e00235865b077106df4af5e9 [file] [log] [blame]
dalyk272d4b02019-08-14 00:37:511// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/net/dns_util.h"
6
Steven Binglerbeb5dbc2019-08-23 23:21:457#include "build/build_config.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/policy/chrome_browser_policy_connector.h"
dalyk272d4b02019-08-14 00:37:5110#include "net/third_party/uri_template/uri_template.h"
11#include "url/gurl.h"
12
dalyk146abc12019-08-29 01:06:2113#if defined(OS_WIN)
14#include "base/enterprise_util.h"
15#endif
16
Steven Binglerdd2307b2019-08-27 20:44:3617namespace chrome_browser_net {
18
19bool IsValidDohTemplate(const std::string& server_template,
dalyk272d4b02019-08-14 00:37:5120 std::string* server_method) {
21 std::string url_string;
22 std::string test_query = "this_is_a_test_query";
23 std::unordered_map<std::string, std::string> template_params(
24 {{"dns", test_query}});
25 std::set<std::string> vars_found;
26 bool valid_template = uri_template::Expand(server_template, template_params,
27 &url_string, &vars_found);
28 if (!valid_template) {
29 // The URI template is malformed.
30 return false;
31 }
32 GURL url(url_string);
33 if (!url.is_valid() || !url.SchemeIs("https")) {
34 // The expanded template must be a valid HTTPS URL.
35 return false;
36 }
37 if (url.host().find(test_query) != std::string::npos) {
38 // The dns variable may not be part of the hostname.
39 return false;
40 }
41 // If the template contains a dns variable, use GET, otherwise use POST.
42 DCHECK(server_method);
43 *server_method =
44 (vars_found.find("dns") == vars_found.end()) ? "POST" : "GET";
45 return true;
46}
Steven Binglerbeb5dbc2019-08-23 23:21:4547
48bool ShouldDisableDohForManaged() {
49#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
dalyk146abc12019-08-29 01:06:2150 if (g_browser_process->browser_policy_connector()->HasMachineLevelPolicies())
51 return true;
52#endif
53#if defined(OS_WIN)
54 if (base::IsMachineExternallyManaged())
55 return true;
Steven Binglerbeb5dbc2019-08-23 23:21:4556#endif
57 return false;
58}
Steven Binglerdd2307b2019-08-27 20:44:3659} // namespace chrome_browser_net