Skip to content

Commit 2844f91

Browse files
committed
Added parameter check when setting Host header
1 parent 821153d commit 2844f91

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

RestSharp.Tests/RestRequestTests.cs

Lines changed: 64 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,67 @@ 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+
[Fact]
33+
public void Cannot_Set_Too_Long_Host_Header()
34+
{
35+
var request = new RestRequest();
36+
37+
var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", new string('a', 256)));
38+
Assert.Equal("value", exception.ParamName);
39+
}
40+
41+
[Theory]
42+
[InlineData("http://localhost")]
43+
[InlineData("hostname 1234")]
44+
[InlineData("-leading.hyphen.not.allowed")]
45+
[InlineData("not.allowéd")]
46+
[InlineData("bad:port")]
47+
[InlineData(" no.leading.white-space")]
48+
[InlineData("no.trailing.white-space ")]
49+
[InlineData(".leading.dot.not.allowed")]
50+
[InlineData("trailing.dot.not.allowed.")]
51+
[InlineData("double.dots..not.allowed")]
52+
[InlineData(".")]
53+
[InlineData(".:2345")]
54+
[InlineData(":5678")]
55+
[InlineData("1234567890123456789012345678901234567890123456789012345678901234.too.long.label")]
56+
public void Cannot_Set_Invalid_Host_Header(string value)
57+
{
58+
var request = new RestRequest();
59+
60+
var exception = Assert.Throws<ArgumentException>(() => request.AddHeader("Host", value));
61+
Assert.Equal("value", exception.ParamName);
62+
}
63+
64+
[Theory]
65+
[InlineData("localhost")]
66+
[InlineData("localhost:1234")]
67+
[InlineData("host.local")]
68+
[InlineData("anotherhost.local:2345")]
69+
[InlineData("www.w3.org")]
70+
[InlineData("www.w3.org:3456")]
71+
[InlineData("8.8.8.8")]
72+
[InlineData("a.1.b.2")]
73+
[InlineData("10.20.30.40:1234")]
74+
[InlineData("0host")]
75+
[InlineData("hypenated-hostname")]
76+
[InlineData("multi--hyphens")]
77+
[InlineData("123456789012345678901234567890123456789012345678901234567890123")]
78+
public void Can_Set_Valid_Host_Header(string value)
79+
{
80+
var request = new RestRequest();
81+
82+
Assert.DoesNotThrow(() => request.AddHeader("Host", value));
83+
}
2084
}
2185
}

RestSharp/RestRequest.cs

Lines changed: 5 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,10 @@ public IRestRequest AddParameter (string name, object value, ParameterType type)
356357
/// <returns></returns>
357358
public IRestRequest AddHeader (string name, string value)
358359
{
360+
if (name == "Host" && (value.Length > 255 || !Regex.IsMatch(value, @"^\w[a-z0-9\-]{0,62}(\.\w[a-z0-9\-]{0,62})*(\:\d+)?$")))
361+
{
362+
throw new ArgumentException("The specified value is not a valid Host header string.", "value");
363+
}
359364
return AddParameter(name, value, ParameterType.HttpHeader);
360365
}
361366

0 commit comments

Comments
 (0)