T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/processor.py:75- Finding
Caller-Controlled Path Allows Arbitrary Local File Reads
- Content
View full analysis
Vulnerability Details
File Location:
scripts/processor.py:75-83, with file-opening sinks atscripts/processor.py:135-153andscripts/processor.py:202-206
Vulnerability Type: Arbitrary local file read through an unvalidated path
Risk Level: HighVulnerable Code
python for file_data in file_list: try: content = self._parse_file( file_data.get('path', ''), file_data.get('name', '') ) file_info = FileInfo( name=file_data.get('name', 'unknown'), path=file_data.get('path', ''), size=file_data.get('size', 0), content=content )The caller-controlled values are passed to the parser without validation:
python def _parse_file(self, file_path: str, filename: str) -> str: """Parse content according to file type.""" ext = os.path.splitext(filename)[1].lower() parsers = { '.pdf': self._parse_pdf, '.docx': self._parse_docx, '.doc': self._parse_docx, '.xlsx': self._parse_xlsx, '.xls': self._parse_xlsx, '.pptx': self._parse_pptx, '.ppt': self._parse_pptx, '.txt': self._parse_txt, '.md': self._parse_txt, '.csv': self._parse_csv, '.json': self._parse_json } parser = parsers.get(ext, self._parse_binary) return parser(file_path)One resulting file-opening sink is:
python def _parse_txt(self, file_path: str) -> str: """Parse a text file.""" try: with open(file_path, 'rb') as f: raw = f.read() result = chardet.detect(raw) encoding = result['encoding'] or 'utf-8' return raw.decode(encoding, errors='ignore')[:50000] except Exception as e: return f"[Text parsing failed: {e}]"Technical Analysis
receive_files()trusts th ...[truncated 2418 chars]- Remediation
View remediation
Remediation Suggestions
- Do not accept arbitrary filesystem paths from callers. Accept opaque, server-generated upload identifiers and resolve them through a trusted upload registry.
- Place uploaded files under a dedicated directory with restrictive permissions.
- Canonicalize both the configured upload root and candidate file path with
os.path.realpath(), then verify containment usingos.path.commonpath(). - Reject absolute paths, traversal outside the upload root, symbolic links, device files, directories, sockets, and other non-regular files.
- Derive the filename and extension from the validated stored file rather than trusting a separate caller-provided
name. - Open files using a race-resistant strategy where supported, such as directory file descriptors and
O_NOFOLLOW, to reduce time-of-check/time-of-use and symbolic-link attacks. - Run the processor under a dedicated least-privileged operating-system account with no access to application secrets or other users' files.
- Add tests covering absolute paths,
../traversal, symbolic links, mismatched names and paths, and files outside the upload directory.
