This rule is a security-oriented rule that discourages the use of functions like rand() and mt_rand(). These functions generate pseudo-random numbers, which are not truly random and can be predictable, making them a weak choice for any situation where security is a concern, such as generating random passwords or tokens.
Using pseudo-random numbers can lead to vulnerabilities in your code. An attacker might be able to predict the output of these functions and exploit this predictability.
To maintain secure coding practices, you can use the random_int() function instead. This function generates cryptographically secure random integers, making it a much safer choice. For example, instead of using $var = rand();, you can use $var = random_int(20, 40);. By following this rule, you can help to ensure that your code is as secure as possible.
Non-Compliant Code Examples
<?php$var=rand();$var=mt_rand(20,40);
Compliant Code Examples
<?php$var=random_int(20,40);
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- php-security # Rules to enforce PHP security.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines