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 bugs in csrf module #91

Merged
merged 3 commits into from
Sep 12, 2013
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ src/*
cover/
venv/
_build
*.sw[op]
.coverage
.tox
*.egg
5 changes: 4 additions & 1 deletion flask_wtf/csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def validate_csrf(data, secret_key=None, time_limit=None):
if not data or '##' not in data:
return False

expires, hmac_csrf = data.split('##')
expires, hmac_csrf = data.split('##', 1)
try:
expires = float(expires)
except:
Expand All @@ -92,6 +92,9 @@ def validate_csrf(data, secret_key=None, time_limit=None):
'WTF_CSRF_SECRET_KEY', current_app.secret_key
)

if 'csrf_token' not in session:
return False

csrf_build = '%s%s' % (session['csrf_token'], expires)
hmac_compare = hmac.new(
to_bytes(secret_key),
Expand Down
18 changes: 18 additions & 0 deletions tests/test_csrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ def invalid(reason):
assert response.status_code == 200
assert 'token missing' in to_unicode(response.data)

def test_invalid_csrf2(self):
# tests with bad token
response = self.client.post("/", data={
"name": "danny",
"csrf_token": "9999999999999##test"
# will work only if greater than time.time()
})
assert response.status_code == 400

def test_invalid_secure_csrf3(self):
# test with multiple separators
response = self.client.post("/", data={
"name": "danny",
"csrf_token": "1378915137.722##foo##bar##and"
# will work only if greater than time.time()
})
assert response.status_code == 400

def test_valid_csrf(self):
response = self.client.get("/")
csrf_token = get_csrf_token(response.data)
Expand Down