Commit e0ec8039 authored by Takeshi Yoshino's avatar Takeshi Yoshino Committed by Commit Bot

Change the port argument of SecurityOrigin::Create() methods to be uint16_t

The automatic fallback to a unique origin in SecurityOrigin::Create() is
error prone. A unique origin should be created explicitly. In the first
place, it never gets such invalid values. To clarify that, change the
type of the port argument to uint16_t.

In SchemeHostPort, add DCHECKs to clarify that the results of
GURL::EffectiveIntPort() can never be outside of the uint16_t range.

One of the users of the methods, the CreateFromTupleWithSuborigin()
method is no longer used by anyone. So, just remove it.

Bug: 788289
Change-Id: Ib0abd718d5553f6b3b06388c4f5c684033202500
Reviewed-on: https://chromium-review.googlesource.com/796172
Commit-Queue: Takeshi Yoshino <tyoshino@chromium.org>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523742}
parent eb5a05ff
...@@ -45,15 +45,6 @@ WebSecurityOrigin WebSecurityOrigin::Create(const WebURL& url) { ...@@ -45,15 +45,6 @@ WebSecurityOrigin WebSecurityOrigin::Create(const WebURL& url) {
return WebSecurityOrigin(SecurityOrigin::Create(url)); return WebSecurityOrigin(SecurityOrigin::Create(url));
} }
WebSecurityOrigin WebSecurityOrigin::CreateFromTupleWithSuborigin(
const WebString& protocol,
const WebString& host,
int port,
const WebString& suborigin) {
return WebSecurityOrigin(
SecurityOrigin::Create(protocol, host, port, suborigin));
}
WebSecurityOrigin WebSecurityOrigin::CreateUnique() { WebSecurityOrigin WebSecurityOrigin::CreateUnique() {
return WebSecurityOrigin(SecurityOrigin::CreateUnique()); return WebSecurityOrigin(SecurityOrigin::CreateUnique());
} }
......
...@@ -28,7 +28,9 @@ ...@@ -28,7 +28,9 @@
#include "platform/weborigin/SecurityOrigin.h" #include "platform/weborigin/SecurityOrigin.h"
#include <stdint.h>
#include <memory> #include <memory>
#include "net/base/url_util.h" #include "net/base/url_util.h"
#include "platform/runtime_enabled_features.h" #include "platform/runtime_enabled_features.h"
#include "platform/weborigin/KURL.h" #include "platform/weborigin/KURL.h"
...@@ -48,8 +50,7 @@ ...@@ -48,8 +50,7 @@
namespace blink { namespace blink {
const int kInvalidPort = 0; const uint16_t kInvalidPort = 0;
const int kMaxAllowedPort = 65535;
static URLSecurityOriginMap* g_url_origin_map = nullptr; static URLSecurityOriginMap* g_url_origin_map = nullptr;
...@@ -535,10 +536,7 @@ scoped_refptr<SecurityOrigin> SecurityOrigin::CreateFromString( ...@@ -535,10 +536,7 @@ scoped_refptr<SecurityOrigin> SecurityOrigin::CreateFromString(
scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol, scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol,
const String& host, const String& host,
int port) { uint16_t port) {
if (port < 0 || port > kMaxAllowedPort)
return CreateUnique();
DCHECK_EQ(host, DecodeURLEscapeSequences(host)); DCHECK_EQ(host, DecodeURLEscapeSequences(host));
String port_part = port ? ":" + String::Number(port) : String(); String port_part = port ? ":" + String::Number(port) : String();
...@@ -547,7 +545,7 @@ scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol, ...@@ -547,7 +545,7 @@ scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol,
scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol, scoped_refptr<SecurityOrigin> SecurityOrigin::Create(const String& protocol,
const String& host, const String& host,
int port, uint16_t port,
const String& suborigin) { const String& suborigin) {
scoped_refptr<SecurityOrigin> origin = Create(protocol, host, port); scoped_refptr<SecurityOrigin> origin = Create(protocol, host, port);
if (!suborigin.IsEmpty()) if (!suborigin.IsEmpty())
......
...@@ -29,7 +29,9 @@ ...@@ -29,7 +29,9 @@
#ifndef SecurityOrigin_h #ifndef SecurityOrigin_h
#define SecurityOrigin_h #define SecurityOrigin_h
#include <stdint.h>
#include <memory> #include <memory>
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "platform/PlatformExport.h" #include "platform/PlatformExport.h"
#include "platform/weborigin/Suborigin.h" #include "platform/weborigin/Suborigin.h"
...@@ -53,10 +55,10 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> { ...@@ -53,10 +55,10 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> {
static scoped_refptr<SecurityOrigin> CreateFromString(const String&); static scoped_refptr<SecurityOrigin> CreateFromString(const String&);
static scoped_refptr<SecurityOrigin> Create(const String& protocol, static scoped_refptr<SecurityOrigin> Create(const String& protocol,
const String& host, const String& host,
int port); uint16_t port);
static scoped_refptr<SecurityOrigin> Create(const String& protocol, static scoped_refptr<SecurityOrigin> Create(const String& protocol,
const String& host, const String& host,
int port, uint16_t port,
const String& suborigin); const String& suborigin);
static scoped_refptr<SecurityOrigin> CreateFromUrlOrigin(const url::Origin&); static scoped_refptr<SecurityOrigin> CreateFromUrlOrigin(const url::Origin&);
url::Origin ToUrlOrigin() const; url::Origin ToUrlOrigin() const;
...@@ -92,10 +94,10 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> { ...@@ -92,10 +94,10 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> {
// Returns 0 if the effective port of this origin is the default for its // Returns 0 if the effective port of this origin is the default for its
// scheme. // scheme.
unsigned short Port() const { return port_; } uint16_t Port() const { return port_; }
// Returns the effective port, even if it is the default port for the // Returns the effective port, even if it is the default port for the
// scheme (e.g. "http" => 80). // scheme (e.g. "http" => 80).
unsigned short EffectivePort() const { return effective_port_; } uint16_t EffectivePort() const { return effective_port_; }
// Returns true if a given URL is secure, based either directly on its // Returns true if a given URL is secure, based either directly on its
// own protocol, or, when relevant, on the protocol of its "inner URL" // own protocol, or, when relevant, on the protocol of its "inner URL"
...@@ -313,8 +315,8 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> { ...@@ -313,8 +315,8 @@ class PLATFORM_EXPORT SecurityOrigin : public RefCounted<SecurityOrigin> {
String host_; String host_;
String domain_; String domain_;
Suborigin suborigin_; Suborigin suborigin_;
unsigned short port_; uint16_t port_;
unsigned short effective_port_; uint16_t effective_port_;
const bool is_unique_; const bool is_unique_;
bool universal_access_; bool universal_access_;
bool domain_was_set_in_dom_; bool domain_was_set_in_dom_;
......
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
#include "platform/weborigin/SecurityOrigin.h" #include "platform/weborigin/SecurityOrigin.h"
#include <stdint.h>
#include "platform/blob/BlobURL.h" #include "platform/blob/BlobURL.h"
#include "platform/testing/RuntimeEnabledFeaturesTestHelpers.h" #include "platform/testing/RuntimeEnabledFeaturesTestHelpers.h"
#include "platform/weborigin/KURL.h" #include "platform/weborigin/KURL.h"
...@@ -43,7 +45,7 @@ ...@@ -43,7 +45,7 @@
namespace blink { namespace blink {
const int kMaxAllowedPort = 65535; const uint16_t kMaxAllowedPort = UINT16_MAX;
class SecurityOriginTest : public ::testing::Test { class SecurityOriginTest : public ::testing::Test {
public: public:
...@@ -53,19 +55,8 @@ class SecurityOriginTest : public ::testing::Test { ...@@ -53,19 +55,8 @@ class SecurityOriginTest : public ::testing::Test {
} }
}; };
TEST_F(SecurityOriginTest, InvalidPortsCreateUniqueOrigins) {
int ports[] = {-100, -1, kMaxAllowedPort + 1, 1000000};
for (size_t i = 0; i < WTF_ARRAY_LENGTH(ports); ++i) {
scoped_refptr<const SecurityOrigin> origin =
SecurityOrigin::Create("http", "example.com", ports[i]);
EXPECT_TRUE(origin->IsUnique())
<< "Port " << ports[i] << " should have generated a unique origin.";
}
}
TEST_F(SecurityOriginTest, ValidPortsCreateNonUniqueOrigins) { TEST_F(SecurityOriginTest, ValidPortsCreateNonUniqueOrigins) {
int ports[] = {0, 80, 443, 5000, kMaxAllowedPort}; uint16_t ports[] = {0, 80, 443, 5000, kMaxAllowedPort};
for (size_t i = 0; i < WTF_ARRAY_LENGTH(ports); ++i) { for (size_t i = 0; i < WTF_ARRAY_LENGTH(ports); ++i) {
scoped_refptr<const SecurityOrigin> origin = scoped_refptr<const SecurityOrigin> origin =
...@@ -436,7 +427,7 @@ TEST_F(SecurityOriginTest, CreateFromTuple) { ...@@ -436,7 +427,7 @@ TEST_F(SecurityOriginTest, CreateFromTuple) {
struct TestCase { struct TestCase {
const char* scheme; const char* scheme;
const char* host; const char* host;
unsigned short port; uint16_t port;
const char* origin; const char* origin;
} cases[] = { } cases[] = {
{"http", "example.com", 80, "http://example.com"}, {"http", "example.com", 80, "http://example.com"},
...@@ -561,7 +552,7 @@ TEST_F(SecurityOriginTest, UrlOriginConversions) { ...@@ -561,7 +552,7 @@ TEST_F(SecurityOriginTest, UrlOriginConversions) {
const char* const url; const char* const url;
const char* const scheme; const char* const scheme;
const char* const host; const char* const host;
unsigned short port; uint16_t port;
} cases[] = { } cases[] = {
// IP Addresses // IP Addresses
{"http://192.168.9.1/", "http", "192.168.9.1", 80}, {"http://192.168.9.1/", "http", "192.168.9.1", 80},
......
...@@ -124,14 +124,6 @@ class WebSecurityOrigin { ...@@ -124,14 +124,6 @@ class WebSecurityOrigin {
#endif #endif
private: private:
// Present only to facilitate conversion from 'url::Origin'; this constructor
// shouldn't be used anywhere else.
BLINK_PLATFORM_EXPORT static WebSecurityOrigin CreateFromTupleWithSuborigin(
const WebString& protocol,
const WebString& host,
int port,
const WebString& suborigin);
WebPrivatePtr<const SecurityOrigin> private_; WebPrivatePtr<const SecurityOrigin> private_;
}; };
......
...@@ -140,8 +140,12 @@ SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) { ...@@ -140,8 +140,12 @@ SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) {
// A valid GURL never returns PORT_INVALID. // A valid GURL never returns PORT_INVALID.
int port = url.EffectiveIntPort(); int port = url.EffectiveIntPort();
if (port == PORT_UNSPECIFIED) if (port == PORT_UNSPECIFIED) {
port = 0; port = 0;
} else {
DCHECK_GE(port, 0);
DCHECK_LE(port, 65535);
}
if (!IsValidInput(scheme, host, port, ALREADY_CANONICALIZED)) if (!IsValidInput(scheme, host, port, ALREADY_CANONICALIZED))
return; return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment