Skip to content

Commit

Permalink
Be strict about identifiers accepted by Interpolate.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomprince committed May 17, 2012
1 parent 74ab736 commit a878a0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
14 changes: 14 additions & 0 deletions master/buildbot/process/properties.py
Expand Up @@ -402,6 +402,8 @@ class Interpolate(util.ComparableMixin):

implements(IRenderable)
compare_attrs = ('fmtstring', 'args', 'kwargs')

identifier_re = re.compile('^[\w-]*$')

def __init__(self, fmtstring, *args, **kwargs):
self.fmtstring = fmtstring
Expand All @@ -419,6 +421,9 @@ def _parse_prop(arg):
prop, repl = arg.split(":", 1)
except ValueError:
prop, repl = arg, None
if not Interpolate.identifier_re.match(prop):
config.error("Property name must be alphanumeric for prop Interpolation '%s'" % arg)
prop = repl = None
return _thePropertyDict, prop, repl

@staticmethod
Expand All @@ -433,13 +438,22 @@ def _parse_src(arg):
except ValueError:
config.error("Must specify both codebase and attribute for src Interpolation '%s'" % arg)
codebase = attr = repl = None
if not Interpolate.identifier_re.match(codebase):
config.error("Codebase must be alphanumeric for src Interpolation '%s'" % arg)
codebase = attr = repl = None
if not Interpolate.identifier_re.match(attr):
config.error("Attribute must be alphanumeric for src Interpolation '%s'" % arg)
codebase = attr = repl = None
return _SourceStampDict(codebase), attr, repl

def _parse_kw(self, arg):
try:
kw, repl = arg.split(":", 1)
except ValueError:
kw, repl = arg, None
if not Interpolate.identifier_re.match(kw):
config.error("Keyword must be alphanumeric for kw Interpolation '%s'" % arg)
kw = repl = None
return _Lazy(self.kwargs), kw, repl

def _parseSubstitution(self, fmt):
Expand Down
16 changes: 16 additions & 0 deletions master/buildbot/test/unit/test_process_properties.py
Expand Up @@ -394,6 +394,22 @@ def test_colon_ternary_hash_bad_delimeter(self):
self.assertRaisesConfigError("invalid Interpolate ternary expression 'one' with delimiter '|'",
lambda: Interpolate("echo '%(prop:P:#?|one)s'"))

def test_prop_invalid_character(self):
self.assertRaisesConfigError("Property name must be alphanumeric for prop Interpolation 'a+a'",
lambda: Interpolate("echo '%(prop:a+a)s'"))

def test_kw_invalid_character(self):
self.assertRaisesConfigError("Keyword must be alphanumeric for kw Interpolation 'a+a'",
lambda: Interpolate("echo '%(kw:a+a)s'"))

def test_src_codebase_invalid_character(self):
self.assertRaisesConfigError("Codebase must be alphanumeric for src Interpolation 'a+a:a'",
lambda: Interpolate("echo '%(src:a+a:a)s'"))

def test_src_attr_invalid_character(self):
self.assertRaisesConfigError("Attribute must be alphanumeric for src Interpolation 'a:a+a'",
lambda: Interpolate("echo '%(src:a:a+a)s'"))


class TestInterpolatePositional(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit a878a0a

Please sign in to comment.