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

Skip to content

Commit 6c8937c

Browse files
committed
Python: Add StringConstCompare to new data-flow queries
In the future, I could imagine we would have something like this, but for now, I'm just keeping it simple. ```codeql /** * A collection of common guards that ensure the checked value cannot have arbitrary * values. * * Currently only supports comparison with constant string value, but could also * include checking whether all characters are alphanumeric, or whether a regex is * matched against the value. * * Such guards will be useful for many taint-tracking queries, but not necessarily * all, which is why you need to opt into these manually. */ class CommonNonArbitraryGuard extends BarrierGuard { CommonNonArbitraryGuard() { this instanceof StringConstCompare } override predicate checks(ControlFlowNode node, boolean branch) { this.(StringConstCompare).checks(node, branch) } } ```
1 parent 12b36b2 commit 6c8937c

6 files changed

Lines changed: 30 additions & 0 deletions

File tree

python/ql/src/semmle/python/security/dataflow/CodeInjection.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ class CodeInjectionConfiguration extends TaintTracking::Configuration {
1818
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
1919

2020
override predicate isSink(DataFlow::Node sink) { sink = any(CodeExecution e).getCode() }
21+
22+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
23+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
24+
}
2125
}

python/ql/src/semmle/python/security/dataflow/CommandInjection.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ class CommandInjectionConfiguration extends TaintTracking::Configuration {
4848
// https://github.com/python/cpython/blob/fa7ce080175f65d678a7d5756c94f82887fc9803/Lib/subprocess.py#L341
4949
not sink.getScope().getEnclosingModule().getName() in ["os", "subprocess", "platform", "popen2"]
5050
}
51+
52+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
53+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
54+
}
5155
}

python/ql/src/semmle/python/security/dataflow/PathInjection.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class PathNotNormalizedConfiguration extends TaintTracking::Configuration {
4646
}
4747

4848
override predicate isSanitizer(DataFlow::Node node) { node instanceof Path::PathNormalization }
49+
50+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
51+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
52+
}
4953
}
5054

5155
/**
@@ -68,6 +72,10 @@ class FirstNormalizationConfiguration extends TaintTracking::Configuration {
6872
override predicate isSink(DataFlow::Node sink) { sink instanceof Path::PathNormalization }
6973

7074
override predicate isSanitizerOut(DataFlow::Node node) { node instanceof Path::PathNormalization }
75+
76+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
77+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
78+
}
7179
}
7280

7381
/** Configuration to find paths from normalizations to sinks that do not go through a check. */
@@ -82,6 +90,8 @@ class NormalizedPathNotCheckedConfiguration extends TaintTracking2::Configuratio
8290

8391
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
8492
guard instanceof Path::SafeAccessCheck
93+
or
94+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
8595
}
8696
}
8797

python/ql/src/semmle/python/security/dataflow/ReflectedXSS.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ class ReflectedXssConfiguration extends TaintTracking::Configuration {
2424
sink = response.getBody()
2525
)
2626
}
27+
28+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
29+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
30+
}
2731
}

python/ql/src/semmle/python/security/dataflow/SqlInjection.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ class SQLInjectionConfiguration extends TaintTracking::Configuration {
1818
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
1919

2020
override predicate isSink(DataFlow::Node sink) { sink = any(SqlExecution e).getSql() }
21+
22+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
23+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
24+
}
2125
}

python/ql/src/semmle/python/security/dataflow/UnsafeDeserialization.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ class UnsafeDeserializationConfiguration extends TaintTracking::Configuration {
2424
sink = d.getAnInput()
2525
)
2626
}
27+
28+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
29+
guard instanceof DataFlow::BarrierGuard::StringConstCompare
30+
}
2731
}

0 commit comments

Comments
 (0)