Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/azure-cli/azure/cli/command_modules/appservice/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -6042,14 +6042,13 @@ def _check_service_principal_permissions(cmd, resource_group_name, key_vault_nam

def _update_host_name_ssl_state(cmd, resource_group_name, webapp_name, webapp,
host_name, ssl_state, thumbprint, slot=None):
Site, HostNameSslState = cmd.get_models('Site', 'HostNameSslState')
updated_webapp = Site(host_name_ssl_states=[HostNameSslState(name=host_name,
ssl_state=ssl_state,
thumbprint=thumbprint,
to_update=True)],
location=webapp.location, tags=webapp.tags)
HostNameSslState = cmd.get_models('HostNameSslState')
webapp.host_name_ssl_states = [HostNameSslState(name=host_name,
ssl_state=ssl_state,
thumbprint=thumbprint,
to_update=True)]
return _generic_site_operation(cmd.cli_ctx, resource_group_name, webapp_name, 'begin_create_or_update',
slot, updated_webapp)
slot, webapp)
Comment on lines +6045 to +6051
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When slot is provided, this helper assumes the passed webapp object represents that slot. In the current call path (_update_ssl_binding fetches client.web_apps.get(...) regardless of slot), this can cause a slot PUT (begin_create_or_update_slot) to reuse the production Site payload (including policy-sensitive fields like https_only), potentially triggering policy denial or unintentionally overwriting slot-specific settings. Consider fetching the Site for the slot (client.web_apps.get_slot(...) or _generic_site_operation(..., 'get', slot)) and using that object for the update when slot is not None.

Copilot uses AI. Check for mistakes.


def _update_ssl_binding(cmd, resource_group_name, name, certificate_thumbprint, ssl_type, hostname, slot=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
view_in_browser,
sync_site_repo,
_match_host_names_from_cert,
_update_host_name_ssl_state,
bind_ssl_cert,
list_publish_profiles,
show_app,
Expand Down Expand Up @@ -639,6 +640,59 @@ def test_update_webapp_platform_release_channel_latest(self):
self.assertEqual(result.additional_properties["properties"]["platformReleaseChannel"], "Latest")


class TestUpdateHostNameSslState(unittest.TestCase):
@mock.patch('azure.cli.command_modules.appservice.custom._generic_site_operation', autospec=True)
def test_update_host_name_ssl_state_passes_full_site(self, generic_site_op_mock):
"""Test that _update_host_name_ssl_state passes the full Site object (not a partial one)
to begin_create_or_update, preserving policy-sensitive fields like https_only."""
cmd_mock = _get_test_cmd()
Site, HostNameSslState, SslState = cmd_mock.get_models('Site', 'HostNameSslState', 'SslState')

webapp = Site(name='mySite', location='eastus', tags={'env': 'prod'})
webapp.https_only = True
webapp.host_name_ssl_states = [
HostNameSslState(name='existing.contoso.com',
ssl_state=SslState.sni_enabled,
thumbprint='EXISTINGTHUMB')
]

_update_host_name_ssl_state(cmd_mock, 'myRg', 'mySite', webapp,
'www.contoso.com', SslState.sni_enabled, 'NEWTHUMB')

generic_site_op_mock.assert_called_once()
call_args = generic_site_op_mock.call_args
passed_site = call_args[0][5] # (cli_ctx, rg, name, op, slot, site)

# The passed object should be the original webapp, preserving all fields
self.assertTrue(passed_site.https_only,
"https_only must be preserved to avoid Azure Policy denial")
self.assertEqual(passed_site.location, 'eastus')
self.assertEqual(passed_site.tags, {'env': 'prod'})

# host_name_ssl_states should contain only the binding being updated
self.assertEqual(len(passed_site.host_name_ssl_states), 1)
ssl_state = passed_site.host_name_ssl_states[0]
self.assertEqual(ssl_state.name, 'www.contoso.com')
self.assertEqual(ssl_state.thumbprint, 'NEWTHUMB')
self.assertTrue(ssl_state.to_update)

@mock.patch('azure.cli.command_modules.appservice.custom._generic_site_operation', autospec=True)
def test_update_host_name_ssl_state_with_slot(self, generic_site_op_mock):
"""Test that slot parameter is correctly forwarded."""
cmd_mock = _get_test_cmd()
Site, SslState = cmd_mock.get_models('Site', 'SslState')
webapp = Site(name='mySite', location='westus')

_update_host_name_ssl_state(cmd_mock, 'myRg', 'mySite', webapp,
'www.contoso.com', SslState.sni_enabled, 'THUMB', slot='staging')

call_args = generic_site_op_mock.call_args
# slot is the 5th positional arg (index 4) after cli_ctx, rg, name, operation_name
self.assertEqual(call_args[0][4], 'staging')
# site is the 6th positional arg (index 5)
self.assertIs(call_args[0][5], webapp)
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_update_host_name_ssl_state_with_slot asserts object identity (assertIs(..., webapp)), which will fail if the implementation is adjusted to avoid in-place mutation or to refetch the correct slot Site (which is likely needed for correctness). It would be less brittle to assert on the payload’s relevant properties (e.g., https_only, location, tags) and/or add a focused test at the _update_ssl_binding level to ensure slot updates use get_slot-retrieved site data.

Suggested change
# site is the 6th positional arg (index 5)
self.assertIs(call_args[0][5], webapp)
# site is the 6th positional arg (index 5); verify it has the expected properties
site_arg = call_args[0][5]
self.assertEqual(site_arg.name, webapp.name)
self.assertEqual(site_arg.location, webapp.location)

Copilot uses AI. Check for mistakes.


class FakedResponse: # pylint: disable=too-few-public-methods
def __init__(self, status_code):
self.status_code = status_code
Expand Down
Loading