Address review comments on port validation.

PiperOrigin-RevId: 946184592
This commit is contained in:
Edwin Wu
2026-07-11 06:05:52 -07:00
committed by Copybara-Service
parent 02354e2968
commit 2aff5d38e0
8 changed files with 363 additions and 25 deletions
@@ -194,7 +194,9 @@ WifiHotspotBwuHandler::CreateUpgradedEndpointChannel(
}
// Add gateway and port to address candidates if address candidates is empty.
if (service_addresses.empty() &&
upgrade_path_info_credentials.has_gateway()) {
upgrade_path_info_credentials.has_gateway() &&
upgrade_path_info_credentials.port() > 0 &&
upgrade_path_info_credentials.port() <= 65535) {
std::vector<char> address_bytes =
GatewayToAddressBytes(upgrade_path_info_credentials.gateway());
if (!address_bytes.empty()) {
@@ -230,7 +232,8 @@ WifiHotspotBwuHandler::CreateUpgradedEndpointChannel(
LOG(ERROR) << "WifiHotspotBwuHandler failed to connect to the WifiHotspot "
"service for endpoint "
<< endpoint_id;
return {Error(socket_result.error().operation_result_code().value())};
return {Error(socket_result.error().operation_result_code().value_or(
OperationResultCode::DETAIL_UNKNOWN))};
}
VLOG(1)
<< "WifiHotspotBwuHandler successfully connected to WifiHotspot service "
@@ -55,8 +55,9 @@ class WifiHotspotTest : public testing::Test {
~WifiHotspotTest() override { env_.Stop(); }
void SetUp() override {
nearby::NearbyFlags::GetInstance().OverrideInt64FlagValue(
platform::config_package_nearby::nearby_platform_feature::
kWifiHotspotConnectionIntervalMillis, 1);
platform::config_package_nearby::nearby_platform_feature::
kWifiHotspotConnectionIntervalMillis,
1);
}
void TearDown() override {
nearby::NearbyFlags::GetInstance().ResetOverridedValues();
@@ -89,9 +90,10 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) {
ExceptionOr<OfflineFrame> upgrade_frame;
auto handler_1 = std::make_unique<WifiHotspotBwuHandler>(
&mediums_HS_ap.GetWifiHotspot(), [&](ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
&mediums_HS_ap.GetWifiHotspot(),
[&](ClientProxy* client,
std::unique_ptr<BwuHandler::IncomingSocketConnection>
mutable_connection) {
LOG(INFO) << "Server socket connection accept call back, Socket name: "
<< mutable_connection->socket->ToString();
accept_latch.CountDown();
@@ -169,5 +171,42 @@ TEST_F(WifiHotspotTest, SoftAPBWUInit_STACreateEndpointChannel) {
EXPECT_FALSE(mediums_HS_sta.GetWifiHotspot().IsConnectedToHotspot());
}
TEST_F(WifiHotspotTest, CreateUpgradedEndpointChannel_RejectGatewayPort0) {
ClientProxy client;
client.AddCancellationFlag(std::string(kEndpointID));
Mediums mediums;
WifiHotspotBwuHandler handler(&mediums.GetWifiHotspot(), nullptr);
UpgradePathInfo path_info;
auto* credentials = path_info.mutable_wifi_hotspot_credentials();
credentials->set_ssid("SSID");
credentials->set_password("password");
credentials->set_gateway("192.168.43.1");
// Port 0
credentials->set_port(0);
auto result = handler.CreateUpgradedEndpointChannel(
&client, std::string(kServiceID), std::string(kEndpointID), path_info);
EXPECT_TRUE(result.has_error());
EXPECT_EQ(result.error().operation_result_code().value(),
OperationResultCode::CONNECTIVITY_WIFI_HOTSPOT_INVALID_CREDENTIAL);
// Port > 65535
credentials->set_port(65536);
result = handler.CreateUpgradedEndpointChannel(
&client, std::string(kServiceID), std::string(kEndpointID), path_info);
EXPECT_TRUE(result.has_error());
EXPECT_EQ(result.error().operation_result_code().value(),
OperationResultCode::CONNECTIVITY_WIFI_HOTSPOT_INVALID_CREDENTIAL);
// Port < 0
credentials->set_port(-1);
result = handler.CreateUpgradedEndpointChannel(
&client, std::string(kServiceID), std::string(kEndpointID), path_info);
EXPECT_TRUE(result.has_error());
EXPECT_EQ(result.error().operation_result_code().value(),
OperationResultCode::CONNECTIVITY_WIFI_HOTSPOT_INVALID_CREDENTIAL);
}
} // namespace connections
} // namespace nearby
@@ -89,9 +89,11 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
}
// Only use ip_address and wifi_port if address_candidates is empty.
if (address_candidates.empty()) {
if (upgrade_path_info_socket.ip_address().size() != 4) {
if (upgrade_path_info_socket.ip_address().size() != 4 ||
upgrade_path_info_socket.wifi_port() <= 0 ||
upgrade_path_info_socket.wifi_port() > 65535) {
LOG(ERROR) << "WifiLanBwuHandler: fallback ip_address size is not 4 "
<< "(IPv4 only).";
<< "or port is invalid (IPv4 only).";
return {
Error(OperationResultCode::CONNECTIVITY_WIFI_LAN_IP_ADDRESS_ERROR)};
}
@@ -121,7 +123,8 @@ WifiLanBwuHandler::CreateUpgradedEndpointChannel(
LOG(ERROR)
<< "WifiLanBwuHandler failed to connect to the WifiLan service ("
<< address_candidate << ") for endpoint " << endpoint_id;
error = Error(socket_result.error().operation_result_code().value());
error = Error(socket_result.error().operation_result_code().value_or(
OperationResultCode::DETAIL_UNKNOWN));
continue;
}
VLOG(1) << "WifiLanBwuHandler successfully connected to WifiLan service ("
@@ -274,6 +274,33 @@ TEST_F(WifiLanBwuHandlerTest,
EXPECT_FALSE(result.has_value());
}
TEST_F(WifiLanBwuHandlerTest,
CreateUpgradedEndpointChannel_RejectFallbackPort0) {
ClientProxy client;
client.AddCancellationFlag(std::string(kEndpointId));
BandwidthUpgradeNegotiationFrame::UpgradePathInfo path_info;
path_info.mutable_wifi_lan_socket()->set_ip_address(kIpv4Address);
// Port 0
path_info.mutable_wifi_lan_socket()->set_wifi_port(0);
auto result = handler_.CreateUpgradedEndpointChannel(
&client, std::string(kServiceId), std::string(kEndpointId), path_info);
EXPECT_FALSE(result.has_value());
// Port > 65535
path_info.mutable_wifi_lan_socket()->set_wifi_port(65536);
result = handler_.CreateUpgradedEndpointChannel(
&client, std::string(kServiceId), std::string(kEndpointId), path_info);
EXPECT_FALSE(result.has_value());
// Port < 0
path_info.mutable_wifi_lan_socket()->set_wifi_port(-1);
result = handler_.CreateUpgradedEndpointChannel(
&client, std::string(kServiceId), std::string(kEndpointId), path_info);
EXPECT_FALSE(result.has_value());
}
TEST_F(WifiLanBwuHandlerTest, InitializeUpgradedMediumForEndpoint_Success) {
MediumEnvironment::Instance().Start({.use_simulated_clock = true});
ClientProxy client;