Skip to content

Commit

Permalink
Refactor backend code ensuring to use nacl.encoding.Base64Encoder in …
Browse files Browse the repository at this point in the history
…place of python base64
  • Loading branch information
evilaliv3 committed Jan 12, 2025
1 parent a6afd18 commit 0fc08f3
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 66 deletions.
5 changes: 3 additions & 2 deletions backend/globaleaks/db/migrations/update_54/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: UTF-8
import base64
import os

from nacl.encoding import Base64Encoder

from globaleaks.db.migrations.update import MigrationBase
from globaleaks.handlers.admin.file import special_files
from globaleaks.models import Model
Expand Down Expand Up @@ -31,7 +32,7 @@ def migrate_File(self):
new_obj.id = uuid4()
new_obj.name = old_obj.id

data = base64.b64decode(old_obj.data)
data = Base64Encoder.decode(old_obj.data)

filepath = os.path.join(State.settings.files_path, new_obj.id)
with open(filepath, 'wb') as out_file:
Expand Down
5 changes: 3 additions & 2 deletions backend/globaleaks/db/migrations/update_65/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: UTF-8
import base64
import os
import shutil

from nacl.encoding import Base64Encoder

from globaleaks.db.migrations.update import MigrationBase
from globaleaks.models import Model
from globaleaks.models.enums import _Enum, EnumUserRole
Expand Down Expand Up @@ -248,7 +249,7 @@ def migrate_InternalTip(self):
new_obj.deprecated_crypto_files_pub_key = old_obj.crypto_files_pub_key

if new_obj.crypto_tip_pub_key and new_obj.label:
new_obj.label = base64.b64encode(GCE.asymmetric_encrypt(new_obj.crypto_tip_pub_key, new_obj.label))
new_obj.label = Base64Encoder.encode(GCE.asymmetric_encrypt(new_obj.crypto_tip_pub_key, new_obj.label))

self.session_new.add(new_obj)

Expand Down
6 changes: 3 additions & 3 deletions backend/globaleaks/handlers/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import base64
import json
import mimetypes
import os
Expand All @@ -9,6 +8,7 @@

from tempfile import NamedTemporaryFile

from nacl.encoding import Base64Encoder
from twisted.internet import abstract
from twisted.protocols.basic import FileSender

Expand All @@ -30,7 +30,7 @@


def decodeString(string):
string = base64.b64decode(string)
string = Base64Encoder.decode(string)
uint8_array = [c for c in string]
uint16_array = []
for i in range(len(uint8_array)):
Expand Down Expand Up @@ -87,7 +87,7 @@ def db_confirmation_check(session, tid, user_id, secret):
State.totp_verify(user.two_factor_secret, secret)
else:
if GCE.is_base64_key(secret):
hash = sha512(base64.b64decode(secret.encode())).decode()
hash = sha512(Base64Encoder.decode(secret.encode())).decode()
else:
hash = GCE.hash_password(secret, user.salt)

Expand Down
11 changes: 6 additions & 5 deletions backend/globaleaks/handlers/custodian/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
#
# Handlers dealing with custodian user functionalities
import base64

from nacl.encoding import Base64Encoder

from globaleaks import models
from globaleaks.handlers.admin.context import admin_serialize_context
Expand Down Expand Up @@ -30,13 +31,13 @@ def get_identityaccessrequest_list(session, tid, user_id, user_key):
elem = serializers.serialize_identityaccessrequest(session, iar)

if iarc.crypto_tip_prv_key:
crypto_tip_prv_key = GCE.asymmetric_decrypt(user_key, base64.b64decode(iarc.crypto_tip_prv_key))
crypto_tip_prv_key = GCE.asymmetric_decrypt(user_key, Base64Encoder.decode(iarc.crypto_tip_prv_key))

if elem['request_motivation']:
elem['request_motivation'] = GCE.asymmetric_decrypt(crypto_tip_prv_key, base64.b64decode(elem['request_motivation'])).decode()
elem['request_motivation'] = GCE.asymmetric_decrypt(crypto_tip_prv_key, Base64Encoder.decode(elem['request_motivation'])).decode()

