Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(handler): restrict file downloads to tasks directory
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 #296

Signed-off-by: kapil971390 <kapil69265@gmail.com>
  • Loading branch information
kapil971390 committed Jun 21, 2026
commit 0b7ca977bb7898cf783cb6208b71a8dd81525167
16 changes: 16 additions & 0 deletions internal/handler/subtitle_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"krillin-ai/internal/response"
"krillin-ai/internal/service"
"krillin-ai/log"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/gin-gonic/gin"
"go.uber.org/zap"
Expand Down Expand Up @@ -141,6 +143,20 @@ func (h Handler) DownloadFile(c *gin.Context) {
}

localFilePath := filepath.Join(".", requestedFile)

// Restrict downloads to the tasks output directory to prevent
// arbitrary file reads (e.g. config/config.toml with API keys).
tasksDir, err := filepath.Abs("tasks")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部错误"})
return
}
absPath, err := filepath.Abs(localFilePath)
if err != nil || !strings.HasPrefix(absPath, tasksDir+string(os.PathSeparator)) {
c.JSON(http.StatusForbidden, gin.H{"error": "禁止访问"})
return
}

if _, err := os.Stat(localFilePath); os.IsNotExist(err) {
response.R(c, response.Response{
Error: -1,
Expand Down