From 5b5f87b7688026862e65874198b9165ea6bd0a8d Mon Sep 17 00:00:00 2001 From: Keyan Song Date: Fri, 30 Oct 2026 19:01:49 -0700 Subject: [PATCH 1/2] [Batch] Fix `key=value` arguments rejecting values that contain `=` Values for `--environment-settings`, `--metadata`, and other `key=value` arguments were split on every `=`, so values such as connection strings or base64 text were rejected. Split on the first `=` only, as resource files already do. Fixes #24887 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../cli/command_modules/batch/_validators.py | 6 +++--- .../batch/tests/latest/test_batch_commands.py | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/batch/_validators.py b/src/azure-cli/azure/cli/command_modules/batch/_validators.py index 3dd0d75f9aa..7e644134ad6 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/_validators.py +++ b/src/azure-cli/azure/cli/command_modules/batch/_validators.py @@ -56,7 +56,7 @@ def duration_format(value): def metadata_item_format(value): """Space-separated values in 'key=value' format.""" try: - data_name, data_value = value.split('=') + data_name, data_value = value.split('=', 1) except ValueError: message = ("Incorrectly formatted metadata. " "Argument values should be in the format a=b c=d") @@ -72,7 +72,7 @@ def string_dictionary_format(values): result = {} try: for value in values.split(' '): - k, v = value.split('=') + k, v = value.split('=', 1) result[k] = v except ValueError: message = ("Incorrectly formatted values. " @@ -84,7 +84,7 @@ def string_dictionary_format(values): def environment_setting_format(value): """Space-separated values in 'key=value' format.""" try: - env_name, env_value = value.split('=') + env_name, env_value = value.split('=', 1) except ValueError: message = ("Incorrectly formatted environment settings. " "Argument values should be in the format a=b c=d") diff --git a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py index d039ea2f320..2d48e24fd1b 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py +++ b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py @@ -52,8 +52,8 @@ def test_batch_metadata_item_format(self): with self.assertRaises(ValueError): _validators.metadata_item_format("test") - with self.assertRaises(ValueError): - _validators.metadata_item_format("name=value=other") + meta = _validators.metadata_item_format("name=value=other") + self.assertEqual(meta, {'name': 'name', 'value': 'value=other'}) def test_batch_environment_setting_format(self): env = _validators.environment_setting_format("name=value") @@ -62,8 +62,21 @@ def test_batch_environment_setting_format(self): with self.assertRaises(ValueError): _validators.environment_setting_format("test") + env = _validators.environment_setting_format("CONN=Server=tcp:x;Database=y") + self.assertEqual(env, {'name': 'CONN', 'value': 'Server=tcp:x;Database=y'}) + + env = _validators.environment_setting_format("TOKEN=YWJj==") + self.assertEqual(env, {'name': 'TOKEN', 'value': 'YWJj=='}) + + def test_batch_string_dictionary_format(self): + result = _validators.string_dictionary_format("a=b c=d=e") + self.assertEqual(result, {'a': 'b', 'c': 'd=e'}) + + with self.assertRaises(ValueError): + _validators.string_dictionary_format("a=b test") + with self.assertRaises(ValueError): - _validators.environment_setting_format("name=value=other") + _validators.string_dictionary_format("") def test_batch_application_package_reference_format(self): ref = _validators.batch_application_package_reference_format("app_1") From 048d5a8fd6a86c9e89350c1e2768eea24c0aa693 Mon Sep 17 00:00:00 2001 From: Keyan Song Date: Wed, 4 Nov 2026 10:09:40 -0800 Subject: [PATCH 2/2] Test `az batch task create` with `=` in `--environment-settings` values Runs the command through the CLI's argument parsing with the Batch client's `create_task` and `get_task` mocked. A quoted SQL connection string (spaces and several `=`) and a second variable reach the task request with their exact names and values; a setting without `=` is still rejected and nothing is sent. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../batch/tests/latest/test_batch_commands.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py index 2d48e24fd1b..5e31d2d9d2b 100644 --- a/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py +++ b/src/azure-cli/azure/cli/command_modules/batch/tests/latest/test_batch_commands.py @@ -3,7 +3,9 @@ # Licensed under the MIT License. See License.txt in the project root for license information. # -------------------------------------------------------------------------------------------- +import io import os +import shlex import unittest import datetime import isodate @@ -17,6 +19,7 @@ from azure.batch import models, BatchClient from azure.core.credentials import AzureNamedKeyCredential +from azure.cli.core.mock import DummyCli from azure.cli.command_modules.batch import _validators from azure.cli.command_modules.batch import _command_type @@ -666,3 +669,43 @@ def test_batch_load_arguments(self): # option = [arg for (name, arg) in args if name == 'node_reboot_option'][0] # self.assertIsNotNone(option.choices) # self.assertFalse([a for a in option.choices if "'" in a]) + + +class TestBatchTaskCreateCommand(unittest.TestCase): + + @staticmethod + def _invoke(command): + account = ('--account-name test1 --account-key ZmFrZV9hY29jdW50X2tleQ== ' + '--account-endpoint https://test1.westus.batch.azure.com') + with mock.patch.object(BatchClient, 'create_task') as create_task, \ + mock.patch.object(BatchClient, 'get_task', return_value=models.BatchTask({'id': 'task1'})): + try: + exit_code = DummyCli().invoke(shlex.split(f'{command} {account}'), out_file=io.StringIO()) + except SystemExit as ex: # argument parsing errors exit with code 2 + exit_code = ex.code + return exit_code, create_task + + def test_batch_task_create_environment_settings(self): + sql_connection = ('Server=tcp:servername.database.windows.net,1433;Initial Catalog=mydb;' + 'Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True') + exit_code, create_task = self._invoke( + 'batch task create --job-id job1 --task-id task1 --command-line "cmd /c set" ' + f'--environment-settings SqlConnection="{sql_connection}" LOG_LEVEL=info') + + self.assertEqual(exit_code, 0) + create_task.assert_called_once() + self.assertEqual(create_task.call_args.kwargs['job_id'], 'job1') + self.assertEqual(create_task.call_args.kwargs['task'].as_dict(), { + 'id': 'task1', + 'commandLine': 'cmd /c set', + 'environmentSettings': [ + {'name': 'SqlConnection', 'value': sql_connection}, + {'name': 'LOG_LEVEL', 'value': 'info'}]}) + + # A setting without '=' is still rejected before any request is sent + exit_code, create_task = self._invoke( + 'batch task create --job-id job1 --task-id task1 --command-line "cmd /c set" ' + '--environment-settings LOG_LEVEL') + + self.assertEqual(exit_code, 2) + create_task.assert_not_called()