forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicConstantsString.ql
More file actions
64 lines (60 loc) · 1.92 KB
/
MagicConstantsString.ql
File metadata and controls
64 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @name Magic strings
* @description A magic string makes code less readable and maintainable.
* @kind problem
* @problem.severity recommendation
* @precision low
* @id java/magic-string
* @tags maintainability
* readability
* statistical
* non-attributable
*/
import java
import MagicConstants
/**
* Holds if the string is a standard system property as defined in:
*
* https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()
*/
predicate isSystemProperty(string e) {
e =
[
"java.version", "java.vendor", "java.specification.version", "java.specification.vendor",
"java.specification.name", "java.class.version", "java.class.path", "java.library.path",
"java.io.tmpdir", "java.compiler", "java.ext.dirs", "os.name", "java.vendor.url", "os.arch",
"os.version", "file.separator", "path.separator", "line.separator", "user.name", "user.home",
"user.dir", "java.home", "java.vm.specification.version", "java.vm.specification.vendor",
"java.vm.specification.name", "java.vm.version", "java.vm.vendor", "java.vm.name"
]
}
predicate trivialContext(Literal e) {
// String concatenation.
e.getParent() instanceof AddExpr
or
e.getParent() instanceof AssignAddExpr
or
exists(MethodCall ma |
ma.getMethod().getName() = "append" and
(e = ma.getAnArgument() or e = ma.getQualifier())
)
or
// Standard property in a call to `System.getProperty()`.
exists(MethodCall ma |
ma.getMethod().getName() = "getProperty" and
e = ma.getAnArgument() and
ma.getMethod().getDeclaringType() instanceof TypeSystem and
isSystemProperty(e.getValue())
)
or
// Message in an exception.
exists(ClassInstanceExpr constr |
constr.getType().(RefType).getAStrictAncestorI().hasName("Exception") and
e = constr.getArgument(0)
)
}
from StringLiteral e, string msg
where
magicConstant(e, msg) and
not trivialContext(e)
select e, msg