Skip to content

Commit

Permalink
Relax validation on user-specified topic aliasing
Browse files Browse the repository at this point in the history
  • Loading branch information
bretambrose committed Nov 9, 2023
1 parent ff4b239 commit 642a336
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 85 deletions.
5 changes: 2 additions & 3 deletions include/aws/mqtt/v5/mqtt5_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ enum aws_mqtt5_client_outbound_topic_alias_behavior_type {
AWS_MQTT5_COTABT_USER,

/**
* Client fails any user-specified topic aliasing and acts on the outbound alias set as an LRU cache.
* Client ignores any user-specified topic aliasing and acts on the outbound alias set as an LRU cache.
*/
AWS_MQTT5_COTABT_LRU,

/**
* Completely disable outbound topic aliasing. Attempting to set a topic alias on a PUBLISH results in
* an error.
* Completely disable outbound topic aliasing.
*/
AWS_MQTT5_COTABT_DISABLED
};
Expand Down
18 changes: 18 additions & 0 deletions source/v5/mqtt5_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,25 @@ static int s_aws_mqtt5_encoder_begin_publish(struct aws_mqtt5_encoder *encoder,

local_publish_view.topic = outbound_topic;
if (outbound_topic_alias != 0) {
AWS_LOGF_DEBUG(
AWS_LS_MQTT5_GENERAL,
"(%p) mqtt5 client encoder - PUBLISH packet using topic alias value %" PRIu16,
(void *)encoder->config.client,
outbound_topic_alias);
if (outbound_topic.len == 0) {
AWS_LOGF_DEBUG(
AWS_LS_MQTT5_GENERAL,
"(%p) mqtt5 client encoder - PUBLISH packet dropping topic field for previously established alias",
(void *)encoder->config.client);
}
local_publish_view.topic_alias = &outbound_topic_alias;
} else {
AWS_FATAL_ASSERT(local_publish_view.topic.len > 0);
AWS_LOGF_DEBUG(
AWS_LS_MQTT5_GENERAL,
"(%p) mqtt5 client encoder - PUBLISH packet not using a topic alias",
(void *)encoder->config.client);
local_publish_view.topic_alias = NULL;
}
}

Expand Down
24 changes: 0 additions & 24 deletions source/v5/mqtt5_options_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1743,30 +1743,6 @@ static int s_aws_mqtt5_packet_publish_view_validate_vs_connection_settings(
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION);
}

if (publish_view->topic_alias != NULL) {
const struct aws_mqtt5_client_options_storage *client_options = client->config;
if (client_options->topic_aliasing_options.outbound_topic_alias_behavior != AWS_MQTT5_COTABT_USER) {
AWS_LOGF_ERROR(
AWS_LS_MQTT5_GENERAL,
"id=%p: aws_mqtt5_packet_publish_view - topic alias set but outbound topic alias behavior has not "
"been set to user controlled",
(void *)publish_view);
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION);
}

if (*publish_view->topic_alias > settings->topic_alias_maximum_to_server) {
AWS_LOGF_ERROR(
AWS_LS_MQTT5_GENERAL,
"id=%p: aws_mqtt5_packet_publish_view - outbound topic alias (%d) exceeds server's topic alias "
"maximum "
"(%d)",
(void *)publish_view,
(int)(*publish_view->topic_alias),
(int)settings->topic_alias_maximum_to_server);
return aws_raise_error(AWS_ERROR_MQTT5_PUBLISH_OPTIONS_VALIDATION);
}
}

