Skip to content

Commit 7272ca7

Browse files
authored
Merge pull request github#10529 from erik-krogh/even-more-alerts
QL: A few more improvements to `ql/alert-message-style-violation`
2 parents 718649d + 609ed70 commit 7272ca7

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

ql/ql/src/codeql/GlobalValueNumbering.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ private predicate classPredicateCallValueNumber(
129129

130130
private predicate literalValueNumber(Literal lit, string value, Type t) {
131131
lit.(String).getValue() = value and
132+
value.length() <= 50 and
132133
t instanceof StringClass
133134
or
134135
lit.(Integer).getValue().toString() = value and

ql/ql/src/queries/style/AlertMessage.ql

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ private AstNode getSelectPart(Select sel, int index) {
2323
(
2424
n = getASubExpression(sel) and loc = n.getLocation()
2525
or
26+
// TODO: Use dataflow instead.
2627
// the strings are behind a predicate call.
2728
exists(Call c, Predicate target | c = getASubExpression(sel) and loc = c.getLocation() |
2829
c.getTarget() = target and
@@ -102,11 +103,11 @@ String shouldStartCapital(Select sel) {
102103
* select foo(), "XSS from using a unsafe value." // <- good
103104
* ```
104105
*/
105-
String avoidHere(string part) {
106+
String avoidHere(Select sel, string part) {
106107
part = ["here", "this location"] and
107108
(
108109
result.getValue().regexpMatch(".*\\b" + part + "\\b.*") and
109-
result = getSelectPart(_, _)
110+
result = getSelectPart(sel, _)
110111
)
111112
}
112113

@@ -184,17 +185,33 @@ String doubleWhitespace(Select sel) {
184185
result.getValue().regexpMatch(".*\\s\\s.*")
185186
}
186187

187-
from AstNode node, string msg
188+
import codeql.GlobalValueNumbering as GVN
189+
190+
/**
191+
* Gets an expression that repeats the alert-loc as a link.
192+
*/
193+
AstNode getAlertLocLink(Select sel) {
194+
exists(GVN::ValueNumber vn |
195+
result = vn.getAnExpr() and
196+
sel.getExpr(0) = vn.getAnExpr()
197+
) and
198+
exists(int msgIndex | sel.getExpr(msgIndex) = sel.getMessage() |
199+
result = sel.getExpr(any(int i | i > msgIndex))
200+
)
201+
}
202+
203+
from AstNode node, string msg, Select sel
188204
where
189205
not node.getLocation().getFile().getAbsolutePath().matches("%/test/%") and
206+
sel.getQueryDoc().getQueryKind() = ["problem", "path-problem"] and
190207
(
191-
node = shouldHaveFullStop(_) and
208+
node = shouldHaveFullStop(sel) and
192209
msg = "Alert message should end with a full stop."
193210
or
194-
node = shouldStartCapital(_) and
211+
node = shouldStartCapital(sel) and
195212
msg = "Alert message should start with a capital letter."
196213
or
197-
exists(string part | node = avoidHere(part) |
214+
exists(string part | node = avoidHere(sel, part) |
198215
part = "here" and
199216
msg =
200217
"Try to use a descriptive phrase instead of \"here\". Use \"this location\" if you can't get around mentioning the current location."
@@ -203,19 +220,22 @@ where
203220
msg = "Try to more descriptive phrase instead of \"this location\" if possible."
204221
)
205222
or
206-
node = avoidArticleInLinkText(_) and
223+
node = avoidArticleInLinkText(sel) and
207224
msg = "Avoid starting a link text with an indefinite article."
208225
or
209-
node = dontQuoteSubstitutions(_) and
226+
node = dontQuoteSubstitutions(sel) and
210227
msg = "Don't quote substitutions in alert messages."
211228
or
212-
node = wrongFlowsPhrase(_, "data") and
229+
node = wrongFlowsPhrase(sel, "data") and
213230
msg = "Use \"flows to\" instead of \"depends on\" in data flow queries."
214231
or
215-
node = wrongFlowsPhrase(_, "taint") and
232+
node = wrongFlowsPhrase(sel, "taint") and
216233
msg = "Use \"depends on\" instead of \"flows to\" in taint tracking queries."
217234
or
218-
node = doubleWhitespace(_) and
235+
node = doubleWhitespace(sel) and
219236
msg = "Avoid using double whitespace in alert messages."
237+
or
238+
node = getAlertLocLink(sel) and
239+
msg = "Don't repeat the alert location as a link."
220240
)
221241
select node, msg

0 commit comments

Comments
 (0)