A class marked with the System.SerializableAttribute has a field or property may refer to the containing object directly or indirectly, allowing for a potential reference cycle.
Rule description
If deserializing untrusted data, then any code processing the deserialized object graph needs to handle reference cycles without going into infinite loops. This includes both code that's part of a deserialization callback and code that processes the object graph after deserialization completed. Otherwise, an attacker could perform a Denial-of-Service attack with malicious data containing a reference cycle.
This rule doesn't necessarily mean there's a vulnerability, but just flags potential reference cycles in deserialized object graphs.
How to fix violations
Don't serialize the class and remove the SerializableAttribute. Or, redesign your application so that the self-referred members can be removed out of the serializable class.
When to suppress warnings
It's safe to suppress a warning from this rule if:
You know the input is trusted. Consider that your application's trust boundary and data flows may change over time.
All code processing the deserialized data detects and handles reference cycles without going into an infinite loop or using excessive resources.
Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
C#
#pragmawarning disable CA5362// The code that's violating the rule is on this line.#pragmawarning restore CA5362
To disable the rule for a file, folder, or project, set its severity to none in the configuration file.
using System;
[Serializable()]
classExampleClass
{
public ExampleClass ExampleProperty {get; set;}
publicint NormalProperty {get; set;}
}
classAnotherClass
{
// The argument passed by could be `JsonConvert.DeserializeObject<ExampleClass>(untrustedData)`.publicvoidAnotherMethod(ExampleClass ec)
{
while(ec != null)
{
Console.WriteLine(ec.ToString());
ec = ec.ExampleProperty;
}
}
}
Solution
C#
using System;
[Serializable()]
classExampleClass
{
[NonSerialized]
public ExampleClass ExampleProperty {get; set;}
publicint NormalProperty {get; set;}
}
classAnotherClass
{
// The argument passed by could be `JsonConvert.DeserializeObject<ExampleClass>(untrustedData)`.publicvoidAnotherMethod(ExampleClass ec)
{
while(ec != null)
{
Console.WriteLine(ec.ToString());
ec = ec.ExampleProperty;
}
}
}
ითანამშრომლეთ ჩვენთან GitHub-ზე
ამ შიგთავსის წყაროს მოძიება GitHub-ზე არის შესაძლებელი, სადაც თქვენ ასევე შეგიძლიათ პრობლემების შექმნა და განხილვა და მოთხოვნების გადმოტანა. დამატებითი ინფორმაციისთვის იხილეთ ჩვენი დამხმარე სახელმძღვანელო.
.NET-(ი)ს უკუკავშირი
.NET არის ღია წყაროს პროექტი. აირჩიეთ ბმული უკუკავშირის გასაგზავნად:
შემოუერთდით Meetup სერიას, რათა შექმნათ მასშტაბური AI გადაწყვეტილებები რეალურ სამყაროში გამოყენების შემთხვევებზე დაყრდნობით თანამემამულე დეველოპერებთან და ექსპერტებთან.