Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion src/Http/Headers/src/SetCookieHeaderValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,15 @@ private static int GetSetCookieLength(StringSegment input, int startIndex, out S
maxAge = -maxAge;
}

result.MaxAge = TimeSpan.FromSeconds(maxAge);
try
{
result.MaxAge = TimeSpan.FromSeconds(maxAge);
}
catch (ArgumentOutOfRangeException)
{
// MaxAge value would overflow TimeSpan, abort
return 0;
}
offset += itemLength;
}
// domain-av = "Domain=" domain-value
Expand Down
26 changes: 26 additions & 0 deletions src/Http/Headers/test/SetCookieHeaderValueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,30 @@ public void SetCookieHeaderValue_TryParseStrictList_FailsForAnyInvalidValues(
Assert.Null(results);
Assert.False(result);
}

[Theory]
[InlineData("name=value; max-age=922337203686")] // One more than TimeSpan.MaxValue.TotalSeconds
[InlineData("name=value; max-age=999999999999999999999")] // Much larger value
[InlineData("name=value; max-age=-922337203686")] // Negative overflow
public void SetCookieHeaderValue_TryParse_MaxAgeOverflow_ReturnsFalse(string value)
{
// Should return false instead of throwing ArgumentOutOfRangeException
bool result = SetCookieHeaderValue.TryParse(value, out var parsedValue);
Assert.False(result);
Assert.Null(parsedValue);
}

[Theory]
[InlineData("name=value; max-age=922337203685")] // Max valid value
[InlineData("name=value; max-age=-922337203685")] // Min valid value
[InlineData("name=value; max-age=0")] // Zero
[InlineData("name=value; max-age=86400")] // One day in seconds
public void SetCookieHeaderValue_TryParse_MaxAgeValid_ReturnsTrue(string value)
{
// Should successfully parse valid max-age values
bool result = SetCookieHeaderValue.TryParse(value, out var parsedValue);
Assert.True(result);
Assert.NotNull(parsedValue);
Assert.NotNull(parsedValue!.MaxAge);
}
}
Loading