Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/606'
Browse files Browse the repository at this point in the history
* origin/pr/606:
  Updating Unit Test for new VM Names validation
  Better error message if new VM Name starts with illegal characters
  • Loading branch information
marmarek committed Jul 19, 2024
2 parents 726c416 + 51a425b commit 71f7ca6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions qubes/tests/api_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,18 @@ def test_334_vm_create_invalid_name(self, storage_mock):
self.assertNotIn('test-###', self.app.domains)
self.assertFalse(self.app.save.called)

with self.assertRaises(qubes.exc.QubesValueError):
self.call_mgmt_func(b'admin.vm.Create.AppVM',
b'dom0', b'test-template', b'name=--helpVM')
self.assertNotIn('--helpVM', self.app.domains)
self.assertFalse(self.app.save.called)

with self.assertRaises(qubes.exc.QubesValueError):
self.call_mgmt_func(b'admin.vm.Create.AppVM',
b'dom0', b'test-template', b'name=1stVM')
self.assertNotIn('1stVM', self.app.domains)
self.assertFalse(self.app.save.called)

@unittest.mock.patch('qubes.storage.Storage.create')
def test_335_vm_create_missing_name(self, storage_mock):
storage_mock.side_effect = self.dummy_coro
Expand Down
8 changes: 8 additions & 0 deletions qubes/vm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def validate_name(holder, prop, value):
raise qubes.exc.QubesValueError(
'VM name must be shorter than 32 characters')

if re.match(r"\A[0-9_-].*\Z", value) is not None:
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(holder, prop, value,
'{} cannot start with hyphen, underscore or numbers'.format(
prop.__name__))
raise qubes.exc.QubesValueError(
'VM name cannot start with hyphen, underscore or numbers')

# this regexp does not contain '+'; if it had it, we should specifically
# disallow 'lost+found' #1440
if re.match(r"\A[a-zA-Z][a-zA-Z0-9_-]*\Z", value) is None:
Expand Down

0 comments on commit 71f7ca6

Please sign in to comment.