fix(codecs): #999 K-9 malformed wire values fail as each codec's documented exception (ArgumentException / ScriptWorkerProtocolException)
This commit is contained in:
@@ -100,7 +100,16 @@ public static class TypedValueCodec
|
||||
throw new ArgumentOutOfRangeException(nameof(maxElements));
|
||||
if (value.CalculateSize() > maxBytes || DecodedBytes(value, maxBytes) > maxBytes)
|
||||
throw new ArgumentException("Decoded value exceeds budget.");
|
||||
return DecodeCore(value, maxElements, maxBytes);
|
||||
try
|
||||
{
|
||||
return DecodeCore(value, maxElements, maxBytes);
|
||||
}
|
||||
catch (Exception ex) when (ex is OverflowException or FormatException)
|
||||
{
|
||||
// #999 / review K-9: an out-of-range narrowing (an Int8 carrying 300) or a malformed GUID is a bad REQUEST,
|
||||
// so it fails as the documented ArgumentException (→ gRPC InvalidArgument), not an unmapped internal error.
|
||||
throw new ArgumentException($"Invalid typed value: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
internal static long DecodedBytes(Wire.TypedValue value, long limit)
|
||||
@@ -132,10 +141,22 @@ public static class TypedValueCodec
|
||||
return size;
|
||||
}
|
||||
|
||||
/// <summary>0001-01-01T00:00:00Z as Unix seconds — the protobuf Timestamp / DateTimeOffset lower bound.</summary>
|
||||
private const long MinTimestampSeconds = -62_135_596_800L;
|
||||
|
||||
/// <summary>9999-12-31T23:59:59Z as Unix seconds — the protobuf Timestamp / DateTimeOffset upper bound.</summary>
|
||||
private const long MaxTimestampSeconds = 253_402_300_799L;
|
||||
|
||||
internal static DateTimeOffset DecodeTimestamp(Timestamp value)
|
||||
{
|
||||
if (value.Nanos % 100 != 0)
|
||||
throw new ArgumentException("Timestamp is finer than domain tick precision.");
|
||||
// #999 / review K-9: Timestamp.ToDateTimeOffset throws InvalidOperationException for seconds/nanos outside the
|
||||
// representable range — which the gRPC service maps to Unavailable, telling the client to retry or fail over a
|
||||
// request that can never succeed. Range-check first so it is the documented ArgumentException.
|
||||
if (value.Seconds < MinTimestampSeconds || value.Seconds > MaxTimestampSeconds
|
||||
|| value.Nanos < 0 || value.Nanos > 999_999_999)
|
||||
throw new ArgumentException("Timestamp is outside the representable range.");
|
||||
return value.ToDateTimeOffset();
|
||||
}
|
||||
|
||||
|
||||
@@ -61,6 +61,21 @@ public static class VariantCodec
|
||||
/// <returns>The boxed value.</returns>
|
||||
/// <exception cref="ScriptWorkerProtocolException">An unknown tag, or a payload that does not match the tag.</exception>
|
||||
public static object? Decode(Variant? variant)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DecodeCore(variant);
|
||||
}
|
||||
catch (Exception ex) when (ex is OverflowException or ArgumentOutOfRangeException)
|
||||
{
|
||||
// #999 / review K-9: an out-of-range payload (a Byte carrying 300, DatetimeUtc ticks past DateTime.MaxValue)
|
||||
// is a protocol fault like any other contradictory payload. OutOfProcessScriptRunner.Complete catches only
|
||||
// ScriptWorkerProtocolException, so these used to fault the run task of a runner documented "never faults".
|
||||
throw new ScriptWorkerProtocolException($"variant tagged {variant!.Type} is out of range: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static object? DecodeCore(Variant? variant)
|
||||
{
|
||||
if (variant is null) return null;
|
||||
return variant.Type switch
|
||||
@@ -112,7 +127,7 @@ public static class VariantCodec
|
||||
var item = array.Items[i];
|
||||
if (item.Type != array.ElementType && item.Type != Variant.Types.TypeTag.Null)
|
||||
throw new ScriptWorkerProtocolException($"array of {array.ElementType} holds a {item.Type} element");
|
||||
result.SetValue(Decode(item), i);
|
||||
result.SetValue(DecodeCore(item), i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -514,4 +514,48 @@ public sealed class GrpcDataCodecTests
|
||||
DataProtoMapper.DecodeBrowseResponse(response, new DataDecodeLimits(1, 10000, 100))
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #999 / review K-9: a malformed wire value fails as the codec's documented <see cref="ArgumentException"/> (the
|
||||
/// gRPC service maps it to <c>InvalidArgument</c>) — never an <see cref="OverflowException"/> /
|
||||
/// <see cref="FormatException"/> (unmapped: an internal error) or an <see cref="InvalidOperationException"/>
|
||||
/// (mapped to <c>Unavailable</c>: the client retries or fails over a request that can never succeed).
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[MemberData(nameof(MalformedValues))]
|
||||
public void Malformed_wire_values_fail_as_ArgumentException(string _, Protos.DataConnection.V1.TypedValue wire)
|
||||
{
|
||||
var ex = Should.Throw<Exception>(() => TypedValueCodec.Decode(wire));
|
||||
ex.ShouldBeAssignableTo<ArgumentException>($"{ex.GetType().Name}: {ex.Message}");
|
||||
}
|
||||
|
||||
/// <summary>K-9: an out-of-range protobuf timestamp is an ArgumentException at the shared timestamp decoder too.</summary>
|
||||
[Fact]
|
||||
public void Out_of_range_timestamps_fail_as_ArgumentException()
|
||||
{
|
||||
var ex = Should.Throw<Exception>(() => TypedValueCodec.DecodeTimestamp(new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = long.MaxValue }));
|
||||
ex.ShouldBeAssignableTo<ArgumentException>($"{ex.GetType().Name}: {ex.Message}");
|
||||
}
|
||||
|
||||
public static TheoryData<string, Protos.DataConnection.V1.TypedValue> MalformedValues()
|
||||
{
|
||||
var data = new TheoryData<string, Protos.DataConnection.V1.TypedValue>
|
||||
{
|
||||
{ "Int8 300", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int8, Signed = 300 } },
|
||||
{ "UInt8 300", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.UInt8, Unsigned = 300 } },
|
||||
{ "Int32 overflow", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int32, Signed = long.MaxValue } },
|
||||
{ "bad guid", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Guid, Guid = "not-a-guid" } },
|
||||
{ "timestamp out of range", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Timestamp, Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = long.MaxValue } } },
|
||||
{ "timestamp bad nanos", new() { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Timestamp, Timestamp = new Google.Protobuf.WellKnownTypes.Timestamp { Seconds = 0, Nanos = -100 } } },
|
||||
};
|
||||
var array = new Protos.DataConnection.V1.TypedValue
|
||||
{
|
||||
Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Array,
|
||||
Array = new Protos.DataConnection.V1.TypedArray { ElementKind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int16 },
|
||||
};
|
||||
array.Array.Dimensions.Add(1);
|
||||
array.Array.Elements.Add(new Protos.DataConnection.V1.TypedValue { Kind = (Protos.DataConnection.V1.ValueKind)(int)DataValueKind.Int16, Signed = 1 << 20 });
|
||||
data.Add("array element overflow", array);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,15 @@ public sealed class ProtocolTests
|
||||
{
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, S = "x" }));
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = (Variant.Types.TypeTag)99 }));
|
||||
Should.Throw<OverflowException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, I = 1 << 20 }));
|
||||
// #999 / review K-9: out-of-range payloads are the codec's documented failure too, never an OverflowException /
|
||||
// ArgumentOutOfRangeException that escapes OutOfProcessScriptRunner.Complete (which catches only this type).
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Int16, I = 1 << 20 }));
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.Byte, U = 300 }));
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = long.MaxValue }));
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(new Variant { Type = Variant.Types.TypeTag.DatetimeUtc, DtTicks = -1 }));
|
||||
var array = new Variant { Type = Variant.Types.TypeTag.Array, Array = new ArrayValue { ElementType = Variant.Types.TypeTag.Byte } };
|
||||
array.Array.Items.Add(new Variant { Type = Variant.Types.TypeTag.Byte, U = 1000 });
|
||||
Should.Throw<ScriptWorkerProtocolException>(() => VariantCodec.Decode(array));
|
||||
}
|
||||
|
||||
private static Envelope Sample() => new()
|
||||
@@ -165,7 +173,7 @@ public sealed class ProtocolTests
|
||||
// A frame that parses must still decode its variants without an unexpected exception type.
|
||||
if (envelope.BodyCase == Envelope.BodyOneofCase.Results)
|
||||
foreach (var r in envelope.Results.Items)
|
||||
try { VariantCodec.Decode(r.Value); } catch (Exception ex) when (ex is ScriptWorkerProtocolException or OverflowException) { }
|
||||
try { VariantCodec.Decode(r.Value); } catch (ScriptWorkerProtocolException) { }
|
||||
}
|
||||
}
|
||||
catch (ScriptWorkerProtocolException)
|
||||
|
||||
Reference in New Issue
Block a user