Skip to content

Commit

Permalink
Detect DataClassificationAttribute on containing type
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Nov 12, 2024
1 parent 32732ae commit 4975e3b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Meziantou.Analyzer/Rules/DoNotLogClassifiedDataAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,23 @@ private void ValidateDataClassification(DiagnosticReporter diagnosticReporter, I
static void ValidateDataClassification(DiagnosticReporter diagnosticReporter, IOperation operation, IOperation reportOperation, INamedTypeSymbol dataClassificationAttributeSymbol)
{
operation = operation.UnwrapConversionOperations();
if (operation is IParameterReferenceOperation parameterReferenceOperation)
if (operation is IParameterReferenceOperation { Parameter: var parameter })
{
if (parameterReferenceOperation.Parameter.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
if (parameter.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || parameter.Type.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
{
diagnosticReporter.ReportDiagnostic(Rule, reportOperation);
}
}
else if (operation is IPropertyReferenceOperation propertyReferenceOperation)
else if (operation is IPropertyReferenceOperation { Property: var property })
{
if (propertyReferenceOperation.Property.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
if (property.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || property.ContainingType.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
{
diagnosticReporter.ReportDiagnostic(Rule, reportOperation);
}
}
else if (operation is IFieldReferenceOperation fieldReferenceOperation)
else if (operation is IFieldReferenceOperation { Field: var field })
{
if (fieldReferenceOperation.Field.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
if (field.HasAttribute(dataClassificationAttributeSymbol, inherits: true) || field.ContainingType.HasAttribute(dataClassificationAttributeSymbol, inherits: true))
{
diagnosticReporter.ReportDiagnostic(Rule, reportOperation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,36 @@ await CreateProjectBuilder()
}

[Fact]
public async Task Logger_BeginScope_DataClassification_Parameter()
public async Task Logger_LogInformation_DataClassification_Parameter_AttributeOnType()
{
const string SourceCode = """
using Microsoft.Extensions.Logging;
ILogger logger = null;
void A([TaxonomyAttribute]int param)
{
logger.LogInformation("{Prop}", [|param|]);
}
[TaxonomyAttribute()]
class Dummy
{
public string Prop;
}
class TaxonomyAttribute : Microsoft.Extensions.Compliance.Classification.DataClassificationAttribute
{
public TaxonomyAttribute() : base(Microsoft.Extensions.Compliance.Classification.DataClassification.Unknown) { }
}
""";
await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task Logger_BeginScope_DataClassification_Property()
{
const string SourceCode = """
using Microsoft.Extensions.Logging;
Expand Down

0 comments on commit 4975e3b

Please sign in to comment.