pySim-shell: Validate that argument to 'apdu' command is proper hexstr

Let's not even send anything to the card if it's not an even number
of hexadecimal digits

Change-Id: I58465244101cc1a976e5a17af2aceea1cf9f9b54
This commit is contained in:
Harald Welte
2023-11-01 23:40:07 +01:00
parent f9ea63ea51
commit 4e59d89a5d
2 changed files with 11 additions and 2 deletions

View File

@@ -1478,3 +1478,12 @@ def is_hexstr_or_decimal(instr: str) -> str:
if len(instr) & 1:
raise ValueError('Input has un-even number of hex digits')
return instr
def is_hexstr(instr: str) -> str:
"""Method that can be used as 'type' in argparse.add_argument() to validate the value consists of
an even sequence of hexadecimal digits only."""
if not all(c in string.hexdigits for c in instr):
raise ValueError('Input must be hexadecimal')
if len(instr) & 1:
raise ValueError('Input has un-even number of hex digits')
return instr