From 494b570f3a23e2c7efec62bce74a2e5dcce946ec Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Tue, 4 Nov 2025 10:02:18 +0100 Subject: [PATCH] [MM-63607] Add length validation for LDAP user fields (#30596) --- server/public/model/utils.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/server/public/model/utils.go b/server/public/model/utils.go index ff86ff5bf6c..e9b2e7f89de 100644 --- a/server/public/model/utils.go +++ b/server/public/model/utils.go @@ -876,3 +876,23 @@ func SliceToMapKey(s ...string) map[string]any { return m } + +// LimitRunes limits the number of runes in a string to the given maximum. +// It returns the potentially truncated string and a boolean indicating whether truncation occurred. +func LimitRunes(s string, maxRunes int) (string, bool) { + runes := []rune(s) + if len(runes) > maxRunes { + return string(runes[:maxRunes]), true + } + + return s, false +} + +// LimitBytes limits the number of bytes in a string to the given maximum. +// It returns the potentially truncated string and a boolean indicating whether truncation occurred. +func LimitBytes(s string, maxBytes int) (string, bool) { + if len(s) > maxBytes { + return s[:maxBytes], true + } + return s, false +}