-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Net.Mail.MailAddress.ToString() issue with embedded double quotes in DisplayName #36752
Comments
Tagging subscribers to this area: @dotnet/ncl |
|
OK, thanks. So is there no built in way to produce a full mail address string which can be re-parsed? I've added an extension method to build the elements up and escape double quotes in the DisplayName property, but it seemed like there should be a method in the framework to produce a string which can be re-parsed using the MailAddress constructor (or anything else that accepts an RFC 2822 format email address). |
Triage: We already allow proper parsing (added in #907). Adding proper serialization as mirroring functionality makes sense. Using |
Hi @karelz - Sorry, I've been busy with project deadlines. As you say, it's a simple change, so I could look at doing a PR. Do you think that it's acceptable to always return a mail address from ToString with escaped embedded double quotes? Can you envisage any circumstances where that could break existing code that uses ToString? |
I was also checking for the issue you opened but when I add double quotes string the values I am getting back the same way so can you explain. Why you need this actually I seen your expected output but it's escaping the already escaped string not the original one so what display name currently returns that seems perfect for me |
And in your extension method are you replacing only double quotes or any other special char can you show extension method ? |
@jaymin93 So, if you have a variable (say 'displayName') that contains:
and you instantiate a new MailAddress with:
if you call the ToString() method and view the result it contains this:
i.e. there are four double quote characters, and the ones around 'The Fonz' are not escaped (to a higher level than the surrounding ones). Therefore, anything trying to parse the mail address will match the first double quote with the one before 'The...', not the one after 'Winkler'. To correctly parse the mail address, the double quotes around 'The Fonz' must be escaped. We're actually getting the error when passing the value from ToString to the constructor of MailAddress again, which should parse it correctly, but I suspect that anything trying to parse an RFC 2822 mail address will have the same problem. There's information on page 9 of the RFC 2822 document about quoted pairs, and an example on page 41 of a mail address containing double quote characters, showing that they have to be escaped. The extension method that I'm using is this: public static string ToSafeString(this MailAddress mailAddress)
{
if (!string.IsNullOrEmpty(mailAddress.DisplayName))
return $"\"{mailAddress.DisplayName.Replace("\"", "\\\"")}\" <{mailAddress.Address}>";
else
return mailAddress.Address;
} As the C# string contains double quote characters that are already escaped, the extension method replaces any embedded double quotes with two additional escape characters, so that the escape character is itself escaped. Does that make sense? |
ok now i got what your concern is all about , and i will be happy to add pr for same if you do not mind . so should i go ahead and add pr ? @karelz i am interested adding pr for same , should i create overload of tostring( bool escape) or should i add replace logic in the existing to string ? |
I think we should just fix ToString() @jaymin93 . Adding any new API surface would require API review and it would defeat the intention to use ToString() for serialization as well as for debug. |
OK so I will go ahead with fixing with replacement logic and send pr will it work? |
That's the best bet - let's hope no surprises / other consideration come into your PR's way ;) Thanks @jaymin93 |
I have added logic for replacement and ran the test,(it's my first pr on gihutb and I am new here) what should I do? to add pr I try to commit the code from vs, obviously I should be not Able to commit in master and it shown me the same error. Then in which branch Should I commit this code? Please guide me in case I am doing any wrong steps for committing code thanks. |
Take a look at https://github.com/dotnet/runtime/blob/master/docs/pr-guide.md
when ready, create PR, reference this issue in description. |
try to build the whole project from command line with tests - at least once. |
The minimum required VS version is 16.6 Preview 2, see the windows-requirements.md file for more infos. |
Can you please try to restore the solution from CLI first? |
Sure, happy to help. How did you build the repository first? Did you invoke "build.cmd clr+libs -rc Release"? |
i used this steps https://github.com/dotnet/runtime/blob/master/docs/workflow/building/libraries/README.md and executed below steps for building lib git clean -xdf :: If you use Visual Studio, you might open System.Text.RegularExpressions.sln here. :: Switch to working on a given library (RegularExpressions in this case) :: Change to test directory :: Then inner loop build / test |
Oh I see what happened here. When you open VS can you also pass in the runtimeconfiguration which you built before: build.cmd -vs System.Net.Mail -rc Release |
i used command you shared build.cmd -vs System.Net.Mail -rc Release still shows the same issue ---------- Starting test discovery for requested test run ---------- |
OK sorry for the issue that you are hitting here. I will look into it later today and will get back to you by tomorrow. |
@ViktorHofer its strange my two machines 1 dell 2 surface go shows issues running test while my other PC which is not accessible most of time runs test well ( very slow 3 minutes to run single but runs ) where as all of them are on same vs and source code and windows required components installed |
Description
If a MailAddress instance is created with a DisplayName that includes embedded double quote characters, the ToString method doesn't properly escape the embedded double quotes.
The ToString method in the repository is currently:
link to above source
If the DisplayName property includes double quotes, then these are not escaped properly before surrounding the string with additional double quotes.
For example, if the display name is 'Henry "The Fonz" Winkler', when the ToString method is called, the resulting string would be:
\"Henry \"The Fonz\" Winkler\" <[email protected]>
but should be:
\"Henry \\\"The Fonz\\\" Winkler\" <[email protected]>
Configuration
Tested on .Net Core v2.2, but the source in this repository looks like it is still an issue in the latest version.
Regression?
Not known if this worked correctly on previous versions of .Net Framework / .Net Core
Other information
The text was updated successfully, but these errors were encountered: