Skip to content

Commit 6ab4e89

Browse files
xml escaping handling
1 parent e21f11b commit 6ab4e89

7 files changed

Lines changed: 48 additions & 51 deletions

File tree

include/aws/s3/private/s3_util.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ int aws_xml_get_body_at_path(
236236
const char *path_name_array[],
237237
struct aws_byte_cursor *out_body);
238238

239-
/* replace " with escaped /"
240-
* Returns initialized aws_byte_buf */
241-
AWS_S3_API
242-
struct aws_byte_buf aws_replace_quote_entities(struct aws_allocator *allocator, struct aws_byte_cursor src);
243-
244239
/* strip quotes if string is enclosed in quotes. does not remove quotes if they only appear on either side of the string
245240
*/
246241
AWS_S3_API

source/s3_auto_ranged_put.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,13 +1756,20 @@ static void s_s3_auto_ranged_put_request_finished(
17561756
aws_xml_get_body_at_path(meta_request->allocator, xml_doc, xml_path, &etag_header_value);
17571757

17581758
if (etag_header_value.len > 0) {
1759-
struct aws_byte_buf etag_header_value_byte_buf =
1760-
aws_replace_quote_entities(meta_request->allocator, etag_header_value);
1761-
1762-
aws_http_headers_set(
1763-
request->send_data.response_headers,
1764-
g_etag_header_name,
1765-
aws_byte_cursor_from_buf(&etag_header_value_byte_buf));
1759+
struct aws_byte_buf etag_header_value_byte_buf;
1760+
aws_byte_buf_init(&etag_header_value_byte_buf, meta_request->allocator, 20);
1761+
if (aws_byte_buf_append_unescaped_xml(
1762+
meta_request->allocator, etag_header_value, &etag_header_value_byte_buf)) {
1763+
AWS_LOGF_ERROR(
1764+
AWS_LS_S3_META_REQUEST, "Server returned unexpected etag format. skipping etag unescaping");
1765+
aws_http_headers_set(
1766+
request->send_data.response_headers, g_etag_header_name, etag_header_value);
1767+
} else {
1768+
aws_http_headers_set(
1769+
request->send_data.response_headers,
1770+
g_etag_header_name,
1771+
aws_byte_cursor_from_buf(&etag_header_value_byte_buf));
1772+
}
17661773

17671774
aws_byte_buf_clean_up(&etag_header_value_byte_buf);
17681775
}

source/s3_copy_object.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@ static struct aws_string *s_etag_new_from_upload_part_copy_response(
569569
const char *xml_path[] = {"CopyPartResult", "ETag", NULL};
570570
aws_xml_get_body_at_path(allocator, xml_doc, xml_path, &etag_within_xml_quotes);
571571

572-
struct aws_byte_buf etag_within_quotes_byte_buf = aws_replace_quote_entities(allocator, etag_within_xml_quotes);
572+
struct aws_byte_buf etag_within_quotes_byte_buf;
573+
aws_byte_buf_init(&etag_within_quotes_byte_buf, allocator, 20);
574+
aws_byte_buf_append_unescaped_xml(allocator, etag_within_xml_quotes, &etag_within_quotes_byte_buf);
573575

574576
struct aws_string *stripped_etag =
575577
aws_strip_quotes(allocator, aws_byte_cursor_from_buf(&etag_within_quotes_byte_buf));
@@ -770,13 +772,20 @@ static void s_s3_copy_object_request_finished(
770772
const char *xml_path[] = {"CompleteMultipartUploadResult", "ETag", NULL};
771773
aws_xml_get_body_at_path(meta_request->allocator, xml_doc, xml_path, &etag_header_value);
772774
if (etag_header_value.len > 0) {
773-
struct aws_byte_buf etag_header_value_byte_buf =
774-
aws_replace_quote_entities(meta_request->allocator, etag_header_value);
775-
776-
aws_http_headers_set(
777-
request->send_data.response_headers,
778-
g_etag_header_name,
779-
aws_byte_cursor_from_buf(&etag_header_value_byte_buf));
775+
struct aws_byte_buf etag_header_value_byte_buf;
776+
aws_byte_buf_init(&etag_header_value_byte_buf, meta_request->allocator, 20);
777+
if (aws_byte_buf_append_unescaped_xml(
778+
meta_request->allocator, etag_header_value, &etag_header_value_byte_buf)) {
779+
AWS_LOGF_ERROR(
780+
AWS_LS_S3_META_REQUEST, "Server returned unexpected etag format. skipping etag unescaping");
781+
aws_http_headers_set(
782+
request->send_data.response_headers, g_etag_header_name, etag_header_value);
783+
} else {
784+
aws_http_headers_set(
785+
request->send_data.response_headers,
786+
g_etag_header_name,
787+
aws_byte_cursor_from_buf(&etag_header_value_byte_buf));
788+
}
780789

781790
aws_byte_buf_clean_up(&etag_header_value_byte_buf);
782791
}

source/s3_list_objects.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,13 @@ static int s_on_list_bucket_result_node_encountered(struct aws_xml_node *node, v
131131
fs_wrapper.fs_info.prefix = aws_byte_cursor_from_string(operation_data->prefix);
132132
}
133133

134-
struct aws_byte_buf trimmed_etag = aws_replace_quote_entities(fs_wrapper.allocator, fs_wrapper.fs_info.e_tag);
135-
fs_wrapper.fs_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
134+
struct aws_byte_buf trimmed_etag;
135+
aws_byte_buf_init(&trimmed_etag, fs_wrapper.allocator, 20);
136+
137+
if (aws_byte_buf_append_unescaped_xml(fs_wrapper.allocator, fs_wrapper.fs_info.e_tag, &trimmed_etag) ==
138+
AWS_OP_SUCCESS) {
139+
fs_wrapper.fs_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
140+
}
136141

137142
int ret_val = AWS_OP_SUCCESS;
138143
if (operation_data->on_object) {

source/s3_list_parts.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,13 @@ static int s_xml_on_ListPartsResult_child(struct aws_xml_node *node, void *user_
142142
return AWS_OP_ERR;
143143
}
144144

145-
struct aws_byte_buf trimmed_etag =
146-
aws_replace_quote_entities(result_wrapper.allocator, result_wrapper.part_info.e_tag);
147-
result_wrapper.part_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
145+
struct aws_byte_buf trimmed_etag;
146+
aws_byte_buf_init(&trimmed_etag, result_wrapper.allocator, 20);
147+
148+
if (aws_byte_buf_append_unescaped_xml(
149+
result_wrapper.allocator, result_wrapper.part_info.e_tag, &trimmed_etag) == AWS_OP_SUCCESS) {
150+
result_wrapper.part_info.e_tag = aws_byte_cursor_from_buf(&trimmed_etag);
151+
}
148152

149153
int ret_val = AWS_OP_SUCCESS;
150154
if (operation_data->on_part) {

source/s3_util.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -333,31 +333,6 @@ void aws_s3_init_default_signing_config(
333333
signing_config->signed_body_value = g_aws_signed_body_value_unsigned_payload;
334334
}
335335

336-
static struct aws_byte_cursor s_quote_entity_literal = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(""");
337-
static struct aws_byte_cursor s_quote_literal = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("\"");
338-
339-
struct aws_byte_buf aws_replace_quote_entities(struct aws_allocator *allocator, struct aws_byte_cursor src) {
340-
struct aws_byte_buf out_buf;
341-
aws_byte_buf_init(&out_buf, allocator, src.len);
342-
343-
for (size_t i = 0; i < src.len; ++i) {
344-
size_t chars_remaining = src.len - i;
345-
346-
if (chars_remaining >= s_quote_entity_literal.len &&
347-
!strncmp((const char *)&src.ptr[i], (const char *)s_quote_entity_literal.ptr, s_quote_entity_literal.len)) {
348-
/* Append quote */
349-
aws_byte_buf_append(&out_buf, &s_quote_literal);
350-
i += s_quote_entity_literal.len - 1;
351-
} else {
352-
/* Append character */
353-
struct aws_byte_cursor character_cursor = aws_byte_cursor_from_array(&src.ptr[i], 1);
354-
aws_byte_buf_append(&out_buf, &character_cursor);
355-
}
356-
}
357-
358-
return out_buf;
359-
}
360-
361336
struct aws_string *aws_strip_quotes(struct aws_allocator *allocator, struct aws_byte_cursor in_cur) {
362337

363338
if (in_cur.len >= 2 && in_cur.ptr[0] == '"' && in_cur.ptr[in_cur.len - 1] == '"') {

tests/s3_util_tests.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ static int s_test_s3_replace_quote_entities(struct aws_allocator *allocator, voi
133133
for (size_t i = 0; i < AWS_ARRAY_SIZE(test_cases); ++i) {
134134
struct test_case *test_case = &test_cases[i];
135135

136-
struct aws_byte_buf result_byte_buf = aws_replace_quote_entities(allocator, test_case->test_string);
136+
struct aws_byte_buf result_byte_buf;
137+
aws_byte_buf_init(&result_byte_buf, allocator, 20);
138+
ASSERT_SUCCESS(aws_byte_buf_append_unescaped_xml(allocator, test_case->test_string, &result_byte_buf));
137139

138140
struct aws_byte_cursor result_byte_cursor = aws_byte_cursor_from_buf(&result_byte_buf);
139141

0 commit comments

Comments
 (0)