Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.sandbox |
| 4 | ~~~~~~~~~~~~~~ |
| 5 | |
| 6 | Adds a sandbox layer to Jinja as it was the default behavior in the old |
| 7 | Jinja 1 releases. This sandbox is slightly different from Jinja 1 as the |
| 8 | default behavior is easier to use. |
| 9 | |
| 10 | The behavior can be changed by subclassing the environment. |
| 11 | |
| 12 | :copyright: Copyright 2008 by Armin Ronacher. |
| 13 | :license: BSD. |
| 14 | """ |
| 15 | from types import FunctionType, MethodType |
| 16 | from jinja2.runtime import Undefined |
| 17 | from jinja2.environment import Environment |
| 18 | |
| 19 | |
| 20 | #: maximum number of items a range may produce |
| 21 | MAX_RANGE = 100000 |
| 22 | |
| 23 | |
| 24 | def safe_range(*args): |
| 25 | """A range that can't generate ranges with a length of more than |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame^] | 26 | MAX_RANGE items. |
| 27 | """ |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 28 | rng = xrange(*args) |
| 29 | if len(rng) > MAX_RANGE: |
| 30 | raise OverflowError('range too big') |
| 31 | return rng |
| 32 | |
| 33 | |
| 34 | def unsafe(f): |
| 35 | """Mark a function as unsafe.""" |
| 36 | f.unsafe_callable = True |
| 37 | return f |
| 38 | |
| 39 | |
| 40 | class SandboxedEnvironment(Environment): |
| 41 | """The sandboxed environment""" |
| 42 | sandboxed = True |
| 43 | |
| 44 | def __init__(self, *args, **kwargs): |
| 45 | Environment.__init__(self, *args, **kwargs) |
| 46 | self.globals['range'] = safe_range |
| 47 | |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 48 | def is_safe_attribute(self, obj, attr, value): |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 49 | """The sandboxed environment will call this method to check if the |
| 50 | attribute of an object is safe to access. Per default all attributes |
| 51 | starting with an underscore are considered private as well as the |
| 52 | special attributes of functions and methods. |
| 53 | """ |
| 54 | if attr.startswith('_'): |
| 55 | return False |
| 56 | if isinstance(obj, FunctionType): |
| 57 | return not attr.startswith('func_') |
| 58 | if isinstance(obj, MethodType): |
| 59 | return not attr.startswith('im_') |
| 60 | return True |
| 61 | |
| 62 | def is_safe_callable(self, obj): |
| 63 | """Check if an object is safely callable. Per default a function is |
| 64 | considered safe unless the `unsafe_callable` attribute exists and is |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame^] | 65 | True. Override this method to alter the behavior, but this won't |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 66 | affect the `unsafe` decorator from this module. |
| 67 | """ |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame^] | 68 | return not (getattr(obj, 'unsafe_callable', False) or \ |
| 69 | getattr(obj, 'alters_data', False)) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 70 | |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 71 | def subscribe(self, obj, argument): |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 72 | """Subscribe an object from sandboxed code.""" |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 73 | is_unsafe = False |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 74 | try: |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 75 | value = getattr(obj, str(argument)) |
| 76 | except (AttributeError, UnicodeError): |
| 77 | pass |
| 78 | else: |
| 79 | if self.is_safe_attribute(obj, argument, value): |
| 80 | return value |
| 81 | is_unsafe = True |
| 82 | try: |
| 83 | return obj[argument] |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 84 | except (TypeError, LookupError): |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 85 | if is_unsafe: |
| 86 | return self.undefined('access to attribute %r of %r object is' |
| 87 | ' unsafe.' % ( |
| 88 | argument, |
| 89 | obj.__class__.__name__ |
| 90 | )) |
| 91 | return self.undefined(obj=obj, name=argument) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 92 | |
| 93 | def call(__self, __obj, *args, **kwargs): |
| 94 | """Call an object from sandboxed code.""" |
| 95 | # the double prefixes are to avoid double keyword argument |
| 96 | # errors when proxying the call. |
| 97 | if not __self.is_safe_callable(__obj): |
| 98 | raise TypeError('%r is not safely callable' % (__obj,)) |
| 99 | return __obj(*args, **kwargs) |