if elem['reply_motivation']:
elem['reply_motivation'] = GCE.asymmetric_decrypt(crypto_tip_prv_key, base64.b64decode(elem['reply_motivation'])).decode()
elem['reply_motivation'] = GCE.asymmetric_decrypt(crypto_tip_prv_key, Base64Encoder.decode(elem['reply_motivation'])).decode()

ret.append(elem)

Expand Down Expand Up @@ -91,7 +92,7 @@ def update_identityaccessrequest(session, tid, user_id, identityaccessrequest_id
models.InternalTip.id == models.IdentityAccessRequest.internaltip_id).one()

if request['reply_motivation'] and itip.crypto_tip_pub_key:
request['reply_motivation'] = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, request['reply_motivation']))
request['reply_motivation'] = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, request['reply_motivation']))

if iar.reply == 'pending':
iar.reply_date = datetime_now()
Expand Down
8 changes: 4 additions & 4 deletions backend/globaleaks/handlers/recipient/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
#
# API handling recipient user functionalities
import base64
import json

from datetime import datetime

from nacl.encoding import Base64Encoder
from sqlalchemy.sql.expression import distinct, func, and_, or_

from globaleaks import models
Expand Down Expand Up @@ -91,12 +91,12 @@ def get_receivertips(session, tid, receiver_id, user_key, language, args={}):
label = itip.label
accessible = rtip.receiver_id == receiver_id
if itip.crypto_tip_pub_key and accessible:
tip_key = GCE.asymmetric_decrypt(user_key, base64.b64decode(rtip.crypto_tip_prv_key))
tip_key = GCE.asymmetric_decrypt(user_key, Base64Encoder.decode(rtip.crypto_tip_prv_key))

if label:
label = GCE.asymmetric_decrypt(tip_key, base64.b64decode(label.encode())).decode()
label = GCE.asymmetric_decrypt(tip_key, Base64Encoder.decode(label.encode())).decode()

answers = json.loads(GCE.asymmetric_decrypt(tip_key, base64.b64decode(answers.encode())).decode())
answers = json.loads(GCE.asymmetric_decrypt(tip_key, Base64Encoder.decode(answers.encode())).decode())
elif itip.crypto_tip_pub_key:
# remove useless and unusable crypted data
answers = ""
Expand Down
3 changes: 2 additions & 1 deletion backend/globaleaks/handlers/recipient/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# API handling export of submissions
import os
from io import BytesIO
from nacl.encoding import Base64Encoder
from twisted.internet.defer import inlineCallbacks
from twisted.internet.threads import deferToThread

Expand All @@ -19,7 +20,7 @@
from globaleaks.orm import transact
from globaleaks.rest import errors
from globaleaks.settings import Settings
from globaleaks.utils.crypto import Base64Encoder, GCE
from globaleaks.utils.crypto import GCE
from globaleaks.utils.fs import directory_traversal_check
from globaleaks.utils.securetempfile import SecureTemporaryFile
from globaleaks.utils.templating import Templating
Expand Down
40 changes: 20 additions & 20 deletions backend/globaleaks/handlers/recipient/rtip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
#
# Handlers dealing with tip interface for receivers (rtip)
import base64
import copy
import json
import os
Expand All @@ -10,6 +9,7 @@

from datetime import datetime, timedelta

from nacl.encoding import Base64Encoder
from twisted.internet.threads import deferToThread
from twisted.internet.defer import inlineCallbacks, returnValue

Expand Down Expand Up @@ -90,14 +90,14 @@ def db_grant_tip_access(session, tid, user_id, user_cc, itip, rtip, receiver_id)

_tip_key = b''
if itip.crypto_tip_pub_key:
_tip_key = GCE.asymmetric_decrypt(user_cc, base64.b64decode(rtip.crypto_tip_prv_key))
_tip_key = GCE.asymmetric_decrypt(user_cc, Base64Encoder.decode(rtip.crypto_tip_prv_key))
_tip_key = GCE.asymmetric_encrypt(new_receiver.crypto_pub_key, _tip_key)

