Prevent native SQL injections

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: php-security/laravel-native-sql-injection

Language: PHP

Severity: Error

Category: Security

CWE: 89

Description

SQL injection is a code injection technique that attackers use to exploit vulnerabilities in a web application’s database layer. This can lead to unauthorized access, data theft, data loss, and even loss of control over the server.

SQL injection vulnerabilities are among the most common and most harmful security flaws in web applications. These vulnerabilities can lead to severe consequences, including data breaches and unauthorized access to sensitive data.

Do not concatenate strings to form SQL queries; instead, use parameterized queries or prepared statements. Concatenating strings can lead to SQL injection if the inputs are not properly sanitized.

Non-Compliant Code Examples

<?php
class UserController extends Controller
{
  public function show($userId) {
    $query = 'SELECT * FROM users WHERE id = ' . $userId;
    $result = dbx_query($connection, $query);
    return view('user.show', ['users' => $result]);
  }

  public function display($userId) {
    $connection = db2_connect($dbName, $dbUser, $dbPass);
    $sql = 'SELECT * FROM users WHERE id = ' . $userId;
    $result = db2_exec($connection, $sql);
    return view('user.show', ['users' => $result]);
  }

  public function findByEmail($email) {
    $query = 'SELECT * FROM users WHERE email = ' . $email;
    $result = mysql_unbuffered_query($query, $connection);
    return view('user.show', ['users' => $result]);
  }
}

Compliant Code Examples

<?php
class UserController extends Controller
{
  public function show($userId) {
    $result = DB::table('users')->where('id', $userId)->get();
    return view('user.show', ['users' => $result]);
  }

  public function display($userId) {
    $connection = db2_connect($dbName, $dbUser, $dbPass);
    $sql = 'SELECT * FROM users WHERE id = ?';
    $stmt = db2_prepare($connection, $sql);
    db2_bind_param($stmt, 1, "userId", DB2_PARAM_IN);
    db2_execute($stmt);
    $result = db2_fetch_assoc($stmt);
    return view('user.show', ['users' => $result]);
  }

  public function findByEmail($email) {
    $result = DB::table('users')->where('email', $email)->get();
    return view('user.show', ['users' => $result]);
  }
}
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: safchain/fix-custom-agent