Skip to content

Commit de2844e

Browse files
committed
Fix ArgumentOutOfRangeException when validating Basic Authorization header
1 parent 8b0b6fe commit de2844e

3 files changed

Lines changed: 49 additions & 12 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Net;
5+
using nanoFramework.TestFramework;
6+
7+
namespace HttpUnitTests
8+
{
9+
internal class HttpListenerRequestTests
10+
{
11+
// Verifies that malformed Authorization header (no space) does not cause a crash
12+
[TestMethod]
13+
public void Add_Authorization_NoSpaceMultipleChars_ShouldNotThrow()
14+
{
15+
var headers = new WebHeaderCollection();
16+
headers.Add("Authorization: a111111");
17+
string value = headers["Authorization"];
18+
Assert.AreEqual("a111111", value);
19+
}
20+
21+
// Verifies that a properly formatted Authorization header (with space) is parsed and stored correctly
22+
[TestMethod]
23+
public void Add_Authorization_ValidBasicToken_ShouldSucceed()
24+
{
25+
var headers = new WebHeaderCollection();
26+
headers.Add("Authorization: Basic a111111");
27+
string value = headers["Authorization"];
28+
Assert.AreEqual("Basic a111111", value);
29+
}
30+
}
31+
}

Tests/HttpUnitTests/HttpUnitTests.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
</PropertyGroup>
2727
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2828
<ItemGroup>
29+
<Compile Include="HttpListenerRequest.cs" />
2930
<Compile Include="HttpUtilityTest.cs" />
3031
<Compile Include="StreamContentTest.cs" />
3132
<Compile Include="ByteArrayContentTest.cs" />

nanoFramework.System.Net.Http/Http/System.Net.HttpListenerRequest.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//
1+
//
22
// Copyright (c) .NET Foundation and Contributors
33
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
44
// See LICENSE file in the project root for full license information.
@@ -206,21 +206,26 @@ internal void ParseHTTPRequest()
206206
if (headerName == "authorization")
207207
{
208208
int sepSpace = headerValue.IndexOf(' ');
209-
string authType = headerValue.Substring(0, sepSpace);
210-
if (authType.ToLower() == "basic")
209+
// Authorization header value must be in format "type credentials". If not, ignore.
210+
if (sepSpace > 0)
211211
{
212-
string authInfo = headerValue.Substring(sepSpace + 1);
213-
// authInfo is base64 encoded username and password.
214-
byte[] authInfoDecoded = Convert.FromBase64String(authInfo);
215-
char[] authInfoDecChar = System.Text.Encoding.UTF8.GetChars(authInfoDecoded);
216-
string strAuthInfo = new string(authInfoDecChar);
217-
// The strAuthInfo comes in format username:password. Parse it.
218-
int sepColon = strAuthInfo.IndexOf(':');
219-
if (sepColon != -1)
212+
string authType = headerValue.Substring(0, sepSpace);
213+
if (authType.ToLower() == "basic")
220214
{
221-
m_NetworkCredentials = new NetworkCredential(strAuthInfo.Substring(0, sepColon), strAuthInfo.Substring(sepColon + 1));
215+
string authInfo = headerValue.Substring(sepSpace + 1);
216+
// authInfo is base64 encoded username and password.
217+
byte[] authInfoDecoded = Convert.FromBase64String(authInfo);
218+
char[] authInfoDecChar = System.Text.Encoding.UTF8.GetChars(authInfoDecoded);
219+
string strAuthInfo = new string(authInfoDecChar);
220+
// The strAuthInfo comes in format username:password. Parse it.
221+
int sepColon = strAuthInfo.IndexOf(':');
222+
if (sepColon != -1)
223+
{
224+
m_NetworkCredentials = new NetworkCredential(strAuthInfo.Substring(0, sepColon), strAuthInfo.Substring(sepColon + 1));
225+
}
222226
}
223227
}
228+
224229
}
225230
}
226231

0 commit comments

Comments
 (0)