Skip to content

Commit cfe8885

Browse files
committed
Validate remote file service names before parsing
1 parent 82ff823 commit cfe8885

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎src/brpc/policy/remote_file_naming_service.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ int RemoteFileNamingService::GetServers(const char *service_name_cstr,
6767
butil::StringPiece proto;
6868
if (pos != butil::StringPiece::npos) {
6969
proto = tmpname.substr(0, pos);
70-
for (pos += 3; tmpname[pos] == '/'; ++pos) {}
70+
for (pos += 3; pos < tmpname.size() && tmpname[pos] == '/'; ++pos) {}
71+
if (pos == tmpname.size()) {
72+
LOG(ERROR) << "No server address in service_name="
73+
<< service_name_cstr;
74+
return -1;
75+
}
7176
tmpname.remove_prefix(pos);
7277
} else {
7378
proto = "http";

‎test/brpc_naming_service_unittest.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,18 @@ TEST(NamingServiceTest, remotefile) {
289289
}
290290
}
291291

292+
TEST(NamingServiceTest, remotefile_invalid_url) {
293+
const char* service_names[] = {
294+
"http://", "bns://", "http:////"
295+
};
296+
for (size_t i = 0; i < ARRAY_SIZE(service_names); ++i) {
297+
brpc::policy::RemoteFileNamingService rfns;
298+
std::vector<brpc::ServerNode> servers;
299+
EXPECT_EQ(-1, rfns.GetServers(service_names[i], &servers));
300+
EXPECT_TRUE(servers.empty());
301+
}
302+
}
303+
292304
// Build a JSON array nested `depth' levels deep, e.g. depth=2 -> "[[]]".
293305
static std::string MakeDeeplyNestedJson(int depth) {
294306
return std::string(depth, '[') + std::string(depth, ']');

0 commit comments

Comments
 (0)