photoprism/pkg/clean/name.go
Michael Mayer 07d8d569a7 Security: Improve user input sanitization, e.g. for album names #2531
Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-09-02 18:39:19 +02:00

37 lines
637 B
Go

package clean
import (
"strings"
"github.com/photoprism/photoprism/pkg/txt"
)
// Name sanitizes and capitalizes names.
func Name(name string) string {
// Empty or too long?
if name == "" || reject(name, txt.ClipDefault) {
return ""
}
// Remove unwanted characters.
name = strings.Map(func(r rune) rune {
if r < 32 || r == 127 {
return -1
}
switch r {
case '"', '$', '%', '\\', '*', '`', ';', '<', '>', '{', '}':
return -1
}
return r
}, name)
name = strings.TrimSpace(name)
// Now empty?
if name == "" {
return ""
}
// Shorten and capitalize.
return txt.Clip(txt.Title(name), txt.ClipDefault)
}