|
16 | 16 |
|
17 | 17 | using SonarAnalyzer.Core.Trackers;
|
18 | 18 |
|
19 |
| -namespace SonarAnalyzer.Core.Rules |
| 19 | +namespace SonarAnalyzer.Core.Rules; |
| 20 | + |
| 21 | +public abstract class CreatingHashAlgorithmsBase<TSyntaxKind> : TrackerHotspotDiagnosticAnalyzer<TSyntaxKind> |
| 22 | + where TSyntaxKind : struct |
20 | 23 | {
|
21 |
| - public abstract class CreatingHashAlgorithmsBase<TSyntaxKind> : TrackerHotspotDiagnosticAnalyzer<TSyntaxKind> |
22 |
| - where TSyntaxKind : struct |
23 |
| - { |
24 |
| - protected const string DiagnosticId = "S4790"; |
25 |
| - protected const string MessageFormat = "Make sure this weak hash algorithm is not used in a sensitive context here."; |
| 24 | + protected const string DiagnosticId = "S4790"; |
| 25 | + protected const string MessageFormat = "Make sure this weak hash algorithm is not used in a sensitive context here."; |
| 26 | + |
| 27 | + private const string CreateMethodName = "Create"; |
| 28 | + private const string HashDataName = "HashData"; |
| 29 | + private const string HashDataAsyncName = "HashDataAsync"; |
| 30 | + private const string TryHashDataName = "TryHashData"; |
| 31 | + |
| 32 | + private readonly KnownType[] algorithmTypes = |
| 33 | + [ |
| 34 | + KnownType.System_Security_Cryptography_DSA, |
| 35 | + KnownType.System_Security_Cryptography_HMACMD5, |
| 36 | + KnownType.System_Security_Cryptography_HMACRIPEMD160, |
| 37 | + KnownType.System_Security_Cryptography_HMACSHA1, |
| 38 | + KnownType.System_Security_Cryptography_MD5, |
| 39 | + KnownType.System_Security_Cryptography_RIPEMD160, |
| 40 | + KnownType.System_Security_Cryptography_SHA1 |
| 41 | + ]; |
26 | 42 |
|
27 |
| - private const string CreateMethodName = "Create"; |
| 43 | + private readonly string[] unsafeAlgorithms = |
| 44 | + [ |
| 45 | + "DSA", |
| 46 | + "System.Security.Cryptography.DSA", |
| 47 | + "HMACMD5", |
| 48 | + "System.Security.Cryptography.HMACMD5", |
| 49 | + "HMACRIPEMD160", |
| 50 | + "System.Security.Cryptography.HMACRIPEMD160", |
| 51 | + "HMACSHA1", |
| 52 | + "System.Security.Cryptography.HMACSHA1", |
| 53 | + "MD5", |
| 54 | + "System.Security.Cryptography.MD5", |
| 55 | + "RIPEMD160", |
| 56 | + "System.Security.Cryptography.RIPEMD160", |
| 57 | + "SHA1", |
| 58 | + "System.Security.Cryptography.SHA1", |
| 59 | + ]; |
| 60 | + |
| 61 | + protected abstract bool IsUnsafeAlgorithm(SyntaxNode argumentNode, SemanticModel model); |
| 62 | + |
| 63 | + protected CreatingHashAlgorithmsBase(IAnalyzerConfiguration configuration) |
| 64 | + : base(configuration, DiagnosticId, MessageFormat) { } |
| 65 | + |
| 66 | + protected override void Initialize(TrackerInput input) |
| 67 | + { |
| 68 | + var oc = Language.Tracker.ObjectCreation; |
| 69 | + oc.Track(input, oc.WhenDerivesOrImplementsAny(algorithmTypes)); |
28 | 70 |
|
29 |
| - private readonly KnownType[] algorithmTypes = |
30 |
| - { |
31 |
| - KnownType.System_Security_Cryptography_DSA, |
32 |
| - KnownType.System_Security_Cryptography_HMACMD5, |
33 |
| - KnownType.System_Security_Cryptography_HMACRIPEMD160, |
34 |
| - KnownType.System_Security_Cryptography_HMACSHA1, |
35 |
| - KnownType.System_Security_Cryptography_MD5, |
36 |
| - KnownType.System_Security_Cryptography_RIPEMD160, |
37 |
| - KnownType.System_Security_Cryptography_SHA1 |
38 |
| - }; |
| 71 | + var tracker = Language.Tracker.Invocation; |
| 72 | + tracker.Track(input, |
| 73 | + tracker.MatchMethod( |
| 74 | + new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, CreateMethodName), |
| 75 | + new MemberDescriptor(KnownType.System_Security_Cryptography_HMAC, CreateMethodName), |
| 76 | + new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, CreateMethodName), |
| 77 | + new MemberDescriptor(KnownType.System_Security_Cryptography_RIPEMD160, CreateMethodName), |
| 78 | + new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, CreateMethodName)), |
| 79 | + tracker.MethodHasParameters(0)); |
39 | 80 |
|
40 |
| - private readonly string[] unsafeAlgorithms = |
41 |
| - { |
42 |
| - "DSA", |
43 |
| - "System.Security.Cryptography.DSA", |
44 |
| - "HMACMD5", |
45 |
| - "System.Security.Cryptography.HMACMD5", |
46 |
| - "HMACRIPEMD160", |
47 |
| - "System.Security.Cryptography.HMACRIPEMD160", |
48 |
| - "HMACSHA1", |
49 |
| - "System.Security.Cryptography.HMACSHA1", |
50 |
| - "MD5", |
51 |
| - "System.Security.Cryptography.MD5", |
52 |
| - "RIPEMD160", |
53 |
| - "System.Security.Cryptography.RIPEMD160", |
54 |
| - "SHA1", |
55 |
| - "System.Security.Cryptography.SHA1" |
56 |
| - }; |
| 81 | + tracker.Track(input, |
| 82 | + tracker.MatchMethod( |
| 83 | + new MemberDescriptor(KnownType.System_Security_Cryptography_AsymmetricAlgorithm, CreateMethodName), |
| 84 | + new MemberDescriptor(KnownType.System_Security_Cryptography_CryptoConfig, "CreateFromName"), |
| 85 | + new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, CreateMethodName), |
| 86 | + new MemberDescriptor(KnownType.System_Security_Cryptography_HashAlgorithm, CreateMethodName), |
| 87 | + new MemberDescriptor(KnownType.System_Security_Cryptography_HMAC, CreateMethodName), |
| 88 | + new MemberDescriptor(KnownType.System_Security_Cryptography_KeyedHashAlgorithm, CreateMethodName), |
| 89 | + new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, CreateMethodName), |
| 90 | + new MemberDescriptor(KnownType.System_Security_Cryptography_RIPEMD160, CreateMethodName), |
| 91 | + new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, CreateMethodName)), |
| 92 | + tracker.ArgumentAtIndexIsAny(0, unsafeAlgorithms)); |
57 | 93 |
|
58 |
| - protected CreatingHashAlgorithmsBase(IAnalyzerConfiguration configuration) |
59 |
| - : base(configuration, DiagnosticId, MessageFormat) { } |
| 94 | + tracker.Track(input, |
| 95 | + tracker.MatchMethod( |
| 96 | + new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, HashDataName), |
| 97 | + new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, TryHashDataName), |
| 98 | + new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, HashDataAsyncName), |
| 99 | + new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, HashDataName), |
| 100 | + new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, TryHashDataName), |
| 101 | + new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, HashDataAsyncName))); |
60 | 102 |
|
61 |
| - protected override void Initialize(TrackerInput input) |
62 |
| - { |
63 |
| - var oc = Language.Tracker.ObjectCreation; |
64 |
| - oc.Track(input, oc.WhenDerivesOrImplementsAny(algorithmTypes)); |
| 103 | + tracker.Track(input, |
| 104 | + tracker.MatchMethod( |
| 105 | + new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, HashDataName)), |
| 106 | + tracker.ArgumentAtIndexIs(3, IsUnsafeAlgorithm)); |
65 | 107 |
|
66 |
| - var t = Language.Tracker.Invocation; |
67 |
| - t.Track(input, |
68 |
| - t.MatchMethod( |
69 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, CreateMethodName), |
70 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_HMAC, CreateMethodName), |
71 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, CreateMethodName), |
72 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_RIPEMD160, CreateMethodName), |
73 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, CreateMethodName)), |
74 |
| - t.MethodHasParameters(0)); |
| 108 | + tracker.Track(input, |
| 109 | + tracker.MatchMethod( |
| 110 | + new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, HashDataName)), |
| 111 | + tracker.ArgumentAtIndexIs(1, IsUnsafeAlgorithm)); |
75 | 112 |
|
76 |
| - t.Track(input, |
77 |
| - t.MatchMethod( |
78 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_AsymmetricAlgorithm, CreateMethodName), |
79 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_CryptoConfig, "CreateFromName"), |
80 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, CreateMethodName), |
81 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_HashAlgorithm, CreateMethodName), |
82 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_HMAC, CreateMethodName), |
83 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_KeyedHashAlgorithm, CreateMethodName), |
84 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_MD5, CreateMethodName), |
85 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_RIPEMD160, CreateMethodName), |
86 |
| - new MemberDescriptor(KnownType.System_Security_Cryptography_SHA1, CreateMethodName)), |
87 |
| - t.ArgumentAtIndexIsAny(0, unsafeAlgorithms)); |
88 |
| - } |
| 113 | + tracker.Track(input, |
| 114 | + tracker.MatchMethod( |
| 115 | + new MemberDescriptor(KnownType.System_Security_Cryptography_DSA, TryHashDataName)), |
| 116 | + tracker.ArgumentAtIndexIs(2, IsUnsafeAlgorithm)); |
89 | 117 | }
|
90 | 118 | }
|
0 commit comments