Skip to content

Commit 2edbadd

Browse files
committed
Warn instead of raise on decryption errors
Should fix updater hanging in these cases
1 parent e2ff3d6 commit 2edbadd

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

telegram/passport/credentials.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
except ImportError:
3535
CRYPTO = False
3636

37-
from telegram import TelegramObject, TelegramError
37+
from telegram import TelegramObject
3838

3939

40-
class TelegramDecryptionError(TelegramError):
40+
class _TelegramDecryptionError(Exception):
4141
pass
4242

4343

@@ -84,7 +84,7 @@ def decrypt(secret, hash, data):
8484
digest.update(data)
8585
data_hash = digest.finalize()
8686
if data_hash != hash:
87-
raise TelegramDecryptionError("Hashes are not equal! {} != {}".format(data_hash, hash))
87+
raise _TelegramDecryptionError("Hashes are not equal! {} != {}".format(data_hash, hash))
8888
return data[bord(data[0]):]
8989

9090

@@ -139,11 +139,15 @@ def de_json(cls, data, bot):
139139
if isinstance(data['data'], dict):
140140
data['data'] = Credentials.de_json(data['data'], bot=bot)
141141
else:
142-
data['secret'] = bot.private_key.decrypt(b64decode(data.get('secret')), OAEP(
143-
mgf=MGF1(algorithm=SHA1()),
144-
algorithm=SHA1(),
145-
label=None
146-
))
142+
try:
143+
data['secret'] = bot.private_key.decrypt(b64decode(data.get('secret')), OAEP(
144+
mgf=MGF1(algorithm=SHA1()),
145+
algorithm=SHA1(),
146+
label=None
147+
))
148+
except ValueError as e:
149+
raise _TelegramDecryptionError(e)
150+
147151
data['data'] = Credentials.de_json(decrypt_json(data.get('secret'),
148152
data.get('hash'),
149153
data.get('data')),
@@ -158,6 +162,7 @@ class Credentials(TelegramObject):
158162
secure_data (:class:`telegram.SecureData`): Credentials for encrypted data
159163
payload (:obj:`str`): Bot-specified payload
160164
"""
165+
161166
def __init__(self, secure_data, payload, bot=None, **kwargs):
162167
# Required
163168
self.secure_data = secure_data
@@ -202,6 +207,7 @@ class SecureData(TelegramObject):
202207
temporary_registration (:class:`telegram.SecureValue`, optional): Credentials for encrypted
203208
temporary registration
204209
"""
210+
205211
def __init__(self,
206212
personal_details=None,
207213
passport=None,
@@ -315,6 +321,7 @@ def to_dict(self):
315321

316322
class _CredentialsBase(TelegramObject):
317323
"""Base class for DataCredentials and FileCredentials."""
324+
318325
def __init__(self, hash, secret, bot=None, **kwargs):
319326
self.hash = hash
320327
self.secret = secret
@@ -357,6 +364,7 @@ class DataCredentials(_CredentialsBase):
357364
hash (:obj:`str`): Checksum of encrypted data
358365
secret (:obj:`str`): Secret of encrypted data
359366
"""
367+
360368
def __init__(self, data_hash, secret, **kwargs):
361369
super(DataCredentials, self).__init__(data_hash, secret, **kwargs)
362370

@@ -382,6 +390,7 @@ class FileCredentials(_CredentialsBase):
382390
hash (:obj:`str`): Checksum of encrypted file
383391
secret (:obj:`str`): Secret of encrypted file
384392
"""
393+
385394
def __init__(self, file_hash, secret, **kwargs):
386395
super(FileCredentials, self).__init__(file_hash, secret, **kwargs)
387396

telegram/passport/passportdata.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
# You should have received a copy of the GNU Lesser Public License
1818
# along with this program. If not, see [http://www.gnu.org/licenses/].
1919
"""Contains information about Telegram Passport data shared with the bot by the user."""
20+
import warnings
2021

2122
from telegram import EncryptedCredentials, EncryptedPassportElement, TelegramObject
23+
from telegram.passport.credentials import _TelegramDecryptionError
2224

2325

2426
class PassportData(TelegramObject):
@@ -54,11 +56,20 @@ def de_json(cls, data, bot):
5456
if not data:
5557
return None
5658

57-
data = super(PassportData, cls).de_json(data, bot)
58-
credentials = data['credentials'] = EncryptedCredentials.de_json(data.get('credentials'),
59-
bot)
60-
data['data'] = EncryptedPassportElement.de_list(data.get('data'), bot,
61-
credentials=credentials)
59+
if not hasattr(bot, 'private_key'):
60+
warnings.warn('Received update with PassportData but no private key is specified! '
61+
'See https://git.io/fAvYd for more info.')
62+
return None
63+
64+
try:
65+
data = super(PassportData, cls).de_json(data, bot)
66+
data['credentials'] = EncryptedCredentials.de_json(data.get('credentials'), bot)
67+
data['data'] = EncryptedPassportElement.de_list(data.get('data'), bot,
68+
credentials=data['credentials'])
69+
except _TelegramDecryptionError as e:
70+
warnings.warn('Telegram passport decryption error: {} '
71+
'See https://git.io/fAvYd for more info.'.format(e))
72+
return None
6273

6374
return cls(bot=bot, **data)
6475

0 commit comments

Comments
 (0)