commit 9b6ae08: [Fix] Fix Content-Type parsing

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Feb 18 15:14:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-02-18 15:10:52 +0000
URL: https://github.com/rspamd/rspamd/commit/9b6ae0883bfb26e2fafd3717c1e66e948917a1fc (HEAD -> master)

[Fix] Fix Content-Type parsing
Issue: #2757

---
 src/libmime/mime_headers.c | 109 +++++++++++++++++++++++----------------------
 src/libmime/mime_parser.c  |   4 +-
 2 files changed, 57 insertions(+), 56 deletions(-)

diff --git a/src/libmime/mime_headers.c b/src/libmime/mime_headers.c
index d0388a632..caf69cd16 100644
--- a/src/libmime/mime_headers.c
+++ b/src/libmime/mime_headers.c
@@ -639,68 +639,69 @@ rspamd_mime_header_decode (rspamd_mempool_t *pool, const gchar *in,
 				if (qmarks < 3) {
 					state = got_encoded_start;
 				}
-				/* Finished encoded boundary */
-				else if (rspamd_rfc2047_parser (c, p - c + 1, &encoding,
-						&cur_charset.begin, &cur_charset.len,
-						&tok_start, &tok_len)) {
-					/* We have a token, so we can decode it from `encoding` */
-					if (token->len > 0) {
-						if (old_charset.len == 0) {
-							memcpy (&old_charset, &cur_charset,
-									sizeof (old_charset));
-						}
-
-						rspamd_mime_header_maybe_save_token (pool, out,
-								token, decoded,
-								&old_charset, &cur_charset);
+				else {
+					/* Finished encoded boundary */
+					if (*c == '"') {
+						/* Quoted string, non-RFC conformant but used by retards */
+						c ++;
 					}
+					if (rspamd_rfc2047_parser (c, p - c + 1, &encoding,
+							&cur_charset.begin, &cur_charset.len,
+							&tok_start, &tok_len)) {
+						/* We have a token, so we can decode it from `encoding` */
+						if (token->len > 0) {
+							if (old_charset.len == 0) {
+								memcpy (&old_charset, &cur_charset,
+										sizeof (old_charset));
+							}
 
-					qmarks = 0;
-					pos = token->len;
-					g_byte_array_set_size (token, pos + tok_len);
+							rspamd_mime_header_maybe_save_token (pool, out,
+									token, decoded,
+									&old_charset, &cur_charset);
+						}
 
-					if (encoding == RSPAMD_RFC2047_QP) {
-						r = rspamd_decode_qp2047_buf (tok_start, tok_len,
-								token->data + pos, tok_len);
+						qmarks = 0;
+						pos = token->len;
+						g_byte_array_set_size (token, pos + tok_len);
 
-						if (r != -1) {
-							token->len = pos + r;
-						}
-						else {
-							/* Cannot decode qp */
-							token->len -= tok_len;
-						}
-					}
-					else {
-						if (rspamd_cryptobox_base64_decode (tok_start, tok_len,
-								token->data + pos, &tok_len)) {
-							token->len = pos + tok_len;
-						}
-						else {
-							/* Cannot decode */
-							token->len -= tok_len;
-						}
-					}
+						if (encoding == RSPAMD_RFC2047_QP) {
+							r = rspamd_decode_qp2047_buf (tok_start, tok_len,
+									token->data + pos, tok_len);
 
-					c = p + 1;
-					state = skip_spaces;
-				}
-				else {
-					/* Not encoded-word */
-					old_charset.len = 0;
+							if (r != -1) {
+								token->len = pos + r;
+							} else {
+								/* Cannot decode qp */
+								token->len -= tok_len;
+							}
+						} else {
+							if (rspamd_cryptobox_base64_decode (tok_start, tok_len,
+									token->data + pos, &tok_len)) {
+								token->len = pos + tok_len;
+							} else {
+								/* Cannot decode */
+								token->len -= tok_len;
+							}
+						}
 
-					if (token->len > 0) {
-						rspamd_mime_header_maybe_save_token (pool, out,
-								token, decoded,
-								&old_charset, &cur_charset);
-					}
+						c = p + 1;
+						state = skip_spaces;
+					} else {
+						/* Not encoded-word */
+						old_charset.len = 0;
 
-					g_string_append_len (out, c, p - c);
-					c = p;
-					state = parse_normal;
-				}
+						if (token->len > 0) {
+							rspamd_mime_header_maybe_save_token (pool, out,
+									token, decoded,
+									&old_charset, &cur_charset);
+						}
 
-			}
+						g_string_append_len (out, c, p - c);
+						c = p;
+						state = parse_normal;
+					}
+				} /* qmarks >= 3 */
+			} /* p == '=' */
 			else {
 				state = got_encoded_start;
 			}
diff --git a/src/libmime/mime_parser.c b/src/libmime/mime_parser.c
index 7151140cb..fb81c36ac 100644
--- a/src/libmime/mime_parser.c
+++ b/src/libmime/mime_parser.c
@@ -609,7 +609,7 @@ rspamd_mime_process_multipart_node (struct rspamd_task *task,
 
 		for (i = 0; i < hdrs->len; i ++) {
 			hdr = g_ptr_array_index (hdrs, i);
-			ct = rspamd_content_type_parse (hdr->value, strlen (hdr->value),
+			ct = rspamd_content_type_parse (hdr->decoded, strlen (hdr->decoded),
 					task->task_pool);
 
 			/* Here we prefer multipart content-type or any content-type */
@@ -1240,7 +1240,7 @@ rspamd_mime_parse_message (struct rspamd_task *task,
 	else {
 		for (i = 0; i < hdrs->len; i ++) {
 			hdr = g_ptr_array_index (hdrs, i);
-			ct = rspamd_content_type_parse (hdr->value, strlen (hdr->value),
+			ct = rspamd_content_type_parse (hdr->decoded, strlen (hdr->decoded),
 					task->task_pool);
 
 			/* Here we prefer multipart content-type or any content-type */


More information about the Commits mailing list