Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(129)

Side by Side Diff: net/base/net_util.cc

Issue 1177933002: Resolve RFC 6761 localhost names to loopback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rsleevi comments Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 27 matching lines...) Expand all
38 #include "base/lazy_instance.h" 38 #include "base/lazy_instance.h"
39 #include "base/logging.h" 39 #include "base/logging.h"
40 #include "base/strings/string_number_conversions.h" 40 #include "base/strings/string_number_conversions.h"
41 #include "base/strings/string_piece.h" 41 #include "base/strings/string_piece.h"
42 #include "base/strings/string_split.h" 42 #include "base/strings/string_split.h"
43 #include "base/strings/string_util.h" 43 #include "base/strings/string_util.h"
44 #include "base/strings/stringprintf.h" 44 #include "base/strings/stringprintf.h"
45 #include "base/strings/utf_string_conversions.h" 45 #include "base/strings/utf_string_conversions.h"
46 #include "base/sys_byteorder.h" 46 #include "base/sys_byteorder.h"
47 #include "base/values.h" 47 #include "base/values.h"
48 #include "net/base/address_list.h"
48 #include "net/base/dns_util.h" 49 #include "net/base/dns_util.h"
49 #include "net/base/ip_address_number.h" 50 #include "net/base/ip_address_number.h"
50 #include "net/base/net_module.h" 51 #include "net/base/net_module.h"
51 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 52 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
52 #include "net/grit/net_resources.h" 53 #include "net/grit/net_resources.h"
53 #include "net/http/http_content_disposition.h" 54 #include "net/http/http_content_disposition.h"
54 #include "url/gurl.h" 55 #include "url/gurl.h"
55 #include "url/third_party/mozilla/url_parse.h" 56 #include "url/third_party/mozilla/url_parse.h"
56 #include "url/url_canon.h" 57 #include "url/url_canon.h"
57 #include "url/url_canon_ip.h" 58 #include "url/url_canon_ip.h"
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 // third_party/WebKit/Source/platform/weborigin/KURL.cpp, 140 // third_party/WebKit/Source/platform/weborigin/KURL.cpp,
140 // KURL::port()) 141 // KURL::port())
141 }; 142 };
142 143
143 // FTP overrides the following restricted ports. 144 // FTP overrides the following restricted ports.
144 static const int kAllowedFtpPorts[] = { 145 static const int kAllowedFtpPorts[] = {
145 21, // ftp data 146 21, // ftp data
146 22, // ssh 147 22, // ssh
147 }; 148 };
148 149
150 std::string NormalizeHostname(const std::string& host) {
151 std::string result = base::StringToLowerASCII(host);
152 if (!result.empty() && *result.rbegin() == '.')
153 result.resize(result.size() - 1);
154 return result;
155 }
156
157 bool IsNormalizedLocalhostTLD(const std::string& host) {
Deprecated (see juliatuttle) 2015/06/16 14:53:39 Do we want to also catch hostnames like "foo.local
estark 2015/06/16 15:40:15 Hmm, I'm not sure. My preference is to stick as cl
158 return EndsWith(host, ".localhost", true);
159 }
160
161 // |host| should be normalized.
162 bool IsLocalHostname(const std::string& host) {
163 return host == "localhost" || host == "localhost.localdomain" ||
164 IsNormalizedLocalhostTLD(host);
165 }
166
167 // |host| should be normalized.
168 bool IsLocal6Hostname(const std::string& host) {
169 return host == "localhost6" || host == "localhost6.localdomain6";
170 }
171
149 } // namespace 172 } // namespace
150 173
151 static base::LazyInstance<std::multiset<int> >::Leaky 174 static base::LazyInstance<std::multiset<int> >::Leaky
152 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER; 175 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER;
153 176
154 size_t GetCountOfExplicitlyAllowedPorts() { 177 size_t GetCountOfExplicitlyAllowedPorts() {
155 return g_explicitly_allowed_ports.Get().size(); 178 return g_explicitly_allowed_ports.Get().size();
156 } 179 }
157 180
158 std::string GetSpecificHeader(const std::string& headers, 181 std::string GetSpecificHeader(const std::string& headers,
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 } 746 }
724 } 747 }
725 748
726 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 749 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
727 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len); 750 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
728 if (!port_field) 751 if (!port_field)
729 return -1; 752 return -1;
730 return base::NetToHost16(*port_field); 753 return base::NetToHost16(*port_field);
731 } 754 }
732 755
756 bool ResolveLocalHostname(const std::string& host,
757 uint16_t port,
758 AddressList* address_list) {
759 static const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1};
760 static const unsigned char kLocalhostIPv6[] = {
761 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
762
763 std::string normalized_host = NormalizeHostname(host);
764
765 address_list->clear();
766
767 bool is_local6 = IsLocal6Hostname(normalized_host);
768 if (!is_local6 && !IsLocalHostname(normalized_host))
769 return false;
770
771 address_list->push_back(
772 IPEndPoint(IPAddressNumber(kLocalhostIPv6,
773 kLocalhostIPv6 + arraysize(kLocalhostIPv6)),
774 port));
775 if (!is_local6) {
776 address_list->push_back(
777 IPEndPoint(IPAddressNumber(kLocalhostIPv4,
778 kLocalhostIPv4 + arraysize(kLocalhostIPv4)),
779 port));
780 }
781
782 return true;
783 }
784
733 bool IsLocalhost(const std::string& host) { 785 bool IsLocalhost(const std::string& host) {
734 if (host == "localhost" || host == "localhost.localdomain" || 786 std::string normalized_host = NormalizeHostname(host);
735 host == "localhost6" || host == "localhost6.localdomain6" || 787 if (IsLocalHostname(normalized_host) || IsLocal6Hostname(normalized_host))
736 IsLocalhostTLD(host))
737 return true; 788 return true;
738 789
739 IPAddressNumber ip_number; 790 IPAddressNumber ip_number;
740 if (ParseIPLiteralToNumber(host, &ip_number)) { 791 if (ParseIPLiteralToNumber(host, &ip_number)) {
741 size_t size = ip_number.size(); 792 size_t size = ip_number.size();
742 switch (size) { 793 switch (size) {
743 case kIPv4AddressSize: { 794 case kIPv4AddressSize: {
744 IPAddressNumber localhost_prefix; 795 IPAddressNumber localhost_prefix;
745 localhost_prefix.push_back(127); 796 localhost_prefix.push_back(127);
746 for (int i = 0; i < 3; ++i) { 797 for (int i = 0; i < 3; ++i) {
(...skipping 10 matching lines...) Expand all
757 808
758 default: 809 default:
759 NOTREACHED(); 810 NOTREACHED();
760 } 811 }
761 } 812 }
762 813
763 return false; 814 return false;
764 } 815 }
765 816
766 bool IsLocalhostTLD(const std::string& host) { 817 bool IsLocalhostTLD(const std::string& host) {
767 const char kLocalhostTLD[] = ".localhost"; 818 return IsNormalizedLocalhostTLD(NormalizeHostname(host));
768 const size_t kLocalhostTLDLength = arraysize(kLocalhostTLD) - 1;
769
770 if (host.empty())
771 return false;
772
773 size_t host_len = host.size();
774 if (*host.rbegin() == '.')
775 --host_len;
776 if (host_len < kLocalhostTLDLength)
777 return false;
778
779 const char* host_suffix = host.data() + host_len - kLocalhostTLDLength;
780 return base::strncasecmp(host_suffix, kLocalhostTLD, kLocalhostTLDLength) ==
781 0;
782 } 819 }
783 820
784 bool HasGoogleHost(const GURL& url) { 821 bool HasGoogleHost(const GURL& url) {
785 static const char* kGoogleHostSuffixes[] = { 822 static const char* kGoogleHostSuffixes[] = {
786 ".google.com", 823 ".google.com",
787 ".youtube.com", 824 ".youtube.com",
788 ".gmail.com", 825 ".gmail.com",
789 ".doubleclick.net", 826 ".doubleclick.net",
790 ".gstatic.com", 827 ".gstatic.com",
791 ".googlevideo.com", 828 ".googlevideo.com",
792 ".googleusercontent.com", 829 ".googleusercontent.com",
793 ".googlesyndication.com", 830 ".googlesyndication.com",
794 ".google-analytics.com", 831 ".google-analytics.com",
795 ".googleadservices.com", 832 ".googleadservices.com",
796 ".googleapis.com", 833 ".googleapis.com",
797 ".ytimg.com", 834 ".ytimg.com",
798 }; 835 };
799 const std::string& host = url.host(); 836 const std::string& host = url.host();
800 for (const char* suffix : kGoogleHostSuffixes) { 837 for (const char* suffix : kGoogleHostSuffixes) {
801 if (EndsWith(host, suffix, false)) 838 if (EndsWith(host, suffix, false))
802 return true; 839 return true;
803 } 840 }
804 return false; 841 return false;
805 } 842 }
806 843
807 } // namespace net 844 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/base/net_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698