Skip to content

Commit e5f2bbb

Browse files
committed
Python: Post-processing query for inline test expectations
1 parent 4750b0d commit e5f2bbb

File tree

7 files changed

+46
-21
lines changed

7 files changed

+46
-21
lines changed

python/ql/test/TestUtilities/InlineExpectationsTest.qll

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,5 @@
55

66
private import python as PY
77
private import codeql.util.test.InlineExpectationsTest
8-
9-
private module Impl implements InlineExpectationsTestSig {
10-
/**
11-
* A class representing line comments in Python. As this is the only form of comment Python
12-
* permits, we simply reuse the `Comment` class.
13-
*/
14-
class ExpectationComment = PY::Comment;
15-
16-
class Location = PY::Location;
17-
}
18-
8+
private import internal.InlineExpectationsTestImpl
199
import Make<Impl>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @kind test-postprocess
3+
*/
4+
5+
private import python
6+
private import codeql.util.test.InlineExpectationsTest as T
7+
private import internal.InlineExpectationsTestImpl
8+
import T::TestPostProcessing
9+
import T::TestPostProcessing::Make<Impl, Input>
10+
11+
private module Input implements T::TestPostProcessing::InputSig<Impl> {
12+
string getRelativeUrl(Location location) {
13+
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
14+
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
15+
f = location.getFile()
16+
|
17+
result =
18+
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
19+
)
20+
}
21+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
private import python as PY
2+
private import codeql.util.test.InlineExpectationsTest
3+
4+
module Impl implements InlineExpectationsTestSig {
5+
/**
6+
* A class representing line comments in Python. As this is the only form of comment Python
7+
* permits, we simply reuse the `Comment` class.
8+
*/
9+
class ExpectationComment = PY::Comment;
10+
11+
class Location = PY::Location;
12+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Numerics/Pythagorean.ql
1+
query: Numerics/Pythagorean.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

python/ql/test/query-tests/Numerics/pythagorean_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
from math import sqrt
44

55
def withPow(a, b):
6-
return sqrt(a**2 + b**2)
6+
return sqrt(a**2 + b**2) # $ Alert
77

88
def withMul(a, b):
9-
return sqrt(a*a + b*b)
9+
return sqrt(a*a + b*b) # $ Alert
1010

1111
def withRef(a, b):
1212
a2 = a**2
1313
b2 = b*b
14-
return sqrt(a2 + b2)
14+
return sqrt(a2 + b2) # $ Alert
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Security/CWE-094/CodeInjection.ql
1+
query: Security/CWE-094/CodeInjection.ql
2+
postprocess: TestUtilities/InlineExpectationsTestQuery.ql
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
from flask import Flask, request
1+
from flask import Flask, request # $ Source=flask
22
app = Flask(__name__)
33

44
@app.route("/code-execution")
55
def code_execution():
66
code = request.args.get("code")
7-
exec(code) # NOT OK
8-
eval(code) # NOT OK
7+
exec(code) # $ Alert=flask
8+
eval(code) # $ Alert=flask
99
cmd = compile(code, "<filename>", "exec")
10-
exec(cmd) # NOT OK
10+
exec(cmd) # $ Alert=flask
1111

1212

1313
@app.route("/safe-code-execution")
@@ -18,5 +18,5 @@ def code_execution():
1818
obj_name = request.args.get("obj")
1919
if obj_name == "foo" or obj_name == "bar":
2020
# TODO: Should not alert on this
21-
obj = eval(obj_name) # OK
21+
obj = eval(obj_name) # $ SPURIOUS: Alert=flask
2222
print(obj, obj*10)

0 commit comments

Comments
 (0)