@@ -11,6 +11,9 @@ private import semmle.python.dataflow.new.TaintTracking
1111private import semmle.python.ApiGraphs
1212private import semmle.python.Concepts
1313private import experimental.semmle.python.Concepts
14+ // This import is done like this to avoid importing the deprecated top-level things that
15+ // would pollute the namespace
16+ private import semmle.python.frameworks.PEP249:: PEP249 as PEP249
1417
1518/**
1619 * Provides models for the `SQLAlchemy` PyPI package.
@@ -128,6 +131,43 @@ private module SqlAlchemy {
128131 DataFlow:: Node instance ( ) { instance ( DataFlow:: TypeTracker:: end ( ) ) .flowsTo ( result ) }
129132 }
130133
134+ /**
135+ * Provides models for the underlying DB-API Connection of a SQLAlchemy Connection.
136+ *
137+ * See https://docs.sqlalchemy.org/en/14/core/connections.html#dbapi-connections.
138+ */
139+ module DBAPIConnection {
140+ /**
141+ * A source of instances of DB-API Connections, extend this class to model new instances.
142+ *
143+ * This can include instantiations of the class, return values from function
144+ * calls, or a special parameter that will be set when functions are called by an external
145+ * library.
146+ *
147+ * Use the predicate `DBAPIConnection::instance()` to get references to instances of DB-API Connections.
148+ */
149+ abstract class InstanceSource extends DataFlow:: LocalSourceNode { }
150+
151+ private class DBAPIConnectionSources extends InstanceSource , PEP249:: Connection:: InstanceSource {
152+ DBAPIConnectionSources ( ) {
153+ this .( DataFlow:: MethodCallNode ) .calls ( Engine:: instance ( ) , "raw_connection" )
154+ or
155+ this .( DataFlow:: AttrRead ) .accesses ( Connection:: instance ( ) , "connection" )
156+ }
157+ }
158+
159+ /** Gets a reference to an instance of DB-API Connections. */
160+ private DataFlow:: TypeTrackingNode instance ( DataFlow:: TypeTracker t ) {
161+ t .start ( ) and
162+ result instanceof InstanceSource
163+ or
164+ exists ( DataFlow:: TypeTracker t2 | result = instance ( t2 ) .track ( t2 , t ) )
165+ }
166+
167+ /** Gets a reference to an instance of DB-API Connections. */
168+ DataFlow:: Node instance ( ) { instance ( DataFlow:: TypeTracker:: end ( ) ) .flowsTo ( result ) }
169+ }
170+
131171 /**
132172 * Provides models for the `sqlalchemy.orm.Session` class
133173 *
0 commit comments