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