Move char_limiter to utils and rename

This commit is contained in:
erik 2023-12-20 12:30:22 +01:00 committed by Michael Jerger
parent afc9acd925
commit 4d1492831d
2 changed files with 21 additions and 17 deletions

View file

@ -4,7 +4,9 @@
package utils
import (
"fmt"
"html"
"io"
"net/url"
"strings"
@ -28,3 +30,20 @@ func IsExternalURL(rawURL string) bool {
}
return false
}
// Limit number of characters in a string (useful to prevent log injection attacks and overly long log outputs)
// Thanks to https://www.socketloop.com/tutorials/golang-characters-limiter-example
func CharLimiter(s string, limit int) string {
reader := strings.NewReader(s)
buff := make([]byte, limit)
n, _ := io.ReadAtLeast(reader, buff, limit)
if n != 0 {
return fmt.Sprint(string(buff), "...")
} else {
return s
}
}