-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStaticArray.ql
More file actions
43 lines (40 loc) · 1.04 KB
/
StaticArray.ql
File metadata and controls
43 lines (40 loc) · 1.04 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
/**
* @name Array constant vulnerable to change
* @description Array constants are mutable and can be changed by malicious code or by accident.
* @kind problem
* @problem.severity recommendation
* @precision medium
* @id cs/static-array
* @tags reliability
* maintainability
* modularity
* external/cwe/cwe-582
*/
import csharp
predicate nonEmptyArrayLiteralOrNull(Expr e) {
e =
any(ArrayCreation arr |
exists(arr.getInitializer().getAnElement())
or
not arr.getALengthArgument().getValue() = "0"
)
or
e instanceof NullLiteral
or
e =
any(ConditionalExpr cond |
nonEmptyArrayLiteralOrNull(cond.getThen()) and
nonEmptyArrayLiteralOrNull(cond.getElse())
)
}
from Field f
where
f.isPublic() and
f.isStatic() and
f.isReadOnly() and
f.getType() instanceof ArrayType and
f.fromSource() and
forall(AssignableDefinition def | def.getTarget() = f |
nonEmptyArrayLiteralOrNull(def.getSource())
)
select f, "The array constant '" + f.getName() + "' is vulnerable to mutation."