Skip to content

Commit 41743b6

Browse files
committed
Python: restrict to caught exceptions
also modernise code
1 parent 24b51e8 commit 41743b6

File tree

1 file changed

+11
-121
lines changed

1 file changed

+11
-121
lines changed

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

Lines changed: 11 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,138 +1661,28 @@ private module Stdlib {
16611661
// ---------------------------------------------------------------------------
16621662
// traceback
16631663
// ---------------------------------------------------------------------------
1664-
/** Gets a reference to the `traceback` module. */
1665-
API::Node traceback() { result = API::moduleImport("traceback") }
1666-
1667-
/**
1668-
* Gets a reference to the attribute `attr_name` of the `traceback` module.
1669-
*/
1670-
private API::Node traceback_attr(string attr_name) { result = traceback().getMember(attr_name) }
1671-
16721664
/** Provides models for the `traceback` module. */
16731665
module traceback {
1674-
private class TracebackFunctionCall extends ErrorInfoSource::Range, DataFlow::CfgNode {
1675-
override CallNode node;
1676-
1666+
private class TracebackFunctionCall extends ErrorInfoSource::Range, DataFlow::CallCfgNode {
16771667
TracebackFunctionCall() {
1678-
node.getFunction() =
1679-
traceback_attr([
1680-
"extract_tb", "extract_stack", "format_list", "format_exception_only",
1681-
"format_exception", "format_exc", "format_tb", "format_stack"
1682-
]).getAUse().asCfgNode()
1668+
this =
1669+
API::moduleImport("traceback")
1670+
.getMember([
1671+
"extract_tb", "extract_stack", "format_list", "format_exception_only",
1672+
"format_exception", "format_exc", "format_tb", "format_stack"
1673+
])
1674+
.getACall()
16831675
}
16841676
}
16851677
}
16861678
}
16871679

1688-
/**
1689-
* Provides models for the `Stdlib.BaseException` class
1690-
*
1691-
* See https://docs.python.org/3/library/exceptions.html#BaseException.
1692-
*/
1693-
module BaseException {
1694-
/** Gets a reference to the `Stdlib.BaseException` class. */
1695-
private DataFlow::Node classRef(DataFlow::TypeTracker t) {
1696-
t.start() and
1697-
result.asExpr().(Name).getId() = "BaseException"
1698-
or
1699-
exists(DataFlow::TypeTracker t2 | result = classRef(t2).track(t2, t))
1700-
}
1701-
1702-
/** Gets a reference to the `Stdlib.BaseException` class. */
1703-
DataFlow::Node classRef() { result = classRef(DataFlow::TypeTracker::end()) }
1704-
1705-
/**
1706-
* A source of an instance of `Stdlib.BaseException`.
1707-
*
1708-
* This can include instantiation of the class, return value from function
1709-
* calls, or a special parameter that will be set when functions are call by external
1710-
* library.
1711-
*
1712-
* Use `BaseException::instance()` predicate to get references to instances of `Stdlib.BaseException`.
1713-
*/
1714-
abstract class InstanceSource extends DataFlow::Node { }
1715-
1716-
/** A direct instantiation of `Stdlib.BaseException`. */
1717-
private class ClassInstantiation extends InstanceSource, DataFlow::CfgNode {
1718-
override CallNode node;
1719-
1720-
ClassInstantiation() { node.getFunction() = classRef().asCfgNode() }
1721-
}
1722-
1723-
/** Gets a reference to an instance of `Stdlib.BaseException`. */
1724-
private DataFlow::Node instance(DataFlow::TypeTracker t) {
1725-
t.start() and
1726-
result instanceof InstanceSource
1727-
or
1728-
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
1729-
}
1730-
1731-
/** Gets a reference to an instance of `Stdlib.BaseException`. */
1732-
DataFlow::Node instance() { result = instance(DataFlow::TypeTracker::end()) }
1733-
}
1734-
1735-
/**
1736-
* Provides models for the `BaseException` class and subclasses.
1737-
*/
1738-
private module Exception {
1739-
/** Gets a reference to the `BaseHTTPRequestHandler` class or any subclass. */
1740-
private DataFlow::Node subclassRef(DataFlow::TypeTracker t) {
1741-
t.start() and
1742-
result = BaseException::classRef()
1743-
or
1744-
// subclasses in project code
1745-
result.asExpr().(ClassExpr).getABase() = subclassRef(t.continue()).asExpr()
1746-
or
1747-
exists(DataFlow::TypeTracker t2 | result = subclassRef(t2).track(t2, t))
1748-
}
1749-
1750-
/** Gets a reference to the `BaseException` class or any subclass. */
1751-
DataFlow::Node subclassRef() { result = subclassRef(DataFlow::TypeTracker::end()) }
1752-
1753-
/** A HTTPRequestHandler class definition (most likely in project code). */
1754-
class ExceptionClassDef extends Class {
1755-
ExceptionClassDef() { this.getParent() = subclassRef().asExpr() }
1756-
}
1757-
1758-
/**
1759-
* A source of instances of the `BaseException` class or any subclass, extend this class to model new instances.
1760-
*
1761-
* This can include instantiations of the class, return values from function
1762-
* calls, or a special parameter that will be set when functions are called by an external
1763-
* library.
1764-
*
1765-
* Use the predicate `classname::instance()` to get references to instances of the `Exception` class or any subclass.
1766-
*/
1767-
abstract class InstanceSource extends DataFlow::Node { }
1768-
1769-
/** The `self` parameter in a method on the `BaseException` class or any subclass. */
1770-
private class SelfParam extends InstanceSource, DataFlow::ParameterNode {
1771-
SelfParam() { exists(ExceptionClassDef cls | cls.getAMethod().getArg(0) = this.getParameter()) }
1772-
}
1773-
1774-
/** Gets a reference to an instance of the `BaseException` class or any subclass. */
1775-
private DataFlow::Node instance(DataFlow::TypeTracker t) {
1776-
t.start() and
1777-
result instanceof InstanceSource
1778-
or
1779-
exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t))
1780-
}
1781-
1782-
/** Gets a reference to an instance of the `BaseException` class or any subclass. */
1783-
DataFlow::Node instance() { result = instance(DataFlow::TypeTracker::end()) }
1784-
}
1785-
1786-
private class Exception extends ExceptionSource::Range {
1787-
Exception() {
1788-
this = Exception::instance()
1789-
or
1790-
this.asExpr() = any(ExceptStmt s).getName()
1791-
}
1680+
private class CaughtException extends ExceptionSource::Range {
1681+
CaughtException() { this.asExpr() = any(ExceptStmt s).getName() }
17921682
}
17931683

17941684
/** A call to `sys.exc_info` */
1795-
private class SysExcInfoCall extends ErrorInfoSource::Range, DataFlow::CfgNode {
1685+
private class SysExcInfoCall extends ErrorInfoSource::Range, DataFlow::CallCfgNode {
17961686
SysExcInfoCall() { this = API::moduleImport("sys").getMember("exc_info").getACall() }
17971687
}
17981688

0 commit comments

Comments
 (0)