new_rtip = db_create_receivertip(session, new_receiver, itip, _tip_key)
new_rtip.new = False
if itip.deprecated_crypto_files_pub_key:
_files_key = GCE.asymmetric_decrypt(user_cc, base64.b64decode(rtip.deprecated_crypto_files_prv_key))
new_rtip.deprecated_crypto_files_prv_key = base64.b64encode(
_files_key = GCE.asymmetric_decrypt(user_cc, Base64Encoder.decode(rtip.deprecated_crypto_files_prv_key))
new_rtip.deprecated_crypto_files_prv_key = Base64Encoder.encode(
GCE.asymmetric_encrypt(new_receiver.crypto_pub_key, _files_key))

wbfiles = session.query(models.WhistleblowerFile) \
Expand Down Expand Up @@ -431,7 +431,7 @@ def db_redact_comment(session, tid, user_id, itip_id, redaction, redaction_data,
content = redact_content(currentMaskedContent.get('content'), new_permanent_redaction)

comment = session.query(models.Comment).get(redaction_data['reference_id'])
comment.content = base64.b64encode(GCE.asymmetric_encrypt(itip_id.crypto_tip_pub_key, content)).decode()
comment.content = Base64Encoder.encode(GCE.asymmetric_encrypt(itip_id.crypto_tip_pub_key, content)).decode()


def db_redact_answers(answers, redaction):
Expand Down Expand Up @@ -484,7 +484,7 @@ def db_redact_answers_recursively(session, tid, user_id, itip_id, redaction, red
_content = answers

if itip_id.crypto_tip_pub_key:
_content = base64.b64encode(
_content = Base64Encoder.encode(
GCE.asymmetric_encrypt(itip_id.crypto_tip_pub_key, json.dumps(_content, cls=JSONEncoder).encode())).decode()

itip_answers = session.query(models.InternalTipAnswers) \
Expand Down Expand Up @@ -514,7 +514,7 @@ def db_redact_whistleblower_identity(session, tid, user_id, itip_id, redaction,

_content = whistleblower_identity
if itip_id.crypto_tip_pub_key:
_content = base64.b64encode(
_content = Base64Encoder.encode(
GCE.asymmetric_encrypt(itip_id.crypto_tip_pub_key, json.dumps(_content, cls=JSONEncoder).encode())).decode()

itip_whistleblower_identity = session.query(models.InternalTipData) \
Expand Down Expand Up @@ -617,7 +617,7 @@ def register_rfile_on_db(session, tid, user_id, itip_id, uploaded_file):
for k in ['name', 'description', 'type', 'size']:
if k == 'size':
uploaded_file[k] = str(uploaded_file[k])
uploaded_file[k] = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, uploaded_file[k]))
uploaded_file[k] = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, uploaded_file[k]))

new_file = models.ReceiverFile()
new_file.id = uploaded_file['filename']
Expand Down Expand Up @@ -660,7 +660,7 @@ def db_get_rtip(session, tid, user_id, itip_id, language):

db_log(session, tid=tid, type='access_report', user_id=user_id, object_id=itip.id)

return serializers.serialize_rtip(session, itip, rtip, language), base64.b64decode(rtip.crypto_tip_prv_key)
return serializers.serialize_rtip(session, itip, rtip, language), Base64Encoder.decode(rtip.crypto_tip_prv_key)


@transact
Expand Down Expand Up @@ -876,7 +876,7 @@ def set_internaltip_variable(session, tid, user_id, itip_id, key, value):
_, _, itip = db_access_rtip(session, tid, user_id, itip_id)

if itip.crypto_tip_pub_key and value and key in ['label']:
value = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, value))
value = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, value))

setattr(itip, key, value)

