This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4185 from gratipay/fix-csrf-annoyance
Fix CSRF annoyance
- Loading branch information
Showing
2 changed files
with
49 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
|
||
from gratipay.security import csrf | ||
from gratipay.testing import Harness | ||
|
||
|
||
class Tests(Harness): | ||
|
||
# st - _sanitize_token | ||
|
||
def test_st_passes_through_good_token(self): | ||
token = 'ddddeeeeaaaaddddbbbbeeeeeeeeffff' | ||
assert csrf._sanitize_token(token) == token | ||
|
||
def test_st_rejects_overlong_token(self): | ||
token = 'ddddeeeeaaaaddddbbbbeeeeeeeefffff' | ||
assert csrf._sanitize_token(token) is None | ||
|
||
def test_st_rejects_underlong_token(self): | ||
token = 'ddddeeeeaaaaddddbbbbeeeeeeeefff' | ||
assert csrf._sanitize_token(token) is None | ||
|
||
def test_st_rejects_goofy_token(self): | ||
token = 'ddddeeeeaaaadddd bbbbeeeeeeeefff' | ||
assert csrf._sanitize_token(token) is None | ||
|
||
|
||
# integration tests | ||
|
||
def test_no_csrf_cookie_gives_403(self): | ||
r = self.client.POST('/', csrf_token=False, raise_immediately=False) | ||
assert r.code == 403 | ||
assert "Bad CSRF cookie" in r.body | ||
assert b'csrf_token' in r.headers.cookie | ||
|
||
def test_bad_csrf_cookie_gives_403(self): | ||
r = self.client.POST('/', csrf_token=b'bad_token', raise_immediately=False) | ||
assert r.code == 403 | ||
assert "Bad CSRF cookie" in r.body | ||
assert r.headers.cookie[b'csrf_token'].value != 'bad_token' | ||
|
||
def test_csrf_cookie_set_for_most_requests(self): | ||
r = self.client.GET('/about/') | ||
assert b'csrf_token' in r.headers.cookie | ||
|
||
def test_no_csrf_cookie_set_for_assets(self): | ||
r = self.client.GET('/assets/gratipay.css') | ||
assert b'csrf_token' not in r.headers.cookie |