if (publish_view->retain && settings->retain_available == false) {
AWS_LOGF_ERROR(
AWS_LS_MQTT5_GENERAL,
Expand Down
3 changes: 0 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ add_test_case(mqtt5_client_options_validation_failure_invalid_keep_alive)
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)
add_test_case(mqtt5_operation_publish_connection_settings_validation_failure_exceeds_topic_alias_maximum)
add_test_case(mqtt5_operation_publish_connection_settings_validation_failure_exceeds_maximum_qos)
add_test_case(mqtt5_operation_publish_connection_settings_validation_failure_invalid_retain)
add_test_case(mqtt5_operation_disconnect_connection_settings_validation_failure_exceeds_maximum_packet_size)
Expand Down Expand Up @@ -365,9 +364,7 @@ add_test_case(mqtt5_client_inbound_alias_failure_disabled)
add_test_case(mqtt5_client_inbound_alias_failure_zero_id)
add_test_case(mqtt5_client_inbound_alias_failure_too_large_id)
add_test_case(mqtt5_client_inbound_alias_failure_unbound_id)
add_test_case(mqtt5_client_outbound_alias_disabled_failure_alias_set)
add_test_case(mqtt5_client_outbound_alias_user_failure_empty_topic)
add_test_case(mqtt5_client_outbound_alias_lru_failure_alias_set)

# a, b, c, r imply notation as the outbound resolver unit tests above
add_test_case(mqtt5_client_outbound_alias_user_success_a_b_ar_br)
Expand Down
22 changes: 0 additions & 22 deletions tests/v5/mqtt5_client_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -5842,18 +5842,6 @@ static int s_do_mqtt5_client_outbound_alias_failure_test(
return AWS_OP_SUCCESS;
}

static int s_mqtt5_client_outbound_alias_disabled_failure_alias_set_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

ASSERT_SUCCESS(s_do_mqtt5_client_outbound_alias_failure_test(allocator, AWS_MQTT5_COTABT_DISABLED));

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(
mqtt5_client_outbound_alias_disabled_failure_alias_set,
s_mqtt5_client_outbound_alias_disabled_failure_alias_set_fn)

static int s_mqtt5_client_outbound_alias_user_failure_empty_topic_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

Expand All @@ -5866,16 +5854,6 @@ AWS_TEST_CASE(
mqtt5_client_outbound_alias_user_failure_empty_topic,
s_mqtt5_client_outbound_alias_user_failure_empty_topic_fn)

static int s_mqtt5_client_outbound_alias_lru_failure_alias_set_fn(struct aws_allocator *allocator, void *ctx) {
(void)ctx;

ASSERT_SUCCESS(s_do_mqtt5_client_outbound_alias_failure_test(allocator, AWS_MQTT5_COTABT_LRU));

return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(mqtt5_client_outbound_alias_lru_failure_alias_set, s_mqtt5_client_outbound_alias_lru_failure_alias_set_fn)

struct outbound_alias_publish {
struct aws_byte_cursor topic;
uint16_t topic_alias;
Expand Down
33 changes: 0 additions & 33 deletions tests/v5/mqtt5_operation_validation_failure_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -1271,39 +1271,6 @@ AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
s_packet_size_init_settings_success_fn,
s_packet_size_init_settings_failure_fn)

static const uint16_t s_topic_alias = 5;

static struct aws_mqtt5_packet_publish_view s_exceeds_topic_alias_maximum_publish_view = {
.topic =
{
.ptr = s_good_topic,
.len = AWS_ARRAY_SIZE(s_good_topic) - 1,
},
.topic_alias = &s_topic_alias,
};

static struct aws_mqtt5_client_options_storage s_dummy_client_options;

static void s_topic_alias_init_settings_success_fn(struct aws_mqtt5_client *dummy_client) {
AWS_ZERO_STRUCT(s_dummy_client_options);
s_dummy_client_options.topic_aliasing_options.outbound_topic_alias_behavior = AWS_MQTT5_COTABT_USER;

dummy_client->config = &s_dummy_client_options;
dummy_client->negotiated_settings.maximum_packet_size_to_server = 100;
dummy_client->negotiated_settings.topic_alias_maximum_to_server = 10;
}

static void s_topic_alias_init_settings_failure_fn(struct aws_mqtt5_client *dummy_client) {
dummy_client->negotiated_settings.topic_alias_maximum_to_server = 2;
}

AWS_CONNECTION_SETTINGS_VALIDATION_FAILURE_TEST3(
publish,
exceeds_topic_alias_maximum,
s_exceeds_topic_alias_maximum_publish_view,
s_topic_alias_init_settings_success_fn,
s_topic_alias_init_settings_failure_fn)

static struct aws_mqtt5_packet_publish_view s_exceeds_maximum_qos_publish_view = {
.topic =
{
Expand Down

0 comments on commit 642a336

Please sign in to comment.