Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4570c29

Browse files
committed
Python: port query
1 parent eb5ed23 commit 4570c29

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @name Uncontrolled data used in path expression
3+
* @description Accessing paths influenced by users can allow an attacker to access unexpected resources.
4+
* @kind path-problem
5+
* @problem.severity error
6+
* @sub-severity high
7+
* @precision high
8+
* @id py/path-injection
9+
* @tags correctness
10+
* security
11+
* external/owasp/owasp-a1
12+
* external/cwe/cwe-022
13+
* external/cwe/cwe-023
14+
* external/cwe/cwe-036
15+
* external/cwe/cwe-073
16+
* external/cwe/cwe-099
17+
*/
18+
19+
import python
20+
import experimental.dataflow.DataFlow
21+
import experimental.dataflow.DataFlow2
22+
import experimental.dataflow.TaintTracking
23+
import experimental.dataflow.TaintTracking2
24+
import experimental.semmle.python.Concepts
25+
import experimental.dataflow.RemoteFlowSources
26+
import DataFlow::PathGraph
27+
28+
/** Configuration to find paths from sources to sinks that contain no checks. */
29+
class UncheckedPathConfiguration extends TaintTracking::Configuration {
30+
UncheckedPathConfiguration() { this = "UncheckedPathConfiguration" }
31+
32+
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
33+
34+
override predicate isSink(DataFlow::Node sink) {
35+
sink = any(FileSystemAccess e).getAPathArgument()
36+
}
37+
38+
override predicate isSanitizer(DataFlow::Node node) { node instanceof PathCheck }
39+
}
40+
41+
/** Configuration to find paths from sources to checks that contain no normalization. */
42+
class CheckUnnormalizedConfiguration extends TaintTracking2::Configuration {
43+
CheckUnnormalizedConfiguration() { this = "CheckUnnormalizedConfiguration" }
44+
45+
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
46+
47+
override predicate isSink(DataFlow::Node sink) { sink instanceof PathCheck }
48+
49+
override predicate isSanitizer(DataFlow::Node node) { node instanceof PathNormalization }
50+
}
51+
52+
class CheckUnnormalized extends DataFlow2::PathNode {
53+
DataFlow::Node sourceNode;
54+
55+
CheckUnnormalized() {
56+
exists(CheckUnnormalizedConfiguration conf, DataFlow2::PathNode source |
57+
sourceNode = source.getNode() and
58+
conf.hasFlowPath(source, this)
59+
)
60+
}
61+
62+
DataFlow::Node getSourceNode() { result = sourceNode }
63+
}
64+
65+
/** Configuration to find paths from checks to sinks that contain no further checks. */
66+
class LastCheckConfiguration extends TaintTracking::Configuration {
67+
LastCheckConfiguration() { this = "UncheckedPathConfiguration" }
68+
69+
override predicate isSource(DataFlow::Node source) {
70+
source = any(CheckUnnormalized cu).getNode()
71+
}
72+
73+
override predicate isSink(DataFlow::Node sink) {
74+
sink = any(FileSystemAccess e).getAPathArgument()
75+
}
76+
77+
override predicate isSanitizer(DataFlow::Node node) { node instanceof PathCheck }
78+
}
79+
80+
from TaintTracking::Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
81+
where
82+
// Path has no check on it.
83+
config instanceof UncheckedPathConfiguration and
84+
config.hasFlowPath(source, sink)
85+
or
86+
// Path has a check on it, but no prior normalization.
87+
config instanceof LastCheckConfiguration and
88+
exists(DataFlow::PathNode c, CheckUnnormalized cu | cu.getNode() = c.getNode() |
89+
config.hasFlowPath(c, sink) and
90+
source.getNode() = cu.getSourceNode()
91+
)
92+
select sink.getNode(), source, sink, "This path depends on $@.", source.getNode(),
93+
"a user-provided value"

0 commit comments

Comments
 (0)