fix(bonfire): replace invalid UTF-8 before the control scan in Sanitize (IN-08, quick-261028-rqd)

This commit is contained in:
Jakub Zych
2026-10-28 20:21:19 +01:00
parent 825a3fd2da
commit 8476a65aa0
3 changed files with 26 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ import (
"path/filepath"
"strings"
"unicode"
"unicode/utf8"
"git.golem15.com/golem15/summercms/internal/argmask"
"git.golem15.com/golem15/summercms/modules/sunscreen"
@@ -102,18 +103,23 @@ func Reported(err error) error {
// text, names an administrator typed) then cannot move the cursor, clear
// the screen or set the window title. Error blocks, process views, failure
// lines and tables pass their text through it, and so does diary's console
// channel. A clean string is returned unchanged.
// channel. Invalid UTF-8 becomes U+FFFD first, so a raw 8-bit C1 byte (such
// as 0x9b, CSI) cannot reach a terminal that honours it, and the result is
// always valid UTF-8. A clean, valid string is returned unchanged.
func Sanitize(s string) string {
clean := true
for _, r := range s {
if r != '\t' && unicode.IsControl(r) {
clean = false
break
clean := utf8.ValidString(s)
if clean {
for _, r := range s {
if r != '\t' && unicode.IsControl(r) {
clean = false
break
}
}
}
if clean {
return s
}
s = strings.ToValidUTF8(s, "\uFFFD")
var b strings.Builder
for _, r := range s {
switch {