Skip to content

Commit aeb665a

Browse files
committed
Merge pull request #505 from neochrome/validate-host-header-argument
Added parameter check when setting Host header
2 parents cdbe353 + 83f79a5 commit aeb665a

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

RestSharp.Tests/RestRequestTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.InteropServices;
45
using System.Text;
56
using Xunit;
67
using System.Globalization;
8+
using Xunit.Extensions;
79

810
namespace RestSharp.Tests {
911
public class RestRequestTests {
@@ -17,5 +19,54 @@ public void Can_Add_Object_With_IntegerArray_property() {
1719
var request = new RestRequest();
1820
request.AddObject(new { Items = new int[] { 2, 3, 4 } });
1921
}
22+
23+
[Fact]
24+
public void Cannot_Set_Empty_Host_Header()
25+
{
26+
var request = new RestRequest();
27+
28+
var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", string.Empty));
29+
Assert.Equal("value", exception.ParamName);
30+
}
31+
32+
[Theory]
33+
[InlineData("http://localhost")]
34+
[InlineData("hostname 1234")]
35+
[InlineData("-leading.hyphen.not.allowed")]
36+
[InlineData("bad:port")]
37+
[InlineData(" no.leading.white-space")]
38+
[InlineData("no.trailing.white-space ")]
39+
[InlineData(".leading.dot.not.allowed")]
40+
[InlineData("double.dots..not.allowed")]
41+
[InlineData(".")]
42+
[InlineData(".:2345")]
43+
[InlineData(":5678")]
44+
public void Cannot_Set_Invalid_Host_Header(string value)
45+
{
46+
var request = new RestRequest();
47+
48+
var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", value));
49+
Assert.Equal("value", exception.ParamName);
50+
}
51+
52+
[Theory]
53+
[InlineData("localhost")]
54+
[InlineData("localhost:1234")]
55+
[InlineData("host.local")]
56+
[InlineData("anotherhost.local:2345")]
57+
[InlineData("www.w3.org")]
58+
[InlineData("www.w3.org:3456")]
59+
[InlineData("8.8.8.8")]
60+
[InlineData("a.1.b.2")]
61+
[InlineData("10.20.30.40:1234")]
62+
[InlineData("0host")]
63+
[InlineData("hypenated-hostname")]
64+
[InlineData("multi--hyphens")]
65+
public void Can_Set_Valid_Host_Header(string value)
66+
{
67+
var request = new RestRequest();
68+
69+
Assert.DoesNotThrow(() => request.AddHeader("Host", value));
70+
}
2071
}
2172
}

RestSharp/RestRequest.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using System.IO;
2020
using System.Linq;
2121
using System.Net;
22+
using System.Text.RegularExpressions;
2223
using RestSharp.Extensions;
2324
using RestSharp.Serializers;
2425

@@ -356,6 +357,12 @@ public IRestRequest AddParameter (string name, object value, ParameterType type)
356357
/// <returns></returns>
357358
public IRestRequest AddHeader (string name, string value)
358359
{
360+
const string portSplit = @":\d+";
361+
Func<string, bool> invalidHost = host => Uri.CheckHostName(Regex.Split(host, portSplit)[0]) == UriHostNameType.Unknown;
362+
if (name == "Host" && invalidHost(value))
363+
{
364+
throw new ArgumentException("The specified value is not a valid Host header string.", "value");
365+
}
359366
return AddParameter(name, value, ParameterType.HttpHeader);
360367
}
361368

0 commit comments

Comments
 (0)