Files
komari/web/api/admin/theme_test.go
T
guocn 404d97d846 fix(theme): prevent path traversal in theme operations
DeleteTheme/UpdateTheme/SetTheme used the user-supplied theme name
(short) directly in filepath.Join("./data/theme", short) before
filesystem operations (os.RemoveAll / os.Stat). Without validation,
a crafted short value like "../../" normalizes outside the theme
directory, letting an authenticated admin delete or access arbitrary
directories within the process working directory.

Reuse the existing isValidThemeShort validator (allowlist of
[a-zA-Z0-9_-]) in all three handlers before the path is built.
Add a regression test covering traversal payloads and absolute paths.
2026-06-29 23:12:42 +08:00

48 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package admin
import "testing"
// TestIsValidThemeShort_PathTraversal 防止 DeleteTheme/UpdateTheme/SetTheme
// 的路径穿越漏洞:req.Short 直接进入 filepath.Join("./data/theme", short),
// 若 short 含 "../" 会规范化到 ./data/theme 之外,配合 os.RemoveAll 可删除
// 工作目录外任意目录。isValidThemeShort 必须拒绝所有此类 payload。
func TestIsValidThemeShort_PathTraversal(t *testing.T) {
// 必须被拒绝:路径穿越 / 绝对路径 / 目录分隔符 / 空值 / default
deny := []string{
"",
"default",
"..",
"../",
"./",
"../../etc",
"../..",
"foo/../bar",
"/etc/passwd",
"foo/bar",
"foo\\bar",
"a b",
"a;b",
"a$(id)",
}
for _, in := range deny {
if isValidThemeShort(in) {
t.Errorf("isValidThemeShort(%q) = true, want false (路径穿越/非法字符未被拦截)", in)
}
}
// 必须被接受:仅字母数字下划线连字符
accept := []string{
"mytheme",
"my-theme",
"my_theme",
"theme123",
"ABC",
"a",
}
for _, in := range accept {
if !isValidThemeShort(in) {
t.Errorf("isValidThemeShort(%q) = false, want true (合法名称被误拒)", in)
}
}
}