Skip to content

fix(handler): restrict file downloads to tasks directory — prevent API key leakage - #297

Merged
wulien merged 1 commit into
krillinai:masterfrom
kapil971390:fix/file-download-path-traversal
Jun 25, 2026
Merged

wulien merged 1 commit into
krillinai:masterfrom
kapil971390:fix/file-download-path-traversal

Conversation

@kapil971390

Copy link
Copy Markdown
Contributor

Summary

The DownloadFile handler in internal/handler/subtitle_task.go accepts arbitrary file paths via the *filepath route parameter and serves them with no directory restriction.

Root cause: filepath.Join(".", requestedFile) resolves absolute paths by design — filepath.Join(".", "/config/config.toml") returns "config/config.toml", not an error. Any caller can read any file the process can read.

Impact: The config/config.toml file typically contains API keys for OpenAI, Anthropic, Deepseek, ElevenLabs, and similar services. A single GET request leaks all of them:

GET /api/file/config/config.toml

Fix: Resolve both the requested path and the tasks/ output directory to absolute paths, then reject requests that don't start with the tasks directory prefix (HTTP 403).

tasksDir, err := filepath.Abs("tasks")
// ...
absPath, err := filepath.Abs(localFilePath)
if err != nil || !strings.HasPrefix(absPath, tasksDir+string(os.PathSeparator)) {
    c.JSON(http.StatusForbidden, gin.H{"error": "禁止访问"})
    return
}

This was found during a security-focused review of the file download endpoint.

Test plan

  • GET /api/file/tasks/<valid-output-file> → 200, file served
  • GET /api/file/config/config.toml → 403 Forbidden
  • GET /api/file/../config/config.toml → 403 Forbidden
  • GET /api/file//etc/passwd → 403 Forbidden

DownloadFile used filepath.Join(".", requestedFile) with no boundary
check, allowing any client to read arbitrary files from the server's
working directory — including config/config.toml which contains all
configured API keys (OpenAI, Aliyun, etc.).

Fix: resolve both the tasks directory and the requested path to
absolute paths, then reject requests whose resolved path does not
start with the tasks directory prefix.

Fixes krillinai#296

Signed-off-by: kapil971390 <kapil69265@gmail.com>
@wulien
wulien merged commit 78ac14a into krillinai:master Jun 25, 2026
wulien added a commit that referenced this pull request Oct 14, 2026
…rsal

fix(handler): restrict file downloads to tasks directory — prevent API key leakage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants