Your project should not use insecure random number functions 58
- Read doc
- Security
- Major
More information: https://insight.symfony.com/what-we-analyse/php.use_insecure_random_function
New rule!
We've recently added this rule to Insight. Don't be surprised to see new suggestions even though the codebase didn't change.
- public function check(): bool
- {
- $CLICSHOPPING_MessageStack = Registry::get('MessageStack');
- if (isset($_GET[$this->_file])) {
- $temp_filename = 'temp_' . mt_rand(100000, 999999);
- while (file_exists(CLICSHOPPING::BASE_DIR . 'Work/Temp/' . $temp_filename)) {
- $temp_filename = 'temp_' . mt_rand(100000, 999999);
- }
- if (isset($_GET[$this->_file])) {
- $temp_filename = 'temp_' . mt_rand(100000, 999999);
- while (file_exists(CLICSHOPPING::BASE_DIR . 'Work/Temp/' . $temp_filename)) {
- $temp_filename = 'temp_' . mt_rand(100000, 999999);
- }
- $input = fopen('php://input', 'r');
- $size = file_put_contents(CLICSHOPPING::BASE_DIR . 'Work/Temp/' . $temp_filename, $input);
- $CLICSHOPPING_MessageStack = Registry::get('MessageStack');
- if ($this->_replace === true) {
- while (file_exists($this->_destination . DIRECTORY_SEPARATOR . $this->getFilename())) {
- $salt = md5(rand(1, 100000));
- $salt = substr($salt, 0, 10);
- $this->setFilename($salt . '_' . $this->getFilename());
- }
- }
- // Boucle infinie pour l'envoi des événements
- while (true) {
- try {
- // Simulate random failure before computing data
- if ($sim_fail_rate > 0 && mt_rand(0, mt_getrandmax()) / mt_getrandmax() < $sim_fail_rate) {
- throw new \Exception('Simulated failure');
- }
- // Récupérer les données de performance
- $data = $monitor->getPerformanceData($range);
- $data['metrics']['error_frequency'] = max(0.0, (float)$sim_error_pct);
- }
- }
- // Optionally force a random connection drop to test auto-reconnect
- if ($sim_drop && (mt_rand(1, 100) <= 5)) { // ~5% chance per tick
- // Flush a final event then terminate the connection
- echo "event: error\n";
- echo "data: {\"error\":\"Simulated connection drop\"}\n\n";
- ob_flush();
- flush();
- $code_length = max(4, min(8, $code_length)); // Limiter entre 4 et 8
- $verification_code = '';
- for ($i = 0; $i < $code_length; $i++) {
- $verification_code .= mt_rand(0, 9);
- }
- $expiry_minutes = defined('EMAIL_VERIFICATION_CODE_EXPIRY') ? (int)EMAIL_VERIFICATION_CODE_EXPIRY : 15;
- $expiry_time = date('Y-m-d H:i:s', time() + ($expiry_minutes * 60));
- */
- private function generateObjectiveId(): string
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- private function generateObjectiveId(): string
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- /**
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- /**
- * Get the objective ID
- */
- private function generateEventId(): string
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- private function generateEventId(): string
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- {
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- return sprintf(
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- /**
- mt_rand(0, 0xffff),
- mt_rand(0, 0x0fff) | 0x4000,
- mt_rand(0, 0x3fff) | 0x8000,
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff),
- mt_rand(0, 0xffff)
- );
- }
- /**
- * Get security statistics for dashboard
- *
- * @return string The temporary password.
- */
- private function generateTemporaryPassword(): string
- {
- return substr(md5(uniqid(rand(), true)), 0, 8);
- }
- /**
- * Inserts the primary customer record into the :table_customers table.
- *
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
- $charactersLength = strlen($characters);
- $randomString = '';
- for ($i = 0; $i < $length; $i++) {
- $randomString .= $characters[rand(0, $charactersLength - 1)];
- }
- return $randomString;
- }
- /**
- * @return string The sanitized or generated product model.
- */
- public function getProductModel(): string
- {
- if (empty($_POST['products_model'])) {
- $rand = rand();
- $products_model = \defined('CONFIGURATION_PREFIX_MODEL') ? CONFIGURATION_PREFIX_MODEL . $rand : '';
- } else {
- $products_model = HTML::sanitize($_POST['products_model']);
- }
- *
- * @return string Returns the anti-spam numeric confirmation string for display or verification purposes.
- */
- public static function getConfirmationNumericAntiSpam(): string
- {
- $random_number = rand(1, 200);
- $number = $random_number + 3;
- $antispam = ' (' . $random_number . ' + ' . CLICSHOPPING::getDef('text_antispam') . ') x 1';
- $_SESSION['createResponseAntiSpam'] = md5($number);
- *
- * @return string The temporary password.
- */
- private function generateTemporaryPassword(): string
- {
- return substr(md5(uniqid(rand(), true)), 0, 8);
- }
- /**
- * Inserts the primary customer record into the :table_customers table.
- *
- $error = true;
- $CLICSHOPPING_MessageStack->add($CLICSHOPPING_Customers->getDef('error_email_address_exist'), 'error');
- }
- if ($error === false) {
- $customers_password = 'clicshopping_' . rand(5, 500);
- // Autorisation aux clients de modifier informations societe et adresse principal + Ajout adresse
- if ($customers_modify_company !== 1) {
- $customers_modify_company = 0;
- }
- */
- public function getPerformanceData(string $range = '24h'): array
- {
- // Generate current metrics
- $metrics = [
- 'request_rate' => rand(20, 80),
- 'average_latency' => rand(100, 300),
- 'error_frequency' => rand(0, 10),
- 'uptime_percentage' => rand(95, 100),
- 'total_requests' => rand(1000, 5000)
- ];
- public function getPerformanceData(string $range = '24h'): array
- {
- // Generate current metrics
- $metrics = [
- 'request_rate' => rand(20, 80),
- 'average_latency' => rand(100, 300),
- 'error_frequency' => rand(0, 10),
- 'uptime_percentage' => rand(95, 100),
- 'total_requests' => rand(1000, 5000)
- ];
- {
- // Generate current metrics
- $metrics = [
- 'request_rate' => rand(20, 80),
- 'average_latency' => rand(100, 300),
- 'error_frequency' => rand(0, 10),
- 'uptime_percentage' => rand(95, 100),
- 'total_requests' => rand(1000, 5000)
- ];
- // Store current metrics for persistence
- // Generate current metrics
- $metrics = [
- 'request_rate' => rand(20, 80),
- 'average_latency' => rand(100, 300),
- 'error_frequency' => rand(0, 10),
- 'uptime_percentage' => rand(95, 100),
- 'total_requests' => rand(1000, 5000)
- ];
- // Store current metrics for persistence
- $this->historyStorage->storeMetrics($metrics);
- $metrics = [
- 'request_rate' => rand(20, 80),
- 'average_latency' => rand(100, 300),
- 'error_frequency' => rand(0, 10),
- 'uptime_percentage' => rand(95, 100),
- 'total_requests' => rand(1000, 5000)
- ];
- // Store current metrics for persistence
- $this->historyStorage->storeMetrics($metrics);
- for ($i = 0; $i < $points; $i++) {
- $timestamp = $baseTime - ($i * $interval);
- $history[] = [
- 'timestamp' => $timestamp,
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'requests' => rand(10, 50),
- 'uptime' => rand(95, 100)
- ];
- }
- for ($i = 0; $i < $points; $i++) {
- $timestamp = $baseTime - ($i * $interval);
- $history[] = [
- 'timestamp' => $timestamp,
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'requests' => rand(10, 50),
- 'uptime' => rand(95, 100)
- ];
- }
- $timestamp = $baseTime - ($i * $interval);
- $history[] = [
- 'timestamp' => $timestamp,
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'requests' => rand(10, 50),
- 'uptime' => rand(95, 100)
- ];
- }
- return array_reverse($history);
- $history[] = [
- 'timestamp' => $timestamp,
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'requests' => rand(10, 50),
- 'uptime' => rand(95, 100)
- ];
- }
- return array_reverse($history);
- }
- {
- $now = time();
- for ($i = 100; $i > 0; $i--) {
- $this->performanceHistory[] = [
- 'timestamp' => $now - ($i * 900), // Every 15 minutes
- 'requests' => rand(100, 1000),
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'status' => rand(1, 100) > 98 ? 'down' : 'up'
- ];
- }
- $now = time();
- for ($i = 100; $i > 0; $i--) {
- $this->performanceHistory[] = [
- 'timestamp' => $now - ($i * 900), // Every 15 minutes
- 'requests' => rand(100, 1000),
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'status' => rand(1, 100) > 98 ? 'down' : 'up'
- ];
- }
- }
- for ($i = 100; $i > 0; $i--) {
- $this->performanceHistory[] = [
- 'timestamp' => $now - ($i * 900), // Every 15 minutes
- 'requests' => rand(100, 1000),
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'status' => rand(1, 100) > 98 ? 'down' : 'up'
- ];
- }
- }
- }
- $this->performanceHistory[] = [
- 'timestamp' => $now - ($i * 900), // Every 15 minutes
- 'requests' => rand(100, 1000),
- 'latency' => rand(50, 200),
- 'error_rate' => rand(0, 5),
- 'status' => rand(1, 100) > 98 ? 'down' : 'up'
- ];
- }
- }
- }
- */
- private function getTotalRequests(): int
- {
- // In a real implementation, this would query the database
- // For simulation, return a random number
- return rand(100, 1000);
- }
- /**
- * Gets the error count (simulated).
- *
- * @return int The error count.
- */
- private function getErrorCount(): int
- {
- // Simulate low error count for healthy status
- return rand(0, 5);
- }
- /**
- * Gets the average response time (simulated).
- *
- *
- * @return float The average response time in milliseconds.
- */
- private function getAverageResponseTime(): float
- {
- return round(rand(100, 800) / 10, 2);
- }
- /**
- * Gets the requests per minute (simulated).
- *
- *
- * @return int The number of requests per minute.
- */
- private function getRequestsPerMinute(): int
- {
- return rand(10, 100);
- }
- /**
- * Gets memory usage information.
- *
- * @return float The CPU usage percentage.
- */
- private function getCpuUsage(): float
- {
- // Simulate CPU usage
- return round(rand(10, 80) / 10, 1);
- }
- /**
- * Gets disk space information.
- *
- // Simulate response time history
- $history = [];
- for ($i = 0; $i < 24; $i++) {
- $history[] = [
- 'hour' => $i,
- 'avg_response_time' => rand(100, 800) / 10
- ];
- }
- return $history;
- }
- private function getErrorTrends(): array
- {
- // Simulate error trends
- return [
- 'hourly' => array_map(function ($hour) {
- return ['hour' => $hour, 'errors' => rand(0, 5)];
- }, range(0, 23)),
- 'daily' => array_map(function ($day) {
- return ['day' => $day, 'errors' => rand(0, 50)];
- }, range(1, 7))
- ];
- return [
- 'hourly' => array_map(function ($hour) {
- return ['hour' => $hour, 'errors' => rand(0, 5)];
- }, range(0, 23)),
- 'daily' => array_map(function ($day) {
- return ['day' => $day, 'errors' => rand(0, 50)];
- }, range(1, 7))
- ];
- }
- /**
- */
- private function getUsagePatterns(): array
- {
- return [
- 'peak_hours' => [9, 10, 11, 14, 15, 16],
- 'avg_concurrent_users' => rand(10, 100),
- 'most_used_endpoints' => [
- '/api/chat' => rand(100, 500),
- '/api/status' => rand(50, 200),
- '/api/health' => rand(20, 100)
- ]
- {
- return [
- 'peak_hours' => [9, 10, 11, 14, 15, 16],
- 'avg_concurrent_users' => rand(10, 100),
- 'most_used_endpoints' => [
- '/api/chat' => rand(100, 500),
- '/api/status' => rand(50, 200),
- '/api/health' => rand(20, 100)
- ]
- ];
- }
- return [
- 'peak_hours' => [9, 10, 11, 14, 15, 16],
- 'avg_concurrent_users' => rand(10, 100),
- 'most_used_endpoints' => [
- '/api/chat' => rand(100, 500),
- '/api/status' => rand(50, 200),
- '/api/health' => rand(20, 100)
- ]
- ];
- }
- 'peak_hours' => [9, 10, 11, 14, 15, 16],
- 'avg_concurrent_users' => rand(10, 100),
- 'most_used_endpoints' => [
- '/api/chat' => rand(100, 500),
- '/api/status' => rand(50, 200),
- '/api/health' => rand(20, 100)
- ]
- ];
- }
- /**
- * @param array $data The order data.
- * @return void
- */
- protected function createOrder(array $data): void
- {
- $orderId = rand(1000, 9999);
- $this->message->sendSuccess(['action' => 'create_order', 'order_id' => $orderId]);
- }
- /**
- * Updates an existing order. Placeholder method.
- } catch (Exception $e) {
- if ($secure === true) {
- throw $e;
- }
- $result = mt_rand($min, $max);
- }
- return $result;
- }
- $code_length = max(4, min(8, $code_length)); // Limiter entre 4 et 8
- $verification_code = '';
- for ($i = 0; $i < $code_length; $i++) {
- $verification_code .= mt_rand(0, 9);
- }
- $expiry_minutes = defined('EMAIL_VERIFICATION_CODE_EXPIRY') ? (int)EMAIL_VERIFICATION_CODE_EXPIRY : 15;
- $expiry_time = date('Y-m-d H:i:s', time() + ($expiry_minutes * 60));
- $code_length = max(4, min(8, $code_length)); // Limiter entre 4 et 8
- $verification_code = '';
- for ($i = 0; $i < $code_length; $i++) {
- $verification_code .= mt_rand(0, 9);
- }
- $expiry_minutes = defined('EMAIL_VERIFICATION_CODE_EXPIRY') ? (int)EMAIL_VERIFICATION_CODE_EXPIRY : 15;
- $expiry_time = date('Y-m-d H:i:s', time() + ($expiry_minutes * 60));
- $code_length = defined('EMAIL_VERIFICATION_CODE_LENGTH') ? (int)EMAIL_VERIFICATION_CODE_LENGTH : 6;
- $code_length = max(4, min(8, $code_length));
- $verification_code = '';
- for ($i = 0; $i < $code_length; $i++) {
- $verification_code .= mt_rand(0, 9);
- }
- // Durée d'expiration
- $expiry_minutes = defined('EMAIL_VERIFICATION_CODE_EXPIRY') ? (int)EMAIL_VERIFICATION_CODE_EXPIRY : 15;