Skip to content

Commit e864741

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 0f3b4b7 commit e864741

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

dev_environment/demo_api_main.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import traceback
1212
from datetime import UTC, datetime
1313
from pathlib import Path
14+
import re
1415

1516
from arctrl import ARC
1617
from arctrl.py.fable_modules.fable_library.async_ import start_as_task # type: ignore[import-untyped]
@@ -122,13 +123,24 @@ def _fallback() -> tuple[str, Path]:
122123
target = (base_resolved / rid).resolve()
123124
return rid, target
124125

126+
# Allow only simple, short directory names consisting of safe characters.
127+
# This ensures that user-controlled identifiers cannot introduce path
128+
# traversal or unexpected filesystem semantics.
129+
safe_name_pattern = re.compile(r"^[A-Za-z0-9_.-]{1,64}$")
130+
125131
if isinstance(raw_id, str) and raw_id.strip():
126132
candidate_id = raw_id.strip()
127133
# Reduce to a single path component and normalize it.
128134
safe_name = os.path.normpath(Path(candidate_id).name)
129-
# Reject empty names, current/parent directory markers, or anything that
130-
# would reintroduce directory components on this platform.
131-
if not safe_name or safe_name in {".", ".."} or "/" in safe_name or "\\" in safe_name:
135+
# Reject empty names, current/parent directory markers, any embedded
136+
# separators, or names that do not match the allowed pattern.
137+
if (
138+
not safe_name
139+
or safe_name in {".", ".."}
140+
or "/" in safe_name
141+
or "\\" in safe_name
142+
or not safe_name_pattern.match(safe_name)
143+
):
132144
arc_id, candidate_dir = _fallback()
133145
else:
134146
candidate_dir = (base_resolved / safe_name).resolve()

0 commit comments

Comments
 (0)