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

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

Issue 5519015: Explicitly whitelist the test server port. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « net/base/net_util.h ('k') | net/test/test_server.h » ('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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <unicode/regex.h> 9 #include <unicode/regex.h>
10 #include <unicode/ucnv.h> 10 #include <unicode/ucnv.h>
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } // namespace 1045 } // namespace
1046 1046
1047 const FormatUrlType kFormatUrlOmitNothing = 0; 1047 const FormatUrlType kFormatUrlOmitNothing = 0;
1048 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0; 1048 const FormatUrlType kFormatUrlOmitUsernamePassword = 1 << 0;
1049 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1; 1049 const FormatUrlType kFormatUrlOmitHTTP = 1 << 1;
1050 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2; 1050 const FormatUrlType kFormatUrlOmitTrailingSlashOnBareHostname = 1 << 2;
1051 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword | 1051 const FormatUrlType kFormatUrlOmitAll = kFormatUrlOmitUsernamePassword |
1052 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname; 1052 kFormatUrlOmitHTTP | kFormatUrlOmitTrailingSlashOnBareHostname;
1053 1053
1054 // TODO(viettrungluu): We don't want non-POD globals; change this. 1054 // TODO(viettrungluu): We don't want non-POD globals; change this.
1055 std::set<int> explicitly_allowed_ports; 1055 std::multiset<int> explicitly_allowed_ports;
1056 1056
1057 GURL FilePathToFileURL(const FilePath& path) { 1057 GURL FilePathToFileURL(const FilePath& path) {
1058 // Produce a URL like "file:///C:/foo" for a regular file, or 1058 // Produce a URL like "file:///C:/foo" for a regular file, or
1059 // "file://///server/path" for UNC. The URL canonicalizer will fix up the 1059 // "file://///server/path" for UNC. The URL canonicalizer will fix up the
1060 // latter case to be the canonical UNC form: "file://server/path" 1060 // latter case to be the canonical UNC form: "file://server/path"
1061 FilePath::StringType url_string(kFileURLPrefix); 1061 FilePath::StringType url_string(kFileURLPrefix);
1062 url_string.append(path.value()); 1062 url_string.append(path.value());
1063 1063
1064 // Now do replacement of some characters. Since we assume the input is a 1064 // Now do replacement of some characters. Since we assume the input is a
1065 // literal filename, anything the URL parser might consider special should 1065 // literal filename, anything the URL parser might consider special should
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 } 1489 }
1490 } 1490 }
1491 // Port not explicitly allowed by FTP, so return the default restrictions. 1491 // Port not explicitly allowed by FTP, so return the default restrictions.
1492 return IsPortAllowedByDefault(port); 1492 return IsPortAllowedByDefault(port);
1493 } 1493 }
1494 1494
1495 bool IsPortAllowedByOverride(int port) { 1495 bool IsPortAllowedByOverride(int port) {
1496 if (explicitly_allowed_ports.empty()) 1496 if (explicitly_allowed_ports.empty())
1497 return false; 1497 return false;
1498 1498
1499 std::set<int>::const_iterator it = 1499 return explicitly_allowed_ports.count(port) > 0;
1500 std::find(explicitly_allowed_ports.begin(),
1501 explicitly_allowed_ports.end(),
1502 port);
1503
1504 return it != explicitly_allowed_ports.end();
1505 } 1500 }
1506 1501
1507 int SetNonBlocking(int fd) { 1502 int SetNonBlocking(int fd) {
1508 #if defined(OS_WIN) 1503 #if defined(OS_WIN)
1509 unsigned long no_block = 1; 1504 unsigned long no_block = 1;
1510 return ioctlsocket(fd, FIONBIO, &no_block); 1505 return ioctlsocket(fd, FIONBIO, &no_block);
1511 #elif defined(OS_POSIX) 1506 #elif defined(OS_POSIX)
1512 int flags = fcntl(fd, F_GETFL, 0); 1507 int flags = fcntl(fd, F_GETFL, 0);
1513 if (-1 == flags) 1508 if (-1 == flags)
1514 return flags; 1509 return flags;
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
1719 replacements.ClearRef(); 1714 replacements.ClearRef();
1720 return url.ReplaceComponents(replacements); 1715 return url.ReplaceComponents(replacements);
1721 } 1716 }
1722 1717
1723 // Specifies a comma separated list of port numbers that should be accepted 1718 // Specifies a comma separated list of port numbers that should be accepted
1724 // despite bans. If the string is invalid no allowed ports are stored. 1719 // despite bans. If the string is invalid no allowed ports are stored.
1725 void SetExplicitlyAllowedPorts(const std::string& allowed_ports) { 1720 void SetExplicitlyAllowedPorts(const std::string& allowed_ports) {
1726 if (allowed_ports.empty()) 1721 if (allowed_ports.empty())
1727 return; 1722 return;
1728 1723
1729 std::set<int> ports; 1724 std::multiset<int> ports;
1730 size_t last = 0; 1725 size_t last = 0;
1731 size_t size = allowed_ports.size(); 1726 size_t size = allowed_ports.size();
1732 // The comma delimiter. 1727 // The comma delimiter.
1733 const std::string::value_type kComma = ','; 1728 const std::string::value_type kComma = ',';
1734 1729
1735 // Overflow is still possible for evil user inputs. 1730 // Overflow is still possible for evil user inputs.
1736 for (size_t i = 0; i <= size; ++i) { 1731 for (size_t i = 0; i <= size; ++i) {
1737 // The string should be composed of only digits and commas. 1732 // The string should be composed of only digits and commas.
1738 if (i != size && !IsAsciiDigit(allowed_ports[i]) && 1733 if (i != size && !IsAsciiDigit(allowed_ports[i]) &&
1739 (allowed_ports[i] != kComma)) 1734 (allowed_ports[i] != kComma))
1740 return; 1735 return;
1741 if (i == size || allowed_ports[i] == kComma) { 1736 if (i == size || allowed_ports[i] == kComma) {
1742 if (i > last) { 1737 if (i > last) {
1743 int port; 1738 int port;
1744 base::StringToInt(allowed_ports.begin() + last, 1739 base::StringToInt(allowed_ports.begin() + last,
1745 allowed_ports.begin() + i, 1740 allowed_ports.begin() + i,
1746 &port); 1741 &port);
1747 ports.insert(port); 1742 ports.insert(port);
1748 } 1743 }
1749 last = i + 1; 1744 last = i + 1;
1750 } 1745 }
1751 } 1746 }
1752 explicitly_allowed_ports = ports; 1747 explicitly_allowed_ports = ports;
1753 } 1748 }
1754 1749
1750 ScopedPortException::ScopedPortException(int port) : port_(port) {
1751 explicitly_allowed_ports.insert(port);
1752 }
1753
1754 ScopedPortException::~ScopedPortException() {
1755 std::multiset<int>::iterator it = explicitly_allowed_ports.find(port_);
1756 if (it != explicitly_allowed_ports.end())
1757 explicitly_allowed_ports.erase(it);
1758 else
1759 NOTREACHED();
1760 }
1761
1755 enum IPv6SupportStatus { 1762 enum IPv6SupportStatus {
1756 IPV6_CANNOT_CREATE_SOCKETS, 1763 IPV6_CANNOT_CREATE_SOCKETS,
1757 IPV6_CAN_CREATE_SOCKETS, 1764 IPV6_CAN_CREATE_SOCKETS,
1758 IPV6_GETIFADDRS_FAILED, 1765 IPV6_GETIFADDRS_FAILED,
1759 IPV6_GLOBAL_ADDRESS_MISSING, 1766 IPV6_GLOBAL_ADDRESS_MISSING,
1760 IPV6_GLOBAL_ADDRESS_PRESENT, 1767 IPV6_GLOBAL_ADDRESS_PRESENT,
1761 IPV6_INTERFACE_ARRAY_TOO_SHORT, 1768 IPV6_INTERFACE_ARRAY_TOO_SHORT,
1762 IPV6_SUPPORT_MAX // Bounding values for enumeration. 1769 IPV6_SUPPORT_MAX // Bounding values for enumeration.
1763 }; 1770 };
1764 1771
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
2053 } 2060 }
2054 2061
2055 int GetPortFromAddrinfo(const struct addrinfo* info) { 2062 int GetPortFromAddrinfo(const struct addrinfo* info) {
2056 uint16* port_field = GetPortFieldFromAddrinfo(info); 2063 uint16* port_field = GetPortFieldFromAddrinfo(info);
2057 if (!port_field) 2064 if (!port_field)
2058 return -1; 2065 return -1;
2059 return ntohs(*port_field); 2066 return ntohs(*port_field);
2060 } 2067 }
2061 2068
2062 } // namespace net 2069 } // namespace net
OLDNEW
« no previous file with comments | « net/base/net_util.h ('k') | net/test/test_server.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698