Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nullable references in security rules #3110

Merged
merged 7 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;

try
{
Expand Down Expand Up @@ -206,7 +206,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand All @@ -225,6 +225,7 @@ public override void Initialize(AnalysisContext context)
continue;
}

RoslynDebug.Assert(kvp.Key.Method != null); // HazardousUsageEvaluations only for invocations.
compilationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;

try
{
Expand Down Expand Up @@ -193,7 +193,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand All @@ -212,6 +212,7 @@ public override void Initialize(AnalysisContext context)
continue;
}

RoslynDebug.Assert(kvp.Key.Method != null); // HazardousUsageEvaluations only for invocations.
compilationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

// TODO(dotpaul): Enable nullable analysis.
#nullable disable

using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -89,14 +86,15 @@ public sealed override void Initialize(AnalysisContext context)
// typeSymbol: The symbol of the type to be analyzed
// relatedFieldSymbol: When relatedFieldSymbol is null, traverse all descendants of typeSymbol to
// find pointer fields; otherwise, traverse to find if relatedFieldSymbol is a pointer field
void LookForSerializationWithPointerFields(ITypeSymbol typeSymbol, IFieldSymbol relatedFieldSymbol)
void LookForSerializationWithPointerFields(ITypeSymbol typeSymbol, IFieldSymbol? relatedFieldSymbol)
{
if (typeSymbol is IPointerTypeSymbol pointerTypeSymbol)
{
// If the field is a valid pointer.
if (pointerTypeSymbol.PointedAtType.TypeKind == TypeKind.Struct ||
pointerTypeSymbol.PointedAtType.TypeKind == TypeKind.Pointer)
{
RoslynDebug.Assert(relatedFieldSymbol != null);
pointerFields.TryAdd(relatedFieldSymbol, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;

try
{
Expand Down Expand Up @@ -168,7 +168,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand All @@ -187,6 +187,7 @@ public override void Initialize(AnalysisContext context)
continue;
}

RoslynDebug.Assert(kvp.Key.Method != null); // HazardousUsageEvaluations only for invocations.
compilationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

// TODO(dotpaul): Enable nullable analysis.
#nullable disable

using System.Collections.Immutable;
using Microsoft.NetCore.Analyzers.Security.Helpers;
using Analyzer.Utilities;
Expand Down Expand Up @@ -109,8 +106,8 @@ public override void Initialize(AnalysisContext analysisContext)
}

INamedTypeSymbol type = method.ContainingType;
DiagnosticDescriptor rule = null;
string algorithmName = null;
DiagnosticDescriptor rule;
string algorithmName;

if (type.DerivesFrom(cryptTypes.MD5))
{
Expand Down Expand Up @@ -139,7 +136,7 @@ public override void Initialize(AnalysisContext analysisContext)
&& method.MetadataName == WellKnownMemberNames.InstanceConstructorName))
{
rule = DoNotUseBrokenCryptographyRule;
algorithmName = cryptTypes.DSA.Name;
algorithmName = "DSA";
}
else if (type.DerivesFrom(cryptTypes.HMACMD5))
{
Expand All @@ -166,16 +163,17 @@ public override void Initialize(AnalysisContext analysisContext)
rule = DoNotUseWeakCryptographyRule;
algorithmName = cryptTypes.HMACRIPEMD160.Name;
}

if (rule != null)
else
{
operationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
rule,
operationAnalysisContext.Operation.Syntax.GetLocation(),
operationAnalysisContext.ContainingSymbol.Name,
algorithmName));
return;
}

operationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
rule,
operationAnalysisContext.Operation.Syntax.GetLocation(),
operationAnalysisContext.ContainingSymbol.Name,
algorithmName));
},
OperationKind.Invocation,
OperationKind.ObjectCreation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;
try
{
lock (rootOperationsNeedingAnalysis)
Expand Down Expand Up @@ -229,7 +229,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand All @@ -248,6 +248,7 @@ public override void Initialize(AnalysisContext context)
continue;
}

RoslynDebug.Assert(kvp.Key.Method != null); // HazardousUsageEvaluations only for invocations.
compilationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;
try
{
lock (rootOperationsNeedingAnalysis)
Expand Down Expand Up @@ -211,7 +211,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Analyzer.Utilities.FlowAnalysis.Analysis.PropertySetAnalysis;
using Analyzer.Utilities.PooledObjects;
Expand Down Expand Up @@ -169,7 +170,7 @@ public sealed override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;
try
{
lock (rootOperationsNeedingAnalysis)
Expand Down Expand Up @@ -199,7 +200,7 @@ public sealed override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand All @@ -218,6 +219,7 @@ public sealed override void Initialize(AnalysisContext context)
continue;
}

RoslynDebug.Assert(kvp.Key.Method != null); // HazardousUsageEvaluations only for invocations.
compilationAnalysisContext.ReportDiagnostic(
Diagnostic.Create(
descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;
try
{
lock (rootOperationsNeedingAnalysis)
Expand Down Expand Up @@ -236,7 +236,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;

try
{
Expand Down Expand Up @@ -188,7 +188,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
DiagnosticDescriptor descriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public override void Initialize(AnalysisContext context)
compilationStartAnalysisContext.RegisterCompilationEndAction(
(CompilationAnalysisContext compilationAnalysisContext) =>
{
PooledDictionary<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult>? allResults = null;
PooledDictionary<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult>? allResults = null;

try
{
Expand Down Expand Up @@ -175,7 +175,7 @@ public override void Initialize(AnalysisContext context)
return;
}

foreach (KeyValuePair<(Location Location, IMethodSymbol Method), HazardousUsageEvaluationResult> kvp
foreach (KeyValuePair<(Location Location, IMethodSymbol? Method), HazardousUsageEvaluationResult> kvp
in allResults)
{
if (kvp.Value == HazardousUsageEvaluationResult.Flagged)
Expand Down
Loading