From b775fdbdef71926dbfdcbb9d90faca1405082d56 Mon Sep 17 00:00:00 2001 From: Jianmin Zhao Date: Fri, 23 May 2025 09:16:27 -0700 Subject: [PATCH] CBL-6952: Revise Database name validation in C4Address.fromURL() We won't check the validity of the database name in the URL. The format of the name should be managed as the name is created. --- Replicator/c4Replicator.cc | 6 ++--- Replicator/tests/ReplicatorAPITest.cc | 26 +++++-------------- .../tests/ReplicatorCollectionSGTest.cc | 2 +- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/Replicator/c4Replicator.cc b/Replicator/c4Replicator.cc index 57b09e840..7763d8530 100644 --- a/Replicator/c4Replicator.cc +++ b/Replicator/c4Replicator.cc @@ -139,8 +139,8 @@ bool C4Replicator::isValidDatabaseName(slice dbName) noexcept { bool C4Address::isValidRemote(slice dbName, C4Error* outError) const noexcept { slice message; if ( !isValidReplicatorScheme(scheme) ) message = "Invalid replication URL scheme (use ws: or wss:)"_sl; - else if ( !C4Replicator::isValidDatabaseName(dbName) ) - message = "Invalid or missing remote database name"_sl; + else if ( dbName.empty() ) + message = "Missing remote database name"_sl; else if ( hostname.size == 0 || port == 0 ) message = "Invalid replication URL (bad hostname or port)"_sl; @@ -286,7 +286,7 @@ bool C4Address::fromURL(slice url, C4Address* address, slice* dbName) { address->path = slice(pathStart, str.buf); *dbName = str; - return C4Replicator::isValidDatabaseName(str); + return !str.empty(); } else { address->path = slice(pathStart, str.end()); return true; diff --git a/Replicator/tests/ReplicatorAPITest.cc b/Replicator/tests/ReplicatorAPITest.cc index 8ee77ae71..96a3bde1f 100644 --- a/Replicator/tests/ReplicatorAPITest.cc +++ b/Replicator/tests/ReplicatorAPITest.cc @@ -102,7 +102,8 @@ TEST_CASE("URL Parsing", "[C][Replicator]") { REQUIRE(c4address_fromURL("wss://localhost/p/d/"_sl, &address, &dbName)); REQUIRE(c4address_fromURL("wss://localhost//p//d/"_sl, &address, &dbName)); - REQUIRE(!c4address_fromURL("ws://example.com/db@name"_sl, &address, &dbName)); + // We don't check the validity of the database name. + REQUIRE(c4address_fromURL("ws://example.com/db@name"_sl, &address, &dbName)); CHECK(dbName == "db@name"_sl); // The following URLs should all be rejected: @@ -121,7 +122,10 @@ TEST_CASE("URL Parsing", "[C][Replicator]") { CHECK(!c4address_fromURL("ws://localhost:/foo"_sl, &address, &dbName)); CHECK(!c4address_fromURL("ws://localhost"_sl, &address, &dbName)); CHECK(!c4address_fromURL("ws://localhost/"_sl, &address, &dbName)); - CHECK(!c4address_fromURL("ws://localhost/B^dn^m*"_sl, &address, &dbName)); + + // We don't check the validity of the database name. + CHECK(c4address_fromURL("ws://localhost/B^dn^m*"_sl, &address, &dbName)); + CHECK(dbName == "B^dn^m*"_sl); CHECK(!c4address_fromURL("ws://snej@example.com/db"_sl, &address, &dbName)); CHECK(!c4address_fromURL("ws://snej@example.com:8080/db"_sl, &address, &dbName)); @@ -163,24 +167,6 @@ TEST_CASE_METHOD(ReplicatorAPITest, "API Invalid Scheme", "[C][Push][!throws]") CHECK(err.code == kC4NetErrInvalidURL); } -// Test missing or invalid database name: -TEST_CASE_METHOD(ReplicatorAPITest, "API Invalid URLs", "[C][Push][!throws]") { - ExpectingExceptions x; - _sg.remoteDBName = ""_sl; - C4Error err; - CHECK(!c4repl_isValidRemote(_sg.address, _sg.remoteDBName, nullptr)); - REQUIRE(!startReplicator(kC4Disabled, kC4OneShot, &err)); - CHECK(err.domain == NetworkDomain); - CHECK(err.code == kC4NetErrInvalidURL); - - _sg.remoteDBName = "Invalid Name"_sl; - err = {}; - CHECK(!c4repl_isValidRemote(_sg.address, _sg.remoteDBName, nullptr)); - REQUIRE(!startReplicator(kC4Disabled, kC4OneShot, &err)); - CHECK(err.domain == NetworkDomain); - CHECK(err.code == kC4NetErrInvalidURL); -} - // Test connection-refused error by connecting to a bogus port of localhost TEST_CASE_METHOD(ReplicatorAPITest, "API Connection Failure", "[C][Push]") { ExpectingExceptions x; diff --git a/Replicator/tests/ReplicatorCollectionSGTest.cc b/Replicator/tests/ReplicatorCollectionSGTest.cc index 83ed268a1..d875643c1 100644 --- a/Replicator/tests/ReplicatorCollectionSGTest.cc +++ b/Replicator/tests/ReplicatorCollectionSGTest.cc @@ -241,7 +241,7 @@ TEST_CASE_METHOD(ReplicatorCollectionSGTest, "Sync with Single Collection SG", " std::vector collectionSpecs{collectionCount}; bool continuous = false; - C4Error expectedError; + C4Error expectedError{}; SECTION("Named Collection") { collectionSpecs = {Roses}; }