|
| 1 | +/** This module provides an API for attribute reads and writes. */ |
| 2 | + |
| 3 | +import DataFlowPublic |
| 4 | +private import DataFlowPrivate |
| 5 | + |
| 6 | +/** |
| 7 | + * A data flow node that reads or writes an attribute of an object. |
| 8 | + * |
| 9 | + * This abstract base class only knows about the base object on which the attribute is being |
| 10 | + * accessed, and the attribute itself, if it is statically inferrable. |
| 11 | + */ |
| 12 | +abstract class AttrRef extends Node { |
| 13 | + /** |
| 14 | + * Gets the data flow node corresponding to the object whose attribute is being read or written. |
| 15 | + */ |
| 16 | + abstract Node getObject(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets the expression control flow node that defines the attribute being accessed. This is |
| 20 | + * usually an identifier or literal. |
| 21 | + */ |
| 22 | + abstract ExprNode getAttributeNameExpr(); |
| 23 | + |
| 24 | + /** Holds if this attribute reference may access an attribute named `attrName`. */ |
| 25 | + predicate mayHaveAttributeName(string attrName) { none() } |
| 26 | + |
| 27 | + /** Gets the name of the attribute being read or written, if it can be determined statically. */ |
| 28 | + abstract string getAttributeName(); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A data flow node that writes an attribute of an object. This includes |
| 33 | + * - Simple attribute writes: `object.attr = value` |
| 34 | + * - Dynamic attribute writes: `setattr(object, attr, value)` |
| 35 | + * - Fields written during class initialization: `class MyClass: attr = value` |
| 36 | + */ |
| 37 | +abstract class AttrWrite extends AttrRef { |
| 38 | + /** Gets the data flow node corresponding to the value that is written to the attribute. */ |
| 39 | + abstract Node getValue(); |
| 40 | +} |
| 41 | + |
| 42 | +/** A simple attribute assignment: `object.attr = value`. */ |
| 43 | +private class AttributeAssignmentAsAttrWrite extends AttrWrite, CfgNode { |
| 44 | + DefinitionNode attr_node; |
| 45 | + |
| 46 | + AttributeAssignmentAsAttrWrite() { this = TCfgNode(attr_node) and attr_node instanceof AttrNode } |
| 47 | + |
| 48 | + override Node getValue() { result = TCfgNode(attr_node.(DefinitionNode).getValue()) } |
| 49 | + |
| 50 | + override Node getObject() { result = TCfgNode(attr_node.(AttrNode).getObject()) } |
| 51 | + |
| 52 | + override ExprNode getAttributeNameExpr() { |
| 53 | + // Attribute names don't exist as `Node`s in the control flow graph, as they can only ever be |
| 54 | + // identifiers, and are therefore represented directly as strings. |
| 55 | + // Use `getAttributeName` to access the name of the attribute. |
| 56 | + none() |
| 57 | + } |
| 58 | + |
| 59 | + override string getAttributeName() { result = attr_node.(AttrNode).getName() } |
| 60 | +} |
| 61 | + |
| 62 | +import semmle.python.types.Builtins |
| 63 | + |
| 64 | +/** Represents `CallNode`s that may refer to calls to built-in functions or classes. */ |
| 65 | +private class BuiltInCallNode extends CallNode { |
| 66 | + string name; |
| 67 | + |
| 68 | + BuiltInCallNode() { |
| 69 | + // TODO disallow instances where `setattr` may refer to an in-scope variable of that name. |
| 70 | + exists(NameNode id | this.getFunction() = id and id.getId() = name and id.isGlobal()) and |
| 71 | + name = any(Builtin b).getName() |
| 72 | + } |
| 73 | + |
| 74 | + /** Gets the name of the built-in function that is called at this `CallNode` */ |
| 75 | + string getBuiltinName() { result = name } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Represents a call to the built-ins that handle dynamic inspection and modification of |
| 80 | + * attributes: `getattr`, `setattr`, and `hasattr`. |
| 81 | + */ |
| 82 | +private class BuiltinAttrCallNode extends BuiltInCallNode { |
| 83 | + BuiltinAttrCallNode() { |
| 84 | + name = "setattr" or |
| 85 | + name = "getattr" or |
| 86 | + name = "hasattr" |
| 87 | + } |
| 88 | + |
| 89 | + /** Gets the control flow node for object on which the attribute is accessed. */ |
| 90 | + ControlFlowNode getObject() { result in [this.getArg(0), this.getArgByName("object")] } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the control flow node for the value that is being written to the attribute. |
| 94 | + * Only relevant for `setattr` calls. |
| 95 | + */ |
| 96 | + ControlFlowNode getValue() { |
| 97 | + // only valid for `setattr` |
| 98 | + name = "setattr" and |
| 99 | + result in [this.getArg(2), this.getArgByName("value")] |
| 100 | + } |
| 101 | + |
| 102 | + /** Gets the control flow node that defines the name of the attribute being accessed. */ |
| 103 | + ControlFlowNode getName() { result in [this.getArg(1), this.getArgByName("name")] } |
| 104 | +} |
| 105 | + |
| 106 | +/** Represents calls to the built-in `setattr`. */ |
| 107 | +private class SetAttrCallNode extends BuiltinAttrCallNode { |
| 108 | + SetAttrCallNode() { name = "setattr" } |
| 109 | +} |
| 110 | + |
| 111 | +/** Represents calls to the built-in `getattr`. */ |
| 112 | +private class GetAttrCallNode extends BuiltinAttrCallNode { |
| 113 | + GetAttrCallNode() { name = "getattr" } |
| 114 | +} |
| 115 | + |
| 116 | +/** An attribute assignment using `setattr`, e.g. `setattr(object, attr, value)` */ |
| 117 | +private class SetAttrCallAsAttrWrite extends AttrWrite, CfgNode { |
| 118 | + SetAttrCallNode setattr_call; |
| 119 | + |
| 120 | + SetAttrCallAsAttrWrite() { this = TCfgNode(setattr_call) } |
| 121 | + |
| 122 | + override Node getValue() { result = TCfgNode(setattr_call.getValue()) } |
| 123 | + |
| 124 | + override Node getObject() { result = TCfgNode(setattr_call.getObject()) } |
| 125 | + |
| 126 | + override ExprNode getAttributeNameExpr() { result = TCfgNode(setattr_call.getName()) } |
| 127 | + |
| 128 | + override string getAttributeName() { |
| 129 | + // TODO track this back using local flow |
| 130 | + exists(StrConst s, Node nodeFrom | |
| 131 | + s = nodeFrom.asExpr() and |
| 132 | + simpleLocalFlowStep*(nodeFrom, this.getAttributeNameExpr()) and |
| 133 | + result = s.getText() |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +/** |
| 139 | + * An attribute assignment via a class field, e.g. |
| 140 | + * ```python |
| 141 | + * class MyClass: |
| 142 | + * attr = value |
| 143 | + * ``` |
| 144 | + * is treated as equivalent to `MyClass.attr = value`. |
| 145 | + */ |
| 146 | +private class ClassDefinitionAsAttrWrite extends AttrWrite, Node { |
| 147 | + ClassExpr cls; |
| 148 | + DefinitionNode attr_node; |
| 149 | + |
| 150 | + ClassDefinitionAsAttrWrite() { |
| 151 | + attr_node instanceof NameNode and |
| 152 | + this.asCfgNode() = attr_node and |
| 153 | + attr_node.getScope() = cls.getInnerScope() |
| 154 | + } |
| 155 | + |
| 156 | + override Node getValue() { result = TCfgNode(attr_node.getValue()) } |
| 157 | + |
| 158 | + override Node getObject() { result = TCfgNode(cls.getAFlowNode()) } |
| 159 | + |
| 160 | + override ExprNode getAttributeNameExpr() { none() } |
| 161 | + |
| 162 | + override string getAttributeName() { result = attr_node.(NameNode).getId() } |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * A read of an attribute on an object. This includes |
| 167 | + * - Simple attribute reads: `object.attr` |
| 168 | + * - Dynamic attribute reads using `getattr`: `getattr(object, attr)` |
| 169 | + * - Qualified imports: `from module import attr as name` |
| 170 | + */ |
| 171 | +abstract class AttrRead extends AttrRef, Node { } |
| 172 | + |
| 173 | +/** A simple attribute read, e.g. `object.attr` */ |
| 174 | +private class AttributeReadAsAttrRead extends AttrRead, CfgNode { |
| 175 | + AttrNode attr_node; |
| 176 | + |
| 177 | + AttributeReadAsAttrRead() { this = TCfgNode(attr_node) } |
| 178 | + |
| 179 | + override Node getObject() { result = TCfgNode(attr_node.getObject()) } |
| 180 | + |
| 181 | + override ExprNode getAttributeNameExpr() { |
| 182 | + // Attribute names don't exist as `Node`s in the control flow graph, as they can only ever be |
| 183 | + // identifiers, and are therefore represented directly as strings. |
| 184 | + // Use `getAttributeName` to access the name of the attribute. |
| 185 | + none() |
| 186 | + } |
| 187 | + |
| 188 | + override string getAttributeName() { result = attr_node.getName() } |
| 189 | +} |
| 190 | + |
| 191 | +/** An attribute read using `getattr`: `getattr(object, attr)` */ |
| 192 | +private class GetAttrCallAsAttrRead extends AttrRead, CfgNode { |
| 193 | + GetAttrCallNode getattr_call; |
| 194 | + |
| 195 | + GetAttrCallAsAttrRead() { this.asCfgNode() = getattr_call } |
| 196 | + |
| 197 | + override Node getObject() { result = TCfgNode(getattr_call.getObject()) } |
| 198 | + |
| 199 | + override ExprNode getAttributeNameExpr() { result = TCfgNode(getattr_call.getName()) } |
| 200 | + |
| 201 | + override string getAttributeName() { |
| 202 | + exists(StrConst s, Node nodeFrom | |
| 203 | + s = nodeFrom.asExpr() and |
| 204 | + simpleLocalFlowStep*(nodeFrom, this.getAttributeNameExpr()) and |
| 205 | + result = s.getText() |
| 206 | + ) |
| 207 | + } |
| 208 | +} |
0 commit comments