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

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 nit 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) {
158 return base::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 std::string GetSpecificHeader(const std::string& headers, 177 std::string GetSpecificHeader(const std::string& headers,
155 const std::string& name) { 178 const std::string& name) {
156 // We want to grab the Value from the "Key: Value" pairs in the headers, 179 // We want to grab the Value from the "Key: Value" pairs in the headers,
157 // which should look like this (no leading spaces, \n-separated) (we format 180 // which should look like this (no leading spaces, \n-separated) (we format
158 // them this way in url_request_inet.cc): 181 // them this way in url_request_inet.cc):
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after
719 } 742 }
720 } 743 }
721 744
722 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) { 745 int GetPortFromSockaddr(const struct sockaddr* address, socklen_t address_len) {
723 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len); 746 const uint16_t* port_field = GetPortFieldFromSockaddr(address, address_len);
724 if (!port_field) 747 if (!port_field)
725 return -1; 748 return -1;
726 return base::NetToHost16(*port_field); 749 return base::NetToHost16(*port_field);
727 } 750 }
728 751
752 bool ResolveLocalHostname(const std::string& host,
753 uint16_t port,
754 AddressList* address_list) {
755 static const unsigned char kLocalhostIPv4[] = {127, 0, 0, 1};
756 static const unsigned char kLocalhostIPv6[] = {
757 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
758
759 std::string normalized_host = NormalizeHostname(host);
760
761 address_list->clear();
762
763 bool is_local6 = IsLocal6Hostname(normalized_host);
764 if (!is_local6 && !IsLocalHostname(normalized_host))
765 return false;
766
767 address_list->push_back(
768 IPEndPoint(IPAddressNumber(kLocalhostIPv6,
769 kLocalhostIPv6 + arraysize(kLocalhostIPv6)),
770 port));
771 if (!is_local6) {
772 address_list->push_back(
773 IPEndPoint(IPAddressNumber(kLocalhostIPv4,
774 kLocalhostIPv4 + arraysize(kLocalhostIPv4)),
775 port));
776 }
777
778 return true;
779 }
780
729 bool IsLocalhost(const std::string& host) { 781 bool IsLocalhost(const std::string& host) {
730 if (host == "localhost" || host == "localhost.localdomain" || 782 std::string normalized_host = NormalizeHostname(host);
731 host == "localhost6" || host == "localhost6.localdomain6" || 783 if (IsLocalHostname(normalized_host) || IsLocal6Hostname(normalized_host))
732 IsLocalhostTLD(host))
733 return true; 784 return true;
734 785
735 IPAddressNumber ip_number; 786 IPAddressNumber ip_number;
736 if (ParseIPLiteralToNumber(host, &ip_number)) { 787 if (ParseIPLiteralToNumber(host, &ip_number)) {
737 size_t size = ip_number.size(); 788 size_t size = ip_number.size();
738 switch (size) { 789 switch (size) {
739 case kIPv4AddressSize: { 790 case kIPv4AddressSize: {
740 IPAddressNumber localhost_prefix; 791 IPAddressNumber localhost_prefix;
741 localhost_prefix.push_back(127); 792 localhost_prefix.push_back(127);
742 for (int i = 0; i < 3; ++i) { 793 for (int i = 0; i < 3; ++i) {
(...skipping 10 matching lines...) Expand all
753 804
754 default: 805 default:
755 NOTREACHED(); 806 NOTREACHED();
756 } 807 }
757 } 808 }
758 809
759 return false; 810 return false;
760 } 811 }
761 812
762 bool IsLocalhostTLD(const std::string& host) { 813 bool IsLocalhostTLD(const std::string& host) {
763 const char kLocalhostTLD[] = ".localhost"; 814 return IsNormalizedLocalhostTLD(NormalizeHostname(host));
764 const size_t kLocalhostTLDLength = arraysize(kLocalhostTLD) - 1;
765
766 if (host.empty())
767 return false;
768
769 size_t host_len = host.size();
770 if (*host.rbegin() == '.')
771 --host_len;
772 if (host_len < kLocalhostTLDLength)
773 return false;
774
775 const char* host_suffix = host.data() + host_len - kLocalhostTLDLength;
776 return base::strncasecmp(host_suffix, kLocalhostTLD, kLocalhostTLDLength) ==
777 0;
778 } 815 }
779 816
780 bool HasGoogleHost(const GURL& url) { 817 bool HasGoogleHost(const GURL& url) {
781 static const char* kGoogleHostSuffixes[] = { 818 static const char* kGoogleHostSuffixes[] = {
782 ".google.com", 819 ".google.com",
783 ".youtube.com", 820 ".youtube.com",
784 ".gmail.com", 821 ".gmail.com",
785 ".doubleclick.net", 822 ".doubleclick.net",
786 ".gstatic.com", 823 ".gstatic.com",
787 ".googlevideo.com", 824 ".googlevideo.com",
788 ".googleusercontent.com", 825 ".googleusercontent.com",
789 ".googlesyndication.com", 826 ".googlesyndication.com",
790 ".google-analytics.com", 827 ".google-analytics.com",
791 ".googleadservices.com", 828 ".googleadservices.com",
792 ".googleapis.com", 829 ".googleapis.com",
793 ".ytimg.com", 830 ".ytimg.com",
794 }; 831 };
795 const std::string& host = url.host(); 832 const std::string& host = url.host();
796 for (const char* suffix : kGoogleHostSuffixes) { 833 for (const char* suffix : kGoogleHostSuffixes) {
797 if (base::EndsWith(host, suffix, false)) 834 if (base::EndsWith(host, suffix, false))
798 return true; 835 return true;
799 } 836 }
800 return false; 837 return false;
801 } 838 }
802 839
803 } // namespace net 840 } // 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