Skip to content

Commit dc52282

Browse files
MUSTAPHA BARKIgithub-advanced-security[bot]
andauthored
Potential fix for code scanning alert no. 16: Uncontrolled data used in path expression
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: MUSTAPHA BARKI <[email protected]>
1 parent bbd8d8b commit dc52282

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

llvm/tools/sancov/coverage-report-server.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,15 @@ def do_GET(self):
162162
self.wfile.write(response.encode("UTF-8", "replace"))
163163
elif self.symcov_data.has_file(norm_path):
164164
filename = norm_path
165-
filepath = os.path.join(self.src_path, filename)
166-
if not os.path.exists(filepath):
165+
# Construct the full file path, normalizing it to avoid traversal
166+
abs_src_path = os.path.realpath(self.src_path)
167+
abs_file_path = os.path.realpath(os.path.join(self.src_path, filename))
168+
# Check containment: file must reside within src_path
169+
if not abs_file_path.startswith(abs_src_path + os.sep):
170+
self.send_response(403)
171+
self.end_headers()
172+
return
173+
if not os.path.exists(abs_file_path):
167174
self.send_response(404)
168175
self.end_headers()
169176
return
@@ -174,7 +181,7 @@ def do_GET(self):
174181

175182
linemap = self.symcov_data.compute_linemap(filename)
176183

177-
with open(filepath, "r", encoding="utf8") as f:
184+
with open(abs_file_path, "r", encoding="utf8") as f:
178185
content = "\n".join(
179186
[
180187
"<span class='{cls}'>{line}&nbsp;</span>".format(

0 commit comments

Comments
 (0)