-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[WinHTTP] Validate header values for Latin1 #116335
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -709,6 +709,20 @@ private static WinHttpChunkMode GetChunkedModeForSend(HttpRequestMessage request | |||||
| return chunkedMode; | ||||||
| } | ||||||
|
|
||||||
| private static bool ContainsOnlyValidLatin1(string headerString) | ||||||
| { | ||||||
| foreach (char c in headerString) | ||||||
| { | ||||||
| // See https://www.rfc-editor.org/rfc/rfc9110.html#section-5.5-5: | ||||||
| // "Field values containing CR, LF, or NUL characters are invalid and dangerous" | ||||||
| if (c <= 0 || c > 255 || c == '\r' || c == '\n') | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
In the future Roslyn may be able to optimize this with pattern matching.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we want to filter for CRLF here, we'd have to do it on a per-header basis. Including new lines is also a valid scenario for My preference would be to rely on the header collection to do the relevant checks here on modern .NET, and you only check that values are |
||||||
| { | ||||||
| return false; | ||||||
| } | ||||||
| } | ||||||
| return true; | ||||||
| } | ||||||
|
|
||||||
| private static void AddRequestHeaders( | ||||||
| SafeWinHttpHandle requestHandle, | ||||||
| HttpRequestMessage requestMessage, | ||||||
|
|
@@ -739,7 +753,12 @@ private static void AddRequestHeaders( | |||||
| } | ||||||
|
|
||||||
| // Serialize general request headers. | ||||||
| requestHeadersBuffer.AppendLine(requestMessage.Headers.ToString()); | ||||||
| string requestHeaders = requestMessage.Headers.ToString(); | ||||||
| if (!ContainsOnlyValidLatin1(requestHeaders)) | ||||||
| { | ||||||
| throw new FormatException(SR.Format(SR.net_http_invalid_header_value, nameof(HttpRequestMessage))); | ||||||
| } | ||||||
| requestHeadersBuffer.AppendLine(requestHeaders); | ||||||
|
|
||||||
| // Serialize entity-body (content) headers. | ||||||
| if (requestMessage.Content != null) | ||||||
|
|
@@ -754,7 +773,12 @@ private static void AddRequestHeaders( | |||||
| requestMessage.Content.Headers.ContentLength = contentLength; | ||||||
| } | ||||||
|
|
||||||
| requestHeadersBuffer.AppendLine(requestMessage.Content.Headers.ToString()); | ||||||
| string contentHeaders = requestMessage.Content.Headers.ToString(); | ||||||
| if (!ContainsOnlyValidLatin1(contentHeaders)) | ||||||
| { | ||||||
| throw new FormatException(SR.Format(SR.net_http_invalid_header_value, nameof(HttpContent))); | ||||||
| } | ||||||
| requestHeadersBuffer.AppendLine(contentHeaders); | ||||||
| } | ||||||
|
|
||||||
| // Add request headers to WinHTTP request handle. | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would vectorization help here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has to build on .NET Framework as well. So I opted for shared, simple implementation, rather than trying to micro-optimize for a subset of targets.