Hide ‘back to …’ link if it’s not your service

This can happen if you click a link for a service you don’t have access
to. We shouldn’t show the back to service link in this case because:
- you shouldn’t be able to find out the service’s name from just knowing
  the link
- if you click the link you only get a `403` anyway
This commit is contained in:
Chris Hill-Scott
2019-01-15 17:31:55 +00:00
parent 7c92847b85
commit 558ae87baa
4 changed files with 57 additions and 3 deletions

View File

@@ -151,8 +151,11 @@ class User(UserMixin):
def has_permission_for_service(self, service_id, permission): def has_permission_for_service(self, service_id, permission):
return permission in self._permissions.get(service_id, []) return permission in self._permissions.get(service_id, [])
def belongs_to_service(self, service_id):
return str(service_id) in self.services
def belongs_to_service_or_403(self, service_id): def belongs_to_service_or_403(self, service_id):
if str(service_id) not in self.services: if not self.belongs_to_service(service_id):
abort(403) abort(403)
def is_locked(self): def is_locked(self):

View File

@@ -2,7 +2,7 @@
{% block fullwidth_content %} {% block fullwidth_content %}
<div id="content"> <div id="content">
{% if current_service and current_user.is_authenticated %} {% if current_service and current_user.is_authenticated and current_user.belongs_to_service(current_service.id) %}
<div class="navigation-service"> <div class="navigation-service">
<a href="{{ url_for('main.show_accounts_or_dashboard') }}">Back to {{ current_service.name }}</a> <a href="{{ url_for('main.show_accounts_or_dashboard') }}">Back to {{ current_service.name }}</a>
</div> </div>

View File

@@ -2,7 +2,12 @@ import pytest
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from flask import url_for from flask import url_for
from tests.conftest import SERVICE_ONE_ID, normalize_spaces from tests.conftest import (
SERVICE_ONE_ID,
normalize_spaces,
service_one,
service_two,
)
SAMPLE_DATA = { SAMPLE_DATA = {
'organisations': [ 'organisations': [
@@ -137,3 +142,44 @@ def test_choose_account_should_not_show_back_to_service_link_if_not_signed_in(
assert page.select_one('h1').text == 'Sign in' # Were not signed in assert page.select_one('h1').text == 'Sign in' # Were not signed in
assert page.select_one('.navigation-service a') is None assert page.select_one('.navigation-service a') is None
@pytest.mark.parametrize('service, expected_status, page_text', (
(service_one, 200, (
'Test Service Switch service '
''
'Dashboard '
'Templates '
'Team members'
)),
(service_two, 403, (
# Page has no back to link
'403 '
'You do not have permission to view this page.'
)),
))
def test_should_not_show_back_to_service_if_user_doesnt_belong_to_service(
client_request,
api_user_active,
fake_uuid,
mock_get_service,
mock_get_service_template,
mock_get_template_folders,
service,
expected_status,
page_text,
):
mock_get_service.return_value = service(api_user_active)
page = client_request.get(
'main.view_template',
service_id=mock_get_service.return_value['id'],
template_id=fake_uuid,
_expected_status=expected_status,
)
assert normalize_spaces(
page.select_one('#content').text
).startswith(
normalize_spaces(page_text)
)

View File

@@ -55,6 +55,11 @@ def service_one(api_user_active):
return service_json(SERVICE_ONE_ID, 'service one', [api_user_active.id]) return service_json(SERVICE_ONE_ID, 'service one', [api_user_active.id])
@pytest.fixture(scope='function')
def service_two(api_user_active):
return service_json(SERVICE_TWO_ID, 'service two', [api_user_active.id])
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def multiple_reply_to_email_addresses(mocker): def multiple_reply_to_email_addresses(mocker):
def _get(service_id): def _get(service_id):