Skip to content

Commit 9be67c0

Browse files
jscarleRob-Hague
andauthored
Cleanup and muting of analyzer warnings (#1357)
* Fixed warnings in tests. * Fixed warnings in main project. * Fix public key. * Further warning cleanup. * Muted remaining warnings. * Final cleanup. * Fix warning. * Fix test classes. * Fix more internals. * Revert "internal" in test projects * Revert more internals. * Revert all non-analyzer changes. --------- Co-authored-by: Rob Hague <[email protected]>
1 parent db3d7e8 commit 9be67c0

36 files changed

+336
-332
lines changed

Renci.SshNet.sln

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2929
EndProject
3030
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D21A4D03-0AC2-4613-BB6D-74D2D16A72CC}"
3131
ProjectSection(SolutionItems) = preProject
32-
test\.editorconfig = test\.editorconfig
3332
test\Directory.Build.props = test\Directory.Build.props
3433
EndProjectSection
3534
EndProject

src/Renci.SshNet/.editorconfig

+29-1
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,18 @@ MA0053.public_class_should_be_sealed = false
108108
# TODO: Remove exclusion when issues are fixed
109109
dotnet_diagnostic.MA0055.severity = none
110110

111+
# MA0089: Optimize string method usage
112+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0089.md
113+
dotnet_diagnostic.MA0089.severity = none
114+
111115
# MA0110: Use the Regex source generator
112116
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0110.md
113117
dotnet_diagnostic.MA0110.severity = none
114118

119+
# MA0026: Fix TODO comment
120+
# https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0026.md
121+
dotnet_diagnostic.MA0026.severity = none
122+
115123
#### .NET Compiler Platform analysers rules ####
116124

117125
# CA1030: Use events where appropriate
@@ -152,10 +160,30 @@ dotnet_diagnostic.CA3075.severity = none
152160
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0004
153161
dotnet_diagnostic.IDE0004.severity = none
154162

163+
# IDE0047: Remove unnecessary parentheses
164+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0047-ide0048
165+
dotnet_diagnostic.IDE0047.severity = none
166+
155167
# IDE0048: Add parentheses for clarity
156-
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0047
168+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0047-ide0048
157169
dotnet_diagnostic.IDE0048.severity = none
158170

159171
# IDE0305: Collection initialization can be simplified
160172
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0305
161173
dotnet_diagnostic.IDE0305.severity = none
174+
175+
# IDE0046: Use conditional expression for return
176+
# https://learn.microsoft.com/en-ca/dotnet/fundamentals/code-analysis/style-rules/ide0046
177+
dotnet_diagnostic.IDE0046.severity = none
178+
179+
# IDE0032: Use auto-implemented property
180+
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0032
181+
dotnet_diagnostic.IDE0032.severity = none
182+
183+
# CA5350: Do Not Use Weak Cryptographic Algorithms
184+
# https://learn.microsoft.com/en-ca/dotnet/fundamentals/code-analysis/quality-rules/ca5350
185+
dotnet_diagnostic.CA5350.severity = none
186+
187+
# CA5351: Do Not Use Broken Cryptographic Algorithms
188+
# https://learn.microsoft.com/en-ca/dotnet/fundamentals/code-analysis/quality-rules/ca5351
189+
dotnet_diagnostic.CA5351.severity = none

src/Renci.SshNet/Abstractions/CryptoAbstraction.cs

-12
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,12 @@ public static System.Security.Cryptography.RandomNumberGenerator CreateRandomNum
3939

4040
public static System.Security.Cryptography.MD5 CreateMD5()
4141
{
42-
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
4342
return System.Security.Cryptography.MD5.Create();
44-
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
4543
}
4644

4745
public static System.Security.Cryptography.SHA1 CreateSHA1()
4846
{
49-
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
5047
return System.Security.Cryptography.SHA1.Create();
51-
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
5248
}
5349

5450
public static System.Security.Cryptography.SHA256 CreateSHA256()
@@ -68,30 +64,22 @@ public static System.Security.Cryptography.SHA512 CreateSHA512()
6864

6965
public static System.Security.Cryptography.HMACMD5 CreateHMACMD5(byte[] key)
7066
{
71-
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
7267
return new System.Security.Cryptography.HMACMD5(key);
73-
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
7468
}
7569

