Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
Property | Value |
---|---|
Rule ID | CA2326 |
Title | Do not use TypeNameHandling values other than None |
Category | Security |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default in .NET 9 | No |
This rule fires when either of the following conditions are met:
None
, is referenced.Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. An attack against an insecure deserializer could, for example, execute commands on the underlying operating system, communicate over the network, or delete files.
This rule finds Newtonsoft.Json.TypeNameHandling values other than None
. If you want to deserialize only when a Newtonsoft.Json.Serialization.ISerializationBinder is specified to restrict deserialized types, disable this rule and enable rules CA2327, CA2328, CA2329, and CA2330 instead.
None
value, if possible.null
or throw an exception to stop deserialization.
None
.It's safe to suppress a warning from this rule if:
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable CA2326
// The code that's violating the rule is on this line.
#pragma warning restore CA2326
To disable the rule for a file, folder, or project, set its severity to none
in the configuration file.
[*.{cs,vb}]
dotnet_diagnostic.CA2326.severity = none
For more information, see How to suppress code analysis warnings.
using Newtonsoft.Json;
public class ExampleClass
{
public JsonSerializerSettings Settings { get; }
public ExampleClass()
{
Settings = new JsonSerializerSettings();
Settings.TypeNameHandling = TypeNameHandling.All; // CA2326 violation.
}
}
Imports Newtonsoft.Json
Public Class ExampleClass
Public ReadOnly Property Settings() As JsonSerializerSettings
Public Sub New()
Settings = New JsonSerializerSettings()
Settings.TypeNameHandling = TypeNameHandling.All ' CA2326 violation.
End Sub
End Class
using Newtonsoft.Json;
public class ExampleClass
{
public JsonSerializerSettings Settings { get; }
public ExampleClass()
{
Settings = new JsonSerializerSettings();
// The default value of Settings.TypeNameHandling is TypeNameHandling.None.
}
}
Imports Newtonsoft.Json
Public Class ExampleClass
Public ReadOnly Property Settings() As JsonSerializerSettings
Public Sub New()
Settings = New JsonSerializerSettings()
' The default value of Settings.TypeNameHandling is TypeNameHandling.None.
End Sub
End Class
CA2327: Do not use insecure JsonSerializerSettings
CA2328: Ensure that JsonSerializerSettings are secure
CA2329: Do not deserialize with JsonSerializer using an insecure configuration
CA2330: Ensure that JsonSerializer has a secure configuration when deserializing
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nå