Skip to content
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
10 changes: 10 additions & 0 deletions src/Http/Headers/src/SetCookieHeaderValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ private static int GetSetCookieLength(StringSegment input, int startIndex, out S
maxAge = -maxAge;
}

// Check if maxAge would cause TimeSpan.FromSeconds to overflow
// TimeSpan.MaxValue.TotalSeconds is approximately 922337203685.4775
const long MaxTimeSpanSeconds = 922337203685L;
const long MinTimeSpanSeconds = -922337203685L;
if (maxAge is > MaxTimeSpanSeconds or < MinTimeSpanSeconds)
{
// MaxAge value would overflow TimeSpan, abort
return 0;
}

result.MaxAge = TimeSpan.FromSeconds(maxAge);
offset += itemLength;
}
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