mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-19 16:30:15 +00:00
Move char_limiter to utils and rename
This commit is contained in:
parent
afc9acd925
commit
4d1492831d
2 changed files with 21 additions and 17 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue