Skip to content

Commit f810340

Browse files
authored
add important remarks to NrbfDecoder (#111286)
* add important remarks to NrbfDecoder * add a warning about TypeName
1 parent 9d6fd09 commit f810340

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/ArrayRecord.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ internal bool IsJagged
6464
/// </param>
6565
/// <returns>An array filled with the data provided in the serialized records.</returns>
6666
/// <exception cref="InvalidOperationException"><paramref name="expectedArrayType" /> does not match the data from the payload.</exception>
67+
/// <remarks>
68+
/// Check the total length of the array by using <see cref="Lengths"/> property before calling this method,
69+
/// as an attacker could have sent you a small payload that will require to allocate a very large array
70+
/// and potentially cause <see cref="OutOfMemoryException"/> and Denial of Service.
71+
/// </remarks>
6772
[RequiresDynamicCode("The code for an array of the specified type might not be available.")]
6873
public Array GetArray(Type expectedArrayType, bool allowNulls = true)
6974
{

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/NrbfDecoder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace System.Formats.Nrbf;
1616
/// <summary>
1717
/// Provides stateless methods for decoding .NET Remoting Binary Format (NRBF) encoded data.
1818
/// </summary>
19+
/// <remarks>
20+
/// NrbfDecoder is an implementation of an NRBF reader, but its behaviors don't strictly follow BinaryFormatter's implementation.
21+
/// Thus the output of NrbfDecoder shouldn't be used to determine whether a call to BinaryFormatter would be safe.
22+
/// </remarks>
1923
public static class NrbfDecoder
2024
{
2125
private static UTF8Encoding ThrowOnInvalidUtf8Encoding { get; } = new(false, throwOnInvalidBytes: true);

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/SZArrayRecord.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ private protected SZArrayRecord(ArrayInfo arrayInfo) : base(arrayInfo)
3434
/// otherwise, <see langword="false" />.
3535
/// </param>
3636
/// <returns>An array filled with the data provided in the serialized records.</returns>
37+
/// <remarks>
38+
/// Check the total length of the array by using <see cref="Length"/> property before calling this method,
39+
/// as an attacker could have sent you a small payload that will require to allocate a very large array
40+
/// and potentially cause <see cref="OutOfMemoryException"/> and Denial of Service.
41+
/// </remarks>
3742
public abstract T?[] GetArray(bool allowNulls = true);
3843

3944
#pragma warning disable IL3051 // RequiresDynamicCode is not required in this particualar case

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/SerializationRecord.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ internal SerializationRecord() // others can't derive from this type
3939
/// Gets the name of the serialized type.
4040
/// </summary>
4141
/// <value>The name of the serialized type.</value>
42+
/// <remarks>
43+
/// Since the provided type name may originate from untrusted input,
44+
/// it should not be utilized for type loading, as it could potentially load a malicious type.
45+
/// </remarks>
4246
public abstract TypeName TypeName { get; }
4347

4448
/// <summary>

src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/SerializationRecordId.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace System.Formats.Nrbf;
1616
/// <summary>
1717
/// The ID of <see cref="SerializationRecord" />.
1818
/// </summary>
19+
/// <remarks>
20+
/// It can be used the detect cycles in decoded records.
21+
/// </remarks>
1922
[DebuggerDisplay("{_id}")]
2023
public readonly struct SerializationRecordId : IEquatable<SerializationRecordId>
2124
{

0 commit comments

Comments
 (0)