refactor: render highlight language (#38793)

Avoid CSS injection

More details are in the comment of CodeBlockAttributes
This commit is contained in:
wxiaoguang
2026-08-06 18:07:36 +08:00
committed by GitHub
parent 231ba1da19
commit 6ff3a65708
17 changed files with 112 additions and 56 deletions
+2 -8
View File
@@ -4,8 +4,6 @@
package internal
import (
"crypto/rand"
"encoding/base64"
"html/template"
"io"
"regexp"
@@ -13,6 +11,7 @@ import (
"sync"
"gitea.dev/modules/htmlutil"
"gitea.dev/modules/util"
"golang.org/x/net/html"
)
@@ -30,12 +29,7 @@ type RenderInternal struct {
}
func (r *RenderInternal) Init(output io.Writer, extraHeadHTML template.HTML) io.WriteCloser {
buf := make([]byte, 12)
_, err := rand.Read(buf)
if err != nil {
panic("unable to generate secure id")
}
return r.init(base64.URLEncoding.EncodeToString(buf), output, extraHeadHTML)
return r.init(util.FastCryptoRandomHex(16), output, extraHeadHTML)
}
func (r *RenderInternal) init(secID string, output io.Writer, extraHeadHTML template.HTML) io.WriteCloser {