fix(exporter): refuse symlinks at file targets and skip tests on Windows
Address Copilot review on #1156: - Per-file symlink check via new _safe_open_for_write() helper. Uses O_NOFOLLOW on POSIX (close TOCTOU window between islink check and open) and falls back to islink + open on Windows. Applied to room files and index.md, mirroring the existing dir-level check. - Tests now wrap os.symlink() in _try_symlink_or_skip() so Windows without Developer Mode and restricted CI sandboxes skip rather than hard-fail. Added two regression tests for the file-level cases (room file, index.md).
This commit is contained in:
+27
-2
@@ -11,6 +11,7 @@ Streams drawers in paginated batches so memory usage stays bounded
|
||||
regardless of palace size.
|
||||
"""
|
||||
|
||||
import errno
|
||||
import os
|
||||
import re
|
||||
from collections import defaultdict
|
||||
@@ -40,6 +41,30 @@ def _reject_symlink(path: str, label: str) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _safe_open_for_write(path: str, mode: str, encoding: str = "utf-8"):
|
||||
"""Open a file for writing, refusing to follow a symlink at the target path.
|
||||
|
||||
On POSIX (O_NOFOLLOW available) the open itself fails with ELOOP if path is
|
||||
a symlink — closing the TOCTOU window between an islink check and the open.
|
||||
On platforms without O_NOFOLLOW (Windows), pre-checks ``os.path.islink``,
|
||||
which is narrower than no check at all.
|
||||
"""
|
||||
o_nofollow = getattr(os, "O_NOFOLLOW", 0)
|
||||
if o_nofollow:
|
||||
flags = os.O_WRONLY | os.O_CREAT | o_nofollow
|
||||
flags |= os.O_APPEND if "a" in mode else os.O_TRUNC
|
||||
try:
|
||||
fd = os.open(path, flags, 0o600)
|
||||
except OSError as e:
|
||||
if e.errno == errno.ELOOP:
|
||||
raise ValueError(f"refusing to write: {path!r} is a symbolic link.") from None
|
||||
raise
|
||||
return os.fdopen(fd, mode, encoding=encoding)
|
||||
if os.path.islink(path):
|
||||
raise ValueError(f"refusing to write: {path!r} is a symbolic link.")
|
||||
return open(path, mode, encoding=encoding)
|
||||
|
||||
|
||||
def export_palace(palace_path: str, output_dir: str, format: str = "markdown") -> dict:
|
||||
"""Export all palace drawers as markdown files organized by wing/room.
|
||||
|
||||
@@ -118,7 +143,7 @@ def export_palace(palace_path: str, output_dir: str, format: str = "markdown") -
|
||||
key = (wing, room)
|
||||
is_new = key not in opened_rooms
|
||||
|
||||
with open(room_path, "a" if not is_new else "w", encoding="utf-8") as f:
|
||||
with _safe_open_for_write(room_path, "a" if not is_new else "w") as f:
|
||||
if is_new:
|
||||
f.write(f"# {wing} / {room}\n\n")
|
||||
opened_rooms.add(key)
|
||||
@@ -168,7 +193,7 @@ def export_palace(palace_path: str, output_dir: str, format: str = "markdown") -
|
||||
index_lines.append("")
|
||||
|
||||
index_path = os.path.join(output_dir, "index.md")
|
||||
with open(index_path, "w", encoding="utf-8") as f:
|
||||
with _safe_open_for_write(index_path, "w") as f:
|
||||
f.write("\n".join(index_lines))
|
||||
|
||||
stats = {
|
||||
|
||||
Reference in New Issue
Block a user