Skip to content

Commit be39346

Browse files
authored
Fix NewDateRegex in StringExtensions restsharp#1556 (restsharp#1557)
* Fix NewDateRegex in StringExtensions restsharp#1556 Previously it had exponential worst-case complexity and was vulnerable to REDoS. * Simple test for new Date(123)
1 parent 0ed7b0a commit be39346

File tree

5 files changed

+37
-1
lines changed

5 files changed

+37
-1
lines changed

src/RestSharp/Extensions/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace RestSharp.Extensions
2525
public static class StringExtensions
2626
{
2727
static readonly Regex DateRegex = new Regex(@"\\?/Date\((-?\d+)(-|\+)?([0-9]{4})?\)\\?/");
28-
static readonly Regex NewDateRegex = new Regex(@"newDate\((-?\d+)*\)");
28+
static readonly Regex NewDateRegex = new Regex(@"newDate\((-?\d+)\)");
2929

3030
static readonly Regex IsUpperCaseRegex = new Regex(@"^[A-Z]+$");
3131

test/RestSharp.Tests/JsonTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ public void Can_Deserialize_DateTimeOffset()
116116
);
117117
}
118118

119+
[Test]
120+
public void Can_Deserialize_NewDateTime()
121+
{
122+
var payload = GetPayLoad<NewDateTimeTestStructure>("newdatetimes.json");
123+
124+
Assert.AreEqual(
125+
new DateTime(2011, 6, 30, 8, 15, 46, 929, DateTimeKind.Utc),
126+
payload.DateTime
127+
);
128+
}
129+
130+
[Test]
131+
public void Can_Deserialize_Negative_NewDateTime()
132+
{
133+
var payload = GetPayLoad<NewDateTimeTestStructure>("newdatetimes.json");
134+
135+
Assert.AreEqual(
136+
new DateTime(1969, 12, 31, 23, 59, 59, 999, DateTimeKind.Utc),
137+
payload.DateTimeNegative
138+
);
139+
}
140+
119141
[Test]
120142
public void Can_Deserialize_Decimal_With_Four_Zeros_After_Floating_Point()
121143
{

test/RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@
8484
<None Update="SampleData\NestedListSample.xml">
8585
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8686
</None>
87+
<None Update="SampleData\newdatetimes.json">
88+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
89+
</None>
8790
<None Update="SampleData\objectproperty.json">
8891
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8992
</None>

test/RestSharp.Tests/SampleClasses/misc.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ public class Iso8601DateTimeTestStructure
222222
public DateTime DateTimeWithOffset { get; set; }
223223
}
224224

225+
public class NewDateTimeTestStructure
226+
{
227+
public DateTime DateTime { get; set; }
228+
229+
public DateTime DateTimeNegative { get; set; }
230+
}
231+
225232
public class TimeSpanTestStructure
226233
{
227234
public TimeSpan Tick { get; set; }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"DateTime": "new Date(1309421746929)",
3+
"DateTimeNegative": "new Date(-1)"
4+
}

0 commit comments

Comments
 (0)