|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `pydantic` PyPI package. |
| 3 | + * |
| 4 | + * See |
| 5 | + * - https://pypi.org/project/pydantic/ |
| 6 | + * - https://pydantic-docs.helpmanual.io/ |
| 7 | + */ |
| 8 | + |
| 9 | +private import python |
| 10 | +private import semmle.python.dataflow.new.DataFlow |
| 11 | +private import semmle.python.dataflow.new.TaintTracking |
| 12 | +private import semmle.python.Concepts |
| 13 | +private import semmle.python.ApiGraphs |
| 14 | + |
| 15 | +/** |
| 16 | + * INTERNAL: Do not use. |
| 17 | + * |
| 18 | + * Provides models for `pydantic` PyPI package. |
| 19 | + * |
| 20 | + * See |
| 21 | + * - https://pypi.org/project/pydantic/ |
| 22 | + * - https://pydantic-docs.helpmanual.io/ |
| 23 | + */ |
| 24 | +module Pydantic { |
| 25 | + /** |
| 26 | + * Provides models for `pydantic.BaseModel` subclasses (a pydantic model). |
| 27 | + * |
| 28 | + * See https://pydantic-docs.helpmanual.io/usage/models/. |
| 29 | + */ |
| 30 | + module BaseModel { |
| 31 | + /** Gets a reference to a `pydantic.BaseModel` subclass (a pydantic model). */ |
| 32 | + API::Node subclassRef() { |
| 33 | + result = API::moduleImport("pydantic").getMember("BaseModel").getASubclass+() |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * A source of instances of `pydantic.BaseModel` subclasses, extend this class to model new instances. |
| 38 | + * |
| 39 | + * This can include instantiations of the class, return values from function |
| 40 | + * calls, or a special parameter that will be set when functions are called by an external |
| 41 | + * library. |
| 42 | + * |
| 43 | + * Use the predicate `BaseModel::instance()` to get references to instances of `pydantic.BaseModel`. |
| 44 | + */ |
| 45 | + abstract class InstanceSource extends DataFlow::LocalSourceNode { } |
| 46 | + |
| 47 | + /** Gets a reference to an instance of a `pydantic.BaseModel` subclass. */ |
| 48 | + private DataFlow::TypeTrackingNode instance(DataFlow::TypeTracker t) { |
| 49 | + t.start() and |
| 50 | + result instanceof InstanceSource |
| 51 | + or |
| 52 | + t.start() and |
| 53 | + instanceStepToPydanticModel(_, result) |
| 54 | + or |
| 55 | + exists(DataFlow::TypeTracker t2 | result = instance(t2).track(t2, t)) |
| 56 | + } |
| 57 | + |
| 58 | + /** Gets a reference to an instance of a `pydantic.BaseModel` subclass. */ |
| 59 | + DataFlow::Node instance() { instance(DataFlow::TypeTracker::end()).flowsTo(result) } |
| 60 | + |
| 61 | + /** |
| 62 | + * A step from an instance of a `pydantic.BaseModel` subclass, that might result in |
| 63 | + * an instance of a `pydantic.BaseModel` subclass. |
| 64 | + * |
| 65 | + * NOTE: We currently overapproximate, and treat all attributes as containing another |
| 66 | + * pydantic model. For the code below, we _could_ limit this to `main_foo` and |
| 67 | + * members of `other_foos`. |
| 68 | + * |
| 69 | + * ```py |
| 70 | + * class MyComplexModel(BaseModel): |
| 71 | + * field: str |
| 72 | + * main_foo: Foo |
| 73 | + * other_foos: List[Foo] |
| 74 | + * ``` |
| 75 | + */ |
| 76 | + private predicate instanceStepToPydanticModel(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 77 | + // attributes (such as `model.foo`) |
| 78 | + nodeFrom = instance() and |
| 79 | + nodeTo.(DataFlow::AttrRead).getObject() = nodeFrom |
| 80 | + or |
| 81 | + // subscripts on attributes (such as `model.foo[0]`) |
| 82 | + nodeFrom.(DataFlow::AttrRead).getObject() = instance() and |
| 83 | + nodeTo.asCfgNode().(SubscriptNode).getObject() = nodeFrom.asCfgNode() |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Extra taint propagation for `pydantic.BaseModel` subclasses. (note that these could also be `pydantic.BaseModel` subclasses) |
| 88 | + */ |
| 89 | + private class AdditionalTaintStep extends TaintTracking::AdditionalTaintStep { |
| 90 | + override predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) { |
| 91 | + // attributes (such as `model.foo`) |
| 92 | + nodeFrom = instance() and |
| 93 | + nodeTo.(DataFlow::AttrRead).getObject() = nodeFrom |
| 94 | + or |
| 95 | + // subscripts on attributes (such as `model.foo[0]`) |
| 96 | + nodeFrom.(DataFlow::AttrRead).getObject() = instance() and |
| 97 | + nodeTo.asCfgNode().(SubscriptNode).getObject() = nodeFrom.asCfgNode() |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments