diff options
Diffstat (limited to 'chromium/url')
-rw-r--r-- | chromium/url/BUILD.gn | 1 | ||||
-rw-r--r-- | chromium/url/gurl.cc | 14 | ||||
-rw-r--r-- | chromium/url/gurl.h | 7 | ||||
-rw-r--r-- | chromium/url/gurl_unittest.cc | 8 | ||||
-rw-r--r-- | chromium/url/origin.cc | 41 | ||||
-rw-r--r-- | chromium/url/origin.h | 5 | ||||
-rw-r--r-- | chromium/url/origin_unittest.cc | 32 | ||||
-rw-r--r-- | chromium/url/scheme_host_port.cc | 2 | ||||
-rw-r--r-- | chromium/url/url_canon_stdstring.h | 2 |
9 files changed, 93 insertions, 19 deletions
diff --git a/chromium/url/BUILD.gn b/chromium/url/BUILD.gn index e7a43342929..b52a2286212 100644 --- a/chromium/url/BUILD.gn +++ b/chromium/url/BUILD.gn @@ -129,6 +129,7 @@ test("url_unittests") { ":url", "//base", "//base/test:test_support", + "//testing/gmock", "//testing/gtest", ] diff --git a/chromium/url/gurl.cc b/chromium/url/gurl.cc index 4cecabc5fc1..84e7bf1dc68 100644 --- a/chromium/url/gurl.cc +++ b/chromium/url/gurl.cc @@ -372,6 +372,20 @@ bool GURL::SchemeIsWSOrWSS() const { return SchemeIs(url::kWsScheme) || SchemeIs(url::kWssScheme); } +bool GURL::SchemeIsCryptographic() const { + if (parsed_.scheme.len <= 0) + return false; + return SchemeIsCryptographic(scheme_piece()); +} + +bool GURL::SchemeIsCryptographic(base::StringPiece lower_ascii_scheme) { + DCHECK(base::IsStringASCII(lower_ascii_scheme)); + DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme); + + return lower_ascii_scheme == url::kHttpsScheme || + lower_ascii_scheme == url::kWssScheme; +} + int GURL::IntPort() const { if (parsed_.port.is_nonempty()) return url::ParsePort(spec_.data(), parsed_.port); diff --git a/chromium/url/gurl.h b/chromium/url/gurl.h index 5bfa17f85bc..1b0669e221a 100644 --- a/chromium/url/gurl.h +++ b/chromium/url/gurl.h @@ -249,9 +249,10 @@ class COMPONENT_EXPORT(URL) GURL { // is minimally trustworthy. For that, see Chromium's |IsOriginSecure| for a // higher-level and more complete semantics. See that function's documentation // for more detail. - bool SchemeIsCryptographic() const { - return SchemeIs(url::kHttpsScheme) || SchemeIs(url::kWssScheme); - } + bool SchemeIsCryptographic() const; + + // As above, but static. Parameter should be lower-case ASCII. + static bool SchemeIsCryptographic(base::StringPiece lower_ascii_scheme); // Returns true if the scheme is "blob". bool SchemeIsBlob() const { diff --git a/chromium/url/gurl_unittest.cc b/chromium/url/gurl_unittest.cc index caf5dd4e1b3..379c04fa3c1 100644 --- a/chromium/url/gurl_unittest.cc +++ b/chromium/url/gurl_unittest.cc @@ -765,6 +765,14 @@ TEST(GURLTest, SchemeIsCryptographic) { EXPECT_FALSE(GURL("ws://foo.bar.com/").SchemeIsCryptographic()); } +TEST(GURLTest, SchemeIsCryptographicStatic) { + EXPECT_TRUE(GURL::SchemeIsCryptographic("https")); + EXPECT_TRUE(GURL::SchemeIsCryptographic("wss")); + EXPECT_FALSE(GURL::SchemeIsCryptographic("http")); + EXPECT_FALSE(GURL::SchemeIsCryptographic("ws")); + EXPECT_FALSE(GURL::SchemeIsCryptographic("ftp")); +} + TEST(GURLTest, SchemeIsBlob) { EXPECT_TRUE(GURL("BLOB://BAR/").SchemeIsBlob()); EXPECT_TRUE(GURL("blob://bar/").SchemeIsBlob()); diff --git a/chromium/url/origin.cc b/chromium/url/origin.cc index 916ad368404..c2dee8868a4 100644 --- a/chromium/url/origin.cc +++ b/chromium/url/origin.cc @@ -10,6 +10,7 @@ #include "base/logging.h" #include "base/stl_util.h" +#include "base/strings/strcat.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_util.h" #include "url/gurl.h" @@ -239,6 +240,30 @@ Origin Origin::DeriveNewOpaqueOrigin() const { return Origin(Nonce(), tuple_); } +std::string Origin::GetDebugString() const { + // Handle non-opaque origins first, as they are simpler. + if (!opaque()) { + std::string out = Serialize(); + if (scheme() == kFileScheme) + base::StrAppend(&out, {" [internally: ", tuple_.Serialize(), "]"}); + return out; + } + + // For opaque origins, log the nonce and precursor as well. Without this, + // EXPECT_EQ failures between opaque origins are nearly impossible to + // understand. + std::string nonce = nonce_->raw_token().is_empty() + ? std::string("nonce TBD") + : nonce_->raw_token().ToString(); + + std::string out = base::StrCat({Serialize(), " [internally: (", nonce, ")"}); + if (tuple_.IsInvalid()) + base::StrAppend(&out, {" anonymous]"}); + else + base::StrAppend(&out, {" derived from ", tuple_.Serialize(), "]"}); + return out; +} + Origin::Origin(SchemeHostPort tuple) : tuple_(std::move(tuple)) { DCHECK(!opaque()); DCHECK(!tuple_.IsInvalid()); @@ -255,21 +280,7 @@ Origin::Origin(const Nonce& nonce, SchemeHostPort precursor) } std::ostream& operator<<(std::ostream& out, const url::Origin& origin) { - out << origin.Serialize(); - - if (origin.opaque()) { - // For opaque origins, log the nonce and precursor as well. Without this, - // EXPECT_EQ failures between opaque origins are nearly impossible to - // understand. - out << " [internally: " << *origin.nonce_; - if (origin.tuple_.IsInvalid()) - out << " anonymous"; - else - out << " derived from " << origin.tuple_; - out << "]"; - } else if (origin.scheme() == kFileScheme) { - out << " [internally: " << origin.tuple_ << "]"; - } + out << origin.GetDebugString(); return out; } diff --git a/chromium/url/origin.h b/chromium/url/origin.h index 08332541ab2..cfcba14ea72 100644 --- a/chromium/url/origin.h +++ b/chromium/url/origin.h @@ -261,6 +261,11 @@ class COMPONENT_EXPORT(URL) Origin { // |d|, and |d| is cross-origin to |a| and |c|. Origin DeriveNewOpaqueOrigin() const; + // Creates a string representation of the object that can be used for logging + // and debugging. It serializes the internal state, such as the nonce value + // and precursor information. + std::string GetDebugString() const; + private: friend class blink::SecurityOrigin; friend class OriginTest; diff --git a/chromium/url/origin_unittest.cc b/chromium/url/origin_unittest.cc index 36f2f5167e2..2f65757a70b 100644 --- a/chromium/url/origin_unittest.cc +++ b/chromium/url/origin_unittest.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "base/macros.h" +#include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" #include "url/gurl.h" #include "url/origin.h" @@ -831,4 +832,35 @@ TEST_F(OriginTest, CanBeDerivedFrom) { } } +TEST_F(OriginTest, GetDebugString) { + Origin http_origin = Origin::Create(GURL("http://192.168.9.1")); + EXPECT_STREQ(http_origin.GetDebugString().c_str(), "http://192.168.9.1"); + + Origin http_opaque_origin = http_origin.DeriveNewOpaqueOrigin(); + EXPECT_THAT( + http_opaque_origin.GetDebugString().c_str(), + ::testing::MatchesRegex( + "null \\[internally: \\(\\w*\\) derived from http://192.168.9.1\\]")); + + Origin data_origin = Origin::Create(GURL("data:")); + EXPECT_STREQ(data_origin.GetDebugString().c_str(), + "null [internally: (nonce TBD) anonymous]"); + + // The nonce of the origin will be initialized if a new opaque origin is + // derived. + Origin data_derived_origin = data_origin.DeriveNewOpaqueOrigin(); + EXPECT_THAT( + data_derived_origin.GetDebugString().c_str(), + ::testing::MatchesRegex("null \\[internally: \\(\\w*\\) anonymous\\]")); + + Origin file_origin = Origin::Create(GURL("file:///etc/passwd")); + EXPECT_STREQ(file_origin.GetDebugString().c_str(), + "file:// [internally: file://]"); + + Origin file_server_origin = + Origin::Create(GURL("file://example.com/etc/passwd")); + EXPECT_STREQ(file_server_origin.GetDebugString().c_str(), + "file:// [internally: file://example.com]"); +} + } // namespace url diff --git a/chromium/url/scheme_host_port.cc b/chromium/url/scheme_host_port.cc index e588aece42f..6af849fe6bd 100644 --- a/chromium/url/scheme_host_port.cc +++ b/chromium/url/scheme_host_port.cc @@ -253,7 +253,7 @@ std::string SchemeHostPort::SerializeInternal(url::Parsed* parsed) const { return result; if (port_ != default_port) { result.push_back(':'); - std::string port(base::UintToString(port_)); + std::string port(base::NumberToString(port_)); parsed->port = Component(result.length(), port.length()); result.append(std::move(port)); } diff --git a/chromium/url/url_canon_stdstring.h b/chromium/url/url_canon_stdstring.h index f693bd08f43..fed2e791ce8 100644 --- a/chromium/url/url_canon_stdstring.h +++ b/chromium/url/url_canon_stdstring.h @@ -13,6 +13,7 @@ #include "base/compiler_specific.h" #include "base/component_export.h" +#include "base/macros.h" #include "base/strings/string_piece.h" #include "url/url_canon.h" @@ -45,6 +46,7 @@ class COMPONENT_EXPORT(URL) StdStringCanonOutput : public CanonOutput { protected: std::string* str_; + DISALLOW_COPY_AND_ASSIGN(StdStringCanonOutput); }; // An extension of the Replacements class that allows the setters to use |