Skip to content

Commit 62c0055

Browse files
committed
validate username length
1 parent e957a64 commit 62c0055

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

source/mqtt_iot_sdk_metrics.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include <stdio.h>
1313

1414
// MQTT payload size https://docs.aws.amazon.com/general/latest/gr/iot-core.html#thing-limits
15-
const int AWS_IOT_MAX_CONTENT_SIZE = 128 * 1024;
15+
const int AWS_IOT_MAX_USERNAME_SIZE = 128 * 1024;
1616
const size_t DEFAULT_QUERY_PARAM_COUNT = 10;
1717

1818
// Build username query string from params_list, the caller is responsible to init and clean up output_username
@@ -54,9 +54,11 @@ int s_build_username_query(
5454
}
5555

5656
if (output_username) {
57-
aws_byte_buf_append(output_username, &param.key);
58-
aws_byte_buf_append(output_username, &key_value_delim);
59-
aws_byte_buf_append(output_username, &param.value);
57+
if (aws_byte_buf_append(output_username, &param.key) ||
58+
aws_byte_buf_append(output_username, &key_value_delim) ||
59+
aws_byte_buf_append(output_username, &param.value)) {
60+
return AWS_OP_ERR;
61+
}
6062
}
6163

6264
if (out_full_username_size) {
@@ -85,7 +87,7 @@ int aws_mqtt_append_sdk_metrics_to_username(
8587

8688
/* Build metrics string */
8789
struct aws_byte_buf metrics_string;
88-
if (aws_byte_buf_init(&metrics_string, allocator, AWS_IOT_MAX_CONTENT_SIZE)) {
90+
if (aws_byte_buf_init(&metrics_string, allocator, AWS_IOT_MAX_USERNAME_SIZE)) {
8991
return AWS_OP_ERR;
9092
}
9193

@@ -149,13 +151,19 @@ int aws_mqtt_append_sdk_metrics_to_username(
149151
size_t total_size = 0;
150152
s_build_username_query(original_username, base_username_length, &params_list, NULL, &total_size);
151153

154+
if (total_size > AWS_IOT_MAX_USERNAME_SIZE) {
155+
goto cleanup;
156+
}
157+
152158
if (output_username && aws_byte_buf_init(output_username, allocator, total_size)) {
153159
goto cleanup;
154160
}
155161

156162
// build final output username
157-
s_build_username_query(
158-
original_username, base_username_length, &params_list, output_username, out_full_username_size);
163+
if(s_build_username_query(
164+
original_username, base_username_length, &params_list, output_username, out_full_username_size)){
165+
goto cleanup;
166+
}
159167

160168
aws_byte_buf_clean_up(&metrics_string);
161169
result = AWS_OP_SUCCESS;

tests/shared_utils_tests.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static int s_test_mqtt_append_sdk_metrics_long_strings(struct aws_allocator *all
291291
(void)ctx;
292292

293293
char long_username[1024];
294-
char long_sdk_name[512];
294+
char long_sdk_name[2*1024*128];
295295

296296
memset(long_username, 'A', sizeof(long_username) - 1);
297297
long_username[sizeof(long_username) - 1] = '\0';
@@ -314,7 +314,6 @@ static int s_test_mqtt_append_sdk_metrics_long_strings(struct aws_allocator *all
314314
// aws_mqtt_append_sdk_metrics_to_username fails when the extra metrics string exceeds buffer limit
315315
ASSERT_FAILS(
316316
aws_mqtt_append_sdk_metrics_to_username(allocator, &original_username, metrics, &output_username, NULL));
317-
ASSERT_INT_EQUALS(aws_last_error(), AWS_ERROR_DEST_COPY_TOO_SMALL);
318317

319318
aws_byte_buf_clean_up(&output_username);
320319

0 commit comments

Comments
 (0)