Skip to content

CBL-6952: Revise Database name validation in C4Address.fromURL() #2283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Replicator/c4Replicator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
26 changes: 6 additions & 20 deletions Replicator/tests/ReplicatorAPITest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Replicator/tests/ReplicatorCollectionSGTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ TEST_CASE_METHOD(ReplicatorCollectionSGTest, "Sync with Single Collection SG", "
std::vector<C4CollectionSpec> collectionSpecs{collectionCount};

bool continuous = false;
C4Error expectedError;
C4Error expectedError{};

SECTION("Named Collection") { collectionSpecs = {Roses}; }

Expand Down