| Title | https://github.com/engigu/ baihu-panel commit cb127a0a5ed0fe5c48d8601b80f1a23c96251323 Path Traversal |
|---|
| Description | Zip-slip path traversal in backup-restore scripts-directory extraction
Affected versions: confirmed on commit `cb127a0a5ed0fe5c48d8601b80f1a23c96251323` (main branch).
## Summary
`BackupService.Restore`, reachable from the admin-only `POST /api/settings/restore` endpoint (file upload), extracts a `scripts/` prefixed entry from the uploaded backup zip through `restoreScriptsDir` in `internal/services/backup_service.go` without validating the entry name for path-traversal sequences. A crafted backup zip containing a member named e.g. `scripts/../../../../etc/cron.d/x` is written to that resolved path on disk, escaping the intended scripts working directory entirely.
## Details
`restoreScriptsDir` takes a zip entry's name, strips the `scripts/` prefix, and joins the remainder directly onto `constant.ScriptsWorkDir` with `filepath.Join`:
```go
func (s *BackupService) restoreScriptsDir(r *zip.ReadCloser) {
scriptsDir := constant.ScriptsWorkDir
for _, f := range r.File {
if len(f.Name) > 8 && f.Name[:8] == "scripts/" {
relPath := f.Name[8:]
if relPath == "" {
continue
}
fpath := filepath.Join(scriptsDir, relPath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, 0755)
continue
}
os.MkdirAll(filepath.Dir(fpath), 0755)
if outFile, err := os.Create(fpath); err == nil {
if rc, err := f.Open(); err == nil {
io.Copy(outFile, rc)
rc.Close()
}
outFile.Close()
}
}
}
}
```
`filepath.Join` collapses `..` segments in `relPath` against `scriptsDir`, and nothing afterward checks that the resulting `fpath` is still inside `scriptsDir`. This is unlike the panel's own general-purpose archive helpers in `internal/utils/archive.go` (`ExtractZip`/`ExtractTar`), which both apply a containment check (`filepath.Rel` plus a `..`-prefix test) per member before writing - `restoreScriptsDir` is a separate extraction loop that never got the same guard.
`POST /api/settings/restore` (`SettingsController.RestoreBackup`) accepts a multipart file upload and passes it straight to `BackupService.Restore`, which calls `restoreScriptsDir` on the resulting zip reader. The route sits behind `middleware.AuthRequired()` and `middleware.AdminRequired()`.
## Steps To Reproduce
(available upon request)
## Impact
An authenticated admin who restores a maliciously crafted backup zip (e.g. one obtained from an untrusted mirror, a shared/community backup, or a compromised backup pipeline) can have the panel process write an arbitrary file to any path writable by the process user, including locations such as cron directories or systemd unit directories depending on deployment, as part of a routine restore action with no indication anything unusual happened.
|
|---|
| Source | ⚠️ https://github.com/engigu/baihu-panel/issues/235 |
|---|
| User | carfeii (UID 97470) |
|---|
| Submission | 11/10/2026 16:01 (20 days ago) |
|---|
| Moderation | 11/30/2026 22:52 (20 days later) |
|---|
| Status | Accepted |
|---|
| VulDB entry | 446842 [engigu baihu-panel up to 1.6.2 backup-restore backup_service.go restoreScriptsDir path traversal] |
|---|
| Points | 20 |
|---|