Skip to content

Commit 0893648

Browse files
authored
Merge pull request #32 from q-verse/umar/qverse/blocl_all_invalid_file_encoding_formats
EDE-559 Block invalid file encoding formats
2 parents 92e40c7 + df743a6 commit 0893648

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

openedx/features/qverse_features/registration/helpers.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ def get_file_encoding(file_path):
4242
file = io.open(file_path, 'r', encoding='utf-8')
4343
encoding = None
4444
try:
45-
encoding = 'utf-8'
4645
_ = file.read()
46+
encoding = 'utf-8'
4747
except UnicodeDecodeError:
48-
encoding = 'utf-16'
48+
file.close()
49+
file = io.open(file_path, 'r', encoding='utf-16')
50+
try:
51+
_ = file.read()
52+
encoding = 'utf-16'
53+
except UnicodeDecodeError:
54+
LOGGER.exception('The file encoding format must be utf-8 or utf-16.')
4955

5056
file.close()
5157
return encoding

openedx/features/qverse_features/registration/signals.py

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def create_users_from_csv_file(sender, instance, created, **kwargs):
5454
dialect = None
5555
try:
5656
encoding = get_file_encoding(instance.admission_file.path)
57+
if not encoding:
58+
LOGGER.exception('Because of invlid file encoding format, user creation process is aborted.')
59+
return
60+
5761
csv_file = io.open(instance.admission_file.path, 'r', encoding=encoding)
5862
try:
5963
dialect = Sniffer().sniff(csv_file.readline())

0 commit comments

Comments
 (0)