ó
 `¾Tc           @   sß   d  Z  d d l Z d d l m Z d d l m Z d d l m Z m Z m	 Z	 d d l m
 Z
 m Z d d l m Z m Z m Z d d l m Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d „  Z d S(   sÐ   
    jinja2.testsuite.security
    ~~~~~~~~~~~~~~~~~~~~~~~~~

    Checks the sandbox and other security features.

    :copyright: (c) 2010 by the Jinja Team.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   JinjaTestCase(   t   Environment(   t   SandboxedEnvironmentt   ImmutableSandboxedEnvironmentt   unsafe(   t   Markupt   escape(   t   SecurityErrort   TemplateSyntaxErrort   TemplateRuntimeError(   t	   text_typet   PrivateStuffc           B   s)   e  Z d  „  Z e d „  ƒ Z d „  Z RS(   c         C   s   d S(   Ni   (    (   t   self(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   bar   s    c         C   s   d S(   Ni*   (    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   foo   s    c         C   s   d S(   NR   (    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   __repr__!   s    (   t   __name__t
   __module__R   R   R   R   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR      s   	t   PublicStuffc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   d S(   Ni   (    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   <lambda>&   s    c         C   s   d S(   Ni*   (    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR   '   s    c         C   s   d S(   NR   (    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR   )   s    (   R   R   R   t   _fooR   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR   %   s   		t   SandboxTestCasec           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s  t  ƒ  } |  j t | j d ƒ j d t ƒ  ƒ|  j | j d ƒ j d t ƒ  ƒ d ƒ |  j t | j d ƒ j d t ƒ  ƒ|  j | j d ƒ j d t ƒ  ƒ d ƒ |  j | j d ƒ j d d ƒ d ƒ |  j | j d	 ƒ j d d
 „  ƒ d ƒ |  j t | j d ƒ j d d ƒd  S(   Ns   {{ foo.foo() }}R   s   {{ foo.bar() }}t   23s   {{ foo._foo() }}s   {{ foo.__class__ }}i*   t    s   {{ foo.func_code }}c           S   s   d  S(   N(   t   None(    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR   9   s    s$   {{ foo.__class__.__subclasses__() }}(   R   t   assert_raisesR   t   from_stringt   renderR   t   assert_equalR   (   R   t   env(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_unsafe/   s    	
(
(%(c         C   sE   t  ƒ  } |  j t | j d ƒ j ƒ |  j t | j d ƒ j ƒ d  S(   Ns   {{ [].append(23) }}s   {{ {1:2}.clear() }}(   R   R   R   R   R   (   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_immutable_environment>   s
    	c         C   s9   t  ƒ  } |  j t | j d ƒ |  j t | j d ƒ d  S(   Ns.   {% for item.attribute in seq %}...{% endfor %}s,   {% for foo, bar.baz in seq %}...{% endfor %}(   R   R   R   R   (   R   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_restrictedE   s
    	c         C   sk  d } t  d ƒ } | | t t | ƒ ƒ t | ƒ k s> t ‚ t  d ƒ d d k sZ t ‚ t  d ƒ i d d 6d k s} t ‚ t t  d ƒ d	 ƒ t  k sŸ t ‚ t  d ƒ } | j ƒ  | k sÃ t ‚ d
 t f d „  ƒ  Y} t  | ƒ  ƒ d k sô t ‚ t  d ƒ | ƒ  d k st ‚ t d ƒ d k s+t ‚ t  d ƒ j ƒ  d k sIt ‚ t  d ƒ j ƒ  d k sgt ‚ d  S(   Ns?   <script type="application/x-some-script">alert("foo");</script>s   <em>username</em>s   <em>%s</em>s
   <bad user>s   <em>&lt;bad user&gt;</em>s   <em>%(username)s</em>t   usernameR   R   t   Fooc           B   s   e  Z d  „  Z d „  Z RS(   c         S   s   d S(   Ns   <em>awesome</em>(    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   __html__b   s    c         S   s   d S(   Nt   awesome(    (   R   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   __unicode__d   s    (   R   R   R#   R%   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR"   a   s   	s   <em>awesome</em>s   <strong>%s</strong>s!   <strong><em>awesome</em></strong>s   "<>&'s   &#34;&lt;&gt;&amp;&#39;s   <em>Foo &amp; Bar</em>s	   Foo & Bars   &lt;test&gt;s   <test>(	   R   R
   R   t   AssertionErrort   typeR#   t   objectt	   striptagst   unescape(   R   R   t   safet   xR"   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_markup_operationsL   s$    ,"c         C   s¸   t  d t ƒ } | j d ƒ } d } | j ƒ  | k s< t ‚ t | j ƒ | k sW t ‚ t | j ƒ | k sr t ‚ | j j d ƒ | k s t ‚ t | j j d ƒ ƒ | k s´ t ‚ d  S(   Nt
   autoescapesf   {% macro say_hello(name) %}<p>Hello {{ name }}!</p>{% endmacro %}{{ say_hello("<blink>foo</blink>") }}s,   <p>Hello &lt;blink&gt;foo&lt;/blink&gt;!</p>s   <blink>foo</blink>(	   R   t   TrueR   R   R&   R
   t   moduleR   t	   say_hello(   R   R   t   tt   escaped_out(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_template_datao   s    c         C   s5   t  ƒ  } | j d ƒ } |  j t | j d t ƒd  S(   Ns"   {{ cls|attr("__subclasses__")() }}t   cls(   R   R   R   R   R   t   int(   R   R   t   tmpl(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   test_attr_filter{   s    	c         C   sã   d „  } xÓ d i  d f d i d d 6d f f D]¬ \ } } } t  ƒ  } | | j d <| j d	 | ƒ } | j | ƒ | k s‚ t ‚ t d g ƒ | _ | j d	 | ƒ } y | j | ƒ Wn t k
 rÍ } q/ X|  j d
 ƒ q/ Wd  S(   Nc         S   s   t  d ƒ ‚ d  S(   Ns   that operator so does not work(   R	   (   t   leftt   right(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt
   disable_op   s    s   1 + 2t   3s   a + 2i   t   at   4t   +s   {{ %s }}s   expected runtime error(	   R   t   binop_tableR   R   R&   t	   frozensett   intercepted_binopsR	   t   fail(   R   R;   t   exprt   ctxt   rvR   R2   t   e(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt!   test_binary_operator_intercepting€   s    	5	c         C   sã   d „  } xÓ d i  d f d i d d 6d f f D]¬ \ } } } t  ƒ  } | | j d <| j d | ƒ } | j | ƒ | k s‚ t ‚ t d g ƒ | _ | j d | ƒ } y | j | ƒ Wn t k
 rÍ } q/ X|  j d	 ƒ q/ Wd  S(
   Nc         S   s   t  d ƒ ‚ d  S(   Ns   that operator so does not work(   R	   (   t   arg(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR;   ’   s    s   -1s   -ai   R=   s   -2t   -s   {{ %s }}s   expected runtime error(	   R   t
   unop_tableR   R   R&   RA   t   intercepted_unopsR	   RC   (   R   R;   RD   RE   RF   R   R2   RG   (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt    test_unary_operator_intercepting‘   s    	5	(
   R   R   R   R   R    R-   R4   R8   RH   RM   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyR   -   s   				#			c          C   s&   t  j ƒ  }  |  j t  j t ƒ ƒ |  S(   N(   t   unittestt	   TestSuitet   addTestt	   makeSuiteR   (   t   suite(    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyRR   £   s    (   t   __doc__RN   t   jinja2.testsuiteR    t   jinja2R   t   jinja2.sandboxR   R   R   R   R   t   jinja2.exceptionsR   R   R	   t   jinja2._compatR
   R(   R   R   R   RR   (    (    (    s_   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/jinja2/testsuite/security.pyt   <module>
   s   v