Skip to content

Conversation

@aclark09
Copy link
Contributor

Remove quotes from MultipartReader boundary

Description

When a request is submitted by HttpClient via MultipartFormDataContent, the boundary value in multipart/formdata request header is enclosed with double quotes. In this case, when using MediaTypeHeaderValue.TryParse to parse the header, the MediaTypeHeaderValue will include those double quotes as boundary when parsing the header. This leads to an exception being thrown.

The solution for now is to remove quotes from the boundary passed in to the MultipartReader constructor. I also added a unit test that checks to see if a boundary with double quotes works.

Fixes #41237

@ghost ghost added area-runtime community-contribution Indicates that the PR has been added by a community member labels Apr 21, 2022
throw new ArgumentNullException(nameof(boundary));
}

boundary = HeaderUtilities.RemoveQuotes(new StringSegment(boundary)).ToString();
Copy link
Member

@Tratcher Tratcher Apr 21, 2022

Choose a reason for hiding this comment

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

Organization: Move this down to where _boundary is being initialized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So just put it right above that line? Yeah I can do that.

Copy link
Member

Choose a reason for hiding this comment

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

Does this allocate when there's nothing to do?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you talking about that fact that RemoveQuotes is called before all of the null checks? I just pushed up a change so that it's now being called right before the boundary variable is actually used, so it shouldn't be allocated unless it's used now.

Copy link
Member

Choose a reason for hiding this comment

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

It doesn't allocate.

if (IsQuoted(input))
{
input = input.Subsegment(1, input.Length - 2);
}
return input;

Choose a reason for hiding this comment

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

Have you checked if there is no string allocation when calling ToString() when double quotes are not present in an input boundary? What about checking if there are double quotes first and only after that to remove them and call ToString()?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the clarification

@Tratcher Tratcher self-assigned this Apr 21, 2022
@Tratcher Tratcher enabled auto-merge (squash) April 22, 2022 17:10
@Tratcher
Copy link
Member

Thanks @aclark09

@Tratcher Tratcher merged commit 2f68ef8 into dotnet:main Apr 22, 2022
@ghost ghost added this to the 7.0-preview5 milestone Apr 22, 2022
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions community-contribution Indicates that the PR has been added by a community member

Projects

None yet

5 participants