-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathMagicConstantsString.ql
More file actions
83 lines (79 loc) · 2.08 KB
/
MagicConstantsString.ql
File metadata and controls
83 lines (79 loc) · 2.08 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @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:
*
* http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperties()
*/
predicate isSystemProperty(string e) {
e = "java.version" or
e = "java.vendor" or
e = "java.vendor.url" or
e = "java.home" or
e = "java.vm.specification.version" or
e = "java.vm.specification.vendor" or
e = "java.vm.specification.name" or
e = "java.vm.version" or
e = "java.vm.vendor" or
e = "java.vm.name" or
e = "java.specification.version" or
e = "java.specification.vendor" or
e = "java.specification.name" or
e = "java.class.version" or
e = "java.class.path" or
e = "java.library.path" or
e = "java.io.tmpdir" or
e = "java.compiler" or
e = "java.ext.dirs" or
e = "os.name" or
e = "os.arch" or
e = "os.version" or
e = "file.separator" or
e = "path.separator" or
e = "line.separator" or
e = "user.name" or
e = "user.home" or
e = "user.dir"
}
predicate trivialContext(Literal e) {
// String concatenation.
e.getParent() instanceof AddExpr
or
e.getParent() instanceof AssignAddExpr
or
exists(MethodAccess ma |
ma.getMethod().getName() = "append" and
(e = ma.getAnArgument() or e = ma.getQualifier())
)
or
// Standard property in a call to `System.getProperty()`.
exists(MethodAccess 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).getASupertype+().hasName("Exception") and
e = constr.getArgument(0)
)
}
from StringLiteral e, string msg
where
magicConstant(e, msg) and
not trivialContext(e)
select e, msg