Skip to content

Commit 63b5b1f

Browse files
committed
python: Inline expectation should have space after $
This was a regex-find-replace from `# \$(?! )` (using a negative lookahead) to `# $ `.
1 parent c1c1cae commit 63b5b1f

File tree

61 files changed

+787
-788
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+787
-788
lines changed

python/ql/test/experimental/query-tests/Security/CWE-022-UnsafeUnpacking/UnsafeUnpack.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22
import shutil
3-
import os
3+
import os
44

55
from flask import Flask, request
66
app = Flask(__name__)
@@ -16,8 +16,8 @@ def download_from_url():
1616
with open(tarpath, "wb") as f:
1717
f.write(response.raw.read())
1818
untarredpath = "/tmp/tmp123"
19-
shutil.unpack_archive(tarpath, untarredpath) # $result=BAD
20-
19+
shutil.unpack_archive(tarpath, untarredpath) # $ result=BAD
20+
2121

2222
# A source catching an S3 filename download
2323
# see boto3: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Client.download_file
@@ -31,7 +31,7 @@ def download_from_url():
3131

3232
s3 = boto3.client('s3')
3333
s3.download_file(bucket_name, remote_ziped_name, local_ziped_path)
34-
shutil.unpack_archive(local_ziped_path, base_dir) # $result=BAD
34+
shutil.unpack_archive(local_ziped_path, base_dir) # $ result=BAD
3535

3636

3737
# wget
@@ -45,11 +45,11 @@ def download_from_url():
4545

4646
# download(url, out, bar) contains out parameter
4747
wget.download(url, compressed_file)
48-
shutil.unpack_archive(compressed_file, base_dir) # $result=BAD
48+
shutil.unpack_archive(compressed_file, base_dir) # $ result=BAD
4949

5050
# download(url) returns filename
5151
compressed_file = wget.download(url)
52-
shutil.unpack_archive(compressed_file, base_dir) # $result=BAD
52+
shutil.unpack_archive(compressed_file, base_dir) # $ result=BAD
5353

5454

5555
# A source coming from a CLI argparse module
@@ -63,7 +63,7 @@ def download_from_url():
6363

6464
args = parser.parse_args()
6565
compressed_file = args.filename
66-
shutil.unpack_archive(compressed_file, base_dir) # $result=BAD
66+
shutil.unpack_archive(compressed_file, base_dir) # $ result=BAD
6767

6868

6969
# A source coming from a CLI and downloaded
@@ -83,8 +83,8 @@ def download_from_url():
8383
tarpath = "/tmp/tmp456/tarball.tar.gz"
8484
with open(tarpath, "wb") as f:
8585
f.write(response.raw.read())
86-
87-
shutil.unpack_archive(tarpath, base_dir) # $result=BAD
86+
87+
shutil.unpack_archive(tarpath, base_dir) # $ result=BAD
8888

8989
# the django upload functionality
9090
# see HttpRequest.FILES: https://docs.djangoproject.com/en/4.1/ref/request-response/#django.http.HttpRequest.FILES
@@ -97,36 +97,36 @@ def simple_upload(request):
9797
base_dir = "/tmp/baase_dir"
9898
if request.method == 'POST':
9999
# Read uploaded files by chunks of data
100-
# see chunks(): https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks
100+
# see chunks(): https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks
101101
savepath = os.path.join(base_dir, "tarball_compressed.tar.gz")
102102
with open(savepath, 'wb+') as wfile:
103103
for chunk in request.FILES["ufile1"].chunks():
104104
wfile.write(chunk)
105-
shutil.unpack_archive(savepath, base_dir) # $result=BAD
105+
shutil.unpack_archive(savepath, base_dir) # $ result=BAD
106106

107107
# Write in binary the uploaded tarball
108108
myfile = request.FILES.get("ufile1")
109109
file_path = os.path.join(base_dir, "tarball.tar")
110110
with file_path.open('wb') as f:
111111
f.write(myfile.read())
112-
shutil.unpack_archive(file_path, base_dir) # $result=BAD
112+
shutil.unpack_archive(file_path, base_dir) # $ result=BAD
113113

