diff --git a/src/libraries/System.Formats.Cbor/src/Resources/Strings.resx b/src/libraries/System.Formats.Cbor/src/Resources/Strings.resx
index 2b05ed271d1c4f..332df9fc8f5384 100644
--- a/src/libraries/System.Formats.Cbor/src/Resources/Strings.resx
+++ b/src/libraries/System.Formats.Cbor/src/Resources/Strings.resx
@@ -183,6 +183,9 @@
CBOR initial byte contains invalid integer encoding
+
+ CBOR initial byte contains reserved additional information.
+
The current CBOR map contains an incomplete key/value pair.
@@ -225,6 +228,9 @@
Not a valid tagged unix time encoding.
+
+ The decoded Unix time is outside the range of DateTimeOffset.
+
Not a valid tagged bignum encoding.
diff --git a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs
index 228ae0bb78d9d4..c4e77634e5f7ec 100644
--- a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs
+++ b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Simple.cs
@@ -179,6 +179,12 @@ public CborSimpleValue ReadSimpleValue()
AdvanceDataItemCounters();
return (CborSimpleValue)value;
default:
+ // Additional information values 28-30 are reserved: https://www.rfc-editor.org/rfc/rfc8949.html#section-3.3
+ if (header.AdditionalInfo is > CborAdditionalInfo.Additional64BitData and < CborAdditionalInfo.IndefiniteLength)
+ {
+ throw new CborContentException(SR.Cbor_Reader_InvalidCbor_InvalidSimpleValueEncoding);
+ }
+
throw new InvalidOperationException(SR.Cbor_Reader_NotASimpleValueEncoding);
}
}
diff --git a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Tag.cs b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Tag.cs
index 2ed46e1704dc1c..62dbb2adfba9df 100644
--- a/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Tag.cs
+++ b/src/libraries/System.Formats.Cbor/src/System/Formats/Cbor/Reader/CborReader.Tag.cs
@@ -90,7 +90,9 @@ public DateTimeOffset ReadDateTimeOffset()
/// The decoded value.
/// The next data item does not have the correct major type.
/// -or-
- /// The next date item does not have the correct semantic tag.
+ /// The next date item does not have the correct semantic tag.
+ /// -or-
+ /// The decoded Unix time is outside the range supported by .
/// The next value has an invalid CBOR encoding.
/// -or-
/// There was an unexpected end of CBOR encoding data.
@@ -131,9 +133,15 @@ public DateTimeOffset ReadUnixTimeSeconds()
throw new CborContentException(SR.Cbor_Reader_InvalidUnixTimeEncoding);
}
}
- catch
+ catch (Exception e)
{
RestoreCheckpoint(in checkpoint);
+
+ if (e is ArgumentOutOfRangeException or OverflowException)
+ {
+ throw new InvalidOperationException(SR.Cbor_Reader_UnixTimeOutOfRange, e);
+ }
+
throw;
}
}
diff --git a/src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Helpers.cs b/src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Helpers.cs
index 54875325efd7a2..d5dc3cef682c1e 100644
--- a/src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Helpers.cs
+++ b/src/libraries/System.Formats.Cbor/tests/Reader/CborReaderTests.Helpers.cs
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
+using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Test.Cryptography;
@@ -310,8 +311,21 @@ public static void VerifyMap(CborReader reader, object[] expectedValues, bool ex
{
"bf01ff", // indefinite-length map key missing a value
"daffffffffff", // tag followed by break byte
+ "fc", // reserved additional information in major type 7
+ "fd",
+ "fe",
+ "81fc", // reserved initial byte in an array
+ "a101fd", // reserved initial byte as a map value
+ "c1fe", // tag followed by a reserved initial byte
};
public static string[] InvalidCborValues => TruncatedCborValues.Concat(MalformedCborValues).ToArray();
+
+ public static IEnumerable