-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathAlertMessage.ql
More file actions
244 lines (230 loc) · 7.29 KB
/
AlertMessage.ql
File metadata and controls
244 lines (230 loc) · 7.29 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* @name Alert message style violation
* @description An alert message that doesn't follow the style guide is harder for end users to digest.
* See the style guide here: https://github.com/github/codeql/blob/main/docs/query-metadata-style-guide.md#alert-messages
* @kind problem
* @problem.severity warning
* @id ql/alert-message-style-violation
* @precision high
*/
import ql
AstNode getASubExpression(Select sel) {
result = sel.getExpr(_)
or
result = getASubExpression(sel).getAChild()
}
/** Gets the `index`th part of the select statement. */
private AstNode getSelectPart(Select sel, int index) {
result =
rank[index](AstNode n, Location loc |
(
n = getASubExpression(sel) and loc = n.getLocation()
or
// TODO: Use dataflow instead.
// the strings are behind a predicate call.
exists(Call c, Predicate target | c = getASubExpression(sel) and loc = c.getLocation() |
c.getTarget() = target and
(
target.getBody().(ComparisonFormula).getAnOperand() = n
or
exists(ClassPredicate sub | sub.overrides(target) |
sub.getBody().(ComparisonFormula).getAnOperand() = n
)
)
)
or
// the string is a variable that is assigned in the `where` clause.
exists(VarAccess v, ComparisonFormula comp, String str |
v = getASubExpression(sel) and
loc = v.getLocation() and
comp.hasOperands(v.getDeclaration().getAnAccess(), str) and
n = str
)
)
|
n
order by
loc.getStartLine(), loc.getStartColumn(), loc.getEndLine(), loc.getEndColumn(),
loc.getFile().getRelativePath()
)
}
/**
* Gets a string element that is the last part of the message, that doesn't end with a period.
*
* For example:
* ```CodeQL
* select foo(), "This is a description" // <- bad
*
* select foo(), "This is a description." // <- good
* ```
*/
String shouldHaveFullStop(Select sel) {
result =
max(AstNode str, int i |
str.getParent*() = sel.getMessage() and str = getSelectPart(sel, i)
|
str order by i
) and
not result.getValue().matches("%.") and
not result.getValue().matches("%?")
}
/**
* Gets a string element that is the first part of the message, that starts with a lower case letter.
*
* For example:
* ```CodeQL
* select foo(), "this is a description." // <- bad
*
* select foo(), "This is a description." // <- good
* ```
*/
String shouldStartCapital(Select sel) {
result =
min(AstNode str, int i |
str.getParent*() = sel.getMessage() and str = getSelectPart(sel, i)
|
str order by i
) and
result.getValue().regexpMatch("^[a-z].*")
}
/**
* Gets a string element that is used in a message that contains "here" or "this location".
*
* For example:
* ```CodeQL
* select foo(), "XSS happens here from using a unsafe value." // <- bad
*
* select foo(), "XSS from using a unsafe value." // <- good
* ```
*/
String avoidHere(Select sel, string part) {
part = ["here", "this location"] and
(
result.getValue().regexpMatch(".*\\b" + part + "\\b.*") and
result = getSelectPart(sel, _)
)
}
/**
* Avoid using an indefinite article ("a" or "an") in a link text.
*
* For example:
* ```CodeQL
* select foo(), "XSS from $@", val, "an unsafe value." // <- bad
*
* select foo(), "XSS from a $@", val, "unsafe value." // <- good
* ```
*
* See https://www.w3.org/WAI/WCAG22/Understanding/link-purpose-in-context.html for the W3C guideline on link text. a
*/
String avoidArticleInLinkText(Select sel) {
result = sel.getExpr((any(int i | i > 1))) and
result = getSelectPart(sel, _) and
result.getValue().regexpMatch("(a|an) .*")
}
/**
* Don't quote substitutions in a message.
*
* For example:
* ```CodeQL
* select foo(), "XSS from '$@'", val, "an unsafe value." // <- bad
*
* select foo(), "XSS from $@", val, "an unsafe value." // <- good
* ```
*/
String dontQuoteSubstitutions(Select sel) {
result = getSelectPart(sel, _) and
result.getValue().matches(["%'$@'%", "%\"$@\"%"])
}
/**
* Gets the kind of the path-query represented by `sel`.
* Either "data" for a dataflow query or "taint" for a taint-tracking query.
*/
private string getQueryKind(Select sel) {
exists(TypeExpr sup |
sup = sel.getVarDecl(_).getType().(ClassType).getDeclaration().getASuperType() and
sup.getResolvedType().(ClassType).getName() = "Configuration"
|
result = "data" and
sup.getModule().getName() = "DataFlow"
or
result = "taint" and
sup.getModule().getName() = "TaintTracking"
)
}
/**
* Gets a string element from a message that uses the wrong phrase for a path query.
* A dataflow query should use "flows to" and a taint-tracking query should use "depends on".
*/
String wrongFlowsPhrase(Select sel, string kind) {
result = getSelectPart(sel, _) and
kind = getQueryKind(sel) and
(
kind = "data" and
result.getValue().matches(["% depends %", "% depend %"])
or
kind = "taint" and
result.getValue().matches(["% flows to %", "% flow to %"])
)
}
/**
* Gets a string element that contains double whitespace.
*/
String doubleWhitespace(Select sel) {
result = getSelectPart(sel, _) and
result.getValue().regexpMatch(".*\\s\\s.*")
}
import codeql.GlobalValueNumbering as GVN
/**
* Gets an expression that repeats the alert-loc as a link.
*/
AstNode getAlertLocLink(Select sel) {
exists(GVN::ValueNumber vn |
result = vn.getAnExpr() and
sel.getExpr(0) = vn.getAnExpr()
) and
exists(int msgIndex | sel.getExpr(msgIndex) = sel.getMessage() |
result = sel.getExpr(any(int i | i > msgIndex))
)
}
from AstNode node, string msg, Select sel
where
not node.getLocation().getFile().getAbsolutePath().matches("%/test/%") and
not node.getLocation().getFile().getAbsolutePath().matches("%/examples/%") and
not node.getLocation().getFile().getAbsolutePath().matches("%/experimental/%") and
not node.getLocation().getFile().getAbsolutePath().matches("%/meta/%") and
sel.getQueryDoc().getQueryKind() = ["problem", "path-problem"] and
(
node = shouldHaveFullStop(sel) and
msg = "Alert message should end with a full stop."
or
node = shouldStartCapital(sel) and
msg = "Alert message should start with a capital letter."
or
exists(string part | node = avoidHere(sel, part) |
part = "here" and
msg =
"Try to use a descriptive phrase instead of \"here\". Use \"this location\" if you can't get around mentioning the current location."
or
part = "this location" and
msg = "Try to more descriptive phrase instead of \"this location\" if possible."
)
or
node = avoidArticleInLinkText(sel) and
msg = "Avoid starting a link text with an indefinite article."
or
node = dontQuoteSubstitutions(sel) and
msg = "Don't quote substitutions in alert messages."
or
node = wrongFlowsPhrase(sel, "data") and
msg = "Use \"flows to\" instead of \"depends on\" in data flow queries."
or
node = wrongFlowsPhrase(sel, "taint") and
msg = "Use \"depends on\" instead of \"flows to\" in taint tracking queries."
or
node = doubleWhitespace(sel) and
msg = "Avoid using double whitespace in alert messages."
or
node = getAlertLocLink(sel) and
msg = "Don't repeat the alert location as a link."
)
select node, msg