Skip to content

Commit 3e42f76

Browse files
roggenkemperandrewshie-sentry
authored andcommitted
fix(detectors): Add filter for queries with &param (#97618)
similar to the existing filter for queries with "?" or other strings we expect in parameterized queries, this pr adds a check to see if the query has "&param" in it (example - `select * from tasks where status = &status`).
1 parent bb0403d commit 3e42f76

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"event_id": "5d6401994d7949d2ac3474f472564370",
3+
"platform": "node",
4+
"message": "",
5+
"datetime": "2025-05-12T22:42:38.642986+00:00",
6+
"breakdowns": {
7+
"span_ops": {
8+
"ops.db": {
9+
"value": 65.715075,
10+
"unit": "millisecond"
11+
},
12+
"total.time": {
13+
"value": 67.105293,
14+
"unit": "millisecond"
15+
}
16+
}
17+
},
18+
"request": {
19+
"url": "http://localhost:3001/vulnerable-login",
20+
"method": "POST",
21+
"data": {
22+
"username": "bob"
23+
}
24+
},
25+
26+
"spans": [
27+
{
28+
"timestamp": 1747089758.637715,
29+
"start_timestamp": 1747089758.572,
30+
"exclusive_time": 65.715075,
31+
"op": "db",
32+
"span_id": "4703181ac343f71a",
33+
"parent_span_id": "91fa92ff0205967d",
34+
"trace_id": "375a86eca09a4a4e91903838dd771f50",
35+
"status": "ok",
36+
"description": "SELECT * FROM users WHERE uid = &uid and username in ('bob')",
37+
"origin": "auto.db.otel.mysql2",
38+
"sentry_tags": {
39+
"description": "SELECT * FROM users WHERE uid = &uid and username in ('bob')"
40+
},
41+
"data": {
42+
"db.system": "mysql",
43+
"db.connection_string": "jdbc:mysql://localhost:3306/injection_test",
44+
"db.name": "injection_test",
45+
"db.statement": "SELECT * FROM users WHERE uid = &uid and username in ('bob')",
46+
"db.user": "root",
47+
"net.peer.name": "localhost",
48+
"net.peer.port": 3306,
49+
"otel.kind": "CLIENT",
50+
"sentry.op": "db",
51+
"sentry.origin": "auto.db.otel.mysql2"
52+
},
53+
"hash": "45330ba0cafa5997"
54+
}
55+
]
56+
}

src/sentry/performance_issues/detectors/sql_injection_detector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def _is_span_eligible(self, span: Span) -> bool:
246246
description[:6].upper() != "SELECT"
247247
or "WHERE" not in description.upper()
248248
or any(keyword in description for keyword in PARAMETERIZED_KEYWORDS)
249+
or re.search(r"&[A-Za-z_][A-Za-z0-9_]*", description)
249250
):
250251
return False
251252

tests/sentry/performance_issues/test_sql_injection_detector.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,7 @@ def test_sql_injection_on_orm_queries(self) -> None:
107107
def test_sql_injection_on_zf1_event(self) -> None:
108108
injection_event = get_event("sql-injection/sql-injection-event-zf1")
109109
assert len(self.find_problems(injection_event)) == 0
110+
111+
def test_sql_injection_on_parameterized_query(self) -> None:
112+
injection_event = get_event("sql-injection/sql-injection-event-parameterized-query")
113+
assert len(self.find_problems(injection_event)) == 0

0 commit comments

Comments
 (0)