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

Rule S4790: Hashing data is security-sensitive #2084

Merged
merged 2 commits into from
Nov 8, 2018

Conversation

duncanp-sonar
Copy link
Contributor

@duncanp-sonar duncanp-sonar commented Nov 7, 2018

Fix #1984

@ghost ghost assigned duncanp-sonar Nov 7, 2018
@ghost ghost added the Status: Needs Review label Nov 7, 2018

namespace SonarAnalyzer.Helpers
{
public class CSharpBaseTypeTracker : BaseTypeTracker<SyntaxKind>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Syntax-level tracker that starts from the base list of inherited/implemented methods types.

CSharp.GeneratedCodeRecognizer.Instance;

protected override SyntaxKind[] TrackedSyntaxKinds { get; } =
new[] { SyntaxKind.ClassDeclaration };
Copy link
Contributor Author

@duncanp-sonar duncanp-sonar Nov 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Symbol-level tracker.
This isn't used by the rule I just added. We can delete it later if we don't use it for any of the other rules.

Copy link

@sonarsource-next sonarsource-next bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube analysis found issues:
Bug Bugs: 1
Vulnerability Vulnerabilities: 0
Code Smell Code Smells: 0

See all issues in SonarQube


bool IsTrackedRelationship(SyntaxNode objectCreationExpression, SemanticModel semanticModel, out Location issueLocation)
{
var baseClassContext = CreateContext(objectCreationExpression, semanticModel);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tracker is slightly different from the others - the syntax node being analyzed is the base list that contains the list of types being derived from or inherited. CreateContext is an abstract method as it's the responsibility of the language-specific child class to extract the type nodes from the list.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not feel nice, but I understand why you did it like this... Perhaps in the future we could split the Tracker into Tracker and Reporter...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting out the reporting would certainly give us more flexibility. Something to keep in mind.

/// Checker method called by <see cref="BaseClassTracker"/> to check whether
/// an issue should be reported because of a type the class is inheriting from.
/// </summary>
public delegate bool BaseClassCondition(BaseTypeContext context, out Location issueLocation);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delegate has an out parameter so the condition can specify where the squiggly should appear.

public override InvocationCondition WhenMethodNameIs(string methodName) =>
(context) => context.Identifier is SimpleNameSyntax nameSyntax
&& nameSyntax.Identifier.ValueText == methodName;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking the method name and not the type here (the hash rule is looking for any method called Create that returns a specific type).

.WithNotConfigurable();

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(rule);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: we could probably push the rule and SupportedDiagnostics up to the base class, so this child class only needs to specify the language-specific resources to use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whisper that to @Evangelink and he will do it in 5min for all rules :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good, more merge conflicts to deal with ;)

InvocationTracker.WhenReturnTypeIs(KnownType.System_Security_Cryptography_HashAlgorithm)
);

BaseClassTracker.Track(context,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll rename this BaseTypeTracker to match the tracker class type name (I renamed the tracker and forgot to rename these fields).

Copy link

@sonarsource-next sonarsource-next bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube analysis found issues:
Bug Bugs: 1
Vulnerability Vulnerabilities: 0
Code Smell Code Smells: 0

See all issues in SonarQube

@duncanp-sonar
Copy link
Contributor Author

Fixes #1984

.WithNotConfigurable();

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } =
ImmutableArray.Create(rule);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whisper that to @Evangelink and he will do it in 5min for all rules :)


bool IsTrackedRelationship(SyntaxNode objectCreationExpression, SemanticModel semanticModel, out Location issueLocation)
{
var baseClassContext = CreateContext(objectCreationExpression, semanticModel);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not feel nice, but I understand why you did it like this... Perhaps in the future we could split the Tracker into Tracker and Reporter...

// If a class both inherits and implements then this tracker will check
// the conditions against Inherits and Implements *separately*
// i.e. the conditions will be called twice
switch (contextNode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could become switch (contextNode.Kind()) and then cast (for perf reasons)

protected override BaseTypeContext CreateContext(SyntaxNode baseTypeList, SemanticModel semanticModel) =>
new BaseTypeContext(baseTypeList, GetBaseTypeNodes(baseTypeList), semanticModel);

private static IEnumerable<SyntaxNode> GetBaseTypeNodes(SyntaxNode contextNode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not abstract GetBaseTypeNodes and call it in the BaseTypeTracker, instead of abstract CreateContext? It kind of shows the intent better...

(context) =>
context.InvokedConstructorSymbol.Value != null &&
context.InvokedConstructorSymbol.Value.IsConstructor() &&
context.InvokedConstructorSymbol.Value.ContainingType.ConstructedFrom.DerivesFrom(baseType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if ConstructedFrom is needed here, but I might be wrong.

Copy link
Contributor

@valhristov valhristov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in general, I think the unused class should be in separate commit...

@duncanp-sonar duncanp-sonar merged commit 71e9134 into feature/hotspots Nov 8, 2018
@duncanp-sonar duncanp-sonar deleted the duncanp/hash4790 branch November 8, 2018 16:01
@ghost ghost removed the Status: Needs Review label Nov 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants