Files
photoprism/pkg/service/http/header/id.go
2025-09-25 17:52:44 +02:00

35 lines
594 B
Go

package header
import "strings"
// ID sanitizes identifier tokens, for example, a session ID, a UUID, or some other string ID.
func ID(s string) string {
if s == "" || len(s) > 4096 {
return ""
}
var prev rune
// Remove all invalid characters.
s = strings.Map(func(r rune) rune {
if r == ' ' && (prev == 0 || prev == ' ') {
return -1
}
prev = r
switch r {
case ' ', '"', '-', '+', '/', '=', '#', '$', '@', ':', ';', '_', '.':
return r
}
if (r < '0' || r > '9') && (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
return -1
}
return r
}, s)
return s
}