From 1aeb50d49756ee7fdac43fb8289c5248ec63a4f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radim=20Karni=C5=A1?= Date: Tue, 18 Mar 2025 16:09:37 +0100 Subject: [PATCH] fix(idf_tools): Validate input features --- tools/idf_tools.py | 20 +++++++++++++++----- tools/install_util.py | 11 +++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/tools/idf_tools.py b/tools/idf_tools.py index 5b43596645..876ad6245d 100755 --- a/tools/idf_tools.py +++ b/tools/idf_tools.py @@ -1783,14 +1783,24 @@ def process_and_check_features(idf_env_obj: IDFEnv, features_str: str) -> List[s """ new_features = [] remove_features = [] - for new_feature_candidate in features_str.split(','): + invalid_features = [] + + for new_feature_candidate in [feature for feature in features_str.split(',') if feature != '']: + # Feature to be added/removed needs to be checked if valid + sanitized_feat = new_feature_candidate.lstrip('-+') + if not os.path.isfile(feature_to_requirements_path(sanitized_feat)): + invalid_features += [sanitized_feat] + continue + if new_feature_candidate.startswith('-'): remove_features += [new_feature_candidate.lstrip('-')] else: - new_feature_candidate = new_feature_candidate.lstrip('+') - # Feature to be added needs to be checked if is valid - if os.path.isfile(feature_to_requirements_path(new_feature_candidate)): - new_features += [new_feature_candidate] + new_features += [new_feature_candidate.lstrip('+')] + + if invalid_features: + fatal(f'The following selected features are not valid: {", ".join(invalid_features)}') + raise SystemExit(1) + idf_env_obj.get_active_idf_record().update_features(tuple(new_features), tuple(remove_features)) return idf_env_obj.get_active_idf_record().features diff --git a/tools/install_util.py b/tools/install_util.py index 1200b8c517..d9f97547a0 100644 --- a/tools/install_util.py +++ b/tools/install_util.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # # SPDX-License-Identifier: Apache-2.0 # This script is used from the $IDF_PATH/install.* scripts. This way the argument parsing can be done at one place and @@ -13,19 +13,22 @@ from itertools import chain def action_extract_features(args: str) -> None: """ - Command line arguments starting with "--enable-" or "--disable" are features. This function selects those, add them signs '+' or '-' and prints them. + Command line arguments starting with "--enable-" or "--disable-" are features. + This function selects those, adds them a '+' or '-' sign and prints them. """ features = ['+core'] # "core" features should be always installed if args: arg_enable_prefix = '--enable-' arg_disable_prefix = '--disable-' - # features to be enabled has prefix '+', disabled has prefix '-' + # features to be enabled have prefix '+', disabled have prefix '-' for arg in args.split(): if arg.startswith(arg_enable_prefix): features.append('+' + arg[len(arg_enable_prefix):]) elif arg.startswith(arg_disable_prefix): features.append('-' + arg[len(arg_disable_prefix):]) + elif arg.startswith('-'): + raise SystemExit(f'ERROR: Invalid feature specifier: {arg}') features = list(set(features)) print(','.join(features)) @@ -75,7 +78,7 @@ optional arguments: {help_opts} show this help message and exit For more information, please see https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/tools/idf-tools.html#install-scripts - """) + """) # noqa: E222 def main() -> None: