Skip to content

Commit 6c46854

Browse files
authored
Merge pull request #225 from Integration-Automation/dev
Fix S2083 taint chain in search-result replace
2 parents f2f84b9 + 4e405f3 commit 6c46854

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

je_editor/pyside_ui/dialog/search_ui/search_replace_widget.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,21 @@ def _replace_selected_result(self) -> None:
485485
return
486486

487487
try:
488-
# 將路徑限制在專案根目錄內,避免路徑穿越
489-
# Confine path to project root to prevent path traversal
488+
# 僅從受信任的 project_root 與經驗證的相對路徑重新組合,
489+
# 切斷從使用者輸入流入 write_text() 的 taint 鏈 (SonarCloud S2083)
490+
# Rebuild the target path from the trusted project root plus a
491+
# validated relative component so user-controlled data never flows
492+
# directly into write_text() (SonarCloud S2083).
490493
project_root = Path(self._get_project_root()).resolve()
491-
resolved = Path(file_path).resolve()
492-
if not resolved.is_file() or (project_root not in resolved.parents and resolved != project_root):
494+
try:
495+
relative_part = Path(file_path).resolve().relative_to(project_root)
496+
except ValueError:
497+
self.status_label.setText(f"Error: invalid file path {file_path}")
498+
return
499+
p = project_root.joinpath(*relative_part.parts)
500+
if not p.is_file():
493501
self.status_label.setText(f"Error: invalid file path {file_path}")
494502
return
495-
p = resolved
496503
raw = p.read_bytes()
497504
enc = "utf-8"
498505
for _enc in ("utf-8-sig", "utf-8", "cp1252", "latin-1"):

0 commit comments

Comments
 (0)