mirror of https://gerrit.osmocom.org/pysim
pySim-shell: Improved argument validation for verify_adm argument
Let's make sure we don't even bother to ask the card to verify anything as ADM1 pin which is not either a sequence of decimal digits or an even number of hex digits (even number of bytes). Change-Id: I4a193a3cf63462fad73d145ab1481070ddf767ca
This commit is contained in:
parent
469db9393f
commit
f9ea63ea51
|
@ -53,6 +53,7 @@ from pySim.transport import init_reader, ApduTracer, argparse_add_reader_args, P
|
||||||
from pySim.cards import card_detect, SimCardBase, UiccCardBase
|
from pySim.cards import card_detect, SimCardBase, UiccCardBase
|
||||||
from pySim.utils import h2b, b2h, i2h, swap_nibbles, rpad, JsonEncoder, bertlv_parse_one, sw_match
|
from pySim.utils import h2b, b2h, i2h, swap_nibbles, rpad, JsonEncoder, bertlv_parse_one, sw_match
|
||||||
from pySim.utils import sanitize_pin_adm, tabulate_str_list, boxed_heading_str, Hexstr, dec_iccid
|
from pySim.utils import sanitize_pin_adm, tabulate_str_list, boxed_heading_str, Hexstr, dec_iccid
|
||||||
|
from pySim.utils import is_hexstr_or_decimal
|
||||||
from pySim.card_handler import CardHandler, CardHandlerAuto
|
from pySim.card_handler import CardHandler, CardHandlerAuto
|
||||||
|
|
||||||
from pySim.filesystem import CardDF, CardADF, CardModel, CardApplication
|
from pySim.filesystem import CardDF, CardADF, CardModel, CardApplication
|
||||||
|
@ -777,7 +778,7 @@ class PySimCommands(CommandSet):
|
||||||
self._cmd.poutput("no description available")
|
self._cmd.poutput("no description available")
|
||||||
|
|
||||||
verify_adm_parser = argparse.ArgumentParser()
|
verify_adm_parser = argparse.ArgumentParser()
|
||||||
verify_adm_parser.add_argument('ADM1', nargs='?', type=str,
|
verify_adm_parser.add_argument('ADM1', nargs='?', type=is_hexstr_or_decimal,
|
||||||
help='ADM1 pin value. If none given, CSV file will be queried')
|
help='ADM1 pin value. If none given, CSV file will be queried')
|
||||||
|
|
||||||
@cmd2.with_argparser(verify_adm_parser)
|
@cmd2.with_argparser(verify_adm_parser)
|
||||||
|
|
|
@ -1467,3 +1467,14 @@ class CardCommandSet:
|
||||||
def all_subclasses(cls) -> set:
|
def all_subclasses(cls) -> set:
|
||||||
"""Recursively get all subclasses of a specified class"""
|
"""Recursively get all subclasses of a specified class"""
|
||||||
return set(cls.__subclasses__()).union([s for c in cls.__subclasses__() for s in all_subclasses(c)])
|
return set(cls.__subclasses__()).union([s for c in cls.__subclasses__() for s in all_subclasses(c)])
|
||||||
|
|
||||||
|
def is_hexstr_or_decimal(instr: str) -> str:
|
||||||
|
"""Method that can be used as 'type' in argparse.add_argument() to validate the value consists of
|
||||||
|
[hexa]decimal digits only."""
|
||||||
|
if instr.isdecimal():
|
||||||
|
return instr
|
||||||
|
if not all(c in string.hexdigits for c in instr):
|
||||||
|
raise ValueError('Input must be [hexa]decimal')
|
||||||
|
if len(instr) & 1:
|
||||||
|
raise ValueError('Input has un-even number of hex digits')
|
||||||
|
return instr
|
||||||
|
|
Loading…
Reference in New Issue