- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
",t};e.buildCustomizationMenuUi=t;function n(e){let t='
",t}function s(e){let n=e.filter.currentValue||e.filter.defaultValue,t='${e.filter.label}
`,e.filter.options.forEach(s=>{let o=s.id===n;t+=``}),t+="${e.filter.label}
`,t+=`ID: swift-security/sql-injection
Language: Unknown
Severity: Error
Category: Security
CWE: 89
This rule detects SQL queries that are constructed by concatenating or interpolating strings with variables. Building queries in this manner makes the application vulnerable to SQL Injection attacks. An attacker could provide malicious input that alters the query’s logic, potentially leading to unauthorized data access, data modification, or execution of arbitrary commands on the database.
To mitigate this vulnerability, use prepared statements with parameterized queries. This practice ensures that user input is treated as data and not as executable code, effectively preventing SQL injection attacks.
import Foundation
func findUserUnsafe(withName userName: String) {
//
// NON-COMPLIANT: String concatenation
// This query is built by joining a string literal containing a SQL keyword
// with a variable. This will be flagged by the rule.
//
let queryConcat = "SELECT * FROM users WHERE name = '" + userName + "'"
// Execute the dangerous query...
// execute(queryConcat)
}
func findUserUnsafeMultiline(withName userName: String) {
//
// NON-COMPLIANT: String concatenation
// This query is built by joining a string literal containing a SQL keyword
// with a variable. This will be flagged by the rule.
//
let queryConcat = """SELECT * FROM users WHERE name ='""" + userName
// Execute the dangerous query...
// execute(queryConcat)
}
func findItemUnsafe(ownedBy owner: String) {
//
// NON-COMPLIANT: String interpolation
// This query uses Swift's string interpolation feature `\()` to embed
// the variable directly into the string. The rule detects the interpolation
// within a string that also contains a SQL keyword.
//
let queryInterpolate = "SELECT * FROM items WHERE owner = '\(owner)'"
// Execute the dangerous query...
// execute(queryInterpolate)
}
func findItemUnsafeMulti(ownedBy owner: String) {
//
// NON-COMPLIANT: String interpolation
// This query uses Swift's string interpolation feature `\()` to embed
// the variable directly into the string. The rule detects the interpolation
// within a string that also contains a SQL keyword.
//
let queryInterpolate = """SELECT * FROM items
WHERE owner = '\(owner)'"""
// Execute the dangerous query...
// execute(queryInterpolate)
}
import Foundation
// Assuming a hypothetical database library that supports parameterized queries.
// The syntax is similar to popular libraries like SQLite.swift or FMDB.
// A placeholder for a database connection object.
class Database {
func prepare(_ statement: String) -> Statement {
// In a real implementation, this would compile the SQL statement.
return Statement(sql: statement)
}
}
// A placeholder for a prepared statement object.
class Statement {
let sql: String
init(sql: String) { self.sql = sql }
// The `run` method would safely bind the parameters and execute the query.
func run(_ bindings: Any...) {
print("Executing query: \(self.sql) with safe bindings: \(bindings)")
}
}
let db = Database() // Assume we have a database connection.
func findUserSafe(withName userName: String) {
//
// COMPLIANT: Using a parameterized query
// The query string uses a placeholder `?` instead of the actual variable.
// The database driver is responsible for safely substituting the `userName`
// value for the placeholder, preventing it from being interpreted as SQL code.
// This pattern is not flagged by the rule.
//
let query = "SELECT * FROM users WHERE name = ?"
let statement = db.prepare(query)
statement.run(userName)
}