Skip to content

Commit 481d1f9

Browse files
committed
Refactor Security.CWE.CWE-297.UnsafeHostnameVerification
1 parent 7bd7ecd commit 481d1f9

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

java/ql/src/Security/CWE/CWE-297/UnsafeHostnameVerification.ql

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import semmle.code.java.dataflow.DataFlow
1616
import semmle.code.java.dataflow.FlowSources
1717
import semmle.code.java.security.Encryption
1818
import semmle.code.java.security.SecurityFlag
19-
import DataFlow::PathGraph
2019
private import semmle.code.java.dataflow.ExternalFlow
2120

2221
/**
@@ -29,7 +28,7 @@ private predicate alwaysReturnsTrue(HostnameVerifierVerify m) {
2928
}
3029

3130
/**
32-
* A class that overrides the `javax.net.ssl.HostnameVerifier.verify` method and **always** returns `true` (though it could also exit due to an uncaught exception), thus
31+
* A class that s the `javax.net.ssl.HostnameVerifier.verify` method and **always** returns `true` (though it could also exit due to an uncaught exception), thus
3332
* accepting any certificate despite a hostname mismatch.
3433
*/
3534
class TrustAllHostnameVerifier extends RefType {
@@ -45,16 +44,14 @@ class TrustAllHostnameVerifier extends RefType {
4544
/**
4645
* A configuration to model the flow of a `TrustAllHostnameVerifier` to a `set(Default)HostnameVerifier` call.
4746
*/
48-
class TrustAllHostnameVerifierConfiguration extends DataFlow::Configuration {
49-
TrustAllHostnameVerifierConfiguration() { this = "TrustAllHostnameVerifierConfiguration" }
50-
51-
override predicate isSource(DataFlow::Node source) {
47+
private module TrustAllHostnameVerifierConfiguration implements DataFlow::ConfigSig {
48+
predicate isSource(DataFlow::Node source) {
5249
source.asExpr().(ClassInstanceExpr).getConstructedType() instanceof TrustAllHostnameVerifier
5350
}
5451

55-
override predicate isSink(DataFlow::Node sink) { sink instanceof HostnameVerifierSink }
52+
predicate isSink(DataFlow::Node sink) { sink instanceof HostnameVerifierSink }
5653

57-
override predicate isBarrier(DataFlow::Node barrier) {
54+
predicate isBarrier(DataFlow::Node barrier) {
5855
// ignore nodes that are in functions that intentionally disable hostname verification
5956
barrier
6057
.getEnclosingCallable()
@@ -80,6 +77,10 @@ class TrustAllHostnameVerifierConfiguration extends DataFlow::Configuration {
8077
}
8178
}
8279

80+
module TrustAllHostnameVerifierFlow = DataFlow::Make<TrustAllHostnameVerifierConfiguration>;
81+
82+
import TrustAllHostnameVerifierFlow::PathGraph
83+
8384
/**
8485
* A sink that sets the `HostnameVerifier` on `HttpsURLConnection`.
8586
*/
@@ -114,10 +115,10 @@ private predicate isNodeGuardedByFlag(DataFlow::Node node) {
114115
}
115116

116117
from
117-
DataFlow::PathNode source, DataFlow::PathNode sink, TrustAllHostnameVerifierConfiguration cfg,
118+
TrustAllHostnameVerifierFlow::PathNode source, TrustAllHostnameVerifierFlow::PathNode sink,
118119
RefType verifier
119120
where
120-
cfg.hasFlowPath(source, sink) and
121+
TrustAllHostnameVerifierFlow::hasFlowPath(source, sink) and
121122
not isNodeGuardedByFlag(sink.getNode()) and
122123
verifier = source.getNode().asExpr().(ClassInstanceExpr).getConstructedType()
123124
select sink, source, sink,

0 commit comments

Comments
 (0)