|
| 1 | +/** This module provides an API for attribute reads and writes. */ |
| 2 | + |
| 3 | +import DataFlowUtil |
| 4 | +import DataFlowPublic |
| 5 | +private import DataFlowPrivate |
| 6 | + |
| 7 | +/** |
| 8 | + * A data flow node that reads or writes an attribute of an object. |
| 9 | + * |
| 10 | + * This abstract base class only knows about the base object on which the attribute is being |
| 11 | + * accessed, and the attribute itself, if it is statically inferrable. |
| 12 | + */ |
| 13 | +abstract class AttrRef extends Node { |
| 14 | + /** |
| 15 | + * Gets the data flow node corresponding to the object whose attribute is being read or written. |
| 16 | + */ |
| 17 | + abstract Node getObject(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Gets the expression node that defines the attribute being accessed, if any. This is |
| 21 | + * usually an identifier or literal. |
| 22 | + */ |
| 23 | + abstract ExprNode getAttributeNameExpr(); |
| 24 | + |
| 25 | + /** |
| 26 | + * Holds if this attribute reference may access an attribute named `attrName`. |
| 27 | + * Uses local data flow to track potential attribute names, which may lead to imprecision. If more |
| 28 | + * precision is needed, consider using `getAttributeName` instead. |
| 29 | + */ |
| 30 | + predicate mayHaveAttributeName(string attrName) { |
| 31 | + attrName = this.getAttributeName() |
| 32 | + or |
| 33 | + exists(Node nodeFrom | |
| 34 | + localFlow(nodeFrom, this.getAttributeNameExpr()) and |
| 35 | + attrName = nodeFrom.asExpr().(StrConst).getText() |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Gets the name of the attribute being read or written. For dynamic attribute accesses, this |
| 41 | + * method is not guaranteed to return a result. For such cases, using `mayHaveAttributeName` may yield |
| 42 | + * better results. |
| 43 | + */ |
| 44 | + abstract string getAttributeName(); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A data flow node that writes an attribute of an object. This includes |
| 49 | + * - Simple attribute writes: `object.attr = value` |
| 50 | + * - Dynamic attribute writes: `setattr(object, attr, value)` |
| 51 | + * - Fields written during class initialization: `class MyClass: attr = value` |
| 52 | + */ |
| 53 | +abstract class AttrWrite extends AttrRef { |
| 54 | + /** Gets the data flow node corresponding to the value that is written to the attribute. */ |
| 55 | + abstract Node getValue(); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Represents a control flow node for a simple attribute assignment. That is, |
| 60 | + * ```python |
| 61 | + * object.attr = value |
| 62 | + * ``` |
| 63 | + * Also gives access to the `value` being written, by extending `DefinitionNode`. |
| 64 | + */ |
| 65 | +private class AttributeAssignmentNode extends DefinitionNode, AttrNode, DataFlowCfgNode { |
| 66 | + override ControlFlowNode getValue() { result = DefinitionNode.super.getValue() } |
| 67 | +} |
| 68 | + |
| 69 | +/** A simple attribute assignment: `object.attr = value`. */ |
| 70 | +private class AttributeAssignmentAsAttrWrite extends AttrWrite, CfgNode { |
| 71 | + override AttributeAssignmentNode node; |
| 72 | + |
| 73 | + override Node getValue() { result.asCfgNode() = node.getValue() } |
| 74 | + |
| 75 | + override Node getObject() { result.asCfgNode() = node.getObject() } |
| 76 | + |
| 77 | + override ExprNode getAttributeNameExpr() { |
| 78 | + // Attribute names don't exist as `Node`s in the control flow graph, as they can only ever be |
| 79 | + // identifiers, and are therefore represented directly as strings. |
| 80 | + // Use `getAttributeName` to access the name of the attribute. |
| 81 | + none() |
| 82 | + } |
| 83 | + |
| 84 | + override string getAttributeName() { result = node.getName() } |
| 85 | +} |
| 86 | + |
| 87 | +import semmle.python.types.Builtins |
| 88 | + |
| 89 | +/** Represents `CallNode`s that may refer to calls to built-in functions or classes. */ |
| 90 | +private class BuiltInCallNode extends CallNode, DataFlowCfgNode { |
| 91 | + string name; |
| 92 | + |
| 93 | + BuiltInCallNode() { |
| 94 | + // TODO disallow instances where the name of the built-in may refer to an in-scope variable of that name. |
| 95 | + exists(NameNode id | this.getFunction() = id and id.getId() = name and id.isGlobal()) and |
| 96 | + name = any(Builtin b).getName() |
| 97 | + } |
| 98 | + |
| 99 | + /** Gets the name of the built-in function that is called at this `CallNode` */ |
| 100 | + string getBuiltinName() { result = name } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Represents a call to the built-ins that handle dynamic inspection and modification of |
| 105 | + * attributes: `getattr`, `setattr`, `hasattr`, and `delattr`. |
| 106 | + */ |
| 107 | +private class BuiltinAttrCallNode extends BuiltInCallNode { |
| 108 | + BuiltinAttrCallNode() { name in ["setattr", "getattr", "hasattr", "delattr"] } |
| 109 | + |
| 110 | + /** Gets the control flow node for object on which the attribute is accessed. */ |
| 111 | + ControlFlowNode getObject() { result in [this.getArg(0), this.getArgByName("object")] } |
| 112 | + |
| 113 | + /** |
| 114 | + * Gets the control flow node for the value that is being written to the attribute. |
| 115 | + * Only relevant for `setattr` calls. |
| 116 | + */ |
| 117 | + ControlFlowNode getValue() { |
| 118 | + // only valid for `setattr` |
| 119 | + name = "setattr" and |
| 120 | + result in [this.getArg(2), this.getArgByName("value")] |
| 121 | + } |
| 122 | + |
| 123 | + /** Gets the control flow node that defines the name of the attribute being accessed. */ |
| 124 | + ControlFlowNode getName() { result in [this.getArg(1), this.getArgByName("name")] } |
| 125 | +} |
| 126 | + |
| 127 | +/** Represents calls to the built-in `setattr`. */ |
| 128 | +private class SetAttrCallNode extends BuiltinAttrCallNode { |
| 129 | + SetAttrCallNode() { name = "setattr" } |
| 130 | +} |
| 131 | + |
| 132 | +/** Represents calls to the built-in `getattr`. */ |
| 133 | +private class GetAttrCallNode extends BuiltinAttrCallNode { |
| 134 | + GetAttrCallNode() { name = "getattr" } |
| 135 | +} |
| 136 | + |
| 137 | +/** An attribute assignment using `setattr`, e.g. `setattr(object, attr, value)` */ |
| 138 | +private class SetAttrCallAsAttrWrite extends AttrWrite, CfgNode { |
| 139 | + override SetAttrCallNode node; |
| 140 | + |
| 141 | + override Node getValue() { result.asCfgNode() = node.getValue() } |
| 142 | + |
| 143 | + override Node getObject() { result.asCfgNode() = node.getObject() } |
| 144 | + |
| 145 | + override ExprNode getAttributeNameExpr() { result.asCfgNode() = node.getName() } |
| 146 | + |
| 147 | + override string getAttributeName() { |
| 148 | + result = this.getAttributeNameExpr().asExpr().(StrConst).getText() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Represents an attribute of a class that is assigned statically during class definition. For instance |
| 154 | + * ```python |
| 155 | + * class MyClass: |
| 156 | + * attr = value |
| 157 | + * ... |
| 158 | + * ``` |
| 159 | + * Instances of this class correspond to the `NameNode` for `attr`, and also gives access to `value` by |
| 160 | + * virtue of being a `DefinitionNode`. |
| 161 | + */ |
| 162 | +private class ClassAttributeAssignmentNode extends DefinitionNode, NameNode, DataFlowCfgNode { } |
| 163 | + |
| 164 | +/** |
| 165 | + * An attribute assignment via a class field, e.g. |
| 166 | + * ```python |
| 167 | + * class MyClass: |
| 168 | + * attr = value |
| 169 | + * ``` |
| 170 | + * is treated as equivalent to `MyClass.attr = value`. |
| 171 | + */ |
| 172 | +private class ClassDefinitionAsAttrWrite extends AttrWrite, CfgNode { |
| 173 | + ClassExpr cls; |
| 174 | + override ClassAttributeAssignmentNode node; |
| 175 | + |
| 176 | + ClassDefinitionAsAttrWrite() { node.getScope() = cls.getInnerScope() } |
| 177 | + |
| 178 | + override Node getValue() { result.asCfgNode() = node.getValue() } |
| 179 | + |
| 180 | + override Node getObject() { result.asCfgNode() = cls.getAFlowNode() } |
| 181 | + |
| 182 | + override ExprNode getAttributeNameExpr() { none() } |
| 183 | + |
| 184 | + override string getAttributeName() { result = node.getId() } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * A read of an attribute on an object. This includes |
| 189 | + * - Simple attribute reads: `object.attr` |
| 190 | + * - Dynamic attribute reads using `getattr`: `getattr(object, attr)` |
| 191 | + * - Qualified imports: `from module import attr as name` |
| 192 | + */ |
| 193 | +abstract class AttrRead extends AttrRef, Node { } |
| 194 | + |
| 195 | +/** |
| 196 | + * A convenience class for embedding `AttrNode` into `DataFlowCfgNode`, as the former is not |
| 197 | + * obviously a subtype of the latter. |
| 198 | + */ |
| 199 | +private class DataFlowAttrNode extends AttrNode, DataFlowCfgNode { } |
| 200 | + |
| 201 | +/** A simple attribute read, e.g. `object.attr` */ |
| 202 | +private class AttributeReadAsAttrRead extends AttrRead, CfgNode { |
| 203 | + override DataFlowAttrNode node; |
| 204 | + |
| 205 | + override Node getObject() { result.asCfgNode() = node.getObject() } |
| 206 | + |
| 207 | + override ExprNode getAttributeNameExpr() { |
| 208 | + // Attribute names don't exist as `Node`s in the control flow graph, as they can only ever be |
| 209 | + // identifiers, and are therefore represented directly as strings. |
| 210 | + // Use `getAttributeName` to access the name of the attribute. |
| 211 | + none() |
| 212 | + } |
| 213 | + |
| 214 | + override string getAttributeName() { result = node.getName() } |
| 215 | +} |
| 216 | + |
| 217 | +/** An attribute read using `getattr`: `getattr(object, attr)` */ |
| 218 | +private class GetAttrCallAsAttrRead extends AttrRead, CfgNode { |
| 219 | + override GetAttrCallNode node; |
| 220 | + |
| 221 | + override Node getObject() { result.asCfgNode() = node.getObject() } |
| 222 | + |
| 223 | + override ExprNode getAttributeNameExpr() { result.asCfgNode() = node.getName() } |
| 224 | + |
| 225 | + override string getAttributeName() { |
| 226 | + result = this.getAttributeNameExpr().asExpr().(StrConst).getText() |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +/** |
| 231 | + * A convenience class for embedding `ImportMemberNode` into `DataFlowCfgNode`, as the former is not |
| 232 | + * obviously a subtype of the latter. |
| 233 | + */ |
| 234 | +private class DataFlowImportMemberNode extends ImportMemberNode, DataFlowCfgNode { } |
| 235 | + |
| 236 | +/** |
| 237 | + * Represents a named import as an attribute read. That is, |
| 238 | + * ```python |
| 239 | + * from module import attr as attr_ref |
| 240 | + * ``` |
| 241 | + * is treated as if it is a read of the attribute `module.attr`, even if `module` is not imported directly. |
| 242 | + */ |
| 243 | +private class ModuleAttributeImportAsAttrRead extends AttrRead, CfgNode { |
| 244 | + override DataFlowImportMemberNode node; |
| 245 | + |
| 246 | + override Node getObject() { result.asCfgNode() = node.getModule(_) } |
| 247 | + |
| 248 | + override ExprNode getAttributeNameExpr() { |
| 249 | + // The name of an imported attribute doesn't exist as a `Node` in the control flow graph, as it |
| 250 | + // can only ever be an identifier, and is therefore represented directly as a string. |
| 251 | + // Use `getAttributeName` to access the name of the attribute. |
| 252 | + none() |
| 253 | + } |
| 254 | + |
| 255 | + override string getAttributeName() { exists(node.getModule(result)) } |
| 256 | +} |
0 commit comments