|
|
6fca8f |
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
|
|
|
6fca8f |
index c5697e6..9465943 100644
|
|
|
6fca8f |
--- a/jinja2/nodes.py
|
|
|
6fca8f |
+++ b/jinja2/nodes.py
|
|
|
6fca8f |
@@ -599,7 +599,7 @@ class Call(Expr):
|
|
|
6fca8f |
|
|
|
6fca8f |
def as_const(self, eval_ctx=None):
|
|
|
6fca8f |
eval_ctx = get_eval_context(self, eval_ctx)
|
|
|
6fca8f |
- if eval_ctx.volatile:
|
|
|
6fca8f |
+ if eval_ctx.volatile or eval_ctx.environment.sandboxed:
|
|
|
6fca8f |
raise Impossible()
|
|
|
6fca8f |
obj = self.node.as_const(eval_ctx)
|
|
|
6fca8f |
|
|
|
6fca8f |
diff --git a/jinja2/sandbox.py b/jinja2/sandbox.py
|
|
|
6fca8f |
index da479c1..7e31a7a 100644
|
|
|
6fca8f |
--- a/jinja2/sandbox.py
|
|
|
6fca8f |
+++ b/jinja2/sandbox.py
|
|
|
6fca8f |
@@ -12,12 +12,19 @@
|
|
|
6fca8f |
:copyright: (c) 2010 by the Jinja Team.
|
|
|
6fca8f |
:license: BSD.
|
|
|
6fca8f |
"""
|
|
|
6fca8f |
+import types
|
|
|
6fca8f |
import operator
|
|
|
6fca8f |
+from collections import Mapping
|
|
|
6fca8f |
from jinja2.environment import Environment
|
|
|
6fca8f |
from jinja2.exceptions import SecurityError
|
|
|
6fca8f |
from jinja2._compat import string_types, function_type, method_type, \
|
|
|
6fca8f |
- traceback_type, code_type, frame_type, generator_type, PY2
|
|
|
6fca8f |
+ traceback_type, code_type, frame_type, generator_type, text_type, PY2
|
|
|
6fca8f |
+from jinja2.utils import Markup
|
|
|
6fca8f |
|
|
|
6fca8f |
+has_format = False
|
|
|
6fca8f |
+if hasattr(text_type, 'format'):
|
|
|
6fca8f |
+ from string import Formatter
|
|
|
6fca8f |
+ has_format = True
|
|
|
6fca8f |
|
|
|
6fca8f |
#: maximum number of items a range may produce
|
|
|
6fca8f |
MAX_RANGE = 100000
|
|
|
6fca8f |
@@ -32,6 +39,12 @@ UNSAFE_METHOD_ATTRIBUTES = set(['im_class', 'im_func', 'im_self'])
|
|
|
6fca8f |
#: unsafe generator attirbutes.
|
|
|
6fca8f |
UNSAFE_GENERATOR_ATTRIBUTES = set(['gi_frame', 'gi_code'])
|
|
|
6fca8f |
|
|
|
6fca8f |
+#: unsafe attributes on coroutines
|
|
|
6fca8f |
+UNSAFE_COROUTINE_ATTRIBUTES = set(['cr_frame', 'cr_code'])
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+#: unsafe attributes on async generators
|
|
|
6fca8f |
+UNSAFE_ASYNC_GENERATOR_ATTRIBUTES = set(['ag_code', 'ag_frame'])
|
|
|
6fca8f |
+
|
|
|
6fca8f |
# On versions > python 2 the special attributes on functions are gone,
|
|
|
6fca8f |
# but they remain on methods and generators for whatever reason.
|
|
|
6fca8f |
if not PY2:
|
|
|
6fca8f |
@@ -92,6 +105,79 @@ _mutable_spec = (
|
|
|
6fca8f |
]))
|
|
|
6fca8f |
)
|
|
|
6fca8f |
|
|
|
6fca8f |
+# Bundled EscapeFormatter class from markupsafe >= 0.21 which is used by
|
|
|
6fca8f |
+# jinja2 for fixing CVE-2016-10745
|
|
|
6fca8f |
+# Copyright 2010 Pallets
|
|
|
6fca8f |
+# BSD 3-Clause License
|
|
|
6fca8f |
+# https://github.com/pallets/markupsafe/blob/79ee6ce0ed93c6da73512f069d7db866d955df04/LICENSE.rst
|
|
|
6fca8f |
+if hasattr(text_type, "format"):
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ class EscapeFormatter(Formatter):
|
|
|
6fca8f |
+ def __init__(self, escape):
|
|
|
6fca8f |
+ self.escape = escape
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def format_field(self, value, format_spec):
|
|
|
6fca8f |
+ if hasattr(value, "__html_format__"):
|
|
|
6fca8f |
+ rv = value.__html_format__(format_spec)
|
|
|
6fca8f |
+ elif hasattr(value, "__html__"):
|
|
|
6fca8f |
+ if format_spec:
|
|
|
6fca8f |
+ raise ValueError(
|
|
|
6fca8f |
+ "Format specifier {0} given, but {1} does not"
|
|
|
6fca8f |
+ " define __html_format__. A class that defines"
|
|
|
6fca8f |
+ " __html__ must define __html_format__ to work"
|
|
|
6fca8f |
+ " with format specifiers.".format(format_spec, type(value))
|
|
|
6fca8f |
+ )
|
|
|
6fca8f |
+ rv = value.__html__()
|
|
|
6fca8f |
+ else:
|
|
|
6fca8f |
+ # We need to make sure the format spec is unicode here as
|
|
|
6fca8f |
+ # otherwise the wrong callback methods are invoked. For
|
|
|
6fca8f |
+ # instance a byte string there would invoke __str__ and
|
|
|
6fca8f |
+ # not __unicode__.
|
|
|
6fca8f |
+ rv = Formatter.format_field(self, value, text_type(format_spec))
|
|
|
6fca8f |
+ return text_type(self.escape(rv))
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+class _MagicFormatMapping(Mapping):
|
|
|
6fca8f |
+ """This class implements a dummy wrapper to fix a bug in the Python
|
|
|
6fca8f |
+ standard library for string formatting.
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ See http://bugs.python.org/issue13598 for information about why
|
|
|
6fca8f |
+ this is necessary.
|
|
|
6fca8f |
+ """
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def __init__(self, args, kwargs):
|
|
|
6fca8f |
+ self._args = args
|
|
|
6fca8f |
+ self._kwargs = kwargs
|
|
|
6fca8f |
+ self._last_index = 0
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def __getitem__(self, key):
|
|
|
6fca8f |
+ if key == '':
|
|
|
6fca8f |
+ idx = self._last_index
|
|
|
6fca8f |
+ self._last_index += 1
|
|
|
6fca8f |
+ try:
|
|
|
6fca8f |
+ return self._args[idx]
|
|
|
6fca8f |
+ except LookupError:
|
|
|
6fca8f |
+ pass
|
|
|
6fca8f |
+ key = str(idx)
|
|
|
6fca8f |
+ return self._kwargs[key]
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def __iter__(self):
|
|
|
6fca8f |
+ return iter(self._kwargs)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def __len__(self):
|
|
|
6fca8f |
+ return len(self._kwargs)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+def inspect_format_method(callable):
|
|
|
6fca8f |
+ if not has_format:
|
|
|
6fca8f |
+ return None
|
|
|
6fca8f |
+ if not isinstance(callable, (types.MethodType,
|
|
|
6fca8f |
+ types.BuiltinMethodType)) or \
|
|
|
6fca8f |
+ callable.__name__ != 'format':
|
|
|
6fca8f |
+ return None
|
|
|
6fca8f |
+ obj = callable.__self__
|
|
|
6fca8f |
+ if isinstance(obj, string_types):
|
|
|
6fca8f |
+ return obj
|
|
|
6fca8f |
+
|
|
|
6fca8f |
|
|
|
6fca8f |
def safe_range(*args):
|
|
|
6fca8f |
"""A range that can't generate ranges with a length of more than
|
|
|
6fca8f |
@@ -146,6 +232,12 @@ def is_internal_attribute(obj, attr):
|
|
|
6fca8f |
elif isinstance(obj, generator_type):
|
|
|
6fca8f |
if attr in UNSAFE_GENERATOR_ATTRIBUTES:
|
|
|
6fca8f |
return True
|
|
|
6fca8f |
+ elif hasattr(types, 'CoroutineType') and isinstance(obj, types.CoroutineType):
|
|
|
6fca8f |
+ if attr in UNSAFE_COROUTINE_ATTRIBUTES:
|
|
|
6fca8f |
+ return True
|
|
|
6fca8f |
+ elif hasattr(types, 'AsyncGeneratorType') and isinstance(obj, types.AsyncGeneratorType):
|
|
|
6fca8f |
+ if attri in UNSAFE_ASYNC_GENERATOR_ATTRIBUTES:
|
|
|
6fca8f |
+ return True
|
|
|
6fca8f |
return attr.startswith('__')
|
|
|
6fca8f |
|
|
|
6fca8f |
|
|
|
6fca8f |
@@ -184,8 +276,8 @@ class SandboxedEnvironment(Environment):
|
|
|
6fca8f |
attributes or functions are safe to access.
|
|
|
6fca8f |
|
|
|
6fca8f |
If the template tries to access insecure code a :exc:`SecurityError` is
|
|
|
6fca8f |
- raised. However also other exceptions may occour during the rendering so
|
|
|
6fca8f |
- the caller has to ensure that all exceptions are catched.
|
|
|
6fca8f |
+ raised. However also other exceptions may occur during the rendering so
|
|
|
6fca8f |
+ the caller has to ensure that all exceptions are caught.
|
|
|
6fca8f |
"""
|
|
|
6fca8f |
sandboxed = True
|
|
|
6fca8f |
|
|
|
6fca8f |
@@ -347,8 +439,24 @@ class SandboxedEnvironment(Environment):
|
|
|
6fca8f |
obj.__class__.__name__
|
|
|
6fca8f |
), name=attribute, obj=obj, exc=SecurityError)
|
|
|
6fca8f |
|
|
|
6fca8f |
+ def format_string(self, s, args, kwargs):
|
|
|
6fca8f |
+ """If a format call is detected, then this is routed through this
|
|
|
6fca8f |
+ method so that our safety sandbox can be used for it.
|
|
|
6fca8f |
+ """
|
|
|
6fca8f |
+ if isinstance(s, Markup):
|
|
|
6fca8f |
+ formatter = SandboxedEscapeFormatter(self, s.escape)
|
|
|
6fca8f |
+ else:
|
|
|
6fca8f |
+ formatter = SandboxedFormatter(self)
|
|
|
6fca8f |
+ kwargs = _MagicFormatMapping(args, kwargs)
|
|
|
6fca8f |
+ rv = formatter.vformat(s, args, kwargs)
|
|
|
6fca8f |
+ return type(s)(rv)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
def call(__self, __context, __obj, *args, **kwargs):
|
|
|
6fca8f |
"""Call an object from sandboxed code."""
|
|
|
6fca8f |
+ fmt = inspect_format_method(__obj)
|
|
|
6fca8f |
+ if fmt is not None:
|
|
|
6fca8f |
+ return __self.format_string(fmt, args, kwargs)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
# the double prefixes are to avoid double keyword argument
|
|
|
6fca8f |
# errors when proxying the call.
|
|
|
6fca8f |
if not __self.is_safe_callable(__obj):
|
|
|
6fca8f |
@@ -366,3 +474,37 @@ class ImmutableSandboxedEnvironment(SandboxedEnvironment):
|
|
|
6fca8f |
if not SandboxedEnvironment.is_safe_attribute(self, obj, attr, value):
|
|
|
6fca8f |
return False
|
|
|
6fca8f |
return not modifies_known_mutable(obj, attr)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+if has_format:
|
|
|
6fca8f |
+ # This really is not a public API apparenlty.
|
|
|
6fca8f |
+ try:
|
|
|
6fca8f |
+ from _string import formatter_field_name_split
|
|
|
6fca8f |
+ except ImportError:
|
|
|
6fca8f |
+ def formatter_field_name_split(field_name):
|
|
|
6fca8f |
+ return field_name._formatter_field_name_split()
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ class SandboxedFormatterMixin(object):
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def __init__(self, env):
|
|
|
6fca8f |
+ self._env = env
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ def get_field(self, field_name, args, kwargs):
|
|
|
6fca8f |
+ first, rest = formatter_field_name_split(field_name)
|
|
|
6fca8f |
+ obj = self.get_value(first, args, kwargs)
|
|
|
6fca8f |
+ for is_attr, i in rest:
|
|
|
6fca8f |
+ if is_attr:
|
|
|
6fca8f |
+ obj = self._env.getattr(obj, i)
|
|
|
6fca8f |
+ else:
|
|
|
6fca8f |
+ obj = self._env.getitem(obj, i)
|
|
|
6fca8f |
+ return obj, first
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ class SandboxedFormatter(SandboxedFormatterMixin, Formatter):
|
|
|
6fca8f |
+ def __init__(self, env):
|
|
|
6fca8f |
+ SandboxedFormatterMixin.__init__(self, env)
|
|
|
6fca8f |
+ Formatter.__init__(self)
|
|
|
6fca8f |
+
|
|
|
6fca8f |
+ class SandboxedEscapeFormatter(SandboxedFormatterMixin, EscapeFormatter):
|
|
|
6fca8f |
+ def __init__(self, env, escape):
|
|
|
6fca8f |
+ SandboxedFormatterMixin.__init__(self, env)
|
|
|
6fca8f |
+ EscapeFormatter.__init__(self, escape)
|