Skip to content

Commit 53691ff

Browse files
Added hardened boundary condition checks. (awslabs#71)
* Added hardened boundary condition checks. * Fix tests and overly aggressive assertions. * Rewrote several parsers to use byte_cursor, applied cr feedback. * Fixed bug in header length calculation. * Fix type warning for msvc. * Add new define for max header name length.
1 parent 691f5c8 commit 53691ff

File tree

6 files changed

+406
-201
lines changed

6 files changed

+406
-201
lines changed

include/aws/event-stream/event_stream.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
/* max header size is 128kb */
2121
#define AWS_EVENT_STREAM_MAX_HEADERS_SIZE (128 * 1024)
2222

23+
/* Max header name length is 127 bytes */
24+
#define AWS_EVENT_STREAM_HEADER_NAME_LEN_MAX (INT8_MAX)
25+
2326
enum aws_event_stream_errors {
2427
AWS_ERROR_EVENT_STREAM_BUFFER_LENGTH_MISMATCH = AWS_ERROR_ENUM_BEGIN_RANGE(AWS_C_EVENT_STREAM_PACKAGE_ID),
2528
AWS_ERROR_EVENT_STREAM_INSUFFICIENT_BUFFER_LEN,
@@ -54,8 +57,7 @@ struct aws_event_stream_message_prelude {
5457

5558
struct aws_event_stream_message {
5659
struct aws_allocator *alloc;
57-
uint8_t *message_buffer;
58-
uint8_t owns_buffer;
60+
struct aws_byte_buf message_buffer;
5961
};
6062

6163
#define AWS_EVENT_STREAM_PRELUDE_LENGTH (uint32_t)(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(uint32_t))
@@ -76,6 +78,7 @@ enum aws_event_stream_header_value_type {
7678
AWS_EVENT_STREAM_HEADER_UUID
7779
};
7880

81+
static const uint16_t UUID_LEN = 16U;
7982
struct aws_event_stream_header_value_pair {
8083
uint8_t header_name_len;
8184
char header_name[INT8_MAX];
@@ -244,6 +247,22 @@ AWS_EVENT_STREAM_API const uint8_t *aws_event_stream_message_buffer(const struct
244247
AWS_EVENT_STREAM_API uint32_t
245248
aws_event_stream_compute_headers_required_buffer_len(const struct aws_array_list *headers);
246249

250+
/**
251+
* Writes headers to buf assuming buf is large enough to hold the data. Prefer this function over the unsafe variant
252+
* 'aws_event_stream_write_headers_to_buffer'.
253+
*
254+
* Returns AWS_OP_SUCCESS if the headers were successfully and completely written and AWS_OP_ERR otherwise.
255+
*/
256+
AWS_EVENT_STREAM_API int aws_event_stream_write_headers_to_buffer_safe(
257+
const struct aws_array_list *headers,
258+
struct aws_byte_buf *buf);
259+
260+
/**
261+
* Deprecated in favor of 'aws_event_stream_write_headers_to_buffer_safe' as this API is unsafe.
262+
*
263+
* Writes headers to buffer and returns the length of bytes written to buffer. Assumes buffer is large enough to
264+
* store the headers.
265+
*/
247266
AWS_EVENT_STREAM_API size_t
248267
aws_event_stream_write_headers_to_buffer(const struct aws_array_list *headers, uint8_t *buffer);
249268

0 commit comments

Comments
 (0)