7670
public static HMACMD5 CreateHMACMD5(byte[] key, int hashSize)
7771
{
78-
#pragma warning disable CA5351 // Do not use broken cryptographic algorithms
7972
return new HMACMD5(key, hashSize);
80-
#pragma warning restore CA5351 // Do not use broken cryptographic algorithms
8173
}
8274

8375
public static System.Security.Cryptography.HMACSHA1 CreateHMACSHA1(byte[] key)
8476
{
85-
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
8677
return new System.Security.Cryptography.HMACSHA1(key);
87-
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
8878
}
8979

9080
public static HMACSHA1 CreateHMACSHA1(byte[] key, int hashSize)
9181
{
92-
#pragma warning disable CA5350 // Do not use weak cryptographic algorithms
9382
return new HMACSHA1(key, hashSize);
94-
#pragma warning restore CA5350 // Do not use weak cryptographic algorithms
9583
}
9684

9785
public static System.Security.Cryptography.HMACSHA256 CreateHMACSHA256(byte[] key)

src/Renci.SshNet/Abstractions/SocketExtensions.cs

-4
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ public void SetCancelled()
5050
SetCompleted();
5151
}
5252

53-
#pragma warning disable S1144 // Unused private types or members should be removed
5453
public AwaitableSocketAsyncEventArgs GetAwaiter()
55-
#pragma warning restore S1144 // Unused private types or members should be removed
5654
{
5755
return this;
5856
}
@@ -68,9 +66,7 @@ void INotifyCompletion.OnCompleted(Action continuation)
6866
}
6967
}
7068

71-
#pragma warning disable S1144 // Unused private types or members should be removed
7269
public void GetResult()
73-
#pragma warning restore S1144 // Unused private types or members should be removed
7470
{
7571
if (_isCancelled)
7672
{

src/Renci.SshNet/Common/BigInteger.cs

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma warning disable SA1028 // Code should not contain trailing whitespace
2-
#pragma warning disable SA1515 // Single-line comment should be preceded by blank line
32
//
43
// System.Numerics.BigInteger
54
//
@@ -46,7 +45,6 @@
4645
*
4746
*
4847
* ***************************************************************************/
49-
#pragma warning restore SA1515 // Single-line comment should be preceded by blank line
5048
#pragma warning restore SA1028 // Code should not contain trailing whitespace
5149

5250
using System;
@@ -4578,9 +4576,7 @@ public readonly byte[] ToByteArray()
45784576

45794577
if (needExtra)
45804578
{
4581-
#pragma warning disable S1854 // Unused assignments should be removed
45824579
res[j++] = 0xFF;
4583-
#pragma warning restore S1854 // Unused assignments should be removed
45844580
}
45854581
}
45864582
else
@@ -4590,9 +4586,7 @@ public readonly byte[] ToByteArray()
45904586
res[j++] = (byte)(word >> 8);
45914587
res[j++] = (byte)(word >> 16);
45924588
res[j++] = (byte)(word >> 24);
4593-
#pragma warning disable S1854 // Unused assignments should be removed
45944589
res[j++] = 0xFF;
4595-
#pragma warning restore S1854 // Unused assignments should be removed
45964590
}
45974591
}
45984592

src/Renci.SshNet/Compression/ZlibStream.cs

-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ public void Write(byte[] buffer, int offset, int count)
5454
}
5555
}
5656

57-
#pragma warning restore SA1005 // Single line comments should begin with single space
5857
#pragma warning restore S125 // Sections of code should not be commented out

src/Renci.SshNet/ForwardedPort.cs

