T05 · Unauthorized Access and Privilege Escalation
- Location
scripts/run_task.py:453- Finding
Unrestricted uploadedPath permits arbitrary local file disclosure to Gitea repositories
- Content
View full analysis
tuple[str, dict]: uploaded = Path(file["uploadedPath"]) if not uploaded.exists(): raise FileNotFoundError(str(uploaded)) owner = target["repoOwner"] repo = target["repoName"] source_file_key = sanitize_filename(file.get("sourceFileId") or uploaded.stem) folder = "code" if is_code_related(file) else source_folder(file.get("fileKind", ""), file.get("originalFileName", "")) archived_path = normalize_repo_path(f"source_files/{folder}/{source_file_key}-{sanitize_filename(file.get('originalFileName') or uploaded.name)}") if (task.get("payload") or {}).get("options", {}).get("archiveSourceFile", True): g.put_file(owner, repo, archived_path, uploaded.read_bytes(), f"research-kb archive source: {uploaded.name} [{task.get('taskId')}]") return archived_path, {"sourceFileId": file.get("sourceFileId"), "path": archived_path} ``` `scripts/run_task.py:482-493`: ```python for file in files: if remaining <= 0: break uploaded = Path(file["uploadedPath"]) try: text = text_extractors.extract_text(str(uploaded), min(max_file_chars, remaining)) except Exception as exc: text = f"[Could not extract text: {exc}]" if not text.strip(): continue header = f"\n\n### {file.get('relativePath', '')}\n\n" chunk = text[:min(max_file_chars, remaining)] lines.append(header + chunk) ``` ### Tech ...[truncated 2468 chars]- Remediation
View remediation
Path: candidate = Path(value) if candidate.is_symlink(): raise ValueError("Symbolic links are not permitted") resolved = candidate.resolve(strict=True) try: resolved.relative_to(UPLOAD_ROOT) except ValueError as exc: raise ValueError("uploadedPath is outside the upload root") from exc if not resolved.is_file(): raise ValueError("uploadedPath must identify a regular file") return resolved ``` 2. Prefer an opaque upload identifier supplied by the task and resolve it to a path through trusted backend state. Do not allow clients to provide arbitrary filesystem paths. 3. Reject symlinks and mitigate time-of-check/time-of-use races. Where practical, open files using platform protections such as `O_NOFOLLOW`, then validate the opened file descriptor. 4. Recompute and compare the file's SHA-256 digest with trusted upload metadata before processing or archiving it. 5. Enforce maximum file sizes before reading content into memory. 6. Validate `repoOwner`, `repoName`, `repoFullName`, `kbType`, and branch against targets authorized by the trusted backend. 7. Replace the administrative token with a repository-scoped bot token holding only the minimum required read and write permissions. 8. Add tests covering absolute paths, `..` traversal, symlink escapes, non-regular files, hash mismatches, and unauthorized repository targets. ]]>
