修复帖内 CSS 污染整页:前后端禁止 style 并消毒 HTML。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-04 13:04:10 +08:00
parent 2a34699f64
commit 860dffd547
9 changed files with 129 additions and 5 deletions

View File

@@ -9,7 +9,9 @@ import (
var (
membersOnlyBlockRe = regexp.MustCompile(`(?is)<members-only\b[^>]*>([\s\S]*?)</members-only>`)
htmlTagRe = regexp.MustCompile(`<[^>]+>`)
// style/script 内文本不能进搜索/摘要,否则会出现 "* {color:red}" 之类噪声
styleOrScriptRe = regexp.MustCompile(`(?is)<(style|script)\b[^>]*>[\s\S]*?</(style|script)>`)
htmlTagRe = regexp.MustCompile(`<[^>]+>`)
)
// RedactMembersOnlyHTML 未登录时移除会员专属区块内的正文,保留长度提示供前端展示
@@ -41,6 +43,7 @@ func StripHTMLForSearch(html string) string {
if html == "" {
return ""
}
html = styleOrScriptRe.ReplaceAllString(html, " ")
text := htmlTagRe.ReplaceAllString(html, " ")
text = strings.ReplaceAll(text, "&nbsp;", " ")
return strings.Join(strings.Fields(text), " ")