-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathTokenBuiltFromUUID.ql
More file actions
69 lines (61 loc) · 2.2 KB
/
TokenBuiltFromUUID.ql
File metadata and controls
69 lines (61 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @name Predictable token
* @description Tokens used for sensitive tasks, such as, password recovery,
* and email confirmation, should not use predictable values.
* @kind path-problem
* @precision medium
* @problem.severity error
* @security-severity 5
* @id py/predictable-token
* @tags security
* external/cwe/cwe-340
*/
import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.ApiGraphs
import semmle.python.dataflow.new.TaintTracking
import DataFlow::PathGraph
class PredictableResultSource extends DataFlow::Node {
PredictableResultSource() {
exists(API::Node uuidCallRet |
uuidCallRet =
API::moduleImport("uuid")
.getMember(["uuid1", "uuid2", "uuid3", "uuid5"])
.getACall()
.getReturn()
|
this = uuidCallRet.asSource()
or
this = uuidCallRet.getMember(["hex", "bytes", "bytes_le"]).asSource()
)
}
}
class TokenAssignmentValueSink extends DataFlow::Node {
TokenAssignmentValueSink() {
exists(Assign a, Expr target | this = DataFlow::exprNode(a.getValue()) |
target = a.getATarget() and
(target instanceof Attribute or target instanceof Name) and
(
target.(Attribute).getName().toLowerCase().matches(["%token", "%code"])
or
target.(Name).getId().toLowerCase().matches(["%token", "%code"])
)
)
}
}
class TokenBuiltFromUUIDConfig extends TaintTracking::Configuration {
TokenBuiltFromUUIDConfig() { this = "TokenBuiltFromUUIDConfig" }
override predicate isSource(DataFlow::Node source) { source instanceof PredictableResultSource }
override predicate isSink(DataFlow::Node sink) { sink instanceof TokenAssignmentValueSink }
override predicate isAdditionalTaintStep(DataFlow::Node nodeFrom, DataFlow::Node nodeTo) {
exists(Call call, Name name |
call.getFunc() = name and
name.getId() = "str" and
nodeFrom = DataFlow::exprNode(call.getArg(0)) and
nodeTo = DataFlow::exprNode(call)
)
}
}
from DataFlow::PathNode source, DataFlow::PathNode sink, TokenBuiltFromUUIDConfig config
where config.hasFlowPath(source, sink)
select sink.getNode(), source, sink, "Token built from $@.", source.getNode(), "predictable value"