Skip to content

Commit 02581a3

Browse files
committed
Move class for getProperty method call to Properties.qll
1 parent 73cb01f commit 02581a3

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

java/ql/lib/semmle/code/java/frameworks/Properties.qll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import semmle.code.java.Type
44
private import semmle.code.java.dataflow.FlowSteps
5+
private import semmle.code.configfiles.ConfigFiles
6+
private import semmle.code.java.dataflow.RangeUtils
57

68
/**
79
* The `java.util.Properties` class.
@@ -43,3 +45,19 @@ class PropertiesStoreMethod extends Method {
4345
(this.getName().matches("store%") or this.getName() = "save")
4446
}
4547
}
48+
49+
/**
50+
* A call to the `getProperty` method of the class `java.util.Properties`.
51+
*/
52+
class PropertiesGetPropertyMethodCall extends MethodCall {
53+
PropertiesGetPropertyMethodCall() { this.getMethod() instanceof PropertiesGetPropertyMethod }
54+
55+
private ConfigPair getPair() {
56+
this.getArgument(0).(ConstantStringExpr).getStringValue() = result.getNameElement().getName()
57+
}
58+
59+
string getPropertyValue() {
60+
result = this.getPair().getValueElement().getValue() or
61+
result = this.getArgument(1).(ConstantStringExpr).getStringValue()
62+
}
63+
}

java/ql/lib/semmle/code/java/security/MaybeBrokenCryptoAlgorithmQuery.qll

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,39 +37,14 @@ private predicate objectToString(MethodCall ma) {
3737
)
3838
}
3939

40-
private class GetPropertyMethodCall extends MethodCall {
41-
GetPropertyMethodCall() { this.getMethod() instanceof PropertiesGetPropertyMethod }
42-
43-
private ConfigPair getPair() {
44-
this.getArgument(0).(ConstantStringExpr).getStringValue() = result.getNameElement().getName()
45-
}
46-
47-
string getPropertyValue() {
48-
result = this.getPair().getValueElement().getValue() or
49-
result = this.getArgument(1).(ConstantStringExpr).getStringValue()
50-
}
51-
}
52-
53-
/**
54-
* Get the string value represented by the given expression.
55-
*
56-
* If the value is a string literal, return the literal value.
57-
* If the value is a call to `java.util.Properties::getProperty`, return the potential values of the property.
58-
*/
59-
string insecureAlgorithmName(DataFlow::Node algo) {
60-
result = algo.asExpr().(StringLiteral).getValue()
61-
or
62-
result = algo.asExpr().(GetPropertyMethodCall).getPropertyValue()
63-
}
64-
6540
/**
6641
* A taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms.
6742
*/
6843
module InsecureCryptoConfig implements DataFlow::ConfigSig {
6944
predicate isSource(DataFlow::Node n) {
7045
n.asExpr() instanceof InsecureAlgoLiteral
7146
or
72-
exists(GetPropertyMethodCall mc | n.asExpr() = mc |
47+
exists(PropertiesGetPropertyMethodCall mc | n.asExpr() = mc |
7348
// Since properties pairs are not included in the java/weak-crypto-algorithm,
7449
// The check for values from properties files can be less strict than `InsecureAlgoLiteral`.
7550
not mc.getPropertyValue().regexpMatch(getSecureAlgorithmRegex())

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,27 @@
1313

1414
import java
1515
import semmle.code.java.security.Encryption
16+
import semmle.code.java.dataflow.DataFlow
17+
import semmle.code.java.frameworks.Properties
1618
import semmle.code.java.security.MaybeBrokenCryptoAlgorithmQuery
1719
import InsecureCryptoFlow::PathGraph
1820

21+
/**
22+
* Get the string value represented by the given expression.
23+
*
24+
* If the value is a string literal, return the literal value.
25+
* If the value is a call to `java.util.Properties::getProperty`, return the potential values of the property.
26+
*/
27+
string getStringValue(DataFlow::Node algo) {
28+
result = algo.asExpr().(StringLiteral).getValue()
29+
or
30+
result = algo.asExpr().(PropertiesGetPropertyMethodCall).getPropertyValue()
31+
}
32+
1933
from InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c
2034
where
2135
sink.getNode().asExpr() = c.getAlgoSpec() and
2236
InsecureCryptoFlow::flowPath(source, sink)
2337
select c, source, sink,
2438
"Cryptographic algorithm $@ may not be secure, consider using a different algorithm.", source,
25-
insecureAlgorithmName(source.getNode())
39+
getStringValue(source.getNode())

0 commit comments

Comments
 (0)