Skip to content

Commit 7a774f6

Browse files
fix: issue where templated error messages would not correctly format when passing in parameter values
* fix: fix issue where templated error messages would not correctly format when passing in parameter values * style: remove old implementation for jinja env setup * fix: resolve issue with jinja2 environment and security concern around autoescape handling in jinja templates
1 parent f976e26 commit 7a774f6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/dve/core_engine/backends/metadata/reporting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BaseReportingConfig(BaseModel):
2828
2929
"""
3030

31-
UNTEMPLATED_FIELDS: ClassVar[set[str]] = {"message"}
31+
UNTEMPLATED_FIELDS: ClassVar[set[str]] = set()
3232
"""Fields that should not be templated."""
3333

3434
emit: Optional[str] = None

src/dve/core_engine/templating.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
from dve.core_engine.type_hints import JSONable, TemplateVariables
1212

1313

14+
class PreserveTemplateUndefined(jinja2.Undefined):
15+
"""
16+
Preserve the original template in instances where the value cannot be populated. Whilst this
17+
may result in templates coming back in the FeedbackMessage object, it's more useful to know
18+
exactly what should have been populated rather than just returning blank values.
19+
"""
20+
def __str__(self):
21+
return "{{" + self._undefined_name + "}}"
22+
23+
1424
class RuleTemplateError(ValueError):
1525
"""A rule template error."""
1626

@@ -21,7 +31,10 @@ def _raise_rule_templating_error(message: str) -> NoReturn:
2131

2232

2333
T = TypeVar("T", bound=JSONable)
24-
ENVIRONMENT = jinja2.Environment(autoescape=False)
34+
ENVIRONMENT = jinja2.Environment(
35+
autoescape=jinja2.select_autoescape(default_for_string=False),
36+
undefined=PreserveTemplateUndefined,
37+
)
2538
ENVIRONMENT.globals["repr"] = repr
2639
ENVIRONMENT.globals["str"] = str
2740
ENVIRONMENT.globals["raise"] = _raise_rule_templating_error

0 commit comments

Comments
 (0)