Li-john1021 90c5d526e2
fix(paths): reject Windows reserved names on host-visible paths (#6102)
* fix(paths): reject Windows reserved names on host-visible paths

Uploads, skill support files, and sandbox path checks now share one helper that rejects CON, PRN, AUX, NUL, COM1-COM9, and LPT1-LPT9 (including names like con.txt) and components that end in a dot or space. The check is not limited to Windows so a Linux host cannot store a name that breaks when the same tree is used on Windows.

* fix(paths): validate quoted local bash paths as whole words

The absolute-path scan stopped at whitespace, so a quoted upload name such as report. final.txt or CON notes.txt was rejected as a truncated segment. Local bash checks now use the shlex argument for traversal and Windows-incompatible segments.

* fix(paths): do not block reads of existing host names

* style(paths): format with ruff 0.15.12
2026-10-01 09:11:19 +08:00

29 lines
1.2 KiB
Python

"""Portability checks for host-visible path segments."""
from __future__ import annotations
_WINDOWS_RESERVED_NAMES = frozenset({"CON", "PRN", "AUX", "NUL"} | {f"COM{i}" for i in range(1, 10)} | {f"LPT{i}" for i in range(1, 10)})
def windows_incompatible_segment(segment: str) -> str | None:
"""Return a reason when *segment* is unsafe to create as a host path component.
Reserved device names and trailing dots or spaces are rejected on every
platform when a name is created: uploads, new custom-skill support files,
and sandbox writes of a path that is not already stored. Reads, deletes of
an existing support file, and access to a path that already exists on the
host do not use this check. Those names alias devices or are silently
stripped on Windows.
``.`` and ``..`` are ignored here; callers already reject traversal.
Do not reject colons, quotes, or control characters here.
"""
if segment in {"", ".", ".."}:
return None
if segment.endswith((" ", ".")):
return "trailing dot or space"
stem = segment.split(".", 1)[0]
if stem.upper() in _WINDOWS_RESERVED_NAMES:
return "reserved Windows device name"
return None