fix(bonfire): replace invalid UTF-8 before the control scan in Sanitize (IN-08, quick-261028-rqd)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user