Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MethodAccessSystemGetProperty predicate #4898

Merged
merged 3 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
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");
}
}