- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-security/sql-injection
Language: Ruby
Severity: Error
Category: Security
CWE: 89
This rule pertains to avoiding SQL Injection, which is a serious security vulnerability that can allow attackers to manipulate or control your database. SQL Injection occurs when untrusted data is inserted into a database query without proper sanitization or parameterization.
In the provided non-compliant code, the SQL queries are constructed by string concatenation with user-provided input. This is a dangerous practice as it allows an attacker to inject arbitrary SQL code into the query. For instance, if an attacker provides an ‘id’ value of “1; DROP TABLE analysis_results;”, it could lead to the deletion of an entire table.
To avoid SQL Injection, always use parameterized queries or prepared statements, which ensure that user-provided input is always treated as literal values, not executable code. In Ruby, you can use the ‘quote’ and ‘sanitize’ methods provided by ActiveRecord, or use ‘?’ placeholders in your SQL queries to safely include user-provided input. For example, you could write: ActiveRecord::Base.connection.execute("UPDATE analysis_results SET running_time_sec = ? WHERE id = ?", time, id)
. This ensures that the ’time’ and ‘id’ values are properly escaped, preventing SQL Injection.
stmt = "UPDATE analysis_results SET running_time_sec=" + time + " WHERE id="+id
stmt = "update analysis_results SET running_time_sec=#{time} WHERE id=#{id}"
stmt = "UPDATE analysis_results SET running_time_sec=%{time} WHERE id=%{id}" % {id: 42, time: 51}
stmt = "UPDATE analysis_results SET running_time_sec=" + time + " WHERE id="+id
stmt = "UPDATE analysis_results SET running_time_sec=#{time} WHERE id=#{id}"
stmt = "UPDATE analysis_results SET running_time_sec=%{time} WHERE id=%{id}" % {id: 42, time: 51}
stmt = "SELECT foo,bar FROM myTable WHERE id="+id
stmt = "SELECT * from mytable WHERE id=#{id}"
stmt = "SELECT plop.foo from my_table WHERE id=%{id}" % {id: 42, time: 51}
stmt = "DELETE FROM myTable WHERE id="+id
stmt = "delete from mytable WHERE id=#{id}"
stmt = "DELETE from my_table WHERE id=%{id}" % {id: 42, time: 51}
stmt = "INSERT INTO myTable VALUES("+id+");"
stmt = "insert into mytable(field1, field2) VALUES (#{field1}, #{field2}})"
stmt = "insert INTO my_table(field1, field2) VALUES (%{field1},%{field2}) " % {field1: 42, field2: 51}