Java: Refactor TaintedPath.ql

This commit is contained in:
Anders Schack-Mulligen
2023-03-07 11:53:03 +01:00
parent cc75a1a97e
commit d839b4038e

View File

@@ -18,32 +18,33 @@ import semmle.code.java.dataflow.FlowSources
private import semmle.code.java.dataflow.ExternalFlow private import semmle.code.java.dataflow.ExternalFlow
import semmle.code.java.security.PathCreation import semmle.code.java.security.PathCreation
import semmle.code.java.security.PathSanitizer import semmle.code.java.security.PathSanitizer
import DataFlow::PathGraph
import TaintedPathCommon import TaintedPathCommon
class TaintedPathConfig extends TaintTracking::Configuration { module TaintedPathConfig implements DataFlow::ConfigSig {
TaintedPathConfig() { this = "TaintedPathConfig" } predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource } predicate isSink(DataFlow::Node sink) {
override predicate isSink(DataFlow::Node sink) {
sink.asExpr() = any(PathCreation p).getAnInput() sink.asExpr() = any(PathCreation p).getAnInput()
or or
sinkNode(sink, ["create-file", "read-file"]) sinkNode(sink, ["create-file", "read-file"])
} }
override predicate isSanitizer(DataFlow::Node sanitizer) { predicate isBarrier(DataFlow::Node sanitizer) {
sanitizer.getType() instanceof BoxedType or sanitizer.getType() instanceof BoxedType or
sanitizer.getType() instanceof PrimitiveType or sanitizer.getType() instanceof PrimitiveType or
sanitizer.getType() instanceof NumberType or sanitizer.getType() instanceof NumberType or
sanitizer instanceof PathInjectionSanitizer sanitizer instanceof PathInjectionSanitizer
} }
override predicate isAdditionalTaintStep(DataFlow::Node n1, DataFlow::Node n2) { predicate isAdditionalFlowStep(DataFlow::Node n1, DataFlow::Node n2) {
any(TaintedPathAdditionalTaintStep s).step(n1, n2) any(TaintedPathAdditionalTaintStep s).step(n1, n2)
} }
} }
module TaintedPath = TaintTracking::Make<TaintedPathConfig>;
import TaintedPath::PathGraph
/** /**
* Gets the data-flow node at which to report a path ending at `sink`. * Gets the data-flow node at which to report a path ending at `sink`.
* *
@@ -52,13 +53,13 @@ class TaintedPathConfig extends TaintTracking::Configuration {
* continue to report there; otherwise we report directly at `sink`. * continue to report there; otherwise we report directly at `sink`.
*/ */
DataFlow::Node getReportingNode(DataFlow::Node sink) { DataFlow::Node getReportingNode(DataFlow::Node sink) {
any(TaintedPathConfig c).hasFlowTo(sink) and TaintedPath::hasFlowTo(sink) and
if exists(PathCreation pc | pc.getAnInput() = sink.asExpr()) if exists(PathCreation pc | pc.getAnInput() = sink.asExpr())
then result.asExpr() = any(PathCreation pc | pc.getAnInput() = sink.asExpr()) then result.asExpr() = any(PathCreation pc | pc.getAnInput() = sink.asExpr())
else result = sink else result = sink
} }
from DataFlow::PathNode source, DataFlow::PathNode sink, TaintedPathConfig conf from TaintedPath::PathNode source, TaintedPath::PathNode sink
where conf.hasFlowPath(source, sink) where TaintedPath::hasFlowPath(source, sink)
select getReportingNode(sink.getNode()), source, sink, "This path depends on a $@.", select getReportingNode(sink.getNode()), source, sink, "This path depends on a $@.",
source.getNode(), "user-provided value" source.getNode(), "user-provided value"