T09 · Insecure Skill Coding Practices
- Location
src/qshare/cli.py:252- Finding
TryCloudflare Share Exposes the Selected File's Entire Parent Directory
- Content
View full analysis
Vulnerability Details
File Location:
src/qshare/cli.py:252-271, 297-305
Vulnerability Type: Improper file-scope restriction and unintended directory exposure
Risk Level: HighVulnerable Code
python class ShareHandler(SimpleHTTPRequestHandler): def __init__(self, *args: object, **kwargs: object) -> None: # ``directory=`` was added to SimpleHTTPRequestHandler in Python 3.7. # Store it ourselves so Python 3.6 can serve the selected file's # parent directory without changing the process working directory. self._share_directory = kwargs.pop("directory", os.getcwd()) super().__init__(*args, **kwargs) def log_message(self, format: str, *args: object) -> None: # noqa: A003 return def translate_path(self, path: str) -> str: """Resolve request paths below the configured share directory.""" path = posixpath.normpath(unquote(urlparse(path).path)) words = [word for word in path.split("/") if word] translated = str(self._share_directory) for word in words: if os.path.dirname(word) or word in (os.curdir, os.pardir): continue translated = os.path.join(translated, word) return translatedpython def start_local_http_server(file_path: Path, port: int) -> Tuple[ShareHTTPServer, threading.Thread]: if not file_path.exists(): raise FileNotFoundError(f"File does not exist: {file_path}") if not file_path.is_file(): raise ValueError(f"Path is not a file: {file_path}") server = ShareHTTPServer(("127.0.0.1", port), partial(ShareHandler, directory=str(file_path.parent))) thread = threading.Thread(target=server.serve_forever, name="qshare-http", daemon=True) thread.start() return server, threadTechnical Analysis
The Skill promises to share one specifically selected local file. However ...[truncated 2199 chars]
- Remediation
View remediation
Remediation Suggestions
Replace the directory-oriented handler with a strict single-file handler:
- Store the selected file's resolved absolute path and expected URL path in the handler.
- Accept only an exact, safely encoded match for the selected filename.
- Return
404 Not Foundfor/, sibling filenames, subdirectories, alternate encodings, and all other paths. - Disable inherited directory-listing behavior by overriding
list_directory()to reject requests. - Avoid mapping arbitrary request paths onto
file_path.parent. - Resolve and compare filesystem paths before opening the file to prevent normalization or symlink-related scope expansion.
- Add regression tests proving that the selected file returns success while
/, sibling files, encoded path variants, and subdirectories return404.
