Skip to content

Commit cab38c0

Browse files
Fix S6964 FP: Properties decorated with the [BindNever] attribute
1 parent 7f5d672 commit cab38c0

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

analyzers/src/SonarAnalyzer.CSharp/Rules/AspNet/AvoidUnderPosting.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private static void GetAllDeclaredProperties(ITypeSymbol type, ConcurrentDiction
115115
.Where(x => x.GetEffectiveAccessibility() == Accessibility.Public
116116
&& x.SetMethod?.DeclaredAccessibility is Accessibility.Public
117117
&& !HasValidateNeverAttribute(x)
118+
&& !x.HasAttribute(KnownType.Microsoft_AspNetCore_Mvc_ModelBinding_BindNeverAttribute)
118119
&& x.DeclaringSyntaxReferences.Length > 0
119120
&& !IgnoreType(x.Type));
120121
foreach (var property in properties)
@@ -140,7 +141,8 @@ private static IEnumerable<INamedTypeSymbol> RelatedTypesToExamine(ITypeSymbol t
140141
IArrayTypeSymbol array => RelatedTypesToExamine(array.ElementType, controllerType),
141142
INamedTypeSymbol collection when collection.DerivesOrImplements(KnownType.System_Collections_Generic_IEnumerable_T) =>
142143
collection.TypeArguments.SelectMany(x => RelatedTypesToExamine(x, controllerType)),
143-
INamedTypeSymbol namedType when type.IsInSameAssembly(controllerType) => [namedType],
144+
INamedTypeSymbol namedType when type.IsInSameAssembly(controllerType)
145+
&& !type.HasAttribute(KnownType.Microsoft_AspNetCore_Mvc_ModelBinding_BindNeverAttribute) => [namedType],
144146
_ => []
145147
};
146148

analyzers/src/SonarAnalyzer.Common/Helpers/KnownType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public sealed partial class KnownType
101101
public static readonly KnownType Microsoft_AspNetCore_Mvc_IActionResult = new("Microsoft.AspNetCore.Mvc.IActionResult");
102102
public static readonly KnownType Microsoft_AspNetCore_Mvc_IgnoreAntiforgeryTokenAttribute = new("Microsoft.AspNetCore.Mvc.IgnoreAntiforgeryTokenAttribute");
103103
public static readonly KnownType Microsoft_AspNetCore_Mvc_Infrastructure_ActionResultObjectValueAttribute = new("Microsoft.AspNetCore.Mvc.Infrastructure.ActionResultObjectValueAttribute");
104+
public static readonly KnownType Microsoft_AspNetCore_Mvc_ModelBinding_BindNeverAttribute = new("Microsoft.AspNetCore.Mvc.ModelBinding.BindNeverAttribute");
104105
public static readonly KnownType Microsoft_AspNetCore_Mvc_ModelBinding_ModelStateDictionary = new("Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary");
105106
public static readonly KnownType Microsoft_AspNetCore_Mvc_ModelBinding_Validation_ValidateNeverAttribute = new("Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidateNeverAttribute");
106107
public static readonly KnownType Microsoft_AspNetCore_Mvc_NonActionAttribute = new("Microsoft.AspNetCore.Mvc.NonActionAttribute");

analyzers/tests/SonarAnalyzer.Test/TestCases/AspNet/AvoidUnderPosting.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.ModelBinding;
23
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
34
using System;
45
using System.Collections.Generic;
@@ -302,3 +303,23 @@ public bool IsProperty // Noncompliant
302303
set;
303304
}
304305
}
306+
307+
namespace UsingBindNeverAttribute
308+
{
309+
public class ModelWithBindNeverProperty
310+
{
311+
[BindNever] public int ValueProperty { get; set; }
312+
}
313+
314+
[BindNever]
315+
public class EntireModelWithBindNeverAttribute
316+
{
317+
public int ValueProperty { get; set; }
318+
}
319+
320+
public class CustomController : Controller
321+
{
322+
[HttpGet] public IActionResult Get(ModelWithBindNeverProperty model) => View(model);
323+
[HttpPost] public IActionResult Post(EntireModelWithBindNeverAttribute model) => View(model);
324+
}
325+
}

0 commit comments

Comments
 (0)