Skip to content

Commit

Permalink
S4347: Implement ShouldExecute
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed May 27, 2024
1 parent b76ac90 commit d77f234
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using SonarAnalyzer.Common.Walkers;
using SonarAnalyzer.SymbolicExecution.Constraints;

namespace SonarAnalyzer.SymbolicExecution.Roslyn.RuleChecks.CSharp;
Expand All @@ -38,7 +39,12 @@ public sealed class SecureRandomSeedsShouldNotBePredictable : HardcodedBytesRule
protected override SymbolicConstraint Hardcoded => CryptographicSeedConstraint.Predictable;
protected override SymbolicConstraint NotHardcoded => CryptographicSeedConstraint.Unpredictable;

public override bool ShouldExecute() => true;
public override bool ShouldExecute()
{
var walker = new Walker();
walker.SafeVisit(Node);
return walker.Result;
}

protected override ProgramState PreProcessSimple(SymbolicContext context)
{
Expand Down Expand Up @@ -147,4 +153,23 @@ private static bool IsSecureRandom(IInvocationOperationWrapper invocation) =>
private static bool IsIRandomGenerator(IInvocationOperationWrapper invocation) =>
invocation.Instance is { } instance
&& instance.Type.Is(KnownType.Org_BouncyCastle_Crypto_Prng_IRandomGenerator);

private sealed class Walker : SafeCSharpSyntaxWalker
{
public bool Result { get; private set; }

public override void Visit(SyntaxNode node)
{
if (!Result)
{
base.Visit(node);
}
}

public override void VisitMemberAccessExpression(MemberAccessExpressionSyntax node) =>
Result |= node.Expression.NameIs("SecureRandom") && node.Name.NameIs("GetInstance");

public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) =>
Result |= node.Type.GetName() is "DigestRandomGenerator" or "VmpcRandomGenerator";
}
}

0 comments on commit d77f234

Please sign in to comment.