Stubs (.pyi) for type definition for connection and cursor objects #67
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
These .pyi files are needed for static vulnerabilities checkers (e.g. Pyre/Pysa to find SQL injections) or static type checkers.
Please note the following types are defined, like MySQLConnectionAbstract and MySQLCursorAbstract:
def connect(*args: Any, **kwargs: Any) -> MySQLConnectionAbstract: ...
...
def connect(self, **kwargs: Any) -> MySQLConnectionAbstract: ...
...
def cursor( self, buffered: Optional[Any] = ..., raw: Optional[Any] = ..., prepared: Optional[Any] = ..., cursor_class: Optional[Any] = ..., dictionary: Optional[Any] = ..., named_tuple: Optional[Any] = ..., ) -> MySQLCursorAbstract: ...
Without such definitions, static type checkers cannot infer the types and thus find vulnerabilities.
There was an issue at the "typeshed" library python/typeshed#146 but they didn't integrate the MySQL stubs. They encourage the stubs to be distributed by the developers of the modules. Please also see https://www.python.org/dev/peps/pep-0484/#the-typeshed-repo
With the above stubs, Pyre https://github.com/facebook/pyre-check/ is finally able to find SQL injections in Python code that works with the "mysql.connector".