114114
# Save uploaded files using FileSystemStorage Django API
115115
# see FileSystemStorage: https://docs.djangoproject.com/en/4.1/ref/files/storage/#django.core.files.storage.FileSystemStorage
116116
for ufile in request.FILES.getlist():
117117
fs = FileSystemStorage()
118118
filename = fs.save(ufile.name, ufile)
119119
uploaded_file_path = fs.path(filename)
120-
shutil.unpack_archive(uploaded_file_path, base_dir) # $result=BAD
121-
120+
shutil.unpack_archive(uploaded_file_path, base_dir) # $ result=BAD
121+
122122
return render(request, 'simple_upload.html')
123123

124124
elif request.method == 'GET':
125125
return render(request, 'simple_upload.html')
126126

127127

128128
import shutil
129-
import os
129+
import os
130130
import tarfile
131131
import tempfile
132132
import argparse
@@ -139,8 +139,8 @@ def simple_upload(request):
139139
args = parser.parse_args()
140140
unsafe_filename_tar = args.filename
141141
with tarfile.TarFile(unsafe_filename_tar, mode="r") as tar:
142-
tar.extractall(path="/tmp/unpack/", members=tar) # $result=BAD
143-
tar = tarfile.open(unsafe_filename_tar)
142+
tar.extractall(path="/tmp/unpack/", members=tar) # $ result=BAD
143+
tar = tarfile.open(unsafe_filename_tar)
144144

145145

146146
from django.shortcuts import render
@@ -152,19 +152,19 @@ def simple_upload(request):
152152
base_dir = "/tmp/baase_dir"
153153
if request.method == 'POST':
154154
# Read uploaded files by chunks of data
155-
# see chunks(): https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks
155+
# see chunks(): https://docs.djangoproject.com/en/4.1/ref/files/uploads/#django.core.files.uploadedfile.UploadedFile.chunks
156156
savepath = os.path.join(base_dir, "tarball_compressed.tar.gz")
157157
with open(savepath, 'wb+') as wfile:
158158
for chunk in request.FILES["ufile1"].chunks():
159159
wfile.write(chunk)
160160

161161
tar = tarfile.open(savepath)
162162
result = []
163-
for member in tar:
164-
if member.issym():
165-
raise ValueError("But it is a symlink")
166-
result.append(member)
167-
tar.extractall(path=tempfile.mkdtemp(), members=result) # $result=BAD
163+
for member in tar:
164+
if member.issym():
165+
raise ValueError("But it is a symlink")
166+
result.append(member)
167+
tar.extractall(path=tempfile.mkdtemp(), members=result) # $ result=BAD
168168
tar.close()
169169

170170

@@ -173,7 +173,7 @@ def simple_upload(request):
173173
with open(tarpath, "wb") as f:
174174
f.write(response.raw.read())
175175
target_dir = "/tmp/unpack"
176-
tarfile.TarFile(tarpath, mode="r").extractall(path=target_dir) # $result=BAD
176+
tarfile.TarFile(tarpath, mode="r").extractall(path=target_dir) # $ result=BAD
177177

178178

179179
from pathlib import Path
@@ -183,7 +183,7 @@ def simple_upload(request):
183183
def default_session() -> boto3.Session:
184184
_SESSION = None
185185
if _SESSION is None:
186-
_SESSION = boto3.Session()
186+
_SESSION = boto3.Session()
187187
return _SESSION
188188

189189
cache = False
@@ -198,4 +198,4 @@ def default_session() -> boto3.Session:
198198
target = cache_dir
199199
else:
200200
target = Path(tempfile.mkdtemp())
201-
shutil.unpack_archive(tmp.name, target) # $result=BAD
201+
shutil.unpack_archive(tmp.name, target) # $ result=BAD

python/ql/test/experimental/query-tests/Security/CWE-1427-PromptInjection/agent_instructions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def get_input1():
77
input = request.args.get("input")
88

9-
agent = Agent(name="Assistant", instructions="This prompt is customized for " + input) # $Alert[py/prompt-injection]
9+
agent = Agent(name="Assistant", instructions="This prompt is customized for " + input) # $ Alert[py/prompt-injection]
1010

