Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMAP: fix: backslash in password quoted string #378

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

qyanu
Copy link

@qyanu qyanu commented Dec 23, 2024

Hello Mickaël,

i recently noticed, that a backslash special character as part of my password was not accepted when accessing the exchange server through davmail's imap interface.

according to tshark, the password was transported from the imap client to davmail as a quoted string, approximately like so: LOGIN "username" "foo\\bar".
My reading of RFC3501 (IMAP version 4 rev 1) is, that after tokenizing and unquoting this would give a password value of foo\bar with a single backslash character (which is the correct password in this example). However, davmail did not correctly unquote the password string token and used thus foo\\bar (with two backslash characters) as password when communicating with the exchange server resulting in failed login.

I used RFC3501 as reference (and not RFC 9051), because davmail announces IMAP4REV1 (and not IMAP4rev2) in ImapConnection.java capability response code.

My analysis of the davmail source code resulted in the following changes:

  • StringUtil.java and StringUtilTest.java have "windows"-newlines, while the whole project uses unix-newlines. I summarily converted the files in a dedicated commit.
  • ImapConnection::ImapTokenizer::nextToken() now does the full imap-compliant unquoting of each token, with the help of a new method StringUtil::parseQuotedImapString()
  • therefore, ImapConnection::parseCredentials() need not treat backslashes in username in an unorthodox way anymore.

For a detailed description, see the commit message and code-comments.

Best regards,
Max

This change introduces a method StringUtil::parseQuotedImapString that
will return the string value of a quoted-string according to the
RFC 3501 (imap v4rev1) ABNF. While the method might not be concise, it
hopefully is correct.

This added method is then used in ImapConnection to correctly unquote
a quoted string token, which was previously done with some deviations
from RFC3501. Specifically, when the quoted string token was a password
transporting a string value that (also) contained one backslash (encoded
as a double-backslash), it would not be unquoted correctly to a single
backslash.

As a side-effect, ImapConnection::parseCredentials() needs not unquote
the first backslash in a username anymore, as all backslashes in all
quoted strings are unquoted correctly.

In order to limit the change in size, the previously existing processing
in ImapConnection::ImapTokenizer::nextToken() is left in-tact by still
calling removeQuotes() in all other cases.

Patch By: Max-Julian Pogner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant