From f4f6f22578658517c06b393ba1ce2abec6cc0caf Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Wed, 23 Apr 2025 01:51:44 -0400 Subject: [PATCH 1/8] Refactor name validation: remove numeric check and simplify character validation - Removed the redundant numeric check as it is already handled by the character validation. - Simplified the name validation logic by keeping only the `checkValidCharacter()` function for validating characters. - The `checkValidCharacter()` function checks for allowed characters: Latin, Greek, Cyrillic, Hebrew, Arabic, and CJK. - Updated `checkNameFormat()` to rely solely on the new character validation logic. --- [core]/esx_identity/server/main.lua | 31 +++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/[core]/esx_identity/server/main.lua b/[core]/esx_identity/server/main.lua index d0461b92..9d00a43d 100644 --- a/[core]/esx_identity/server/main.lua +++ b/[core]/esx_identity/server/main.lua @@ -84,16 +84,35 @@ local function formatDate(str) return date end -local function checkAlphanumeric(str) - return (string.match(str, "%W")) -end +local function checkValidCharacter(str) + for _, code in utf8.codes(str) do -local function checkForNumbers(str) - return (string.match(str, "%d")) + local isBasicLatin = (code >= 0x0041 and code <= 0x005A) or (code >= 0x0061 and code <= 0x007A) + local isSpaceOrDash = (code == 0x0020 or code == 0x002D) + local isLatinExtended = (code >= 0x00C0 and code <= 0x02AF) + local isGreek = (code >= 0x0370 and code <= 0x03FF) + local isCyrillic = (code >= 0x0400 and code <= 0x04FF) + local isHebrew = (code >= 0x05D0 and code <= 0x05EA) + local isArabic = + (code >= 0x0620 and code <= 0x063F) or + (code >= 0x0641 and code <= 0x064A) or + (code >= 0x066E and code <= 0x066F) or + (code >= 0x0671 and code <= 0x06D3) or + (code == 0x06D5) or + (code >= 0x0750 and code <= 0x077F) or + (code >= 0x08A0 and code <= 0x08BD) + local isCJK = (code >= 0x4E00 and code <= 0x9FFF) + + if not (isBasicLatin or isSpaceOrDash or isLatinExtended or isGreek or isCyrillic or isHebrew or isArabic or isCJK) then + return false + end + end + + return true end local function checkNameFormat(name) - if not checkAlphanumeric(name) and not checkForNumbers(name) then + if checkValidCharacter(name) then local stringLength = string.len(name) return stringLength > 0 and stringLength < Config.MaxNameLength end From 77be031e8239b4612f166b1b97b0329ecdc03bdb Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Mon, 28 Apr 2025 17:19:49 -0400 Subject: [PATCH 2/8] Add early returns --- [core]/esx_identity/server/main.lua | 38 +++++++++++++++-------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/[core]/esx_identity/server/main.lua b/[core]/esx_identity/server/main.lua index 9d00a43d..5dcc4d7a 100644 --- a/[core]/esx_identity/server/main.lua +++ b/[core]/esx_identity/server/main.lua @@ -86,28 +86,30 @@ end local function checkValidCharacter(str) for _, code in utf8.codes(str) do + if not ( - local isBasicLatin = (code >= 0x0041 and code <= 0x005A) or (code >= 0x0061 and code <= 0x007A) - local isSpaceOrDash = (code == 0x0020 or code == 0x002D) - local isLatinExtended = (code >= 0x00C0 and code <= 0x02AF) - local isGreek = (code >= 0x0370 and code <= 0x03FF) - local isCyrillic = (code >= 0x0400 and code <= 0x04FF) - local isHebrew = (code >= 0x05D0 and code <= 0x05EA) - local isArabic = - (code >= 0x0620 and code <= 0x063F) or - (code >= 0x0641 and code <= 0x064A) or - (code >= 0x066E and code <= 0x066F) or - (code >= 0x0671 and code <= 0x06D3) or - (code == 0x06D5) or - (code >= 0x0750 and code <= 0x077F) or - (code >= 0x08A0 and code <= 0x08BD) - local isCJK = (code >= 0x4E00 and code <= 0x9FFF) - - if not (isBasicLatin or isSpaceOrDash or isLatinExtended or isGreek or isCyrillic or isHebrew or isArabic or isCJK) then + (code >= 0x0041 and code <= 0x005A) or -- Basic Latin uppercase + (code >= 0x0061 and code <= 0x007A) or -- Basic Latin lowercase + (code == 0x0020 or code == 0x002D) or -- Space or dash + (code >= 0x00C0 and code <= 0x02AF) or -- Latin Extended + (code >= 0x0370 and code <= 0x03FF) or -- Greek + (code >= 0x0400 and code <= 0x04FF) or -- Cyrillic + (code >= 0x05D0 and code <= 0x05EA) or -- Hebrew letters + ( -- Arabic + (code >= 0x0620 and code <= 0x063F) or + (code >= 0x0641 and code <= 0x064A) or + (code >= 0x066E and code <= 0x066F) or + (code >= 0x0671 and code <= 0x06D3) or + (code == 0x06D5) or + (code >= 0x0750 and code <= 0x077F) or + (code >= 0x08A0 and code <= 0x08BD) + ) or + (code >= 0x4E00 and code <= 0x9FFF) -- CJK + ) + then return false end end - return true end From 8f07d854eb677f5f1376e90090d3e51bbcfddd5d Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Wed, 7 May 2025 12:23:25 -0400 Subject: [PATCH 3/8] refactor(main.lua): use ESX.IsValidLocaleString with configurable character sets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaced the manual character validation logic in checkNameFormat with ESX.IsValidLocaleString to centralize string validation and allow configuration of valid character sets per locale. Added support for Config.ValidCharacterSets to enable or disable specific Unicode blocks such as Greek, Cyrillic, Hebrew, Arabic, or CJK (Chinese, Japanese, Korean). This change introduces greater flexibility for multilingual servers while maintaining basic Latin character support by default. ⚠️ Behavior changes: Characters that were previously allowed may now be rejected depending on the server’s locale configuration. --- [core]/esx_identity/server/main.lua | 31 +---------------------------- 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/[core]/esx_identity/server/main.lua b/[core]/esx_identity/server/main.lua index 5dcc4d7a..9ee283da 100644 --- a/[core]/esx_identity/server/main.lua +++ b/[core]/esx_identity/server/main.lua @@ -84,37 +84,8 @@ local function formatDate(str) return date end -local function checkValidCharacter(str) - for _, code in utf8.codes(str) do - if not ( - - (code >= 0x0041 and code <= 0x005A) or -- Basic Latin uppercase - (code >= 0x0061 and code <= 0x007A) or -- Basic Latin lowercase - (code == 0x0020 or code == 0x002D) or -- Space or dash - (code >= 0x00C0 and code <= 0x02AF) or -- Latin Extended - (code >= 0x0370 and code <= 0x03FF) or -- Greek - (code >= 0x0400 and code <= 0x04FF) or -- Cyrillic - (code >= 0x05D0 and code <= 0x05EA) or -- Hebrew letters - ( -- Arabic - (code >= 0x0620 and code <= 0x063F) or - (code >= 0x0641 and code <= 0x064A) or - (code >= 0x066E and code <= 0x066F) or - (code >= 0x0671 and code <= 0x06D3) or - (code == 0x06D5) or - (code >= 0x0750 and code <= 0x077F) or - (code >= 0x08A0 and code <= 0x08BD) - ) or - (code >= 0x4E00 and code <= 0x9FFF) -- CJK - ) - then - return false - end - end - return true -end - local function checkNameFormat(name) - if checkValidCharacter(name) then + if ESX.IsValidLocaleString(name) then local stringLength = string.len(name) return stringLength > 0 and stringLength < Config.MaxNameLength end From e9fe5babdb783b997bda7ab8689000d412b10909 Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Wed, 7 May 2025 12:31:14 -0400 Subject: [PATCH 4/8] Refactor: Add ESX.IsValidLocaleString function to validate locale-based characters This commit introduces the new `ESX.IsValidLocaleString` function, which checks if a given string contains valid characters based on the configured locale. It validates characters from various locales, such as Greek, Cyrillic, Hebrew, Arabic, and Chinese, in addition to basic Latin characters. The function is flexible, allowing for the inclusion of additional character sets via the `Config.ValidCharacterSets` configuration. Changes include: - A new function, `ESX.IsValidLocaleString`, added to validate characters based on the locale. - The validation takes into account default Latin ranges as well as specific ranges for supported languages. - The function supports dynamic locale configurations by using `Config.ValidCharacterSets`. This refactor improves handling of locale-specific characters and provides a way to easily extend validation for new locales as needed. --- [core]/es_extended/shared/functions.lua | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 184def1b..c24ead38 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -210,3 +210,72 @@ function ESX.Await(conditionFunc, errorMessage, timeoutMs) return false end + +---@param str string +---@return boolean +function ESX.IsValidLocaleString(str) + local locale = string.lower(Config.Locale) + + local defaultRanges ={ + {0x0041, 0x005A}, -- Basic Latin uppercase + {0x0061, 0x007A}, -- Basic Latin lowercase + {0x0020, 0x0020}, -- Space + {0x002D, 0x002D}, -- Dash + {0x00C0, 0x02AF} -- Latin Extended + } + + local localeRanges = { + ["el"] = { {0x0370, 0x03FF} }, -- Greek + ["sr"] ={ {0x0400, 0x04FF} }, -- Cyrillic + ["he"] ={ {0x05D0, 0x05EA} }, -- Hebrew letters + ["ar"] = { + {0x0620, 0x063F}, -- Arabic + {0x0641, 0x064A}, + {0x066E, 0x066F}, + {0x0671, 0x06D3}, + {0x06D5, 0x06D5}, + {0x0750, 0x077F}, + {0x08A0, 0x08BD} + }, + ["zh-cn"] ={ {0x4E00, 0x9FFF} } -- CJK + } + + local validRanges = {} + + for i = 1, #defaultRanges do + validRanges[#validRanges + 1] = defaultRanges[i] + end + + if localeRanges[locale] then + for i = 1, #localeRanges[locale] do + validRanges[#validRanges + 1] = localeRanges[locale][i] + end + end + + if Config.ValidCharacterSets then + for charset, enabled in pairs(Config.ValidCharacterSets) do + if enabled and charset ~= locale and localeRanges[charset] then + for i = 1, #localeRanges[charset] do + validRanges[#validRanges + 1] = localeRanges[charset][i] + end + end + end + end + + for _, code in utf8.codes(str) do + local isValid = false + + for _, range in ipairs(validRanges) do + if code >= range[1] and code <= range[2] then + isValid = true + break + end + end + + if not isValid then + return false + end + end + + return true +end From e30d4e49f5279fc1f6ba5bf3b5cabdd711a19105 Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Wed, 7 May 2025 12:34:42 -0400 Subject: [PATCH 5/8] Added support for additional character sets and language customization This update introduces a new configuration option `Config.ValidCharacterSets` to allow support for additional character sets in case the server is multilingual. This allows the server to handle character sets such as Greek, Cyrillic, Hebrew, Arabic, and East Asian languages (Chinese, Japanese, Korean). By default, these character sets are disabled (`false`). The user can enable them if needed, depending on the language requirements of the server. --- [core]/es_extended/shared/config/main.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/[core]/es_extended/shared/config/main.lua b/[core]/es_extended/shared/config/main.lua index 108d292d..8932e30c 100644 --- a/[core]/es_extended/shared/config/main.lua +++ b/[core]/es_extended/shared/config/main.lua @@ -39,6 +39,14 @@ Config.AdminGroups = { ["admin"] = true, } +Config.ValidCharacterSets = { -- Only enable additional charsets if your server is multilingual. By default everything is false. + ['el'] = false, -- Greek + ['sr'] = false, -- Cyrillic + ['he'] = false, -- Hebrew + ['ar'] = false, -- Arabic + ['zh-cn'] = false -- Chinese, Japanese, Korean +} + Config.EnablePaycheck = true -- enable paycheck Config.LogPaycheck = false -- Logs paychecks to a nominated Discord channel via webhook (default is false) Config.EnableSocietyPayouts = false -- pay from the society account that the player is employed at? Requirement: esx_society From 01e29f6e9b28310dd151caf5694016995e3e7d67 Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Thu, 8 May 2025 10:33:03 -0400 Subject: [PATCH 6/8] refactor(locale): improve performance and type safety in IsValidLocaleString - Replaced `ipairs` with a numeric `for` loop in the UTF-8 validation step for better performance. - Stored `validRanges[i]` in a local `range` variable to avoid duplicate table lookups. - Added a call to `ESX.ValidateType(str, 'string')` to ensure input is of type string before processing. These changes improve both performance and code safety. --- [core]/es_extended/shared/functions.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index c24ead38..7da3e887 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -214,6 +214,10 @@ end ---@param str string ---@return boolean function ESX.IsValidLocaleString(str) + if not ESX.ValidateType(str, 'string') then + return false + end + local locale = string.lower(Config.Locale) local defaultRanges ={ @@ -265,7 +269,8 @@ function ESX.IsValidLocaleString(str) for _, code in utf8.codes(str) do local isValid = false - for _, range in ipairs(validRanges) do + for i = 1, #validRanges do + local range = validRanges[i] if code >= range[1] and code <= range[2] then isValid = true break From 099bd7dd4645e972edf662f7df54314459219959 Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Thu, 8 May 2025 13:03:40 -0400 Subject: [PATCH 7/8] refactor(functions.lua): use table.unpack to initialize validRanges Replaces manual loop for copying `defaultRanges` into `validRanges` with `table.unpack`. Avoids unnecessary iteration and duplication. --- [core]/es_extended/shared/functions.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 7da3e887..32c7ea33 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -244,11 +244,7 @@ function ESX.IsValidLocaleString(str) ["zh-cn"] ={ {0x4E00, 0x9FFF} } -- CJK } - local validRanges = {} - - for i = 1, #defaultRanges do - validRanges[#validRanges + 1] = defaultRanges[i] - end + local validRanges = { table.unpack(defaultRanges) } if localeRanges[locale] then for i = 1, #localeRanges[locale] do From dc512f97d29da782449dc1d4d88e6d4fbdf8a4b2 Mon Sep 17 00:00:00 2001 From: YOMAN1792 <94007829+YOMAN1792@users.noreply.github.com> Date: Thu, 8 May 2025 14:18:57 -0400 Subject: [PATCH 8/8] feat(functions.lua): add optional digit support to IsValidLocaleString Modified the ESX.IsValidLocaleString function to accept an optional `allowDigits` parameter. When true, the function now allows numerical characters (0-9) in addition to the configured character ranges. This improves string validation flexibility across locales. --- [core]/es_extended/shared/functions.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 32c7ea33..8e326150 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -212,8 +212,9 @@ function ESX.Await(conditionFunc, errorMessage, timeoutMs) end ---@param str string +---@param allowDigits boolean? Allow numbers if necessary ---@return boolean -function ESX.IsValidLocaleString(str) +function ESX.IsValidLocaleString(str, allowDigits) if not ESX.ValidateType(str, 'string') then return false end @@ -228,6 +229,10 @@ function ESX.IsValidLocaleString(str) {0x00C0, 0x02AF} -- Latin Extended } + if allowDigits then + defaultRanges[#defaultRanges + 1] = {0x0030, 0x0039} -- 0-9 Numbers + end + local localeRanges = { ["el"] = { {0x0370, 0x03FF} }, -- Greek ["sr"] ={ {0x0400, 0x04FF} }, -- Cyrillic