Skip to content

Commit 9349e69

Browse files
committed
Java: Add ToStringMethod
1 parent 8adaee0 commit 9349e69

File tree

9 files changed

+24
-27
lines changed

9 files changed

+24
-27
lines changed

java/ql/src/Security/CWE/CWE-209/StackTraceExposure.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ predicate stackTraceExpr(Expr exception, MethodAccess stackTraceString) {
7979
printStackCall.getAnArgument() = printWriter and
8080
printStackCall.getQualifier() = exception and
8181
stackTraceString.getQualifier() = stringWriterVar.getAnAccess() and
82-
stackTraceString.getMethod().getName() = "toString" and
83-
stackTraceString.getMethod().getNumberOfParameters() = 0
82+
stackTraceString.getMethod() instanceof ToStringMethod
8483
)
8584
}
8685

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ class InsecureAlgoLiteral extends ShortStringLiteral {
3333
}
3434

3535
predicate objectToString(MethodAccess ma) {
36-
exists(Method m |
36+
exists(ToStringMethod m |
3737
m = ma.getMethod() and
38-
m.hasName("toString") and
3938
m.getDeclaringType() instanceof TypeObject and
4039
variableTrack(ma.getQualifier()).getType().getErasure() instanceof TypeObject
4140
)

java/ql/src/Security/CWE/CWE-798/HardcodedCredentialsApiCall.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class HardcodedCredentialApiCallConfiguration extends DataFlow::Configuration {
1919

2020
override predicate isSource(DataFlow::Node n) {
2121
n.asExpr() instanceof HardcodedExpr and
22-
not n.asExpr().getEnclosingCallable().getName() = "toString"
22+
not n.asExpr().getEnclosingCallable() instanceof ToStringMethod
2323
}
2424

2525
override predicate isSink(DataFlow::Node n) { n.asExpr() instanceof CredentialsApiSink }
2626

2727
override predicate isAdditionalFlowStep(DataFlow::Node node1, DataFlow::Node node2) {
2828
node1.asExpr().getType() instanceof TypeString and
29-
exists(MethodAccess ma | ma.getMethod().getName().regexpMatch("getBytes|toCharArray") |
29+
exists(MethodAccess ma | ma.getMethod().hasName(["getBytes", "toCharArray"]) |
3030
node2.asExpr() = ma and
3131
ma.getQualifier() = node1.asExpr()
3232
)

java/ql/src/Violations of Best Practice/Undesirable Calls/CallsToStringToString.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
import java
1212

13-
from MethodAccess ma, Method tostring
13+
from MethodAccess ma, ToStringMethod tostring
1414
where
15-
tostring.hasName("toString") and
1615
tostring.getDeclaringType() instanceof TypeString and
1716
ma.getMethod() = tostring
1817
select ma, "Redundant call to 'toString' on a String object."

java/ql/src/Violations of Best Practice/Undesirable Calls/DefaultToString.ql

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@ import java
1414
import semmle.code.java.StringFormat
1515

1616
predicate explicitToStringCall(Expr e) {
17-
exists(MethodAccess ma, Method toString | toString = ma.getMethod() |
18-
e = ma.getQualifier() and
19-
toString.getName() = "toString" and
20-
toString.getNumberOfParameters() = 0 and
21-
not toString.isStatic()
17+
exists(MethodAccess ma |
18+
ma.getMethod() instanceof ToStringMethod and
19+
e = ma.getQualifier()
2220
)
2321
}
2422

25-
predicate directlyDeclaresToString(Class c) {
26-
exists(Method m | m.getDeclaringType() = c |
27-
m.getName() = "toString" and
28-
m.getNumberOfParameters() = 0
29-
)
30-
}
23+
predicate directlyDeclaresToString(Class c) { any(ToStringMethod m).getDeclaringType() = c }
3124

3225
predicate inheritsObjectToString(Class t) {
3326
not directlyDeclaresToString(t.getSourceDeclaration()) and

java/ql/src/experimental/Security/CWE/CWE-297/InsecureLdapEndpoint.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ predicate isBooleanTrue(Expr expr) {
6868
or
6969
exists(MethodAccess ma |
7070
expr = ma and
71-
ma.getMethod().hasName("toString") and
71+
ma.getMethod() instanceof ToStringMethod and
7272
ma.getQualifier().(FieldAccess).getField().hasName("TRUE") and
7373
ma.getQualifier()
7474
.(FieldAccess)

java/ql/src/semmle/code/java/JDK.qll

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class EqualsMethod extends Method {
353353
class HashCodeMethod extends Method {
354354
HashCodeMethod() {
355355
this.hasName("hashCode") and
356-
this.getNumberOfParameters() = 0
356+
this.hasNoParameters()
357357
}
358358
}
359359

@@ -365,6 +365,14 @@ class CloneMethod extends Method {
365365
}
366366
}
367367

368+
/** A method with the same signature as `java.lang.Object.toString`. */
369+
class ToStringMethod extends Method {
370+
ToStringMethod() {
371+
this.hasName("toString") and
372+
this.hasNoParameters()
373+
}
374+
}
375+
368376
/**
369377
* The public static `main` method, with a single formal parameter
370378
* of type `String[]` and return type `void`.

java/ql/src/semmle/code/java/dispatch/ObjFlow.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private predicate source(RefType t, ObjNode n) {
194194
private predicate sink(ObjNode n) {
195195
exists(MethodAccess toString |
196196
toString.getQualifier() = n.asExpr() and
197-
toString.getMethod().hasName("toString")
197+
toString.getMethod() instanceof ToStringMethod
198198
) and
199199
n.getTypeBound().getErasure() instanceof TypeObject
200200
}

java/ql/src/semmle/code/java/security/ControlledString.qll

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import semmle.code.java.Expr
88
import semmle.code.java.security.Validation
99

1010
/**
11-
* Holds if `method` is a `toString()` method on a boxed type. These never return special characters.
11+
* Holds if `method` is a `toString()` method on a boxed type, with or without parameters.
12+
* These never return special characters.
1213
*/
1314
private predicate boxedToString(Method method) {
1415
method.getDeclaringType() instanceof BoxedType and
@@ -44,11 +45,9 @@ private predicate controlledStringProp(Expr src, Expr dest) {
4445
exists(AddExpr concatOp | concatOp = dest | src = concatOp.getAnOperand())
4546
or
4647
// `toString()` on a safe string is safe.
47-
exists(MethodAccess toStringCall, Method toString |
48+
exists(MethodAccess toStringCall |
4849
src = toStringCall.getQualifier() and
49-
toString = toStringCall.getMethod() and
50-
toString.hasName("toString") and
51-
toString.getNumberOfParameters() = 0 and
50+
toStringCall.getMethod() instanceof ToStringMethod and
5251
dest = toStringCall
5352
)
5453
}

0 commit comments

Comments
 (0)