1111
result = Runner.run_sync(agent, "This is a user message.")
1212
print(result.final_output)
@@ -22,17 +22,17 @@ def get_input2():
2222
input=[
2323
{
2424
"role": "user",
25-
"content": input, # $Alert[py/prompt-injection]
25+
"content": input, # $ Alert[py/prompt-injection]
2626
}
27-
]
27+
]
2828
)
2929

3030
result2 = Runner.run_sync(
3131
agent,
3232
[
3333
{
3434
"role": "user",
35-
"content": input, # $Alert[py/prompt-injection]
35+
"content": input, # $ Alert[py/prompt-injection]
3636
}
37-
]
37+
]
3838
)

python/ql/test/experimental/query-tests/Security/CWE-176/samples.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@app.route("/unsafe1")
88
def unsafe1():
99
user_input = escape(request.args.get("ui"))
10-
normalized_user_input = unicodedata.normalize("NFKC", user_input) # $result=BAD
10+
normalized_user_input = unicodedata.normalize("NFKC", user_input) # $ result=BAD
1111
return render_template("result.html", normalized_user_input=normalized_user_input)
1212

1313

@@ -17,14 +17,14 @@ def unsafe1bis():
1717
if user_input.isascii():
1818
normalized_user_input = user_input
1919
else:
20-
normalized_user_input = unicodedata.normalize("NFC", user_input) # $result=BAD
20+
normalized_user_input = unicodedata.normalize("NFC", user_input) # $ result=BAD
2121
return render_template("result.html", normalized_user_input=normalized_user_input)
2222

2323

2424
@app.route("/safe1")
2525
def safe1():
2626
normalized_user_input = unicodedata.normalize(
2727
"NFKC", request.args.get("ui")
28-
) # $result=OK
28+
) # $ result=OK
2929
user_input = escape(normalized_user_input)
3030
return render_template("result.html", normalized_user_input=user_input)

python/ql/test/library-tests/dataflow/global-flow/test.py

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
# Simple assignment
44

5-
g = [5] # $writes=g
5+
g = [5] # $ writes=g
66

77
# Multiple assignment
88

9-
g1, g2 = [6], [7] # $writes=g1 writes=g2
9+
g1, g2 = [6], [7] # $ writes=g1 writes=g2
1010

1111
# Assignment that's only referenced in this scope.
1212

13-
unreferenced_g = [8] # $writes=unreferenced_g
13+
unreferenced_g = [8] # $ writes=unreferenced_g
1414
print(unreferenced_g)
1515

1616
# Testing modifications of globals
@@ -24,98 +24,98 @@
2424
# but currently our analysis thinks `g_mod` might be used in the `print` call
2525
g_mod = [10] # $ SPURIOUS: writes=g_mod
2626
print("foo")
27-
g_mod = [100] # $writes=g_mod
27+
g_mod = [100] # $ writes=g_mod
2828

2929
# Modification by mutation
3030

31-
g_ins = [50] # $writes=g_ins
31+
g_ins = [50] # $ writes=g_ins
3232
print(g_ins)
3333
g_ins.append(75)
3434

3535
# A global with multiple potential definitions
3636

37-
import unknown_module # $writes=unknown_module
37+
import unknown_module # $ writes=unknown_module
3838
if unknown_module.attr:
39-
g_mult = [200] # $writes=g_mult
39+
g_mult = [200] # $ writes=g_mult
4040
else:
41-
g_mult = [300] # $writes=g_mult
41+
g_mult = [300] # $ writes=g_mult
4242

4343
# A global variable that may be redefined depending on some unknown value
4444

45-
g_redef = [400] # $writes=g_redef
45+
g_redef = [400] # $ writes=g_redef
4646
if unknown_module.attr:
47-
g_redef = [500] # $writes=g_redef
47+
g_redef = [500] # $ writes=g_redef
4848

