|
@@ -14,7 +14,7 @@ from jinja2.environment import TemplateModule
|
|
|
from jinja2.exceptions import TemplateSyntaxError
|
|
from jinja2.exceptions import TemplateSyntaxError
|
|
|
from jinja2.filters import _GroupTuple
|
|
from jinja2.filters import _GroupTuple
|
|
|
from jinja2.meta import find_referenced_templates
|
|
from jinja2.meta import find_referenced_templates
|
|
|
-from jinja2.runtime import BlockReference, LoopContext, Macro, TemplateReference, Undefined
|
|
|
|
|
|
|
+from jinja2.runtime import BlockReference, LoopContext, Macro, StrictUndefined, TemplateReference, Undefined
|
|
|
from jinja2.sandbox import SandboxedEnvironment, SecurityError
|
|
from jinja2.sandbox import SandboxedEnvironment, SecurityError
|
|
|
from jinja2.utils import Cycler, Joiner, Namespace
|
|
from jinja2.utils import Cycler, Joiner, Namespace
|
|
|
from markupsafe import Markup
|
|
from markupsafe import Markup
|
|
@@ -25,6 +25,8 @@ from netbox.constants import TEMPLATE_RENDERING_DEFAULT, TEMPLATE_RENDERING_FEAT
|
|
|
from netbox.registry import registry
|
|
from netbox.registry import registry
|
|
|
from utilities.template_objects import (
|
|
from utilities.template_objects import (
|
|
|
NATIVE_VALUE_TYPES,
|
|
NATIVE_VALUE_TYPES,
|
|
|
|
|
+ ChoiceValue,
|
|
|
|
|
+ RelatedObjectList,
|
|
|
TemplateObject,
|
|
TemplateObject,
|
|
|
TemplateObjectList,
|
|
TemplateObjectList,
|
|
|
get_query_function,
|
|
get_query_function,
|
|
@@ -124,7 +126,7 @@ class RestrictedSandboxedEnvironment(SandboxedEnvironment):
|
|
|
ReturnDict, ReturnList, # Serializer output
|
|
ReturnDict, ReturnList, # Serializer output
|
|
|
Markup, _GroupTuple, # Escaped strings and groupby() results
|
|
Markup, _GroupTuple, # Escaped strings and groupby() results
|
|
|
*NATIVE_VALUE_TYPES, # IP addresses, dates and times
|
|
*NATIVE_VALUE_TYPES, # IP addresses, dates and times
|
|
|
- TemplateObject, TemplateObjectList,
|
|
|
|
|
|
|
+ TemplateObject, TemplateObjectList, ChoiceValue, RelatedObjectList,
|
|
|
)
|
|
)
|
|
|
# Jinja's own template objects, matched including subclasses (e.g. StrictUndefined)
|
|
# Jinja's own template objects, matched including subclasses (e.g. StrictUndefined)
|
|
|
allowed_template_types = (
|
|
allowed_template_types = (
|
|
@@ -156,6 +158,8 @@ class RestrictedSandboxedEnvironment(SandboxedEnvironment):
|
|
|
return self.is_allowed(getattr(obj, '__self__', obj))
|
|
return self.is_allowed(getattr(obj, '__self__', obj))
|
|
|
|
|
|
|
|
def getattr(self, obj, attribute):
|
|
def getattr(self, obj, attribute):
|
|
|
|
|
+ if obj is None:
|
|
|
|
|
+ return self.none_undefined(attribute)
|
|
|
if type(obj) in self.item_only_types:
|
|
if type(obj) in self.item_only_types:
|
|
|
return super().getitem(obj, attribute)
|
|
return super().getitem(obj, attribute)
|
|
|
if not self.is_allowed(obj):
|
|
if not self.is_allowed(obj):
|
|
@@ -163,10 +167,18 @@ class RestrictedSandboxedEnvironment(SandboxedEnvironment):
|
|
|
return super().getattr(obj, attribute)
|
|
return super().getattr(obj, attribute)
|
|
|
|
|
|
|
|
def getitem(self, obj, argument):
|
|
def getitem(self, obj, argument):
|
|
|
|
|
+ if obj is None:
|
|
|
|
|
+ return self.none_undefined(argument)
|
|
|
if not (self.is_allowed(obj) or type(obj) in self.item_only_types):
|
|
if not (self.is_allowed(obj) or type(obj) in self.item_only_types):
|
|
|
return self.unsafe_undefined(obj, argument)
|
|
return self.unsafe_undefined(obj, argument)
|
|
|
return super().getitem(obj, argument)
|
|
return super().getitem(obj, argument)
|
|
|
|
|
|
|
|
|
|
+ def none_undefined(self, name):
|
|
|
|
|
+ # An attribute of None (e.g. object.tenant.name for a null tenant) renders empty, as in legacy mode, even where
|
|
|
|
|
+ # undefined names and attributes are otherwise strict
|
|
|
|
|
+ undefined = Undefined if self.undefined is StrictUndefined else self.undefined
|
|
|
|
|
+ return undefined(obj=None, name=name)
|
|
|
|
|
+
|
|
|
def unsafe_undefined(self, obj, attribute):
|
|
def unsafe_undefined(self, obj, attribute):
|
|
|
# Fail rather than rendering an empty string
|
|
# Fail rather than rendering an empty string
|
|
|
raise SecurityError(f"Access to attribute {attribute!r} of {type(obj).__name__!r} object is not permitted.")
|
|
raise SecurityError(f"Access to attribute {attribute!r} of {type(obj).__name__!r} object is not permitted.")
|
|
@@ -226,14 +238,16 @@ def render_jinja2(template_code, context, environment_params=None, data_file=Non
|
|
|
The optional `filters` argument is a mapping of additional Jinja2 filters to make available for this render only
|
|
The optional `filters` argument is a mapping of additional Jinja2 filters to make available for this render only
|
|
|
(e.g. context-specific sanitization filters). These take precedence over the default and user-configured filters.
|
|
(e.g. context-specific sanitization filters). These take precedence over the default and user-configured filters.
|
|
|
|
|
|
|
|
- Pass a RestrictedRenderState as `restricted` to render in the restricted environment. If it has a user, the
|
|
|
|
|
- query() function is available.
|
|
|
|
|
|
|
+ Pass a RestrictedRenderState as `restricted` to render in the restricted environment, which uses StrictUndefined
|
|
|
|
|
+ unless `environment_params` sets `undefined`. If the state has a user, the query() function is available.
|
|
|
"""
|
|
"""
|
|
|
if restricted is not None and not isinstance(restricted, RestrictedRenderState):
|
|
if restricted is not None and not isinstance(restricted, RestrictedRenderState):
|
|
|
raise TypeError(f"restricted must be a RestrictedRenderState or None (found {type(restricted).__name__})")
|
|
raise TypeError(f"restricted must be a RestrictedRenderState or None (found {type(restricted).__name__})")
|
|
|
environment_params = dict(environment_params or {})
|
|
environment_params = dict(environment_params or {})
|
|
|
if restricted is not None:
|
|
if restricted is not None:
|
|
|
environment_params.pop('finalize', None)
|
|
environment_params.pop('finalize', None)
|
|
|
|
|
+ # Fail on undefined names and attributes (e.g. a misspelled field) rather than rendering an empty string
|
|
|
|
|
+ environment_params.setdefault('undefined', StrictUndefined)
|
|
|
|
|
|
|
|
if debug:
|
|
if debug:
|
|
|
extensions = list(environment_params.get('extensions', []))
|
|
extensions = list(environment_params.get('extensions', []))
|