[nginx] Mail: fixed backslash handling in IMAP literals.

Maxim Dounin mdounin at mdounin.ru
Wed May 19 01:27:26 UTC 2021


details:   https://hg.nginx.org/nginx/rev/ccdf83bee8c1
branches:  
changeset: 7841:ccdf83bee8c1
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Wed May 19 03:13:23 2021 +0300
description:
Mail: fixed backslash handling in IMAP literals.

Previously, s->backslash was set if any of the arguments was a quoted
string with a backslash character.  After successful command parsing
this resulted in all arguments being filtered to remove backslashes.
This is, however, incorrect, as backslashes should not be removed from
IMAP literals.  For example:

   S: * OK IMAP4 ready
   C: a01 login {9}
   S: + OK
   C: user\name "pass\"word"
   S: * BAD internal server error

resulted in "Auth-User: username" instead of "Auth-User: user\name"
as it should.

Fix is to apply backslash filtering on per-argument basis during parsing.

diffstat:

 src/mail/ngx_mail_imap_handler.c |  26 ++------------------------
 src/mail/ngx_mail_parse.c        |  18 +++++++++++++++++-
 2 files changed, 19 insertions(+), 25 deletions(-)

diffs (79 lines):

diff -r 379d461eccf4 -r ccdf83bee8c1 src/mail/ngx_mail_imap_handler.c
--- a/src/mail/ngx_mail_imap_handler.c	Wed May 19 03:13:22 2021 +0300
+++ b/src/mail/ngx_mail_imap_handler.c	Wed May 19 03:13:23 2021 +0300
@@ -101,10 +101,9 @@ ngx_mail_imap_init_protocol(ngx_event_t 
 void
 ngx_mail_imap_auth_state(ngx_event_t *rev)
 {
-    u_char              *p, *dst, *src, *end;
-    ngx_str_t           *arg;
+    u_char              *p;
     ngx_int_t            rc;
-    ngx_uint_t           tag, i;
+    ngx_uint_t           tag;
     ngx_connection_t    *c;
     ngx_mail_session_t  *s;
 
@@ -158,27 +157,6 @@ ngx_mail_imap_auth_state(ngx_event_t *re
         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth command: %i",
                        s->command);
 
-        if (s->backslash) {
-
-            arg = s->args.elts;
-
-            for (i = 0; i < s->args.nelts; i++) {
-                dst = arg[i].data;
-                end = dst + arg[i].len;
-
-                for (src = dst; src < end; dst++) {
-                    *dst = *src;
-                    if (*src++ == '\\') {
-                        *dst = *src++;
-                    }
-                }
-
-                arg[i].len = dst - arg[i].data;
-            }
-
-            s->backslash = 0;
-        }
-
         switch (s->mail_state) {
 
         case ngx_imap_start:
diff -r 379d461eccf4 -r ccdf83bee8c1 src/mail/ngx_mail_parse.c
--- a/src/mail/ngx_mail_parse.c	Wed May 19 03:13:22 2021 +0300
+++ b/src/mail/ngx_mail_parse.c	Wed May 19 03:13:23 2021 +0300
@@ -227,7 +227,7 @@ invalid:
 ngx_int_t
 ngx_mail_imap_parse_command(ngx_mail_session_t *s)
 {
-    u_char      ch, *p, *c;
+    u_char      ch, *p, *c, *dst, *src, *end;
     ngx_str_t  *arg;
     enum {
         sw_start = 0,
@@ -470,6 +470,22 @@ ngx_mail_imap_parse_command(ngx_mail_ses
                 }
                 arg->len = p - s->arg_start;
                 arg->data = s->arg_start;
+
+                if (s->backslash) {
+                    dst = s->arg_start;
+                    end = p;
+
+                    for (src = dst; src < end; dst++) {
+                        *dst = *src;
+                        if (*src++ == '\\') {
+                            *dst = *src++;
+                        }
+                    }
+
+                    arg->len = dst - s->arg_start;
+                    s->backslash = 0;
+                }
+
                 s->arg_start = NULL;
 
                 switch (ch) {


More information about the nginx-devel mailing list