Expand Down Expand Up @@ -947,12 +947,12 @@ def create_identityaccessrequest(session, tid, user_id, user_cc, itip_id, reques
"""
user, rtip, itip = db_access_rtip(session, tid, user_id, itip_id)

crypto_tip_prv_key = GCE.asymmetric_decrypt(user_cc, base64.b64decode(rtip.crypto_tip_prv_key))
crypto_tip_prv_key = GCE.asymmetric_decrypt(user_cc, Base64Encoder.decode(rtip.crypto_tip_prv_key))

iar = models.IdentityAccessRequest()
iar.internaltip_id = itip.id
iar.request_user_id = user.id
iar.request_motivation = base64.b64encode(
iar.request_motivation = Base64Encoder.encode(
GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, request['request_motivation']))
session.add(iar)
session.flush()
Expand All @@ -962,7 +962,7 @@ def create_identityaccessrequest(session, tid, user_id, user_cc, itip_id, reques
iarc = models.IdentityAccessRequestCustodian()
iarc.identityaccessrequest_id = iar.id
iarc.custodian_id = custodian.id
iarc.crypto_tip_prv_key = base64.b64encode(GCE.asymmetric_encrypt(custodian.crypto_pub_key, crypto_tip_prv_key))
iarc.crypto_tip_prv_key = Base64Encoder.encode(GCE.asymmetric_encrypt(custodian.crypto_pub_key, crypto_tip_prv_key))
session.add(iarc)
custodians += 1

Expand Down Expand Up @@ -996,7 +996,7 @@ def create_comment(session, tid, user_id, itip_id, content, visibility='public')

_content = content
if itip.crypto_tip_pub_key:
_content = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, content)).decode()
_content = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, content)).decode()

comment = models.Comment()
comment.internaltip_id = itip.id
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def create_redaction(session, tid, user_id, data):
else:
content_str = data.get('content', str(data))
content_bytes = content_str.encode()
mask_content = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, content_bytes)).decode()
mask_content = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, content_bytes)).decode()

redaction = models.Redaction()
redaction.id = data.get('id')
Expand Down Expand Up @@ -1257,8 +1257,8 @@ def get(self, wbfile_id):
self.check_file_presence(filelocation)

if tip_prv_key:
tip_prv_key = GCE.asymmetric_decrypt(self.session.cc, base64.b64decode(tip_prv_key))
name = GCE.asymmetric_decrypt(tip_prv_key, base64.b64decode(name.encode())).decode()
tip_prv_key = GCE.asymmetric_decrypt(self.session.cc, Base64Encoder.decode(tip_prv_key))
name = GCE.asymmetric_decrypt(tip_prv_key, Base64Encoder.decode(name.encode())).decode()

try:
# First attempt
Expand All @@ -1268,7 +1268,7 @@ def get(self, wbfile_id):
if not tip_prv_key2:
raise

files_prv_key2 = GCE.asymmetric_decrypt(self.session.cc, base64.b64decode(tip_prv_key2))
files_prv_key2 = GCE.asymmetric_decrypt(self.session.cc, Base64Encoder.decode(tip_prv_key2))
filelocation = GCE.streaming_encryption_open('DECRYPT', files_prv_key2, filelocation)

yield self.write_file_as_download(name, filelocation, pgp_key)
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def download_rfile(self, session, tid, user_id, file_id):
except:
raise errors.ResourceNotFound
else:
return rfile.name, rfile.id, base64.b64decode(rtip.crypto_tip_prv_key), pgp_key
return rfile.name, rfile.id, Base64Encoder.decode(rtip.crypto_tip_prv_key), pgp_key

@inlineCallbacks
def get(self, rfile_id):
Expand All @@ -1324,7 +1324,7 @@ def get(self, rfile_id):

if tip_prv_key:
tip_prv_key = GCE.asymmetric_decrypt(self.session.cc, tip_prv_key)
name = GCE.asymmetric_decrypt(tip_prv_key, base64.b64decode(name.encode())).decode()
name = GCE.asymmetric_decrypt(tip_prv_key, Base64Encoder.decode(name.encode())).decode()
filelocation = GCE.streaming_encryption_open('DECRYPT', tip_prv_key, filelocation)

yield self.write_file_as_download(name, filelocation, pgp_key)
Expand Down
5 changes: 3 additions & 2 deletions backend/globaleaks/handlers/whistleblower/attachment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
#
# Handler dealing with submissions file uploads and subsequent submissions attachments
import base64

from nacl.encoding import Base64Encoder

from globaleaks import models
from globaleaks.handlers.base import BaseHandler
Expand Down Expand Up @@ -34,7 +35,7 @@ def register_ifile_on_db(session, tid, internaltip_id, uploaded_file):

if itip.crypto_tip_pub_key:
for k in ['name', 'type', 'size']:
uploaded_file[k] = base64.b64encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, str(uploaded_file[k])))
uploaded_file[k] = Base64Encoder.encode(GCE.asymmetric_encrypt(itip.crypto_tip_pub_key, str(uploaded_file[k])))

new_file = models.InternalFile()
new_file.id = uploaded_file['filename']
Expand Down
Loading

0 comments on commit 0fc08f3

Please sign in to comment.