diff options
author | Saptak S <saptak013@gmail.com> | 2024-03-09 00:13:40 +0530 |
---|---|---|
committer | Saptak S <saptak013@gmail.com> | 2024-03-09 00:13:40 +0530 |
commit | 2ef15395d4a01ec56867c9a3dd60161a193f9380 (patch) | |
tree | 52dc4d2b89348355e34b37a014b96ec4d118f30a | |
parent | ad61786b0f317003586d175d0525094e63a782ca (diff) | |
download | onionshare-2ef15395d4a01ec56867c9a3dd60161a193f9380.tar.gz onionshare-2ef15395d4a01ec56867c9a3dd60161a193f9380.zip |
Allow only ascii characters
-rw-r--r-- | cli/onionshare_cli/web/chat_mode.py | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/cli/onionshare_cli/web/chat_mode.py b/cli/onionshare_cli/web/chat_mode.py index dd151451..02466e2b 100644 --- a/cli/onionshare_cli/web/chat_mode.py +++ b/cli/onionshare_cli/web/chat_mode.py @@ -49,18 +49,27 @@ class ChatModeWeb: self.define_routes() def remove_unallowed_characters(self, text): - allowed_unicode_categories = [ - 'L', # All letters - 'N', # All numbers - ] - allowed_special_characters = [ - '-', # dash - '_', # underscore - ' ', # single space - ] + """ + 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): - 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( ch for ch in text if allowed_character(ch) |