Skip to content

Commit

Permalink
Merge pull request #4898 from JLLeitschuh/feat/JLL/system_get_property
Browse files Browse the repository at this point in the history
Add MethodAccessSystemGetProperty predicate
  • Loading branch information
aschackmull authored Jan 5, 2021
2 parents 7f25efd + ba4a562 commit 26a9ba4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
15 changes: 15 additions & 0 deletions java/ql/src/semmle/code/java/JDK.qll
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,21 @@ class MethodSystemGetProperty extends Method {
}
}

/**
* An access to a method named `getProperty` on class `java.lang.System`.
*/
class MethodAccessSystemGetProperty extends MethodAccess {
MethodAccessSystemGetProperty() { getMethod() instanceof MethodSystemGetProperty }

/**
* Holds if this call has a compile-time constant first argument with the value `propertyName`.
* For example: `System.getProperty("user.dir")`.
*/
predicate hasCompileTimeConstantGetPropertyName(string propertyName) {
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = propertyName
}
}

/**
* Any method named `exit` on class `java.lang.Runtime` or `java.lang.System`.
*/
Expand Down
35 changes: 35 additions & 0 deletions java/ql/test/library-tests/JDK/PrintAst.expected
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,38 @@ jdk/A.java:
# 28| 0: [ArrayTypeAccess] ...[]
# 28| 0: [TypeAccess] String
# 28| 5: [BlockStmt] stmt
jdk/SystemGetPropertyCall.java:
# 0| [CompilationUnit] SystemGetPropertyCall
# 3| 1: [Class] SystemGetPropertyCall
# 4| 3: [FieldDeclaration] String USER_DIR_PROPERTY, ...;
# 4| -1: [TypeAccess] String
# 4| 0: [StringLiteral] "user.dir"
# 6| 4: [Method] a
# 6| 3: [TypeAccess] void
# 6| 5: [BlockStmt] stmt
# 7| 0: [ExprStmt] stmt
# 7| 0: [MethodAccess] getProperty(...)
# 7| -1: [TypeAccess] System
# 7| 0: [StringLiteral] "user.dir"
# 10| 5: [Method] b
# 10| 3: [TypeAccess] void
# 10| 5: [BlockStmt] stmt
# 11| 0: [ExprStmt] stmt
# 11| 0: [MethodAccess] getProperty(...)
# 11| -1: [TypeAccess] System
# 11| 0: [StringLiteral] "user.dir"
# 11| 1: [StringLiteral] "HOME"
# 14| 6: [Method] c
# 14| 3: [TypeAccess] void
# 14| 5: [BlockStmt] stmt
# 15| 0: [ExprStmt] stmt
# 15| 0: [MethodAccess] getProperty(...)
# 15| -1: [TypeAccess] System
# 15| 0: [VarAccess] USER_DIR_PROPERTY
# 18| 7: [Method] d
# 18| 3: [TypeAccess] void
# 18| 5: [BlockStmt] stmt
# 19| 0: [ExprStmt] stmt
# 19| 0: [MethodAccess] getProperty(...)
# 19| -1: [TypeAccess] System
# 19| 0: [StringLiteral] "random.property"
3 changes: 3 additions & 0 deletions java/ql/test/library-tests/JDK/SystemGetPropertyCall.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| jdk/SystemGetPropertyCall.java:7:9:7:38 | getProperty(...) |
| jdk/SystemGetPropertyCall.java:11:9:11:46 | getProperty(...) |
| jdk/SystemGetPropertyCall.java:15:9:15:45 | getProperty(...) |
5 changes: 5 additions & 0 deletions java/ql/test/library-tests/JDK/SystemGetPropertyCall.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import java

from MethodAccessSystemGetProperty ma
where ma.hasCompileTimeConstantGetPropertyName("user.dir")
select ma
21 changes: 21 additions & 0 deletions java/ql/test/library-tests/JDK/jdk/SystemGetPropertyCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package jdk;

public class SystemGetPropertyCall {
private static final String USER_DIR_PROPERTY = "user.dir";

void a() {
System.getProperty("user.dir");
}

void b() {
System.getProperty("user.dir", "HOME");
}

void c() {
System.getProperty(USER_DIR_PROPERTY);
}

void d() {
System.getProperty("random.property");
}
}

0 comments on commit 26a9ba4

Please sign in to comment.