Python: add PyMongo read results as sources for `py/sql-injection` in second-order SQL construction flows
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
Hướng nghiên cứu
Bắt đầu bằng cách đọc PyMongo.qll, PEP249.qll và phần mô hình hóa source và sink hiện có của py/sql-injection. Theo dõi cách kết quả từ find, find_one và find_one_and_* có thể đi vào luồng SQL-injection hiện có, sau đó xác thực hành vi dự kiến bằng một ví dụ second-order rút gọn và các bài kiểm thử của query; hoàn thành khi các giá trị PyMongo đã được lưu persisted đến được sink execute hiện có mà không cần mô hình hóa sink mới.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- CodeQL
- Star
- 10.1k
- Fork
- 2.1k
- Merge trung bình
- 2 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 129
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của github/codeql
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
C#: cs/simplifiable-boolean-expression false positive on Nullable<bool> compared with a literal Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
false-positive
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
false-positive
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
Tất cả issue của github/codeql
Issue tương tự
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
antlr/grammars-v4#5014 · 1 bình luận ·
-
Edit: CW+ Đang mởchannels:edit check:passed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
registrystack/registry-stack#1239 ·
-
Area/ICP Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
wso2/product-integrator#2514 ·
-
P2
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100