Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some validation for the JSONP callback #1627

Merged
merged 1 commit into from
Apr 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pyramid/renderers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextlib
import json
import os
import re

from zope.interface import (
implementer,
Expand All @@ -23,6 +24,8 @@

from pyramid.events import BeforeRender

from pyramid.httpexceptions import HTTPBadRequest

from pyramid.path import caller_package

from pyramid.response import _get_response_factory
Expand Down Expand Up @@ -308,6 +311,8 @@ def default(obj):

json_renderer_factory = JSON() # bw compat

JSONP_VALID_CALLBACK = re.compile(r"^[a-zA-Z_$][0-9a-zA-Z_$]+$")

class JSONP(JSON):
""" `JSONP <http://en.wikipedia.org/wiki/JSONP>`_ renderer factory helper
which implements a hybrid json/jsonp renderer. JSONP is useful for
Expand Down Expand Up @@ -388,7 +393,11 @@ def _render(value, system):
body = val
if request is not None:
callback = request.GET.get(self.param_name)

if callback is not None:
if not JSONP_VALID_CALLBACK.match(callback):
raise HTTPBadRequest('Invalid JSONP callback function name.')

ct = 'application/javascript'
body = '%s(%s);' % (callback, val)
response = request.response
Expand Down
8 changes: 8 additions & 0 deletions pyramid/tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ def test_render_without_request(self):
result = renderer({'a':'1'}, {})
self.assertEqual(result, '{"a": "1"}')

def test_render_to_jsonp_invalid_callback(self):
from pyramid.httpexceptions import HTTPBadRequest
renderer_factory = self._makeOne()
renderer = renderer_factory(None)
request = testing.DummyRequest()
request.GET['callback'] = '78mycallback'
self.assertRaises(HTTPBadRequest, renderer, {'a':'1'}, {'request':request})


class Dummy:
pass
Expand Down