Skip to content

Commit f8f2a47

Browse files
benyuzCopilotjosesimoes
authored
Fix ArgumentOutOfRangeException when header value is empty (#488)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: José Simões <jose.simoes@eclo.solutions>
1 parent a1719f7 commit f8f2a47

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

Tests/HttpUnitTests/HttpUnitTests.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<Compile Include="MockContent.cs" />
3636
<Compile Include="Properties\AssemblyInfo.cs" />
3737
<Compile Include="UriUnitTests.cs" />
38+
<Compile Include="WebHeaderCollectionTests.cs" />
3839
</ItemGroup>
3940
<ItemGroup>
4041
<Content Include="packages.lock.json" />
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
using System;
8+
using System.Net;
9+
10+
namespace HttpUnitTests
11+
{
12+
[TestClass]
13+
public class WebHeaderCollectionTests
14+
{
15+
[TestMethod]
16+
public void Add_Authorization_BearerWithSpaceAndNoValue()
17+
{
18+
var headers = new WebHeaderCollection();
19+
headers.Add("Authorization: Bearer ");
20+
}
21+
22+
[TestMethod]
23+
public void Add_Authorization_NoSpaceSingleChar()
24+
{
25+
var headers = new WebHeaderCollection();
26+
headers.Add("Authorization: 1");
27+
}
28+
29+
[TestMethod]
30+
public void Add_Authorization_ValidBearer()
31+
{
32+
var headers = new WebHeaderCollection();
33+
headers.Add("Authorization: Bearer a11111");
34+
string value = headers["Authorization"];
35+
Assert.AreEqual("Bearer a11111", value);
36+
}
37+
38+
[TestMethod]
39+
public void Add_Authorization_ValidTestValue()
40+
{
41+
var headers = new WebHeaderCollection();
42+
headers.Add("Authorization: test 1");
43+
string value = headers["Authorization"];
44+
Assert.AreEqual("test 1", value);
45+
}
46+
47+
[TestMethod]
48+
public void Add_Authorization_ValidSingleLetterPair()
49+
{
50+
var headers = new WebHeaderCollection();
51+
headers.Add("Authorization: a b");
52+
string value = headers["Authorization"];
53+
Assert.AreEqual("a b", value);
54+
}
55+
[TestMethod]
56+
public void Add_Authorization_EmptyValue()
57+
{
58+
var headers = new WebHeaderCollection();
59+
headers.Add("Authorization:");
60+
string value = headers["Authorization"];
61+
Assert.AreEqual(string.Empty, value);
62+
}
63+
64+
[TestMethod]
65+
public void Add_Authorization_ColonWithSpaceOnly()
66+
{
67+
var headers = new WebHeaderCollection();
68+
headers.Add("Authorization: ");
69+
string value = headers["Authorization"];
70+
Assert.AreEqual(string.Empty, value);
71+
}
72+
73+
[TestMethod]
74+
public void Add_NullHeader_ThrowsArgumentNullException()
75+
{
76+
var headers = new WebHeaderCollection();
77+
Assert.ThrowsException(typeof(ArgumentNullException), () => headers.Add(null));
78+
}
79+
80+
[TestMethod]
81+
public void Add_EmptyHeader_ThrowsArgumentNullException()
82+
{
83+
var headers = new WebHeaderCollection();
84+
Assert.ThrowsException(typeof(ArgumentNullException), () => headers.Add(string.Empty));
85+
}
86+
87+
[TestMethod]
88+
public void Add_HeaderWithNoColon_ThrowsArgumentException()
89+
{
90+
var headers = new WebHeaderCollection();
91+
Assert.ThrowsException(typeof(ArgumentException), () => headers.Add("Authorization"));
92+
}
93+
94+
[TestMethod]
95+
public void Add_HeaderNameWithSpace_ThrowsArgumentException()
96+
{
97+
var headers = new WebHeaderCollection();
98+
Assert.ThrowsException(typeof(ArgumentException), () => headers.Add("My Header: value"));
99+
}
100+
}
101+
}

nanoFramework.System.Net.Http/Http/System.Net.WebHeaders.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,16 @@ public void Add(string header)
427427
}
428428

429429
string name = header.Substring(0, colpos);
430-
string value = header.Substring(colpos + 1);
430+
// Handle empty header value
431+
string value;
432+
if (colpos + 1 >= header.Length)
433+
{
434+
value = string.Empty;
435+
}
436+
else
437+
{
438+
value = header.Substring(colpos + 1);
439+
}
431440

432441
name = CheckBadChars(name, false);
433442
ThrowOnRestrictedHeader(name);

0 commit comments

Comments
 (0)