From ccc220154544dbb05e071c905a4c12b2d2dffa6e Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Thu, 5 May 2016 07:43:11 -0400 Subject: [PATCH] Modified ParamaterList.TryParse() to handle quoted rfc2231-encoded param values Fixes issue #239 --- MimeKit/ParameterList.cs | 11 +++++++++++ UnitTests/ContentDispositionTests.cs | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/MimeKit/ParameterList.cs b/MimeKit/ParameterList.cs index b3c8aebdbd..e23cbfe213 100644 --- a/MimeKit/ParameterList.cs +++ b/MimeKit/ParameterList.cs @@ -1029,6 +1029,17 @@ internal static bool TryParse (ParserOptions options, byte[] text, ref int index } hex.Reset (); } else if (param.Encoded) { + // Note: param value is not supposed to be quoted, but issue #239 illustrates + // that this can happen in the wild. Hopefully we will not need to worry + // about quoted-pairs. + if (length >= 2 && buffer[startIndex] == (byte) '"') { + if (buffer[startIndex + length - 1] == (byte) '"') + length--; + + startIndex++; + length--; + } + value = DecodeRfc2231 (out encoding, ref decoder, hex, buffer, startIndex, length, true); method = ParameterEncodingMethod.Rfc2231; hex.Reset (); diff --git a/UnitTests/ContentDispositionTests.cs b/UnitTests/ContentDispositionTests.cs index fbf201ff04..914acb7a06 100644 --- a/UnitTests/ContentDispositionTests.cs +++ b/UnitTests/ContentDispositionTests.cs @@ -148,6 +148,17 @@ public void TestChineseFilename2047 () Assert.AreEqual ("GB18030", param.Encoding.HeaderName, "The filename encoding did not match."); } + [Test] + public void TestIssue239 () + { + const string text = " attachment; size=1049971;\n\tfilename*=\"utf-8''SBD%20%C5%A0kodov%C3%A1k%2Ejpg\""; + const string expected = "SBD Škodovák.jpg"; + ContentDisposition disposition; + + Assert.IsTrue (ContentDisposition.TryParse (text, out disposition), "Failed to parse Content-Disposition"); + Assert.AreEqual (expected, disposition.FileName, "The filename value does not match."); + } + [Test] public void TestFormData () {