Skip to content

Commit a3222d1

Browse files
Potential fix for pull request finding 'CodeQL / Uncontrolled data used in path expression'
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 986dcfd commit a3222d1

1 file changed

Lines changed: 12 additions & 45 deletions

File tree

dev_environment/demo_api_main.py

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -70,55 +70,22 @@ def _handle_error(arc_dir: Path, rdi: str, arc_id: str, exc: Exception) -> None:
7070
tb = traceback.format_exc()
7171
print(f"Error writing ARC for {rdi}/{arc_id}: {exc}")
7272

73-
# Ensure that error logging always happens under the configured OUTPUT_ROOT,
74-
# regardless of how arc_dir was derived. This avoids using any potentially
75-
# untrusted path prefixes.
76-
base_root = OUTPUT_ROOT.resolve()
77-
78-
# Derive a simple, safe subdirectory name from the provided arc_dir/arc_id.
79-
# Prefer the final path component of arc_dir; fall back to arc_id; and
80-
# finally to a generic name if necessary.
81-
candidate_name = arc_dir.name if arc_dir.name not in {"", ".", ".."} else arc_id
82-
if not isinstance(candidate_name, str) or not candidate_name:
83-
candidate_name = "unknown"
84-
85-
# Reuse the same safe-name pattern as in upload_arc.
86-
safe_name_pattern = re.compile(r"^[A-Za-z0-9_.-]{1,64}$")
87-
candidate_name = candidate_name.strip()
88-
if (
89-
not candidate_name
90-
or candidate_name in {".", ".."}
91-
or "/" in candidate_name
92-
or "\\" in candidate_name
93-
or not safe_name_pattern.match(candidate_name)
94-
):
95-
safe_name = "unknown"
96-
else:
97-
safe_name = candidate_name
98-
99-
# Build the final directory for error logging under the safe root.
100-
safe_arc_dir = (base_root / safe_name).resolve()
101-
try:
102-
common_root = os.path.commonpath([str(base_root), str(safe_arc_dir)])
103-
except ValueError:
104-
# Fall back to logging under a generic error directory if something goes wrong.
105-
safe_arc_dir = (base_root / "errors").resolve()
106-
common_root = os.path.commonpath([str(base_root), str(safe_arc_dir)])
107-
108-
if common_root != str(base_root):
109-
# As an additional safeguard, if the computed directory is not under
110-
# OUTPUT_ROOT, force it into a fixed "errors" directory and re-validate.
111-
safe_arc_dir = (base_root / "errors").resolve()
112-
try:
113-
common_root = os.path.commonpath([str(base_root), str(safe_arc_dir)])
114-
except ValueError:
73+
# Always log errors under a fixed subdirectory of OUTPUT_ROOT to avoid
74+
# any dependence on user-controlled identifiers when constructing paths.
75+
errors_root = (OUTPUT_ROOT / "errors").resolve()
76+
errors_root.mkdir(parents=True, exist_ok=True)
77+
78+
error_path = errors_root / "error.txt"
11579
# In the unlikely event that commonpath still fails, log directly under
116-
# the resolved OUTPUT_ROOT itself.
117-
safe_arc_dir = base_root
80+
handle.write(f"RDI: {rdi}\n")
81+
handle.write(f"ARC ID: {arc_id}\n")
82+
handle.write(f"ARC directory: {arc_dir}\n")
83+
handle.write(f"Exception: {exc}\n\n")
11884
else:
119-
if common_root != str(base_root):
12085
# If the resolved "errors" directory is still not under OUTPUT_ROOT,
86+
_chown_tree(errors_root)
12187
# fall back to using the OUTPUT_ROOT directory itself.
88+
12289
safe_arc_dir = base_root
12390

12491
safe_arc_dir.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)