Python: add PyMongo read results as sources for `py/sql-injection` in second-order SQL construction flows
还没有人认领这个 Issue。
评估
调研方向
先阅读 PyMongo.qll、PEP249.qll 以及现有的 py/sql-injection source 和 sink 建模。跟踪 find、find_one 和 find_one_and_* 的结果如何进入现有的 SQL-injection 流程,然后使用一个精简的二阶示例和 query 的测试来验证预期行为;当持久化的 PyMongo 值能够到达现有的 execute sink,且无需新增 sink 建模时,即视为完成。
由索引模型根据 Issue 内容生成。
描述
py/sql-injection already appears to model the sink side correctly through the existing DB-API / PEP249.qll coverage for execute(...). The gap seems to be on the source side for a common second-order pattern: values read from MongoDB with PyMongo are later reused in dynamically constructed SQL. I ran into this while triaging KBase Metrics (CVE-2022-4860), but the underlying issue is broader than that one project.
A reduced example looks like this:
from pymongo import MongoClient
import psycopg2
def sync_users():
users = []
for record in MongoClient(uri).auth.users.find({"role": "dev"}, {"user": 1, "_id": 0}):
users.append(record["user"])
in_clause = "', '".join(users)
sql = (
"update user_info set active = true "
"where username in ('" + in_clause + "')"
)
cur = psycopg2.connect(dsn).cursor()
cur.execute(sql)
My reading of the current modeling is that this flow falls between two existing pieces: PyMongo.qll models collection operations for NoSQL semantics, while py/sql-injection starts from active threat-model sources that do not seem to cover data read back from PyMongo collections. As a result, the query has the right sink and the right string-building path shape, but no source that can reach it.
I do not think this needs a new query or wider sink modeling. The fix seems fairly contained: add source coverage for values obtained from common PyMongo read APIs such as find, find_one, and find_one_and_*, so that those results can participate in the existing py/sql-injection flow. If widening default behavior is a concern, this could also live behind an opt-in threat-model bucket for persisted database results rather than being treated as generic local input.
This pattern is common in real Python codebases, especially in cron jobs, reporting jobs, migration scripts, sync workers, and ETL-style code that bridges Mongo-backed application state into relational stores. Bandit's B608 already flags the same family syntactically by recognizing SQL-shaped string construction passed to execute(), so there is at least external evidence that this is a practical and recurring pattern. CodeQL seems close to covering it already; the missing piece is verifiable semantic coverage for PyMongo-backed persisted data flowing into the existing SQL sinks.
- 主要语言
- CodeQL
- 星标
- 10.1k
- 派生
- 2.1k
- 平均合并
- 2 天 10 小时
- 30 天内合并 PR
- 134
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
github/codeql 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
false-positive
难度 2/5 1-3 小时 新手友好度 70/100
-
false-positive
难度 3/5 1-2 天 新手友好度 68/100
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
-
难度 2/5 1-3 小时 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 90/100
splunk/token-meter#56 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
-
bug status: needs triage
难度 2/5 1-3 小时 新手友好度 84/100