-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Java: Add Guard Classes for checking OS & unify System Property Access #8032
Changes from all commits
cd073a2
39828fd
3cdfc00
4951344
9f5022e
fd63107
5913c9a
dad9a02
82d3cd8
3c53a05
a7adbb7
85de9f3
fea5006
103c770
31527a6
7ab193d
5243fe3
523ddb7
b282c7f
5b651f2
a21992a
2a6c4e9
ecb8911
1c98642
50ff2c2
451661d
09cc8ee
b11340c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import Member | ||
import semmle.code.java.security.ExternalProcess | ||
private import semmle.code.java.dataflow.FlowSteps | ||
|
||
// --- Standard types --- | ||
/** The class `java.lang.Object`. */ | ||
|
@@ -37,6 +38,27 @@ class StringLengthMethod extends Method { | |
StringLengthMethod() { this.hasName("length") and this.getDeclaringType() instanceof TypeString } | ||
} | ||
|
||
/** | ||
* The methods on the class `java.lang.String` that are used to perform partial matches with a specified substring or char. | ||
*/ | ||
class StringPartialMatchMethod extends Method { | ||
StringPartialMatchMethod() { | ||
this.hasName([ | ||
"contains", "startsWith", "endsWith", "matches", "indexOf", "lastIndexOf", "regionMatches" | ||
]) and | ||
this.getDeclaringType() instanceof TypeString | ||
} | ||
|
||
/** | ||
* Gets the index of the parameter that is being matched against. | ||
*/ | ||
int getMatchParameterIndex() { | ||
if this.hasName("regionMatches") | ||
then this.getParameterType(result) instanceof TypeString | ||
else result = 0 | ||
} | ||
} | ||
|
||
/** The class `java.lang.StringBuffer`. */ | ||
class TypeStringBuffer extends Class { | ||
TypeStringBuffer() { this.hasQualifiedName("java.lang", "StringBuffer") } | ||
|
@@ -228,11 +250,13 @@ class MethodSystemGetenv extends Method { | |
/** | ||
* Any method named `getProperty` on class `java.lang.System`. | ||
*/ | ||
class MethodSystemGetProperty extends Method { | ||
class MethodSystemGetProperty extends ValuePreservingMethod { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't a value-preserving method. A value-preserving method is Do you mean to say you want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
/**
* Searches for the property with the specified key in this property list.
* If the key is not found in this property list, the default property list,
* and its defaults, recursively, are then checked. The method returns the
* default value argument if the property is not found.
*
* @param key the hashtable key.
* @param defaultValue a default value.
*
* @return the value in this property list with the specified key value.
* @see #setProperty
* @see #defaults
*/
public String getProperty(String key, String defaultValue) {
String val = getProperty(key);
return (val == null) ? defaultValue : val;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doh right, I'd forgotten those overloads existed. |
||
MethodSystemGetProperty() { | ||
this.hasName("getProperty") and | ||
this.getDeclaringType() instanceof TypeSystem | ||
} | ||
|
||
override predicate returnsValue(int arg) { arg = 1 } | ||
} | ||
|
||
/** | ||
|
@@ -244,6 +268,9 @@ class MethodAccessSystemGetProperty extends MethodAccess { | |
/** | ||
* Holds if this call has a compile-time constant first argument with the value `propertyName`. | ||
* For example: `System.getProperty("user.dir")`. | ||
* | ||
* Note: Better to use `semmle.code.java.environment.SystemProperty#getSystemProperty` instead | ||
* as that predicate covers ways of accessing the same information via various libraries. | ||
*/ | ||
predicate hasCompileTimeConstantGetPropertyName(string propertyName) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smowton wondering if I should deprecate this in favor of the new way of finding all expressions that access system properties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't deprecate since we use it ourselves, but expand your comment a little to point out that the recommended predicate covers more libraries' ways of accessing the same information. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As of my changes, it's only ever now used in my code, it isn't used anywhere else in this repository. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, at your request, I've improved my comment |
||
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = propertyName | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
import java | ||
import dataflow.DefUse | ||
private import semmle.code.java.environment.SystemProperty | ||
|
||
/** | ||
* A library method that formats a number of its arguments according to a | ||
|
@@ -312,27 +313,7 @@ private predicate formatStringValue(Expr e, string fmtvalue) { | |
or | ||
formatStringValue(e.(ChooseExpr).getAResultExpr(), fmtvalue) | ||
or | ||
exists(Method getprop, MethodAccess ma, string prop | | ||
e = ma and | ||
ma.getMethod() = getprop and | ||
getprop.hasName("getProperty") and | ||
getprop.getDeclaringType().hasQualifiedName("java.lang", "System") and | ||
getprop.getNumberOfParameters() = 1 and | ||
ma.getAnArgument().(StringLiteral).getValue() = prop and | ||
(prop = "line.separator" or prop = "file.separator" or prop = "path.separator") and | ||
fmtvalue = "x" // dummy value | ||
) | ||
or | ||
exists(Field f | | ||
e = f.getAnAccess() and | ||
f.getDeclaringType() instanceof TypeFile and | ||
fmtvalue = "x" // dummy value | ||
| | ||
f.hasName("pathSeparator") or | ||
f.hasName("pathSeparatorChar") or | ||
f.hasName("separator") or | ||
f.hasName("separatorChar") | ||
) | ||
e = getSystemProperty(["line.separator", "file.separator", "path.separator"]) and fmtvalue = "x" // dummy value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simpler, and more comprehensive! 😄 |
||
) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume this is a pretty large import, I hope it's fine and won't break anything. Should I do this differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is ok -- note that in FlowSteps.qll's
Frameworks
module you should private-import this file back. This is to ensure all queries using FlowSteps see the same set of standard value-preserving methods etc, and so the related QL can be evaluated once for the whole query suite, not re-evaluated per query as it would need to be if each one defined extra flow steps.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!