Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public string DispositionType
get { return _dispositionType; }
set
{
CheckDispositionTypeFormat(value, "value");
CheckDispositionTypeFormat(value, nameof(value));
_dispositionType = value;
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ protected ContentDispositionHeaderValue(ContentDispositionHeaderValue source)

public ContentDispositionHeaderValue(string dispositionType)
{
CheckDispositionTypeFormat(dispositionType, "dispositionType");
CheckDispositionTypeFormat(dispositionType, nameof(dispositionType));
_dispositionType = dispositionType;
}

Expand Down Expand Up @@ -501,6 +501,7 @@ private bool TryDecodeMime(string input, out string output)
{
return false;
}

string[] parts = processedInput.Split('?');
// "=, encodingName, encodingType, encodedData, ="
if (parts.Length != 5 || parts[0] != "\"=" || parts[4] != "=\"" || parts[2].ToLowerInvariant() != "b")
Expand Down Expand Up @@ -564,18 +565,27 @@ private string Encode5987(string input)
private bool TryDecode5987(string input, out string output)
{
output = null;
string[] parts = input.Split('\'');
if (parts.Length != 3)

int quoteIndex = input.IndexOf('\'');
if (quoteIndex == -1)
{
return false;
}

int lastQuoteIndex = input.LastIndexOf('\'');
if (quoteIndex == lastQuoteIndex || input.IndexOf('\'', quoteIndex + 1) != lastQuoteIndex)
{
return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests that hit this? I don't see any in either the functional or unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub Added tests in a new commit. Let me know if/when this is ready to be merged, and I'll squash it.

}

string encodingString = input.Substring(0, quoteIndex);
string dataString = input.Substring(lastQuoteIndex + 1, input.Length - (lastQuoteIndex + 1));

StringBuilder decoded = new StringBuilder();
try
{
Encoding encoding = Encoding.GetEncoding(parts[0]);
Encoding encoding = Encoding.GetEncoding(encodingString);

string dataString = parts[2];
byte[] unescapedBytes = new byte[dataString.Length];
int unescapedBytesCount = 0;
for (int index = 0; index < dataString.Length; index++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ public void FileNameStar_AddNameParameterThenUseProperty_ParametersEntryIsOverwr
contentDisposition.Parameters.Remove(fileNameStar);
Assert.Null(contentDisposition.FileNameStar);
}

[Theory]
[InlineData("no_quotes")]
[InlineData("one'quote")]
[InlineData("'triple'quotes'")]
public void FileNameStar_NotTwoQuotes_IsNull(string value)
{
ContentDispositionHeaderValue contentDisposition = new ContentDispositionHeaderValue("inline");

// Note that uppercase letters are used. Comparison should happen case-insensitive.
NameValueHeaderValue fileNameStar = new NameValueHeaderValue("FILENAME*", value);
contentDisposition.Parameters.Add(fileNameStar);
Assert.Equal(1, contentDisposition.Parameters.Count);
Assert.Same(fileNameStar, contentDisposition.Parameters.First());
Assert.Null(contentDisposition.FileNameStar); // Decode failure
}

[Fact]
public void FileNameStar_NeedsEncoding_EncodedAndDecodedCorrectly()
Expand All @@ -213,8 +229,7 @@ public void FileNameStar_UnknownOrBadEncoding_PropertyFails()
NameValueHeaderValue fileNameStar = new NameValueHeaderValue("FILENAME*", "utf-99'lang'File%CZName.bat");
contentDisposition.Parameters.Add(fileNameStar);
Assert.Equal(1, contentDisposition.Parameters.Count);
Assert.Equal("FILENAME*", contentDisposition.Parameters.First().Name);
Assert.Equal("utf-99'lang'File%CZName.bat", contentDisposition.Parameters.First().Value);
Assert.Same(fileNameStar, contentDisposition.Parameters.First());
Assert.Null(contentDisposition.FileNameStar); // Decode failure

contentDisposition.FileNameStar = "new_name";
Expand Down