Skip to content

Commit 2bec69e

Browse files
committed
Ql4Ql: Re-factor the ql/mising-security-metadata query.
1 parent 23b9db8 commit 2bec69e

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

ql/ql/src/codeql_ql/ast/Ast.qll

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,43 @@ class QueryDoc extends QLDoc {
202202

203203
override string getAPrimaryQlClass() { result = "QueryDoc" }
204204

205-
/** Gets the @kind for the query */
205+
/** Gets the @kind for the query. */
206206
string getQueryKind() {
207207
result = this.getContents().regexpCapture("(?s).*@kind ([\\w-]+)\\s.*", 1)
208208
}
209209

210-
/** Gets the @name for the query */
210+
/** Gets the @name for the query. */
211211
string getQueryName() {
212212
result = this.getContents().regexpCapture("(?s).*@name (.+?)(?=\\n).*", 1)
213213
}
214214

215-
/** Gets the id part (without language) of the @id */
215+
/** Gets the id part (without language) of the @id. */
216216
string getQueryId() {
217217
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-/]+)\\s.*", 2)
218218
}
219219

220-
/** Gets the language of the @id */
220+
/** Gets the language of the @id. */
221221
string getQueryLanguage() {
222222
result = this.getContents().regexpCapture("(?s).*@id (\\w+)/([\\w\\-/]+)\\s.*", 1)
223223
}
224+
225+
/** Gets the @precision for the query. */
226+
string getQueryPrecision() {
227+
result = this.getContents().regexpCapture("(?s).*@precision ([\\w\\-]+)\\s.*", 1)
228+
}
229+
230+
/** Gets the @security-severity for the query. */
231+
string getQuerySecuritySeverity() {
232+
result = this.getContents().regexpCapture("(?s).*@security\\-severity ([\\d\\.]+)\\s.*", 1)
233+
}
234+
235+
/** Gets the individual @tags for the query. */
236+
string getQueryTags() {
237+
exists(string tags | tags = this.getContents().regexpCapture("(?s).*@tags ([^@]+)", 1) |
238+
result = tags.splitAt("*").trim() and
239+
result.regexpMatch("[\\w\\s\\-]+")
240+
)
241+
}
224242
}
225243

226244
class BlockComment extends TBlockComment, Comment {

ql/ql/src/queries/style/MissingSecurityMetadata.ql

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,30 @@
1010

1111
import ql
1212

13-
predicate missingSecuritySeverity(QLDoc doc) {
14-
exists(string s | s = doc.getContents() |
15-
exists(string securityTag | securityTag = s.splitAt("@") |
16-
securityTag.matches("tags%security%")
17-
) and
18-
exists(string precisionTag | precisionTag = s.splitAt("@") |
19-
precisionTag.matches("precision %")
20-
) and
21-
not exists(string securitySeverity | securitySeverity = s.splitAt("@") |
22-
securitySeverity.matches("security-severity %")
23-
)
24-
)
13+
private predicate unInterestingLocation(File f) {
14+
f.getRelativePath().matches("%/" + ["experimental", "examples", "test"] + "/%")
2515
}
2616

27-
predicate missingSecurityTag(QLDoc doc) {
28-
exists(string s | s = doc.getContents() |
29-
exists(string securitySeverity | securitySeverity = s.splitAt("@") |
30-
securitySeverity.matches("security-severity %")
31-
) and
32-
exists(string precisionTag | precisionTag = s.splitAt("@") |
33-
precisionTag.matches("precision %")
34-
) and
35-
not exists(string securityTag | securityTag = s.splitAt("@") |
36-
securityTag.matches("tags%security%")
37-
)
38-
)
17+
predicate missingSecuritySeverity(QueryDoc doc) {
18+
doc.getQueryTags() = "security" and
19+
exists(doc.getQueryPrecision()) and
20+
not exists(doc.getQuerySecuritySeverity())
21+
}
22+
23+
predicate missingSecurityTag(QueryDoc doc) {
24+
exists(doc.getQuerySecuritySeverity()) and
25+
exists(doc.getQueryPrecision()) and
26+
not doc.getQueryTags() = "security"
3927
}
4028

41-
from TopLevel t, string msg
29+
from TopLevel t, QueryDoc doc, string msg
4230
where
43-
t.getLocation().getFile().getBaseName().matches("%.ql") and
44-
not t.getLocation()
45-
.getFile()
46-
.getRelativePath()
47-
.matches("%/" + ["experimental", "examples", "test"] + "/%") and
31+
doc = t.getQLDoc() and
32+
not unInterestingLocation(t.getLocation().getFile()) and
4833
(
49-
missingSecuritySeverity(t.getQLDoc()) and
34+
missingSecuritySeverity(doc) and
5035
msg = "This query file is missing a `@security-severity` tag."
5136
or
52-
missingSecurityTag(t.getQLDoc()) and msg = "This query file is missing a `@tag security`."
37+
missingSecurityTag(doc) and msg = "This query file is missing a `@tags security`."
5338
)
5439
select t, msg
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| testcases/BadNoSecurity.ql:1:1:16:9 | TopLevel | This query file is missing a `@tag security`. |
1+
| testcases/BadNoSecurity.ql:1:1:16:9 | TopLevel | This query file is missing a `@tags security`. |
22
| testcases/BadNoSeverity.ql:1:1:16:9 | TopLevel | This query file is missing a `@security-severity` tag. |

0 commit comments

Comments
 (0)