49-
def global_access(): # $writes=global_access
49+
def global_access(): # $ writes=global_access
5050
l = 5
51-
print(g) # $reads=g
52-
print(g1) # $reads=g1
53-
print(g2) # $reads=g2
54-
print(g_mod) # $reads=g_mod
55-
print(g_ins) # $reads=g_ins
56-
print(g_mult) # $reads=g_mult
57-
print(g_redef) # $reads=g_redef
58-
59-
def print_g_mod(): # $writes=print_g_mod
60-
print(g_mod) # $reads=g_mod
61-
62-
def global_mod(): # $writes=global_mod
51+
print(g) # $ reads=g
52+
print(g1) # $ reads=g1
53+
print(g2) # $ reads=g2
54+
print(g_mod) # $ reads=g_mod
55+
print(g_ins) # $ reads=g_ins
56+
print(g_mult) # $ reads=g_mult
57+
print(g_redef) # $ reads=g_redef
58+
59+
def print_g_mod(): # $ writes=print_g_mod
60+
print(g_mod) # $ reads=g_mod
61+
62+
def global_mod(): # $ writes=global_mod
6363
global g_mod
64-
g_mod += [150] # $reads,writes=g_mod
65-
print_g_mod() # $reads=print_g_mod
64+
g_mod += [150] # $ reads,writes=g_mod
65+
print_g_mod() # $ reads=print_g_mod
6666

67-
def global_inside_local_function(): # $writes=global_inside_local_function
67+
def global_inside_local_function(): # $ writes=global_inside_local_function
6868
def local_function():
69-
print(g) # $reads=g
69+
print(g) # $ reads=g
7070
local_function()
7171

7272
## Imports
7373

7474

7575
# Direct imports
7676

77-
import foo_module # $writes=foo_module
77+
import foo_module # $ writes=foo_module
7878

79-
def use_foo(): # $writes=use_foo
80-
print(foo_module.attr) # $reads=foo_module
79+
def use_foo(): # $ writes=use_foo
80+
print(foo_module.attr) # $ reads=foo_module
8181

8282
# Partial imports
8383

84-
from bar import baz_attr, quux_attr # $writes=baz_attr writes=quux_attr
84+
from bar import baz_attr, quux_attr # $ writes=baz_attr writes=quux_attr
8585

86-
def use_partial_import(): # $writes=use_partial_import
87-
print(baz_attr, quux_attr) # $reads=baz_attr reads=quux_attr
86+
def use_partial_import(): # $ writes=use_partial_import
87+
print(baz_attr, quux_attr) # $ reads=baz_attr reads=quux_attr
8888

8989
# Aliased imports
9090

91-
from spam_module import ham_attr as eggs_attr # $writes=eggs_attr
91+
from spam_module import ham_attr as eggs_attr # $ writes=eggs_attr
9292

93-
def use_aliased_import(): # $writes=use_aliased_import
94-
print(eggs_attr) # $reads=eggs_attr
93+
def use_aliased_import(): # $ writes=use_aliased_import
94+
print(eggs_attr) # $ reads=eggs_attr
9595

9696
# Import star (unlikely to work unless we happen to extract/model the referenced module)
9797

9898
# Unknown modules
9999

100100
from unknown import *
101101

102-
def secretly_use_unknown(): # $writes=secretly_use_unknown
103-
print(unknown_attr) # $reads=unknown_attr
102+
def secretly_use_unknown(): # $ writes=secretly_use_unknown
103+
print(unknown_attr) # $ reads=unknown_attr
104104

105105
# Known modules
106106

107107
from known import *
108108

109-
def secretly_use_known(): # $writes=secretly_use_known
110-
print(known_attr) # $reads=known_attr
109+
def secretly_use_known(): # $ writes=secretly_use_known
110+
print(known_attr) # $ reads=known_attr
111111

112112
# Local import in function
113113

114-
def imports_locally(): # $writes=imports_locally
114+
def imports_locally(): # $ writes=imports_locally
115115
import mod1
116116

117117
# Global import hidden in function
118118

119-
def imports_stuff(): # $writes=imports_stuff
119+
def imports_stuff(): # $ writes=imports_stuff
120120
global mod2
121-
import mod2 # $writes=mod2
121+
import mod2 # $ writes=mod2

0 commit comments

Comments
 (0)