Skip to content

Commit

Permalink
Prep for SecretText on IsolatedStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthurvdv committed Jan 8, 2024
1 parent d957010 commit a7aebe6
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions Design/Rule0043SecretText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,56 @@ public class Rule0043SecretText : DiagnosticAnalyzer
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create<DiagnosticDescriptor>(DiagnosticDescriptors.Rule0043SecretText);

private static readonly string authorization = "Authorization";

private static readonly List<string> buildInMethodNames = new List<string>
{
"add",
"getvalues",
"tryaddwithoutvalidation"
};

public override void Initialize(AnalysisContext context) => context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeHttpObjects), OperationKind.InvocationExpression);
public override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeHttpObjects), OperationKind.InvocationExpression);
// TODO: enable after Spring2024OrGreater release
// context.RegisterOperationAction(new Action<OperationAnalysisContext>(this.AnalyzeIsolatedStorage), OperationKind.InvocationExpression);
}

private void AnalyzeIsolatedStorage(OperationAnalysisContext ctx)
{
// TODO: enable after Spring2024OrGreater release
// if (!VersionChecker.IsSupported(ctx.ContainingSymbol, VersionCompatibility.Spring2024OrGreater)) return;

if (ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoletePending || ctx.ContainingSymbol.GetContainingObjectTypeSymbol().IsObsoleteRemoved) return;
if (ctx.ContainingSymbol.IsObsoletePending || ctx.ContainingSymbol.IsObsoleteRemoved) return;

IInvocationExpression operation = (IInvocationExpression)ctx.Operation;
if (operation.Arguments.Count() < 3) return;

IMethodSymbol targetMethod = operation.TargetMethod;
if (targetMethod == null || targetMethod.ContainingSymbol.Kind != SymbolKind.Class) return;
if (!SemanticFacts.IsSameName(targetMethod.ContainingSymbol.Name, "IsolatedStorage")) return;

int argumentIndex;
switch (operation.TargetMethod.Name.ToLowerInvariant())
{
case "get":
argumentIndex = 2;
break;
case "set":
case "setencrypted":
argumentIndex = 1;
break;
default:
argumentIndex = -1;
break;
}

if (argumentIndex == -1) return;

if (!IsArgumentOfTypeSecretText(operation.Arguments[argumentIndex]))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0043SecretText, ctx.Operation.Syntax.GetLocation()));
}

private void AnalyzeHttpObjects(OperationAnalysisContext ctx)
{
Expand Down Expand Up @@ -51,10 +93,15 @@ private void AnalyzeHttpObjects(OperationAnalysisContext ctx)

if (!IsAuthorizationArgument(operation.Arguments[0])) return;

if (operation.Arguments[1].Parameter.OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() != NavTypeKind.SecretText)
if (!IsArgumentOfTypeSecretText(operation.Arguments[1]))
ctx.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.Rule0043SecretText, ctx.Operation.Syntax.GetLocation()));
}

private bool IsArgumentOfTypeSecretText(IArgument argument)
{
return argument.Parameter.OriginalDefinition.GetTypeSymbol().GetNavTypeKindSafe() == NavTypeKind.SecretText;
}

private static bool IsAuthorizationArgument(IArgument argument)
{
switch (argument.Syntax.Kind)
Expand Down

0 comments on commit a7aebe6

Please sign in to comment.