Skip to content

Commit 78881f6

Browse files
committed
fb
1 parent b64e7bc commit 78881f6

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/Servers/Kestrel/Core/src/Internal/Infrastructure/HttpUtilities.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ public static string GetHeaderName(this ReadOnlySpan<byte> span)
9191

9292
var str = string.Create(span.Length, span, static (destination, source) =>
9393
{
94-
if (Ascii.ToUtf16(source, destination, out var written) != OperationStatus.Done
95-
|| destination.Contains('\0'))
94+
if (source.Contains((byte)0)
95+
|| Ascii.ToUtf16(source, destination, out var written) != OperationStatus.Done)
9696
{
9797
KestrelBadHttpRequestException.Throw(RequestRejectionReason.InvalidCharactersInHeaderName);
9898
}
99-
100-
Debug.Assert(written == destination.Length);
99+
else
100+
{
101+
Debug.Assert(written == destination.Length);
102+
}
101103
});
102104

103105
return str;
@@ -125,28 +127,28 @@ public static string GetRequestHeaderString(this ReadOnlySpan<byte> span, string
125127

126128
// New Line characters (CR, LF) are considered invalid at this point.
127129
// Null characters are also not allowed.
128-
var invalidCharIndex = checkForNewlineChars ?
129-
((ReadOnlySpan<char>)result).IndexOfAny('\r', '\n', '\0')
130-
: ((ReadOnlySpan<char>)result).IndexOf('\0');
130+
var hasInvalidChar = checkForNewlineChars ?
131+
((ReadOnlySpan<char>)result).ContainsAny('\r', '\n', '\0')
132+
: ((ReadOnlySpan<char>)result).Contains('\0');
131133

132-
if (invalidCharIndex >= 0)
134+
if (hasInvalidChar)
133135
{
134-
ThrowForInvalidCharacter(result[invalidCharIndex]);
136+
ThrowForInvalidCharacter(checkForNewlineChars);
135137
}
136138

137139
return result;
138140
}
139141

140142
[MethodImpl(MethodImplOptions.NoInlining)]
141-
private static void ThrowForInvalidCharacter(char invalidCharacter)
143+
private static void ThrowForInvalidCharacter(bool checkForNewlines)
142144
{
143-
if (invalidCharacter == 0)
145+
if (checkForNewlines)
144146
{
145-
throw new InvalidOperationException("Null characters are not allowed in request headers.");
147+
throw new InvalidOperationException("Newline characters (CR/LF) or Null are not allowed in request headers.");
146148
}
147149
else
148150
{
149-
throw new InvalidOperationException("Newline characters (CR/LF) are not allowed in request headers.");
151+
throw new InvalidOperationException("Null characters are not allowed in request headers.");
150152
}
151153
}
152154

0 commit comments

Comments
 (0)