Bug 18800 - IndexOutOfRangeException when MultipartFormDataContent filename value contains whitespace
Summary: IndexOutOfRangeException when MultipartFormDataContent filename value contain...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: master
Hardware: Macintosh Windows
: Normal normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2014-04-03 12:44 UTC by leankitryan
Modified: 2015-03-25 10:18 UTC (History)
6 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description leankitryan 2014-04-03 12:44:57 UTC
var fileName = "~stacktrace & stuff.txt";
var content = new MultipartFormDataContent();
var fileStream = new FileStream(String.Format(@"/mnt/sdcard/Download/{0}", fileName), FileMode.Open, FileAccess.Read);
content.Add(new StreamContent(fileStream), "attachmentFile", fileName);

throws the following exception at content.Add 

System.IndexOutOfRangeException: Array index is out of range.
  at System.Net.Http.Headers.Lexer.IsValidToken (System.String input) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.ContentDispositionHeaderValue.EncodeBase64Value (System.String value) [0x00000] in <filename unknown>:0 
  at System.Net.Http.Headers.ContentDispositionHeaderValue.set_FileName (System.String value) [0x00000] in <filename unknown>:0 

If I remove spaces and special characters no exception is thrown. I've tried wrapping the value in quotes but that doesn't seem to help either. Inspecting the HTTP headers this code generates shows mono.droid is not wrapping the filename value in quotes however both standard.net and mono properly wrap this value in quotes and do not throw an exception.
Comment 2 Udham Singh 2014-04-04 06:40:37 UTC
Exception log : https://gist.github.com/saurabh360/6ca2d61c63db589e1287
Comment 3 Jonathan Pryor 2014-04-04 11:56:17 UTC
This is a Mono BCL bug:

$ csharp -r:System.Net.Http
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> using System.Net.Http;
csharp> using System.IO;
csharp> var content = new MultipartFormDataContent();
csharp> var source  = new StreamContent(new MemoryStream());
csharp> content.Add(source, "attachmentFile", "~stacktrace & stuff.txt");

> System.IndexOutOfRangeException: Array index is out of range.
>   at System.Net.Http.Headers.Lexer.IsValidToken (System.String input) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.Headers.ContentDispositionHeaderValue.EncodeBase64Value (System.String value) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.Headers.ContentDispositionHeaderValue.set_FileName (System.String value) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.MultipartFormDataContent.AddContentDisposition (System.Net.Http.HttpContent content, System.String name, System.String fileName) [0x00000] in <filename unknown>:0 
>   at System.Net.Http.MultipartFormDataContent.Add (System.Net.Http.HttpContent content, System.String name, System.String fileName) [0x00000] in <filename unknown>:0 
>   at <InteractiveExpressionClass>.Host (System.Object& $retval) [0x00000] in <filename unknown>:0 
>   at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <filename unknown>:0 
>   at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <filename unknown>:0
Comment 4 Marek Safar 2014-11-27 05:59:23 UTC
Fixed in master
Comment 5 Ruben Buniatyan 2015-03-25 09:46:27 UTC
It still throws for the method Add(content, name, fileName) when the 'fileName' parameter contains double quotes.
Comment 6 Marek Safar 2015-03-25 10:18:30 UTC
content.Add(source, "attachmentFile", "\"");

fails for me with mono 4 as expected

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.


Create a new report for Bug 18800 on Developer Community or GitHub if you have new information to add and do not yet see a matching report.

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments


Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Related Links: