修设置页主题色块被模板过滤器替换成 ZgotmplZ

html/template 对 style 属性做 CSS 过滤,linear-gradient(...) 未标记安全会被整体替换成
ZgotmplZ —— 线上设置页四个主题色块一直是灰底(既有问题,不是这次配色改动引入)。

- FuncMap 新增 safeCSS(string → template.CSS);值只来自 internal/theme.swatchOf,
  由两个已校验的色值拼成,不接受用户输入,所以这里放开是安全的
- user/profile.html 的色块改 {{ safeCSS .Swatch }}
- 新增 TestThemeSwatchRenders:渲染 user/profile,断言不含 ZgotmplZ 且含渐变串;
  已做变异验证(去掉 safeCSS 后该用例失败)
This commit is contained in:
tamakyi
2026-10-21 18:53:10 +08:00
parent 27856dd4a2
commit 1f29d3eb2e
4 changed files with 47 additions and 1 deletions
+23
View File
@@ -17,6 +17,7 @@ import (
"github.com/tamakyi/TamaBox/internal/i18n"
"github.com/tamakyi/TamaBox/internal/security/moderation"
templatepkg "github.com/tamakyi/TamaBox/internal/template"
"github.com/tamakyi/TamaBox/internal/theme"
)
// 审核严格度下拉框的解析:不依赖属性的书写顺序或是否存在额外属性
@@ -423,3 +424,25 @@ func TestRenderUserQuestionList(t *testing.T) {
t.Fatal("restriction banner should show expiry date")
}
}
// TestThemeSwatchRenders 主题色块走的是 inline style="background: <gradient>"。
// html/template 对 style 属性做 CSS 过滤,未标记安全的 linear-gradient(...) 会被换成
// ZgotmplZ —— 设置页每个主题的色块都变成灰底。这个用例钉住「色块真的渲染出来」。
func TestThemeSwatchRenders(t *testing.T) {
base := profileBaseData()
base["Themes"] = []theme.Theme{
{Slug: "sakura", Swatch: "linear-gradient(135deg, #fceef3 0 45%, #ba3e66 45% 100%)"},
}
base["CurrentTheme"] = "sakura"
out, err := renderToString(t, "user/profile", base)
if err != nil {
t.Fatalf("render: %v", err)
}
if strings.Contains(out, "ZgotmplZ") {
t.Fatal("色块被 html/template 过滤成 ZgotmplZ(检查是否漏了 safeCSS)")
}
if !strings.Contains(out, "linear-gradient(135deg, #fceef3 0 45%, #ba3e66 45% 100%)") {
t.Fatal("渲染结果里找不到主题色块的渐变")
}
}