You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The converters cast reader.GetDouble() to long for both milliseconds and seconds. If the JSON number exceeds Int64 range or contains fractional seconds, this could truncate/overflow silently. Consider validating range, rejecting NaN/Infinity, and handling integral tokens via TryGetInt64 first to avoid precision loss.
publicoverrideDateTimeOffsetRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){returnDateTimeOffset.FromUnixTimeMilliseconds((long)reader.GetDouble());// still might get a double}publicoverridevoidWrite(Utf8JsonWriterwriter,DateTimeOffsetvalue,JsonSerializerOptionsoptions){writer.WriteNumberValue(value.ToUnixTimeMilliseconds());}}internalclassDateTimeOffsetSecondsConverter:JsonConverter<DateTimeOffset>{publicoverrideDateTimeOffsetRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){returnDateTimeOffset.FromUnixTimeSeconds((long)reader.GetDouble());// still might get a double}publicoverridevoidWrite(Utf8JsonWriterwriter,DateTimeOffsetvalue,JsonSerializerOptionsoptions){writer.WriteNumberValue(value.ToUnixTimeSeconds());}
Only DateTimeOffsetConverter is registered globally; the new DateTimeOffsetSecondsConverter relies on property-level attributes. Confirm all cookie-related payloads (requests and responses) use the attribute-backed model; otherwise, seconds-based values may be misinterpreted elsewhere. Consider adding targeted options or context-aware converters if any path omits the attribute.
DateTimeOffsetConverter now reads with GetDouble but still writes milliseconds, while DateTimeOffsetSecondsConverter writes seconds; ensure the correct converter is consistently applied per field to avoid mixed units. Verify that all BiDi messages using DateTimeOffset still expecting milliseconds haven’t inadvertently switched or conflicted with the new seconds-based handling, preventing subtle timestamp bugs across the protocol.
internalclassDateTimeOffsetConverter:JsonConverter<DateTimeOffset>{publicoverrideDateTimeOffsetRead(refUtf8JsonReaderreader,TypetypeToConvert,JsonSerializerOptionsoptions){returnDateTimeOffset.FromUnixTimeMilliseconds((long)reader.GetDouble());// still might get a double}publicoverridevoidWrite(Utf8JsonWriterwriter,DateTimeOffsetvalue,JsonSerializerOptionsoptions){writer.WriteNumberValue(value.ToUnixTimeMilliseconds());
...(clipped 15lines)
// Default converter handles milliseconds and is registered globally.// Broker.cs
...
new DateTimeOffsetConverter(),// Uses milliseconds
...// Cookie expiry correctly uses a specific converter for seconds.// Cookie.cspublic sealed record Cookie(
...,[property:JsonConverter(typeof(DateTimeOffsetSecondsConverter))]DateTimeOffset? Expiry
);// A hypothetical other message uses the default. Is this correct?publicsealedrecordOtherMessage(DateTimeOffset?Timestamp);// Implicitly uses millisecond converter.
After:
// Default converter handles milliseconds and is registered globally.// Broker.cs
...
new DateTimeOffsetConverter(),// Uses milliseconds
...// All timestamp fields are audited and have the correct converter applied explicitly.// Cookie.cspublic sealed record Cookie(
...,[property:JsonConverter(typeof(DateTimeOffsetSecondsConverter))]DateTimeOffset? Expiry
);// Other message is verified against the spec and the correct converter is chosen.publicsealedrecordOtherMessage([JsonConverter(typeof(DateTimeOffsetSecondsConverter))]DateTimeOffset?Timestamp);
Suggestion importance[1-10]: 9
__
Why: This is a critical suggestion that highlights a potential architectural risk of inconsistent data serialization, as mixing timestamp units (seconds vs. milliseconds) can introduce subtle, hard-to-debug bugs.
High
Possible issue
Safely parse unix milliseconds
Casting a potentially large double to long can overflow or silently truncate fractional milliseconds. Handle both integer and floating-point tokens explicitly and validate range before conversion to avoid incorrect dates and exceptions.
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- return DateTimeOffset.FromUnixTimeMilliseconds((long)reader.GetDouble()); // still might get a double+ // Handle both integer and floating-point number tokens safely+ long millis;+ if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt64(out var intVal))+ {+ millis = intVal;+ }+ else+ {+ var doubleVal = reader.GetDouble();+ if (doubleVal > long.MaxValue || doubleVal < long.MinValue)+ {+ throw new JsonException("Unix time milliseconds out of range.");+ }+ millis = Convert.ToInt64(Math.Truncate(doubleVal));+ }+ return DateTimeOffset.FromUnixTimeMilliseconds(millis);
}
Apply / Chat
Suggestion importance[1-10]: 7
__
Why: The suggestion correctly identifies that casting a double to a long can cause an overflow and improves robustness by adding range checks and explicitly handling integer and floating-point tokens.
Medium
Robust unix seconds parsing
Similar to milliseconds, direct cast from double risks precision loss and overflow. Support both integer and floating-point tokens with bounds checking and truncation to whole seconds before converting.
public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
- return DateTimeOffset.FromUnixTimeSeconds((long)reader.GetDouble()); // still might get a double+ long seconds;+ if (reader.TokenType == JsonTokenType.Number && reader.TryGetInt64(out var intVal))+ {+ seconds = intVal;+ }+ else+ {+ var doubleVal = reader.GetDouble();+ if (doubleVal > long.MaxValue || doubleVal < long.MinValue)+ {+ throw new JsonException("Unix time seconds out of range.");+ }+ seconds = Convert.ToInt64(Math.Truncate(doubleVal));+ }+ return DateTimeOffset.FromUnixTimeSeconds(seconds);
}
Apply / Chat
Suggestion importance[1-10]: 7
__
Why: Similar to the previous suggestion, this one correctly identifies a potential overflow when casting a double to a long and provides a more robust implementation with proper checks.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
https://w3c.github.io/webdriver/#dfn-cookie-expiry-time
It is not milliseconds, it is seconds.
💥 What does this PR do?
Add custom json converter especially for cookie expiry only.
🔄 Types of changes
PR Type
Bug fix
Description
Fix cookie expiry time format from milliseconds to seconds per WebDriver spec
Add new DateTimeOffsetSecondsConverter for proper unix timestamp handling
Remove unused TimeSpanConverter and update Cookie model
Update test assertions to validate expiry time correctly
Diagram Walkthrough
File Walkthrough
Broker.cs
Remove unused TimeSpanConverter registrationdotnet/src/webdriver/BiDi/Communication/Broker.cs
DateTimeOffsetConverter.cs
Add seconds-based DateTimeOffset converterdotnet/src/webdriver/BiDi/Communication/Json/Converters/DateTimeOffsetConverter.cs
TimeSpanConverter.cs
Remove unused TimeSpanConverter classdotnet/src/webdriver/BiDi/Communication/Json/Converters/TimeSpanConverter.cs
Cookie.cs
Update Cookie model expiry typedotnet/src/webdriver/BiDi/Network/Cookie.cs
SetCookieCommand.cs
Apply converter to SetCookie commanddotnet/src/webdriver/BiDi/Storage/SetCookieCommand.cs
StorageTest.cs
Update cookie expiry test validationdotnet/test/common/BiDi/Storage/StorageTest.cs