Do not generate insecure session IDs

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: php-security/insecure-session-id

Language: PHP

Severity: Error

Category: Security

CWE: 340

Description

Session IDs are vital to maintaining state in your web applications and thus, it’s crucial to ensure that these IDs are secure and not easily guessable. If an attacker is able to predict or guess a session ID, they can hijack a user’s session, gain unauthorized access to their data, and perform actions on their behalf. This can result in severe data breaches and loss of trust from your users.

To avoid violating this rule, always generate sufficiently long and random session IDs. Also, avoid setting session IDs from user input, as this can open up the possibility for session fixation attacks.

Non-Compliant Code Examples

<?php
session_id(bin2hex(random_bytes(6)));
session_id($_POST["s_id"]);

Compliant Code Examples

<?php
session_id(bin2hex(random_bytes(16)));
session_regenerate_id();
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: rtrieu/product-analytics-ui-changes