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

Fix 500s due to bad form data #308

Merged
merged 4 commits into from May 10, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions liberapay/main.py
Expand Up @@ -4,6 +4,7 @@
from six.moves.urllib.parse import quote as urlquote

import aspen
import aspen.http.mapping

from liberapay import canonize, fill_accept_header, insert_constants, utils, wireup
from liberapay.cron import Cron
Expand Down Expand Up @@ -125,8 +126,16 @@ def return_500_for_exception(website, exception):
]


# Monkey patch aspen.Response
# ===========================
# Monkey patch aspen
# ==================

pop = aspen.http.mapping.Mapping.pop
def _pop(self, name, default=aspen.http.mapping.NO_DEFAULT):
try:
return pop(self, name, default)
except KeyError:
raise aspen.Response(400, "Missing key: %s" % repr(name))
aspen.http.mapping.Mapping.pop = _pop

if hasattr(aspen.Response, 'redirect'):
raise Warning('aspen.Response.redirect() already exists')
Expand Down
5 changes: 4 additions & 1 deletion tests/py/test_sign_in.py
Expand Up @@ -162,6 +162,9 @@ def test_email_login_bad_token(self):
def sign_in(self, custom):
data = dict(good_data)
for k, v in custom.items():
if v is None:
del data['sign-in.'+k]
continue
data['sign-in.'+k] = v
return self.client.POST('/sign-in', data, raise_immediately=False)

Expand Down Expand Up @@ -207,5 +210,5 @@ def test_sign_in_bad_email(self):
assert r.code == 400

def test_sign_in_terms_not_checked(self):
r = self.sign_in(dict(terms=''))
r = self.sign_in(dict(terms=None))
assert r.code == 400
8 changes: 7 additions & 1 deletion www/%username/identity.spt
Expand Up @@ -39,11 +39,17 @@ if request.method == 'POST':
v = body.get(k)
if v:
setattr(account, k, v)
else:
error = _("You haven't filled all the required fields.")
break

dob = body.get(p+'Birthday', '')
if dob:
try:
dt = datetime(*map(int, dob.split('-')))
year, month, day = map(int, dob.split('-'))
# the above raises ValueError if the number of parts isn't 3
# or if any part isn't an integer
dt = datetime(year, month, day)
setattr(account, p+'Birthday', (dt - EPOCH).total_seconds())
except ValueError:
error = _("Invalid date of birth.")
Expand Down