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

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: style tweaks, simplification 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
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 bool IsLocalHostname(const std::string& host) {
151 std::string lowercased_host = base::StringToLowerASCII(host);
Ryan Sleevi 2015/06/12 01:00:04 Suggestion: To reduce comparisons for the forms, a
estark 2015/06/12 05:59:46 Done.
152 return (lowercased_host == "localhost" ||
153 lowercased_host == "localhost.localdomain" ||
154 lowercased_host == "localhost." ||
155 lowercased_host == "localhost.localdomain." ||
156 IsLocalhostTLD(lowercased_host));
157 }
158
159 bool IsLocal6Hostname(const std::string& host) {
160 std::string lowercased_host = base::StringToLowerASCII(host);
161 return (lowercased_host == "localhost6" ||
162 lowercased_host == "localhost6.localdomain6" ||
163 lowercased_host == "localhost6." ||
164 lowercased_host == "localhost6.localdomain6.");
165 }
166
149 } // namespace 167 } // namespace
150 168
151 static base::LazyInstance<std::multiset<int> >::Leaky 169 static base::LazyInstance<std::multiset<int> >::Leaky
152 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER; 170 g_explicitly_allowed_ports = LAZY_INSTANCE_INITIALIZER;
153 171
154 size_t GetCountOfExplicitlyAllowedPorts() { 172 size_t GetCountOfExplicitlyAllowedPorts() {
155 return g_explicitly_allowed_ports.Get().size(); 173 return g_explicitly_allowed_ports.Get().size();
156 } 174 }
157 175
158 std::string GetSpecificHeader(const std::string& headers, 176 std::string GetSpecificHeader(const std::string& headers,
(...skipping 564 matching lines...) Expand 10 before | Expand all | Expand 10 after
723 } 741 }
724 } 742 }
725 743
726 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 744 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
727 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len); 745 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
728 if (!port_field) 746 if (!port_field)
729 return -1; 747 return -1;
730 return base::NetToHost16(*port_field); 748 return base::NetToHost16(*port_field);
731 } 749 }
732 750
751 bool ResolveLocalHostname(const std::string& host,
752 uint16_t port,
753 AddressList* address_list) {
754 const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1};
755 const unsigned char kLocalhostIPv6[] = {
756 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
Ryan Sleevi 2015/06/12 01:00:04 nit: Can make these static (so that they're in the
estark 2015/06/12 05:59:46 Done.
757
758 address_list->clear();
759
760 bool isLocal6 = IsLocal6Hostname(host);
Ryan Sleevi 2015/06/12 01:00:04 naming: is_local6
estark 2015/06/12 05:59:46 Done.
761 if (!isLocal6 && !IsLocalHostname(host))
762 return false;
763
764 address_list->push_back(
765 IPEndPoint(IPAddressNumber(kLocalhostIPv6,
766 kLocalhostIPv6 + arraysize(kLocalhostIPv6)),
767 port));
768 if (!isLocal6) {
769 address_list->push_back(
770 IPEndPoint(IPAddressNumber(kLocalhostIPv4,
771 kLocalhostIPv4 + arraysize(kLocalhostIPv4)),
772 port));
773 }
774
775 return true;
776 }
777
733 bool IsLocalhost(const std::string& host) { 778 bool IsLocalhost(const std::string& host) {
734 if (host == "localhost" || host == "localhost.localdomain" || 779 if (IsLocalHostname(host) || IsLocal6Hostname(host))
735 host == "localhost6" || host == "localhost6.localdomain6" ||
736 IsLocalhostTLD(host))
737 return true; 780 return true;
738 781
739 IPAddressNumber ip_number; 782 IPAddressNumber ip_number;
740 if (ParseIPLiteralToNumber(host, &ip_number)) { 783 if (ParseIPLiteralToNumber(host, &ip_number)) {
741 size_t size = ip_number.size(); 784 size_t size = ip_number.size();
742 switch (size) { 785 switch (size) {
743 case kIPv4AddressSize: { 786 case kIPv4AddressSize: {
744 IPAddressNumber localhost_prefix; 787 IPAddressNumber localhost_prefix;
745 localhost_prefix.push_back(127); 788 localhost_prefix.push_back(127);
746 for (int i = 0; i < 3; ++i) { 789 for (int i = 0; i < 3; ++i) {
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
798 }; 841 };
799 const std::string& host = url.host(); 842 const std::string& host = url.host();
800 for (const char* suffix : kGoogleHostSuffixes) { 843 for (const char* suffix : kGoogleHostSuffixes) {
801 if (EndsWith(host, suffix, false)) 844 if (EndsWith(host, suffix, false))
802 return true; 845 return true;
803 } 846 }
804 return false; 847 return false;
805 } 848 }
806 849
807 } // namespace net 850 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698