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

MailboxAddress.TryParse not respecting parser options #465

Closed
jjxtra opened this issue Feb 18, 2019 · 5 comments
Closed

MailboxAddress.TryParse not respecting parser options #465

jjxtra opened this issue Feb 18, 2019 · 5 comments
Labels
wontfix This will not be worked on

Comments

@jjxtra
Copy link

jjxtra commented Feb 18, 2019

The following code is returning true when it should be returning false:

bool result = MailboxAddress.TryParse(ParserOptions.Default, "asdasdadsasd", out _);
result = MailboxAddress.TryParse("asdasdadsasd", out _);
// result is true for both, however ParserOptions.Default.AllowAddressesWithoutDomain is false by default.

Issue #331 was closed, but this bug remains for over a year.

@jstedfast
Copy link
Owner

Apply the following patch to your local checkout:

diff --git a/MimeKit/InternetAddress.cs b/MimeKit/InternetAddress.cs
index 9bf27526..db22262b 100644
--- a/MimeKit/InternetAddress.cs
+++ b/MimeKit/InternetAddress.cs
@@ -662,6 +662,13 @@ namespace MimeKit {
                                        return false;
                                }
 
+                               if (!options.AllowAddressesWithoutDomain) {
+                                       if (throwOnError)
+                                               throw new ParseException (string.Format ("Incomplete addr-spec token at offset {0}", startIndex), startIndex, index);
+
+                                       return false;
+                               }
+
                                // rewind back to the beginning of the local-part
                                index = startIndex;
 

Now try running the unit tests and see all the things that break.

This is a non-trivial problem to fix. Dare I say, impossible to fix without introducing far worse breakages.

@jstedfast
Copy link
Owner

I never should have added that option because I can't make it work the way you expect it to work due to real-world inputs that MimeKit must handle.

@jstedfast
Copy link
Owner

You can VERY EASILY change your code to do the following:

if (addr.IndexOf ('@') != -1 && MailboxAddress.TryParse (addr, out mailbox)) {
    // do stuff
}

@jjxtra
Copy link
Author

jjxtra commented Feb 20, 2019 via email

@jstedfast
Copy link
Owner

The option is still used to make adjustments to the parser behavior, just not the way you are expecting.

I would say the option is misleadingly named.

Here's the unit test:

[Test]
public void TestParseMailboxWithUnquotedCommaInName ()
{
	const string text = "Worthington, Warren <[email protected]>";
	InternetAddress addr;

	AssertParse (text);

	// default options should parse this as a single mailbox address
	addr = InternetAddress.Parse (text);
	Assert.AreEqual ("Worthington, Warren", addr.Name);

	// this should fail when we allow mailbox addresses w/o a domain
	var options = ParserOptions.Default.Clone ();
	options.AllowAddressesWithoutDomain = true;

	try {
		addr = InternetAddress.Parse (options, text);
		Assert.Fail ("Should not have parsed \"{0}\" with AllowAddressesWithoutDomain = true", text);
	} catch (ParseException pex) {
		Assert.AreEqual (text.IndexOf (','), pex.TokenIndex, "TokenIndex");
		Assert.AreEqual (text.IndexOf (','), pex.ErrorIndex, "ErrorIndex");
	} catch (Exception ex) {
		Assert.Fail ("Should not have thrown {0}", ex.GetType ().Name);
	}
}

@jstedfast jstedfast added the wontfix This will not be worked on label Feb 21, 2019
jstedfast added a commit that referenced this issue Feb 23, 2019
…Options.AllowAdddressesWithoutDomain

ParserOptions.AllowUnquotedCommasInAddresses is now effectively the same
as the old version of AllowAddressesWithoutDomain (but inverted).

ParserOptions.AllowAdddressesWithoutDomain now works as people expected it to work.

Fixes issue #465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants