File tree Expand file tree Collapse file tree 2 files changed +10
-3
lines changed Expand file tree Collapse file tree 2 files changed +10
-3
lines changed Original file line number Diff line number Diff line change 55import binascii
66import datetime
77from base64 import b64decode
8+ import string
89from typing import Any
910
1011from dateutil .parser import isoparse
@@ -109,11 +110,13 @@ def validate_icon(value: Any) -> bool:
109110 if len (value ) < 1 :
110111 return False
111112 for icon in value :
112- # base64data must contain valid base64 data
113- if len (icon ["base64data" ]) < 4 :
113+ # remove all whitespace from the base64 payload
114+ data = icon ["base64data" ].translate ({ord (c ): None for c in string .whitespace })
115+ # check that the content is valid base64 encoded data
116+ if len (data ) < 4 :
114117 return False
115118 try :
116- _ = b64decode (icon [ "base64data" ] , validate = True )
119+ _ = b64decode (data , validate = True )
117120 except binascii .Error :
118121 return False
119122 # mediatype must be a supported image format
Original file line number Diff line number Diff line change @@ -127,6 +127,8 @@ def test_validate_list_of_dicts(
127127 ([{"base64data" : "" , "mediatype" : "image/png" }], False ),
128128 ([{"base64data" : "foobar" , "mediatype" : "image/png" }], False ),
129129 ([{"base64data" : "Zm9v" , "mediatype" : "text/plain" }], False ),
130+ ([{"base64data" : "Zm^v" , "mediatype" : "image/png" }], False ),
131+ ([{"base64data" : " Zm\n 9v\n " , "mediatype" : "image/png" }], True ),
130132 ],
131133 indirect = False ,
132134 ids = [
@@ -135,6 +137,8 @@ def test_validate_list_of_dicts(
135137 "Empty base64data" ,
136138 "Invalid base64data" ,
137139 "Invalid mediatype" ,
140+ "Invalid characters in base64data" ,
141+ "Whitespace in base64data" ,
138142 ],
139143)
140144def test_validate_icon (value : Any , expected : bool ) -> None :
You can’t perform that action at this time.
0 commit comments