misc: don't expose extra keys with invalid format as variables (#53345)

This commit is contained in:
Frédéric Péters 2021-04-22 13:57:32 +02:00
parent e407a3c8af
commit 711cecc69a
2 changed files with 13 additions and 10 deletions

View File

@ -2635,8 +2635,8 @@ def test_lazy_structured_items(pub, variable_test_data):
'type': 'formula',
'value': repr(
[
{'id': '1', 'text': 'un', 'more': 'foo', 'url': 'xxx'},
{'id': '2', 'text': 'deux', 'more': 'bar', 'url': 'yyy'},
{'id': '1', 'text': 'un', 'more': 'foo', 'url': 'xxx', 'invalid:key': 'xxx'},
{'id': '2', 'text': 'deux', 'more': 'bar', 'url': 'yyy', 'invalid:key': 'yyy'},
]
),
}
@ -2680,14 +2680,16 @@ def test_lazy_structured_items(pub, variable_test_data):
tmpl = Template('{% for x in form_var_plop_structured %}{{x.more}}{% endfor %}')
assert tmpl.render(context) == 'foobar'
assert 'form_var_plop_0_url' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop_1_more' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop_structured' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop_raw' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
flat_keys = pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop_0_url' in flat_keys
assert 'form_var_plop_1_more' in flat_keys
assert 'form_var_plop_structured' in flat_keys
assert 'form_var_plop_raw' in flat_keys
assert 'form_var_plop_0_invalid:key' not in flat_keys
assert 'form_var_plop2' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop2_raw' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop2_structured' in pub.substitutions.get_context_variables(mode='lazy').get_flat_keys()
assert 'form_var_plop2' in flat_keys
assert 'form_var_plop2_raw' in flat_keys
assert 'form_var_plop2_structured' in flat_keys
def test_formdata_user_field(pub, variable_test_data):

View File

@ -804,7 +804,8 @@ class LazyFieldVarStructured(LazyFieldVar):
def walk(base, value):
if isinstance(value, dict):
for k, v in value.items():
walk(k if not base else base + '_' + k, v)
if CompatibilityNamesDict.valid_key_regex.match(k):
walk(k if not base else base + '_' + k, v)
else:
keys.append(base)