Skip to content

Commit

Permalink
validate port
Browse files Browse the repository at this point in the history
  • Loading branch information
graebm committed Dec 16, 2023
1 parent 905c7f1 commit b6e6773
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions source/v5/mqtt5_options_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -3356,14 +3356,20 @@ int aws_mqtt5_client_options_validate(const struct aws_mqtt5_client_options *opt
}
}

if (aws_socket_validate_port_for_connect(
options->port, options->socket_options ? options->socket_options->domain : AWS_SOCKET_IPV4)) {
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid port in mqtt5 client configuration");
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION);
}

if (options->http_proxy_options != NULL) {
if (options->http_proxy_options->host.len == 0) {
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "proxy host name not set in mqtt5 client configuration");
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION);
}

if (options->http_proxy_options->port == 0) {
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "proxy port not set in mqtt5 client configuration");
if (aws_socket_validate_port_for_connect(options->http_proxy_options->port, AWS_SOCKET_IPV4)) {
AWS_LOGF_ERROR(AWS_LS_MQTT5_GENERAL, "invalid proxy port in mqtt5 client configuration");
return aws_raise_error(AWS_ERROR_MQTT5_CLIENT_OPTIONS_VALIDATION);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ add_test_case(mqtt5_client_options_validation_failure_no_publish_received)
add_test_case(mqtt5_client_options_validation_failure_invalid_socket_options)
add_test_case(mqtt5_client_options_validation_failure_invalid_connect)
add_test_case(mqtt5_client_options_validation_failure_invalid_keep_alive)
add_test_case(mqtt5_client_options_validation_failure_invalid_port)
add_test_case(mqtt5_operation_subscribe_connection_settings_validation_failure_exceeds_maximum_packet_size)
add_test_case(mqtt5_operation_unsubscribe_connection_settings_validation_failure_exceeds_maximum_packet_size)
add_test_case(mqtt5_operation_publish_connection_settings_validation_failure_exceeds_maximum_packet_size)
Expand Down
10 changes: 10 additions & 0 deletions tests/v5/mqtt5_operation_validation_failure_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ static struct aws_mqtt5_client_options s_good_client_options = {
.ptr = s_server_reference,
.len = AWS_ARRAY_SIZE(s_server_reference) - 1,
},
.port = 1883,
.socket_options = &s_good_socket_options,
.connect_options = &s_good_connect,
.ping_timeout_ms = 5000,
Expand Down Expand Up @@ -1183,6 +1184,15 @@ AWS_CLIENT_CREATION_VALIDATION_FAILURE(
s_good_client_options,
s_make_invalid_keep_alive_client_options)

static void s_make_invalid_port_client_options(struct aws_mqtt5_client_options *options) {
options->port = 0xFFFFFFFF;
}

AWS_CLIENT_CREATION_VALIDATION_FAILURE(
invalid_port,
s_good_client_options,
s_make_invalid_port_client_options)

#define AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST_PREFIX(packet_type, failure_reason, init_success_settings_fn) \
static int s_mqtt5_operation_##packet_type##_connection_settings_validation_failure_##failure_reason##_fn( \
struct aws_allocator *allocator, void *ctx) { \
Expand Down

0 comments on commit b6e6773

Please sign in to comment.