Skip to content

Fix escaped strings for TryGetDateTime(Offset) #63534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public static bool IsValidDateTimeOffsetParseLength(int length)
return IsInRangeInclusive(length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumEscapedDateTimeOffsetParseLength);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsValidUnescapedDateTimeOffsetParseLength(int length)
{
return IsInRangeInclusive(length, JsonConstants.MinimumDateTimeParseLength, JsonConstants.MaximumDateTimeOffsetParseLength);
}

/// <summary>
/// Parse the given UTF-8 <paramref name="source"/> as extended ISO 8601 format.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static bool TryGetEscapedDateTime(ReadOnlySpan<byte> source, out DateTime
sourceUnescaped = sourceUnescaped.Slice(0, written);
Debug.Assert(!sourceUnescaped.IsEmpty);

if (sourceUnescaped.Length <= JsonConstants.MaximumDateTimeOffsetParseLength
if (JsonHelpers.IsValidUnescapedDateTimeOffsetParseLength(sourceUnescaped.Length)
&& JsonHelpers.TryParseAsISO(sourceUnescaped, out DateTime tmp))
{
value = tmp;
Expand All @@ -285,7 +285,7 @@ public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan<byte> source, out Da
sourceUnescaped = sourceUnescaped.Slice(0, written);
Debug.Assert(!sourceUnescaped.IsEmpty);

if (sourceUnescaped.Length <= JsonConstants.MaximumDateTimeOffsetParseLength
if (JsonHelpers.IsValidUnescapedDateTimeOffsetParseLength(sourceUnescaped.Length)
&& JsonHelpers.TryParseAsISO(sourceUnescaped, out DateTimeOffset tmp))
{
value = tmp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,31 @@ void assertOdd(JsonElement elem)
}
}

[Theory]
[InlineData(">>")]
[InlineData(">>>")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, could you also add some of the inputs as listed in the original report?

">>", ">> a", "> a>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

[InlineData(">>a")]
[InlineData(">a>")]
public static void TryGetDateTimeAndOffset_InvalidPropertyValue(string testData)
{
string jsonString = JsonSerializer.Serialize(new { DateTimeProperty = testData });

byte[] dataUtf8 = Encoding.UTF8.GetBytes(jsonString);

using (JsonDocument doc = JsonDocument.Parse(dataUtf8, default))
{
JsonElement root = doc.RootElement;

Assert.False(root.GetProperty("DateTimeProperty").TryGetDateTime(out DateTime datetimeValue));
Assert.Equal(default, datetimeValue);
Assert.Throws<FormatException>(() => root.GetProperty("DateTimeProperty").GetDateTime());

Assert.False(root.GetProperty("DateTimeProperty").TryGetDateTimeOffset(out DateTimeOffset datetimeOffsetValue));
Assert.Equal(default, datetimeOffsetValue);
Assert.Throws<FormatException>(() => root.GetProperty("DateTimeProperty").GetDateTimeOffset());
}
}

[Theory]
[InlineData("")]
[InlineData(" ")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,23 @@ static void Test(string testString, bool isFinalBlock)
Test(testString, isFinalBlock: true);
Test(testString, isFinalBlock: false);
}

[Theory]
[InlineData(@"""\u001c\u0001""")]
[InlineData(@"""\u001c\u0001\u0001""")]
public static void TryGetDateTimeAndOffset_InvalidPropertyValue(string testString)
{
var dataUtf8 = Encoding.UTF8.GetBytes(testString);
var json = new Utf8JsonReader(dataUtf8);
Assert.True(json.Read());

Assert.False(json.TryGetDateTime(out var dateTime));
Assert.Equal(default, dateTime);
JsonTestHelper.AssertThrows<FormatException>(json, (json) => json.GetDateTime());

Assert.False(json.TryGetDateTimeOffset(out var dateTimeOffset));
Assert.Equal(default, dateTimeOffset);
JsonTestHelper.AssertThrows<FormatException>(json, (json) => json.GetDateTimeOffset());
}
}
}