-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ public virtual void Start()
7171
/// <summary>
7272
/// Stops port forwarding.
7373
/// </summary>
74-
#pragma warning disable CA1716 // Identifiers should not match keywords
7574
public virtual void Stop()
76-
#pragma warning restore CA1716 // Identifiers should not match keywords
7775
{
7876
if (IsStarted)
7977
{

src/Renci.SshNet/ISftpClient.cs

-2
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,7 @@ public interface ISftpClient : IBaseClient
592592
/// <exception cref="SftpPathNotFoundException"><paramref name="path"/> was not found on the remote host.</exception>
593593
/// <exception cref="ArgumentNullException"><paramref name="path" /> is <see langword="null"/>.</exception>
594594
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
595-
#pragma warning disable CA1716 // Identifiers should not match keywords
596595
ISftpFile Get(string path);
597-
#pragma warning restore CA1716 // Identifiers should not match keywords
598596

599597
/// <summary>
600598
/// Gets the <see cref="SftpFileAttributes"/> of the file on the path.

src/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Renci.SshNet
1515
/// </summary>
1616
public class KeyboardInteractiveAuthenticationMethod : AuthenticationMethod, IDisposable
1717
{
18-
private readonly RequestMessage _requestMessage;
18+
private readonly RequestMessageKeyboardInteractive _requestMessage;
1919
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
2020
private Session _session;
2121
private EventWaitHandle _authenticationCompleted = new AutoResetEvent(initialState: false);

src/Renci.SshNet/Messages/Authentication/FailureMessage.cs

-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ protected override void LoadData()
6868
/// </summary>
6969
protected override void SaveData()
7070
{
71-
#pragma warning disable MA0025 // Implement the functionality instead of throwing NotImplementedException
7271
throw new NotImplementedException();
73-
#pragma warning restore MA0025 // Implement the functionality instead of throwing NotImplementedException
7472
}
7573

7674
internal override void Process(Session session)
@@ -81,9 +79,7 @@ internal override void Process(Session session)
8179
/// <inheritdoc/>
8280
public override string ToString()
8381
{
84-
#pragma warning disable MA0089 // Optimize string method usage
8582
return $"SSH_MSG_USERAUTH_FAILURE {string.Join(",", AllowedAuthentications)} ({nameof(PartialSuccess)}:{PartialSuccess})";
86-
#pragma warning restore MA0089 // Optimize string method usage
8783
}
8884
}
8985
}

src/Renci.SshNet/PasswordAuthenticationMethod.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Renci.SshNet
1515
/// </summary>
1616
public class PasswordAuthenticationMethod : AuthenticationMethod, IDisposable
1717
{
18-
private readonly RequestMessage _requestMessage;
18+
private readonly RequestMessagePassword _requestMessage;
1919
private readonly byte[] _password;
2020
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
2121
private Session _session;

src/Renci.SshNet/PrivateKeyAuthenticationMethod.cs

-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public class PrivateKeyAuthenticationMethod : AuthenticationMethod, IDisposable
1717
{
1818
private AuthenticationResult _authenticationResult = AuthenticationResult.Failure;
1919
private EventWaitHandle _authenticationCompleted = new ManualResetEvent(initialState: false);
20-
#pragma warning disable S1450 // Private fields only used as local variables in methods should become local variables
2120
private bool _isSignatureRequired;
22-
#pragma warning restore S1450 // Private fields only used as local variables in methods should become local variables
2321
private bool _isDisposed;
2422

2523
/// <summary>

src/Renci.SshNet/Security/Cryptography/EcdsaDigitalSignature.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
22
using System.Globalization;
3-
#if NETFRAMEWORK
4-
using System.Security.Cryptography;
5-
#endif // NETFRAMEWORK
63

74
using Renci.SshNet.Common;
85

@@ -45,7 +42,7 @@ public override bool Verify(byte[] input, byte[] signature)
4542
var sig_size = _key.KeyLength == 521 ? 132 : _key.KeyLength / 4;
4643
var ssh_data = new SshDataSignature(signature, sig_size);
4744
#if NETFRAMEWORK
48-
var ecdsa = (ECDsaCng)_key.Ecdsa;
45+
var ecdsa = _key.Ecdsa;
4946
ecdsa.HashAlgorithm = _key.HashAlgorithm;
5047
return ecdsa.VerifyData(input, ssh_data.Signature);
5148
#else
@@ -63,7 +60,7 @@ public override bool Verify(byte[] input, byte[] signature)
6360
public override byte[] Sign(byte[] input)
6461
{
6562
#if NETFRAMEWORK
66-
var ecdsa = (ECDsaCng)_key.Ecdsa;
63+
var ecdsa = _key.Ecdsa;
6764
ecdsa.HashAlgorithm = _key.HashAlgorithm;
6865
var signed = ecdsa.SignData(input);
6966
#else

src/Renci.SshNet/Security/Cryptography/EcdsaKey.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,17 @@ public override BigInteger[] Public
221221
/// </summary>
222222
public byte[] PrivateKey { get; private set; }
223223

224+
#if NETFRAMEWORK
225+
/// <summary>
226+
/// Gets the <see cref="ECDsa"/> object.
227+
/// </summary>
228+
public ECDsaCng Ecdsa { get; private set; }
229+
#else
224230
/// <summary>
225231
/// Gets the <see cref="ECDsa"/> object.
226232
/// </summary>
227233
public ECDsa Ecdsa { get; private set; }
234+
#endif
228235

229236
/// <summary>
230237
/// Initializes a new instance of the <see cref="EcdsaKey"/> class.
@@ -371,7 +378,7 @@ private void Import(string curve_oid, byte[] publickey, byte[] privatekey)
371378
PrivateKey = privatekey;
372379
}
373380

374-
var headerSize = Marshal.SizeOf(typeof(BCRYPT_ECCKEY_BLOB));
381+
var headerSize = Marshal.SizeOf<BCRYPT_ECCKEY_BLOB>();
375382
var blobSize = headerSize + qx.Length + qy.Length;
376383
if (privatekey != null)
377384
{

src/Renci.SshNet/Security/Cryptography/HMACMD5.cs

-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ public class HMACMD5 : System.Security.Cryptography.HMACMD5
1515
/// </summary>
1616
/// <param name="key">The key.</param>
1717
public HMACMD5(byte[] key)
18-
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
1918
: base(key)
20-
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
2119
{
2220
#pragma warning disable MA0056 // Do not call overridable members in constructor
2321
_hashSize = base.HashSize;
@@ -31,9 +29,7 @@ public HMACMD5(byte[] key)
3129
/// <param name="key">The key.</param>
3230
/// <param name="hashSize">The size, in bits, of the computed hash code.</param>
3331
public HMACMD5(byte[] key, int hashSize)
34-
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
3532
: base(key)
36-
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
3733
{
3834
_hashSize = hashSize;
3935
}
@@ -57,9 +53,7 @@ public override int HashSize
5753
/// </returns>
5854
protected override byte[] HashFinal()
5955
{
60-
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
6156
var hash = base.HashFinal();
62-
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
6357
return hash.Take(HashSize / 8);
6458
}
6559
}

src/Renci.SshNet/Security/Cryptography/HMACSHA1.cs

-6
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ public class HMACSHA1 : System.Security.Cryptography.HMACSHA1
1616
/// </summary>
1717
/// <param name="key">The key.</param>
1818
public HMACSHA1(byte[] key)
19-
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
2019
: base(key)
21-
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms
2220
{
2321
#pragma warning disable MA0056 // Do not call overridable members in constructor
2422
_hashSize = base.HashSize;
@@ -31,9 +29,7 @@ public HMACSHA1(byte[] key)
3129
/// <param name="key">The key.</param>
3230
/// <param name="hashSize">The size, in bits, of the computed hash code.</param>
3331
public HMACSHA1(byte[] key, int hashSize)
34-
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
3532
: base(key)
36-
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms
3733
{
3834
_hashSize = hashSize;
3935
}
@@ -57,9 +53,7 @@ public override int HashSize
5753
/// </returns>
5854
protected override byte[] HashFinal()
5955
{
60-
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
6156
var hash = base.HashFinal();
62-
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms
6357
return hash.Take(HashSize / 8);
6458
}
6559
}

src/Renci.SshNet/Sftp/SftpFileStream.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ namespace Renci.SshNet.Sftp
1313
/// Exposes a <see cref="Stream"/> around a remote SFTP file, supporting both synchronous and asynchronous read and write operations.
1414
/// </summary>
1515
/// <threadsafety static="true" instance="false"/>
16-
#pragma warning disable CA1844 // Provide memory-based overrides of async methods when subclassing 'Stream'
16+
#pragma warning disable IDE0079 // We intentionally want to suppress the below warning.
17+
[SuppressMessage("Performance", "CA1844: Provide memory-based overrides of async methods when subclassing 'Stream'", Justification = "TODO: This should be addressed in the future.")]
18+
#pragma warning restore IDE0079
1719
public class SftpFileStream : Stream
18-
#pragma warning restore CA1844 // Provide memory-based overrides of async methods when subclassing 'Stream'
1920
{
2021
private readonly object _lock = new object();
2122
private readonly int _readBufferSize;
@@ -88,7 +89,6 @@ public override bool CanTimeout
8889
/// <exception cref="NotSupportedException">A class derived from Stream does not support seeking. </exception>
8990
/// <exception cref="ObjectDisposedException">Methods were called after the stream was closed. </exception>
9091
/// <exception cref="IOException">IO operation failed. </exception>
91-
[SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "Be design this is the exception that stream need to throw.")]
9292
public override long Length
9393
{
9494
get

0 commit comments

Comments
 (0)