Allow only ascii characters

This commit is contained in:
Saptak S 2024-03-09 00:13:40 +05:30
parent ad61786b0f
commit 2ef15395d4
No known key found for this signature in database
GPG Key ID: 7B7F1772C0C6FCBF

View File

@ -49,18 +49,27 @@ class ChatModeWeb:
self.define_routes() self.define_routes()
def remove_unallowed_characters(self, text): def remove_unallowed_characters(self, text):
allowed_unicode_categories = [ """
'L', # All letters Sanitize username to remove unwanted characters.
'N', # All numbers Allowed characters right now are:
] - all ASCII numbers
allowed_special_characters = [ - all ASCII letters
'-', # dash - dash, underscore and single space
'_', # underscore """
' ', # single space
]
def allowed_character(ch): def allowed_character(ch):
return unicodedata.category(ch)[0] in allowed_unicode_categories or ch in allowed_special_characters allowed_unicode_categories = [
'L', # All letters
'N', # All numbers
]
allowed_special_characters = [
'-', # dash
'_', # underscore
' ', # single space
]
return (
unicodedata.category(ch)[0] in allowed_unicode_categories and ord(ch) < 128
) or ch in allowed_special_characters
return "".join( return "".join(
ch for ch in text if allowed_character(ch) ch for ch in text if allowed_character(ch)