Example #1
0
 def assert_failure(self, res, code=None):
     # counterintuitively, failure to login will return a 200
     # (compared to a redirect).
     self.assertEqual(res.status, 200)
     # recaptcha is done entirely in JS
     if code != "BAD_CAPTCHA":
         self.assertTrue(error_list[code] in _force_unicode(res.body))
Example #2
0
 def add(cls, link, text):
     name = c.user.name if c.user_is_loggedin else "<AUTOMATED>"
     now = datetime.now(g.tz).strftime("%Y-%m-%d %H:%M:%S")
     text = "[%s: %s] %s" % (name, now, text)
     rowkey = cls._rowkey(link)
     column = {uuid1(): _force_unicode(text)}
     cls._set_values(rowkey, column)
     return text
Example #3
0
def conditional_websafe(text=''):
    from wrapped import Templated, CacheStub

    if text.__class__ == _Unsafe:
        return text
    elif isinstance(text, Templated):
        return _Unsafe(text.render())
    elif isinstance(text, CacheStub):
        return _Unsafe(text)
    elif text is None:
        return ""
    elif text.__class__ != unicode:
        text = _force_unicode(text)
    return c_websafe(text)
Example #4
0
def scriptsafe_dumps(obj, **kwargs):
    """
    Like `json.dumps()`, but safe for use in `<script>` blocks.

    Also nice for response bodies that might be consumed by terrible browsers!

    You should avoid using this to template data into inline event handlers.
    When possible, you should do something like this instead:
    ```
    <button
      onclick="console.log($(this).data('json-thing'))"
      data-json-thing="${json_thing}">
    </button>
    ```
    """
    text = _force_unicode(json.dumps(obj, **kwargs))
    # wrap the response in _Unsafe so conditional_websafe doesn't touch it
    # TODO: this might be a hot path soon, C-ify it?
    return _Unsafe(text.translate(_json_escapes))
Example #5
0
def jssafe(text=u''):
    """Prevents text from breaking outside of string literals in JS"""
    if text.__class__ != unicode:
        text = _force_unicode(text)
    # wrap the response in _Unsafe so conditional_websafe doesn't touch it
    return _Unsafe(text.translate(_js_escapes))
Example #6
0
def websafe(text=''):
    if text.__class__ != unicode:
        text = _force_unicode(text)
    #wrap the response in _Unsafe so make_websafe doesn't unescape it
    return _Unsafe(c_websafe(text))
Example #7
0
def websafe_json(text=""):
    return c_websafe_json(_force_unicode(text))
Example #8
0
def unsafe(text=''):
    return _Unsafe(_force_unicode(text))