Python: Post-processing query for inline test expectations

This commit is contained in:
Tom Hvitved 2024-09-23 14:20:28 +02:00
Родитель 4750b0de94
Коммит e5f2bbb6ec
7 изменённых файлов: 46 добавлений и 21 удалений

Просмотреть файл

@ -5,15 +5,5 @@
private import python as PY
private import codeql.util.test.InlineExpectationsTest
private module Impl implements InlineExpectationsTestSig {
/**
* A class representing line comments in Python. As this is the only form of comment Python
* permits, we simply reuse the `Comment` class.
*/
class ExpectationComment = PY::Comment;
class Location = PY::Location;
}
private import internal.InlineExpectationsTestImpl
import Make<Impl>

Просмотреть файл

@ -0,0 +1,21 @@
/**
* @kind test-postprocess
*/
private import python
private import codeql.util.test.InlineExpectationsTest as T
private import internal.InlineExpectationsTestImpl
import T::TestPostProcessing
import T::TestPostProcessing::Make<Impl, Input>
private module Input implements T::TestPostProcessing::InputSig<Impl> {
string getRelativeUrl(Location location) {
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
f = location.getFile()
|
result =
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
)
}
}

Просмотреть файл

@ -0,0 +1,12 @@
private import python as PY
private import codeql.util.test.InlineExpectationsTest
module Impl implements InlineExpectationsTestSig {
/**
* A class representing line comments in Python. As this is the only form of comment Python
* permits, we simply reuse the `Comment` class.
*/
class ExpectationComment = PY::Comment;
class Location = PY::Location;
}

Просмотреть файл

@ -1 +1,2 @@
Numerics/Pythagorean.ql
query: Numerics/Pythagorean.ql
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

Просмотреть файл

@ -3,12 +3,12 @@
from math import sqrt
def withPow(a, b):
return sqrt(a**2 + b**2)
return sqrt(a**2 + b**2) # $ Alert
def withMul(a, b):
return sqrt(a*a + b*b)
return sqrt(a*a + b*b) # $ Alert
def withRef(a, b):
a2 = a**2
b2 = b*b
return sqrt(a2 + b2)
return sqrt(a2 + b2) # $ Alert

Просмотреть файл

@ -1 +1,2 @@
Security/CWE-094/CodeInjection.ql
query: Security/CWE-094/CodeInjection.ql
postprocess: TestUtilities/InlineExpectationsTestQuery.ql

Просмотреть файл

@ -1,13 +1,13 @@
from flask import Flask, request
from flask import Flask, request # $ Source=flask
app = Flask(__name__)
@app.route("/code-execution")
def code_execution():
code = request.args.get("code")
exec(code) # NOT OK
eval(code) # NOT OK
exec(code) # $ Alert=flask
eval(code) # $ Alert=flask
cmd = compile(code, "<filename>", "exec")
exec(cmd) # NOT OK
exec(cmd) # $ Alert=flask
@app.route("/safe-code-execution")
@ -18,5 +18,5 @@ def code_execution():
obj_name = request.args.get("obj")
if obj_name == "foo" or obj_name == "bar":
# TODO: Should not alert on this
obj = eval(obj_name) # OK
obj = eval(obj_name) # $ SPURIOUS: Alert=flask
print(obj, obj*10)