Skip to content

Python: Add Pandas SQLi sinks #19594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions python/ql/lib/semmle/python/frameworks/Pandas.qll
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,17 @@ private module Pandas {

override DataFlow::Node getCode() { result = this.getParameter(0, "expr").asSink() }
}

/**
* A Call to `pandas.read_sql` or `pandas.read_sql_query`
* which allows for executing raw SQL queries against a database.
* See https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html
*/
class ReadSqlCall extends SqlExecution::Range, DataFlow::CallCfgNode {
ReadSqlCall() {
this = API::moduleImport("pandas").getMember(["read_sql", "read_sql_query"]).getACall()
}

override DataFlow::Node getSql() { result in [this.getArg(0), this.getArgByName("sql")] }
}
}
4 changes: 4 additions & 0 deletions python/ql/src/change-notes/2025-05-26-pandas-sqli-sinks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Added SQL injection models from the `pandas` PyPI package.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pandas as pd

import sqlite3

df = pd.DataFrame({'temp_c': [17.0, 25.0]}, index=['Portland', 'Berkeley'])
df.sample().query("query") # $getCode="query"
Expand Down Expand Up @@ -55,11 +55,12 @@
df.query("query") # $getCode="query"
df.eval("query") # $getCode="query"

df = pd.read_sql_query("filepath", 'postgres:///db_name')
connection = sqlite3.connect("pets.db")
df = pd.read_sql_query("sql query", connection) # $getSql="sql query"
df.query("query") # $getCode="query"
df.eval("query") # $getCode="query"

df = pd.read_sql("filepath", 'postgres:///db_name')
df = pd.read_sql("sql query", connection) # $getSql="sql query"
df.query("query") # $getCode="query"
df.eval("query") # $getCode="query"

Expand Down