Merge branch 'master' of gitorious.org:mediagoblin/mediagoblin

This commit is contained in:
Christopher Allan Webber 2011-12-01 16:59:22 -06:00
commit 8e2af2843f
9 changed files with 47 additions and 17 deletions

View File

@ -107,12 +107,6 @@ class MediaGoblinApp(object):
def __call__(self, environ, start_response): def __call__(self, environ, start_response):
request = Request(environ) request = Request(environ)
# pass the request through our meddleware classes
for m in self.meddleware:
response = m.process_request(request)
if response is not None:
return response(environ, start_response)
## Routing / controller loading stuff ## Routing / controller loading stuff
path_info = request.path_info path_info = request.path_info
route_match = self.routing.match(path_info) route_match = self.routing.match(path_info)
@ -164,6 +158,13 @@ class MediaGoblinApp(object):
return render_404(request)(environ, start_response) return render_404(request)(environ, start_response)
controller = common.import_component(route_match['controller']) controller = common.import_component(route_match['controller'])
# pass the request through our meddleware classes
for m in self.meddleware:
response = m.process_request(request, controller)
if response is not None:
return response(environ, start_response)
request.start_response = start_response request.start_response = start_response
# get the response from the controller # get the response from the controller

View File

@ -28,7 +28,7 @@ class EditForm(wtforms.Form):
_('Tags'), _('Tags'),
[tag_length_validator], [tag_length_validator],
description=_( description=_(
"Seperate tags by commas or spaces.")) "Seperate tags by commas."))
slug = wtforms.TextField( slug = wtforms.TextField(
_('Slug'), _('Slug'),
[wtforms.validators.Required(message=_("The slug can't be empty"))], [wtforms.validators.Required(message=_("The slug can't be empty"))],

View File

@ -25,7 +25,7 @@ class BaseMeddleware(object):
def __init__(self, mg_app): def __init__(self, mg_app):
self.app = mg_app self.app = mg_app
def process_request(self, request): def process_request(self, request, controller):
pass pass
def process_response(self, request, response): def process_response(self, request, response):

View File

@ -31,6 +31,13 @@ else:
getrandbits = random.getrandbits getrandbits = random.getrandbits
def csrf_exempt(func):
"""Decorate a Controller to exempt it from CSRF protection."""
func.csrf_enabled = False
return func
class CsrfForm(Form): class CsrfForm(Form):
"""Simple form to handle rendering a CSRF token and confirming it """Simple form to handle rendering a CSRF token and confirming it
is included in the POST.""" is included in the POST."""
@ -58,7 +65,7 @@ class CsrfMeddleware(BaseMeddleware):
CSRF_KEYLEN = 64 CSRF_KEYLEN = 64
SAFE_HTTP_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE") SAFE_HTTP_METHODS = ("GET", "HEAD", "OPTIONS", "TRACE")
def process_request(self, request): def process_request(self, request, controller):
"""For non-safe requests, confirm that the tokens are present """For non-safe requests, confirm that the tokens are present
and match. and match.
""" """
@ -75,9 +82,11 @@ class CsrfMeddleware(BaseMeddleware):
# if this is a non-"safe" request (ie, one that could have # if this is a non-"safe" request (ie, one that could have
# side effects), confirm that the CSRF tokens are present and # side effects), confirm that the CSRF tokens are present and
# valid # valid
if request.method not in self.SAFE_HTTP_METHODS \ if (getattr(controller, 'csrf_enabled', True) and
and ('gmg.verify_csrf' in request.environ or request.method not in self.SAFE_HTTP_METHODS and
'paste.testing' not in request.environ): ('gmg.verify_csrf' in request.environ or
'paste.testing' not in request.environ)
):
return self.verify_tokens(request) return self.verify_tokens(request)

View File

@ -19,7 +19,8 @@ from mediagoblin.meddleware import BaseMeddleware
class NoOpMeddleware(BaseMeddleware): class NoOpMeddleware(BaseMeddleware):
def process_request(self, request):
def process_request(self, request, controller):
pass pass
def process_response(self, request, response): def process_response(self, request, response):

View File

@ -32,4 +32,4 @@ class SubmitStartForm(wtforms.Form):
_('Tags'), _('Tags'),
[tag_length_validator], [tag_length_validator],
description=_( description=_(
"Seperate tags by commas or spaces.")) "Seperate tags by commas."))

View File

@ -30,7 +30,7 @@
{{ wtforms_util.render_divs(cp_form) }} {{ wtforms_util.render_divs(cp_form) }}
<div class="form_submit_buttons"> <div class="form_submit_buttons">
<input type="submit" value="submit" class="button_form"/> <input type="submit" value="{% trans %}Submit{% endtrans %}" class="button_form"/>
</div> </div>
</div> </div>

View File

@ -47,7 +47,7 @@
<a href="{{ next_url }}"><img class="pagination_arrow" src="{{ request.staticdirect('/images/pagination_right.png') }}" alt="Next page" /></a> <a href="{{ next_url }}"><img class="pagination_arrow" src="{{ request.staticdirect('/images/pagination_right.png') }}" alt="Next page" /></a>
{% endif %} {% endif %}
<br /> <br />
Go to page: {% trans %}Go to page:{% endtrans %}
{%- for page in pagination.iter_pages() %} {%- for page in pagination.iter_pages() %}
{% if page %} {% if page %}
{% if page != pagination.page %} {% if page != pagination.page %}

View File

@ -69,3 +69,22 @@ def test_csrf_token_must_match(test_app):
mg_globals.app_config['csrf_cookie_name'])}, mg_globals.app_config['csrf_cookie_name'])},
extra_environ={'gmg.verify_csrf': True}).\ extra_environ={'gmg.verify_csrf': True}).\
status_int == 200 status_int == 200
@setup_fresh_app
def test_csrf_exempt(test_app):
# monkey with the views to decorate a known endpoint
import mediagoblin.auth.views
from mediagoblin.meddleware.csrf import csrf_exempt
mediagoblin.auth.views.login = csrf_exempt(
mediagoblin.auth.views.login
)
# construct a request with no cookie or form token
assert test_app.post('/auth/login/',
extra_environ={'gmg.verify_csrf': True},
expect_errors=False).status_int == 200
# restore the CSRF protection in case other tests expect it
mediagoblin.auth.views.login.csrf_enabled = True