rspec/rules/S2245/csharp/rule.adoc

37 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-06-30 12:48:07 +02:00
Using pseudorandom number generators (PRNGs) is security-sensitive. For example, it has led in the past to the following vulnerabilities:
2020-06-30 12:48:07 +02:00
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2013-6386[CVE-2013-6386]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3419[CVE-2006-3419]
* http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-4102[CVE-2008-4102]
When software generates predictable values in a context requiring unpredictability, it may be possible for an attacker to guess the next value that will be generated, and use this guess to impersonate another user or access sensitive information.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
As the ``++System.Random++`` class relies on a pseudorandom number generator, it should not be used for security-critical applications or for protecting sensitive data. In such context, the ``++System.Cryptography.RandomNumberGenerator++`` class which relies on a cryptographically strong random number generator (RNG) should be used in place.
2020-06-30 12:48:07 +02:00
include::../ask-yourself.adoc[]
include::../recommended.adoc[]
== Sensitive Code Example
----
var random = new Random(); // Sensitive use of Random
byte[] data = new byte[16];
random.NextBytes(data);
return BitConverter.ToString(data); // Check if this value is used for hashing or encryption
----
== Compliant Solution
----
using System.Security.Cryptography;
...
var randomGenerator = RandomNumberGenerator.Create(); // Compliant for security-sensitive use cases
byte[] data = new byte[16];
randomGenerator.GetBytes(data);
return BitConverter.ToString(data);
----
include::../see.adoc[]