Skip to content

Commit 0420ac7

Browse files
authored
Merge pull request #4820 from RasmusWL/add-pymysql-modeling
Approved by yoff
2 parents 90dbb60 + daf4186 commit 0420ac7

File tree

9 files changed

+46
-4
lines changed

9 files changed

+46
-4
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added model of `PyMySQL` PyPI package as a SQL interface following PEP249, resulting in additional sinks for `py/sql-injection`.

python/ql/src/semmle/python/Frameworks.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ private import semmle.python.frameworks.Django
77
private import semmle.python.frameworks.Fabric
88
private import semmle.python.frameworks.Flask
99
private import semmle.python.frameworks.Invoke
10-
private import semmle.python.frameworks.MySQLdb
1110
private import semmle.python.frameworks.MysqlConnectorPython
11+
private import semmle.python.frameworks.MySQLdb
1212
private import semmle.python.frameworks.Psycopg2
13+
private import semmle.python.frameworks.PyMySQL
1314
private import semmle.python.frameworks.Stdlib
1415
private import semmle.python.frameworks.Yaml

python/ql/src/semmle/python/frameworks/MySQLdb.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private import PEP249
1717
* - https://mysqlclient.readthedocs.io/index.html
1818
* - https://pypi.org/project/MySQL-python/
1919
*/
20-
module MySQLdb {
20+
private module MySQLdb {
2121
// ---------------------------------------------------------------------------
2222
// MySQLdb
2323
// ---------------------------------------------------------------------------

python/ql/src/semmle/python/frameworks/MysqlConnectorPython.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private import PEP249
1717
* - https://dev.mysql.com/doc/connector-python/en/
1818
* - https://dev.mysql.com/doc/connector-python/en/connector-python-example-connecting.html
1919
*/
20-
module MysqlConnectorPython {
20+
private module MysqlConnectorPython {
2121
// ---------------------------------------------------------------------------
2222
// mysql
2323
// ---------------------------------------------------------------------------

python/ql/src/semmle/python/frameworks/Psycopg2.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private import PEP249
1717
* - https://www.psycopg.org/docs/
1818
* - https://pypi.org/project/psycopg2/
1919
*/
20-
module Psycopg2 {
20+
private module Psycopg2 {
2121
// ---------------------------------------------------------------------------
2222
// Psycopg
2323
// ---------------------------------------------------------------------------
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Provides classes modeling security-relevant aspects of the `PyMySQL` PyPI package.
3+
* See https://pypi.org/project/PyMySQL/
4+
*/
5+
6+
private import python
7+
private import semmle.python.dataflow.new.DataFlow
8+
private import semmle.python.dataflow.new.RemoteFlowSources
9+
private import semmle.python.Concepts
10+
private import PEP249
11+
12+
/**
13+
* Provides models for the `PyMySQL` PyPI package.
14+
* See https://pypi.org/project/PyMySQL/
15+
*/
16+
private module PyMySQL {
17+
/** Gets a reference to the `pymysql` module. */
18+
private DataFlow::Node pymysql(DataFlow::TypeTracker t) {
19+
t.start() and
20+
result = DataFlow::importNode("pymysql")
21+
or
22+
exists(DataFlow::TypeTracker t2 | result = pymysql(t2).track(t2, t))
23+
}
24+
25+
/** Gets a reference to the `pymysql` module. */
26+
DataFlow::Node pymysql() { result = pymysql(DataFlow::TypeTracker::end()) }
27+
28+
/** PyMySQL implements PEP 249, providing ways to execute SQL statements against a database. */
29+
class PyMySQLPEP249 extends PEP249Module {
30+
PyMySQLPEP249() { this = pymysql() }
31+
}
32+
}

python/ql/test/experimental/library-tests/frameworks/pymysql/ConceptsTest.expected

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import python
2+
import experimental.meta.ConceptsTest
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pymysql
2+
connection = pymysql.connect(host="localhost", user="user", password="passwd")
3+
4+
cursor = connection.cursor()
5+
cursor.execute("some sql", (42,)) # $ getSql="some sql"

0 commit comments

Comments
 (0)