@@ -581,6 +581,12 @@ def api_get_header():
581581 resp += f'\n { msg_of_the_day } '
582582 return ok_response (resp )
583583
584+ class SignatureUnwrappingError (Exception ):
585+
586+ def __init__ (self , user_error_message : str ):
587+ # Message without any sensitive data that can be presented to the user.
588+ self .user_error_message = user_error_message
589+ super ().__init__ (self , user_error_message )
584590
585591def _unwrap_signed_container_request (request : Request , max_age_s : int = 60 ) -> ty .Any :
586592 """
@@ -596,46 +602,46 @@ def _unwrap_signed_container_request(request: Request, max_age_s: int = 60) -> t
596602 content = request .get_json (force = True , silent = True )
597603 if not content :
598604 log .warning ('Got request without JSON body' )
599- raise Exception ('Request is missing JSON body' )
605+ raise SignatureUnwrappingError ('Request is missing JSON body' )
600606
601607 if not isinstance (content , str ):
602608 log .warning (f'Invalid type { type (content )} ' )
603- raise Exception ('Invalid request' )
609+ raise SignatureUnwrappingError ('Invalid request' )
604610
605611 s = TimedSerializer (b"" , salt = 'from-container-to-web' )
606612 try :
607613 _ , unsafe_content = s .loads_unsafe (content )
608614 except :
609615 log .warning (f'Failed to decode payload' , exc_info = True )
610- raise Exception ('Error during decoding' )
616+ raise SignatureUnwrappingError ('Error during decoding' )
611617
612618 #This instance ID (['instance_id']) is just used to calculate the signature (['data']),
613619 #thus we do not have to iterate over all instance. After checking the signature,
614620 #this id must be compared to signed one (['data']['instance_id']).
615621 instance_id = unsafe_content .get ('instance_id' )
616622 if instance_id is None :
617623 log .warning ('Missing instance_id' )
618- raise Exception ('Missing instance_id' )
624+ raise SignatureUnwrappingError ('Missing instance_id' )
619625
620626 try :
621627 instance_id = int (instance_id )
622628 except :
623629 log .warning (f'Failed to convert { instance_id } to int' , exc_info = True )
624- raise Exception ('Invalid instance ID' )
630+ raise SignatureUnwrappingError ('Invalid instance ID' )
625631
626632 instance = Instance .query .filter (Instance .id == instance_id ).one_or_none ()
627633 if not instance :
628634 log .warning (f'Failed to find instance with ID { instance_id } ' )
629- raise Exception ("Unable to find given instance" )
635+ raise SignatureUnwrappingError ("Unable to find given instance" )
630636
631637 instance_key = instance .get_key ()
632638
633639 s = TimedSerializer (instance_key , salt = 'from-container-to-web' )
634640 try :
635641 signed_content = s .loads (content , max_age = max_age_s )
636- except Exception as e :
642+ except SignatureUnwrappingError as e :
637643 log .warning (f'Invalid request' , exc_info = True )
638- raise Exception ('Invalid request' )
644+ raise SignatureUnwrappingError ('Invalid request' )
639645
640646 return signed_content
641647
@@ -652,8 +658,8 @@ def api_instance_reset():
652658 """
653659 try :
654660 content = _unwrap_signed_container_request (request )
655- except Exception as e :
656- return error_response (str ( e ) )
661+ except SignatureUnwrappingError as e :
662+ return error_response (e . user_error_message )
657663
658664 instance_id = content .get ('instance_id' )
659665 try :
@@ -699,8 +705,8 @@ def api_instance_submit():
699705 """
700706 try :
701707 content : ty .Dict [str , ty .Any ] = _unwrap_signed_container_request (request )
702- except Exception as e :
703- return error_response (str ( e ) )
708+ except SignatureUnwrappingError as e :
709+ return error_response (e . user_error_message )
704710
705711 instance_id = content ['instance_id' ]
706712 try :
@@ -785,8 +791,8 @@ def api_instance_info():
785791 """
786792 try :
787793 content = _unwrap_signed_container_request (request )
788- except Exception as e :
789- return error_response (str ( e ) )
794+ except SignatureUnwrappingError as e :
795+ return error_response (e . user_error_message )
790796
791797 instance_id = content .get ('instance_id' )
792798 try :
0 commit comments