Skip to content
Open
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
8 changes: 6 additions & 2 deletions app/controllers/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ def login_post():
flash('Login successful!', FLASH_SUCCESS)

# Retrieve and clear the redirect from session
# Only allow relative URLs to prevent redirecting to external sites
# Only allow relative URLs to prevent redirecting to external sites.
# '//evil.com' and '\\evil.com' also start with '/', and browsers
# happily resolve them as other hosts. wonderful spec behavior.
redirect_url = session.pop('login_redirect', '/')
if not redirect_url.startswith('/'):
if (not redirect_url.startswith('/')
or redirect_url.startswith('//')
or '\\' in redirect_url):
redirect_url = '/'
return redirect(redirect_url)
else:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_controller_edge_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def test_login_does_not_redirect_to_external_referrer(client, alice):
assert response.location == '/'


@pytest.mark.parametrize('referrer', [
'http://localhost//evil.example',
'http://localhost/\\evil.example',
])
def test_login_does_not_redirect_to_protocol_relative_referrer(client, alice, referrer):
client.get('/login', headers={'Referer': referrer})
response = client.post('/login', data={
'name': 'alice', 'password': 'alice-password',
})
assert response.location == '/'


def test_login_missing_and_invalid_name(client):
missing = client.post('/login', data={'name': '', 'password': ''})
invalid = client.post('/login', data={'name': '<script>', 'password': 'x'})
Expand Down