Skip to content

Commit a24722a

Browse files
Fix save_value issue
1 parent efac086 commit a24722a

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

async_include/crypto.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
from Crypto.Cipher import AES
66

77

8-
def encrypt(key, data):
8+
def encrypt(key: str, text: str):
99
cipher = AES.new(key.encode('utf-8'), AES.MODE_EAX)
10-
encrypted_data, tag = cipher.encrypt_and_digest(data.encode('utf-8'))
10+
encrypted_data, tag = cipher.encrypt_and_digest(text.encode('utf-8'))
1111
return cipher.nonce, encrypted_data, tag
1212

1313

14-
def decrypt(key, nonce, encrypted_data, tag):
14+
def decrypt(key: str, nonce: str, encrypted_data: str, tag: str):
1515
cipher = AES.new(
16-
key.encode('utf-8'), AES.MODE_EAX, nonce.encode('latin-1')
16+
key.encode('utf-8'), AES.MODE_EAX, nonce.encode('utf-8')
1717
)
1818
data = cipher.decrypt_and_verify(
19-
encrypted_data.encode('latin-1'), tag.encode('latin-1')
19+
encrypted_data.encode('utf-8'), tag.encode('utf-8')
2020
)
2121
return data.decode('utf-8')

async_include/templatetags/async_include.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def async_include(context, template_path, *args, **kwargs):
126126
sql_query, params = context_object.query.sql_with_params()
127127

128128
nonce, encrypted_sql, tag = crypto.encrypt(
129-
key=settings.SECRET_KEY[:16], data=sql_query
129+
key=settings.SECRET_KEY[:16], text=sql_query
130130
)
131131

132132
replacements['context'][context_object_name] = {
@@ -142,7 +142,10 @@ def async_include(context, template_path, *args, **kwargs):
142142
# Safe values are sent as is to the view
143143
# that will render the template
144144
else:
145-
context_object_as_str = '{0}'.format(context_object)
145+
if isinstance(context_object, str):
146+
context_object_as_str = context_object
147+
else:
148+
context_object_as_str = '{0}'.format(context_object)
146149
replacements['context'][context_object_name] = {
147150
'type': 'safe_value',
148151
'value': context_object,

async_include/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def get_template(request):
101101
# Checking if JSON has been tampered
102102
if (
103103
context_object_load_params['__checksum__'] !=
104-
checksum.make(value_as_str)
104+
checksum.make(value)
105105
):
106106
return HttpResponse(
107107
status=403,

0 commit comments

Comments
 (0)