Don't allow {{ 'foo' }} unquoted jinja value

The unquoted jinja feature only works when the unquoted value is valid
YAML.

Don't allow `foo: {{ 'bar' }}` when the internal value is a quoted
string. This form is dubious and probably never seen in the wild. We
just don't give it special treatment so it fails same as before.

Also added check to assert jinja key is a string.
pull/36331/head
Ingy döt Net 7 years ago
parent 1761a18356
commit c87af621cb

@ -48,20 +48,18 @@ def _construct_scalar(loader, node):
map_as_key_key = node.value[0][0] map_as_key_key = node.value[0][0]
map_as_key_val = node.value[0][1] map_as_key_val = node.value[0][1]
# And have key that is a mapping and a null value: # And have key that is a mapping, and a value that is null:
if map_as_key_key.tag == 'tag:yaml.org,2002:map' and \ if map_as_key_key.tag == 'tag:yaml.org,2002:map' and \
map_as_key_val.tag == 'tag:yaml.org,2002:null': map_as_key_val.tag == 'tag:yaml.org,2002:null':
# Get the string intended jinja string value: # Get the intended jinja string value:
scalar_node = map_as_key_key.value[0][0] jinja_string = map_as_key_key.value[0][0]
# Put the braces back onto it: # If jinja string was not quoted:
if scalar_node.style in ['"', "'"]: if jinja_string.tag == 'tag:yaml.org,2002:str' and \
# With quotes if original was quoted: jinja_string.style == '':
return "{{'%s'}}" % scalar_node.value # Add jinja double braces back in:
return "{{%s}}" % jinja_string.value
# Else unquoted:
return "{{%s}}" % scalar_node.value
# Else process mapping as usual: # Else process mapping as usual:
return loader.construct_mapping(node) return loader.construct_mapping(node)

Loading…
Cancel
Save