Skip to content

Commit

Permalink
Only permit alpha-numerics and ._- for instance type names.
Browse files Browse the repository at this point in the history
Fixes bug 977187.

Change-Id: I883204a508f39441c172f3c42c8be3d6598c35d8
  • Loading branch information
jk0 committed May 21, 2012
1 parent 53583fa commit e6b42d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions nova/compute/instance_types.py
Expand Up @@ -20,6 +20,8 @@

"""Built-in instance properties."""

import re

from nova import context
from nova import db
from nova import exception
Expand All @@ -29,6 +31,8 @@
FLAGS = flags.FLAGS
LOG = logging.getLogger(__name__)

INVALID_NAME_REGEX = re.compile("[^\w\.\- ]")


def create(name, memory, vcpus, root_gb, ephemeral_gb, flavorid, swap=None,
rxtx_factor=None):
Expand All @@ -48,6 +52,12 @@ def create(name, memory, vcpus, root_gb, ephemeral_gb, flavorid, swap=None,
'rxtx_factor': rxtx_factor,
}

# ensure name does not contain any special characters
invalid_name = INVALID_NAME_REGEX.search(name)
if invalid_name:
msg = _("names can only contain [a-zA-Z0-9_.- ]")
raise exception.InvalidInput(reason=msg)

# ensure some attributes are integers and greater than or equal to 0
for option in kwargs:
try:
Expand Down
7 changes: 7 additions & 0 deletions nova/tests/test_instance_types.py
Expand Up @@ -88,6 +88,13 @@ def test_instance_type_create_then_delete(self):
new_list = instance_types.get_all_types()
self.assertEqual(original_list, new_list)

def test_instance_type_create_with_special_characters(self):
"""Ensure instance types raises InvalidInput for invalid characters"""
name = "foo.bar!@#$%^-test_name"
flavorid = "flavor1"
self.assertRaises(exception.InvalidInput, instance_types.create,
name, 256, 1, 120, 100, flavorid)

def test_get_all_instance_types(self):
"""Ensures that all instance types can be retrieved"""
session = sql_session.get_session()
Expand Down

0 comments on commit e6b42d7

Please sign in to comment.