2022-04-15 09:42:07 +02:00
|
|
|
package clean
|
2021-12-14 20:01:39 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Log sanitizes strings created from user input in response to the log4j debacle.
|
|
|
|
func Log(s string) string {
|
2022-04-04 14:21:43 +02:00
|
|
|
if s == "" {
|
|
|
|
return "''"
|
|
|
|
} else if reject(s, 512) {
|
2021-12-14 20:01:39 +01:00
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
spaces := false
|
2021-12-14 20:01:39 +01:00
|
|
|
|
|
|
|
// Remove non-printable and other potentially problematic characters.
|
|
|
|
s = strings.Map(func(r rune) rune {
|
2022-09-02 18:39:19 +02:00
|
|
|
if r < 32 || r == 127 {
|
2021-12-14 20:01:39 +01:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r {
|
2022-04-04 14:21:43 +02:00
|
|
|
case ' ':
|
|
|
|
spaces = true
|
|
|
|
return r
|
2021-12-14 20:01:39 +01:00
|
|
|
case '`', '"':
|
|
|
|
return '\''
|
|
|
|
case '\\', '$', '<', '>', '{', '}':
|
|
|
|
return '?'
|
|
|
|
default:
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
}, s)
|
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
// Contains spaces?
|
|
|
|
if spaces {
|
2021-12-14 20:01:39 +01:00
|
|
|
return fmt.Sprintf("'%s'", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-09-02 18:39:19 +02:00
|
|
|
// LogQuote sanitizes a string and puts it in single quotes for logging.
|
|
|
|
func LogQuote(s string) string {
|
|
|
|
if s = Log(s); s[0] != '\'' {
|
|
|
|
return fmt.Sprintf("'%s'", s)
|
|
|
|
} else {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 20:01:39 +01:00
|
|
|
// LogLower sanitizes strings created from user input and converts them to lowercase.
|
|
|
|
func LogLower(s string) string {
|
|
|
|
return Log(strings.ToLower(s))
|
|
|
|
}
|