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
@@ -26,6 +26,7 @@
#include "connections/medium_selector.h"
#include "internal/platform/exception.h"
#include "internal/platform/logging.h"
#include "internal/platform/service_address.h"
namespace nearby {
namespace connections {
@@ -75,6 +76,11 @@ inline bool WithinRange(int value, int min, int max) {
return value >= min && value <= max;
}
bool IsValidWifiLanServiceAddress(const ServiceAddress& service_address) {
return !service_address.IsLoopbackAddress() &&
!service_address.IsLinkLocalAddress();
}
Exception EnsureValidConnectionRequestFrame(
const ConnectionRequestFrame& frame) {
if (frame.endpoint_id().empty()) return {Exception::kInvalidProtocolBuffer};
@@ -234,21 +240,29 @@ Exception EnsureValidBandwidthUpgradeWifiHotspotPathAvailableFrame(
!WithinRange(wifi_hotspot_credentials.password().length(),
kWifiPasswordSsidMinLength, kWifiPasswordSsidMaxLength))
return {Exception::kInvalidProtocolBuffer};
if (!wifi_hotspot_credentials.has_gateway() &&
wifi_hotspot_credentials.address_candidates_size() == 0)
return {Exception::kInvalidProtocolBuffer};
const std::regex ip4_pattern(std::string(kIpv4PatternString).c_str());
if (!wifi_hotspot_credentials.gateway().empty() &&
!(std::regex_match(wifi_hotspot_credentials.gateway(), ip4_pattern))) {
if ((!wifi_hotspot_credentials.has_gateway() ||
wifi_hotspot_credentials.gateway().empty()) &&
wifi_hotspot_credentials.address_candidates_size() == 0) {
return {Exception::kInvalidProtocolBuffer};
}
for (const auto& address_candidate :
wifi_hotspot_credentials.address_candidates()) {
if (!address_candidate.has_ip_address() || !address_candidate.has_port()) {
const std::regex ip4_pattern(std::string(kIpv4PatternString).c_str());
if (wifi_hotspot_credentials.has_gateway() &&
!wifi_hotspot_credentials.gateway().empty()) {
if (!(std::regex_match(wifi_hotspot_credentials.gateway(), ip4_pattern))) {
return {Exception::kInvalidProtocolBuffer};
}
if (address_candidate.ip_address().size() != 4 &&
address_candidate.ip_address().size() != 16) {
if (!wifi_hotspot_credentials.has_port() ||
!WithinRange(wifi_hotspot_credentials.port(), 1, 65535)) {
return {Exception::kInvalidProtocolBuffer};
}
}
for (const auto& address_candidate :
wifi_hotspot_credentials.address_candidates()) {
ServiceAddress service_address;
if (!ServiceAddressFromProto(address_candidate, service_address)) {
return {Exception::kInvalidProtocolBuffer};
}
}
@@ -265,6 +279,29 @@ Exception EnsureValidBandwidthUpgradeWifiLanPathAvailableFrame(
return {Exception::kInvalidProtocolBuffer};
}
if (wifi_lan_socket.has_ip_address()) {
location::nearby::connections::ServiceAddress proto;
proto.set_ip_address(wifi_lan_socket.ip_address());
proto.set_port(wifi_lan_socket.wifi_port());
ServiceAddress service_address;
if (!ServiceAddressFromProto(proto, service_address)) {
return {Exception::kInvalidProtocolBuffer};
}
if (!IsValidWifiLanServiceAddress(service_address)) {
return {Exception::kInvalidProtocolBuffer};
}
}
for (const auto& address_candidate : wifi_lan_socket.address_candidates()) {
ServiceAddress service_address;
if (!ServiceAddressFromProto(address_candidate, service_address)) {
return {Exception::kInvalidProtocolBuffer};
}
if (!IsValidWifiLanServiceAddress(service_address)) {
return {Exception::kInvalidProtocolBuffer};
}
}
// For backwards compatibility reasons, no other fields should be null-checked
// for this frame. Parameter checking (eg. must be within this range) is fine.
return {Exception::kSuccess};