From afd89ca36de806efdbc4b62c61fcfe8783a784db Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 3 Feb 2024 13:46:05 +0100 Subject: [PATCH] Contstrain argparse integers to permitted range In many casese we used type=int permitting any integer value, positive or negative without a constratint in size. However, in reality often we're constrained to unsigned 8 or 16 bit ranges. Let's use the auto_uint{8,16} functions to enforce this within argparse before we even try to encode something that won't work. Change-Id: I35c81230bc18e2174ec1930aa81463f03bcd69c8 --- pySim/filesystem.py | 20 ++++++++++---------- pySim/ts_102_222.py | 14 +++++++------- pySim/utils.py | 3 +++ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pySim/filesystem.py b/pySim/filesystem.py index 2bc5f014..47bdb574 100644 --- a/pySim/filesystem.py +++ b/pySim/filesystem.py @@ -38,7 +38,7 @@ from typing import cast, Optional, Iterable, List, Dict, Tuple, Union from smartcard.util import toBytes -from pySim.utils import sw_match, h2b, b2h, i2h, is_hex, auto_int, Hexstr, is_hexstr +from pySim.utils import sw_match, h2b, b2h, i2h, is_hex, auto_int, auto_uint8, auto_uint16, Hexstr, is_hexstr from pySim.construct import filter_dict, parse_construct, build_construct from pySim.exceptions import * from pySim.jsonpath import js_path_find, js_path_modify @@ -589,9 +589,9 @@ class TransparentEF(CardEF): read_bin_parser = argparse.ArgumentParser() read_bin_parser.add_argument( - '--offset', type=int, default=0, help='Byte offset for start of read') + '--offset', type=auto_uint16, default=0, help='Byte offset for start of read') read_bin_parser.add_argument( - '--length', type=int, help='Number of bytes to read') + '--length', type=auto_uint16, help='Number of bytes to read') @cmd2.with_argparser(read_bin_parser) def do_read_binary(self, opts): @@ -611,7 +611,7 @@ class TransparentEF(CardEF): upd_bin_parser = argparse.ArgumentParser() upd_bin_parser.add_argument( - '--offset', type=int, default=0, help='Byte offset for start of read') + '--offset', type=auto_uint16, default=0, help='Byte offset for start of read') upd_bin_parser.add_argument('data', type=is_hexstr, help='Data bytes (hex format) to write') @cmd2.with_argparser(upd_bin_parser) @@ -810,9 +810,9 @@ class LinFixedEF(CardEF): read_rec_parser = argparse.ArgumentParser() read_rec_parser.add_argument( - 'record_nr', type=int, help='Number of record to be read') + 'record_nr', type=auto_uint8, help='Number of record to be read') read_rec_parser.add_argument( - '--count', type=int, default=1, help='Number of records to be read, beginning at record_nr') + '--count', type=auto_uint8, default=1, help='Number of records to be read, beginning at record_nr') @cmd2.with_argparser(read_rec_parser) def do_read_record(self, opts): @@ -828,7 +828,7 @@ class LinFixedEF(CardEF): read_rec_dec_parser = argparse.ArgumentParser() read_rec_dec_parser.add_argument( - 'record_nr', type=int, help='Number of record to be read') + 'record_nr', type=auto_uint8, help='Number of record to be read') read_rec_dec_parser.add_argument('--oneline', action='store_true', help='No JSON pretty-printing, dump as a single line') @@ -869,7 +869,7 @@ class LinFixedEF(CardEF): upd_rec_parser = argparse.ArgumentParser() upd_rec_parser.add_argument( - 'record_nr', type=int, help='Number of record to be read') + 'record_nr', type=auto_uint8, help='Number of record to be read') upd_rec_parser.add_argument('data', type=is_hexstr, help='Data bytes (hex format) to write') @cmd2.with_argparser(upd_rec_parser) @@ -881,7 +881,7 @@ class LinFixedEF(CardEF): upd_rec_dec_parser = argparse.ArgumentParser() upd_rec_dec_parser.add_argument( - 'record_nr', type=int, help='Number of record to be read') + 'record_nr', type=auto_uint8, help='Number of record to be read') upd_rec_dec_parser.add_argument('data', help='Abstract data (JSON format) to write') upd_rec_dec_parser.add_argument('--json-path', type=str, help='JSON path to modify specific element of record only') @@ -902,7 +902,7 @@ class LinFixedEF(CardEF): edit_rec_dec_parser = argparse.ArgumentParser() edit_rec_dec_parser.add_argument( - 'record_nr', type=int, help='Number of record to be edited') + 'record_nr', type=auto_uint8, help='Number of record to be edited') @cmd2.with_argparser(edit_rec_dec_parser) def do_edit_record_decoded(self, opts): diff --git a/pySim/ts_102_222.py b/pySim/ts_102_222.py index bf5e29f5..ef6f69dc 100644 --- a/pySim/ts_102_222.py +++ b/pySim/ts_102_222.py @@ -24,7 +24,7 @@ from cmd2 import CommandSet, with_default_category, with_argparser import argparse from pySim.exceptions import * -from pySim.utils import h2b, swap_nibbles, b2h, JsonEncoder +from pySim.utils import h2b, swap_nibbles, b2h, JsonEncoder, auto_uint8, auto_uint16 from pySim.ts_102_221 import * @@ -112,13 +112,13 @@ class Ts102222Commands(CommandSet): create_required = create_parser.add_argument_group('required arguments') create_optional = create_parser.add_argument_group('optional arguments') create_required.add_argument('--ef-arr-file-id', required=True, type=str, help='Referenced Security: File Identifier of EF.ARR') - create_required.add_argument('--ef-arr-record-nr', required=True, type=int, help='Referenced Security: Record Number within EF.ARR') - create_required.add_argument('--file-size', required=True, type=int, help='Size of file in octets') + create_required.add_argument('--ef-arr-record-nr', required=True, type=auto_uint8, help='Referenced Security: Record Number within EF.ARR') + create_required.add_argument('--file-size', required=True, type=auto_uint16, help='Size of file in octets') create_required.add_argument('--structure', required=True, type=str, choices=['transparent', 'linear_fixed', 'ber_tlv'], help='Structure of the to-be-created EF') create_optional.add_argument('--short-file-id', type=str, help='Short File Identifier as 2-digit hex string') create_optional.add_argument('--shareable', action='store_true', help='Should the file be shareable?') - create_optional.add_argument('--record-length', type=int, help='Length of each record in octets') + create_optional.add_argument('--record-length', type=auto_uint16, help='Length of each record in octets') @cmd2.with_argparser(create_parser) def do_create_ef(self, opts): @@ -160,11 +160,11 @@ class Ts102222Commands(CommandSet): createdf_optional = createdf_parser.add_argument_group('optional arguments') createdf_sja_optional = createdf_parser.add_argument_group('sysmoISIM-SJA optional arguments') createdf_required.add_argument('--ef-arr-file-id', required=True, type=str, help='Referenced Security: File Identifier of EF.ARR') - createdf_required.add_argument('--ef-arr-record-nr', required=True, type=int, help='Referenced Security: Record Number within EF.ARR') + createdf_required.add_argument('--ef-arr-record-nr', required=True, type=auto_uint8, help='Referenced Security: Record Number within EF.ARR') createdf_optional.add_argument('--shareable', action='store_true', help='Should the file be shareable?') createdf_optional.add_argument('--aid', type=is_hexstr, help='Application ID (creates an ADF, instead of a DF)') # mandatory by spec, but ignored by several OS, so don't force the user - createdf_optional.add_argument('--total-file-size', type=int, help='Physical memory allocated for DF/ADi in octets') + createdf_optional.add_argument('--total-file-size', type=auto_uint16, help='Physical memory allocated for DF/ADi in octets') createdf_sja_optional.add_argument('--permit-rfm-create', action='store_true') createdf_sja_optional.add_argument('--permit-rfm-delete-terminate', action='store_true') createdf_sja_optional.add_argument('--permit-other-applet-create', action='store_true') @@ -208,7 +208,7 @@ class Ts102222Commands(CommandSet): resize_ef_parser.add_argument('NAME', type=str, help='Name or FID of file to be resized') resize_ef_parser._action_groups.pop() resize_ef_required = resize_ef_parser.add_argument_group('required arguments') - resize_ef_required.add_argument('--file-size', required=True, type=int, help='Size of file in octets') + resize_ef_required.add_argument('--file-size', required=True, type=auto_uint16, help='Size of file in octets') @cmd2.with_argparser(resize_ef_parser) def do_resize_ef(self, opts): diff --git a/pySim/utils.py b/pySim/utils.py index a31fbe0e..73e5040e 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -927,6 +927,9 @@ def auto_uint7(x): def auto_uint8(x): return _auto_uint(x, 255) +def auto_uint16(x): + return _auto_uint(x, 65535) + def expand_hex(hexstring, length): """Expand a given hexstring to a specified length by replacing "." or ".." with a filler that is derived from the neighboring nibbles respective