Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
The extract() function in PHP can be used to import variables into the local symbol table from an array. However, using it on untrusted data, such as user input, can lead to a variety of security vulnerabilities, including arbitrary code execution and SQL injection, making it a dangerous practice.
By using extract() on untrusted data, you may inadvertently create variables that overwrite important ones, or worse, you could execute harmful code that was injected by a malicious user.
To adhere to this rule, you should explicitly assign and sanitize user input rather than using extract(). This will ensure your code remains secure and compliant.
Non-Compliant Code Examples
<?php// Insecure: Using extract() on untrusted data from $_GET
extract($_GET);echo"Hello, $name!";// Insecure: Using extract() on untrusted data from $_POST
extract($_POST);if($isAdmin){echo"Welcome, admin!";}else{echo"Welcome, user!";}// Insecure: Using extract() on untrusted data from $_FILES
extract($_FILES['uploadedFile']);if(move_uploaded_file($tmp_name,"uploads/$name")){echo"File uploaded successfully!";}else{echo"File upload failed.";}?>
Compliant Code Examples
<?php// Secure: Explicitly assign and sanitize user input
$name=htmlspecialchars($_GET['name'],ENT_QUOTES,'UTF-8');echo"Hello, $name!";// Secure: Explicitly assign and validate user input
$isAdmin=isset($_POST['isAdmin'])&&$_POST['isAdmin']=='1';if($isAdmin){echo"Welcome, admin!";}else{echo"Welcome, user!";}// Secure: Explicitly handle file upload variables and validate
$file=$_FILES['uploadedFile'];$uploadDir='uploads/';$uploadFile=$uploadDir.basename($file['name']);if(move_uploaded_file($file['tmp_name'],$uploadFile)){echo"File uploaded successfully!";}else{echo"File upload failed.";}?>
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