mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-01 20:55:16 +09:00
Authorize resolved shared-variable reads
GHSA-893r-j92f-5q9x: both resolved-variables routes took the resource id, and the hierarchical one its workspace, project and environment ids, straight from the query string and checked none of them. Any account could name another workspace's id and read its variable groups. The caller must now be able to read the target resource and every scope asked for; unknown resource types fail closed.
This commit is contained in:
1 parent
60030b0b4e
commit
1c6c43326e
2 files changed
+148
No files matched your search
@@ -168,6 +168,64 @@ def _check_resource_write(resource_type, resource_id):
|
||||
return _bad('access denied', 403)
|
||||
|
||||
|
||||
def _check_resource_read(resource_type, resource_id):
|
||||
"""Authorize reading the variables resolved FOR a target resource.
|
||||
|
||||
The read-side twin of ``_check_resource_write``, and it fails closed the
|
||||
same way: the resolved set is the target's effective configuration, so the
|
||||
caller must be able to read the target itself. Returns None when allowed,
|
||||
else a ``(response, status)`` tuple.
|
||||
"""
|
||||
user = get_current_user()
|
||||
if user is None:
|
||||
return _bad('authentication required', 401)
|
||||
if user.is_admin:
|
||||
return None
|
||||
if resource_type in ('application', 'wordpress'):
|
||||
application = _resolve_app(resource_type, resource_id)
|
||||
if application is None:
|
||||
return _bad('resource not found', 404)
|
||||
if not ResourceGrantService.can_access_app(user, application):
|
||||
return _bad('access denied', 403)
|
||||
return None
|
||||
if resource_type == 'database':
|
||||
from app.models.managed_database import ManagedDatabase
|
||||
try:
|
||||
row = ManagedDatabase.query_active().filter_by(id=int(resource_id)).first()
|
||||
except (TypeError, ValueError):
|
||||
row = None
|
||||
if row is None:
|
||||
return _bad('resource not found', 404)
|
||||
if row.workspace_id is None:
|
||||
return _bad('access denied', 403)
|
||||
return require_workspace_access(row.workspace_id, user)
|
||||
if resource_type == 'server':
|
||||
from app.services.workspace_service import WorkspaceService
|
||||
if WorkspaceService.get_accessible_server(user, str(resource_id)) is None:
|
||||
return _bad('resource not found', 404)
|
||||
return None
|
||||
return _bad('access denied', 403)
|
||||
|
||||
|
||||
def _check_scope_context(context):
|
||||
"""Authorize every scope a hierarchical resolve was asked to layer in.
|
||||
|
||||
The scope ids arrive as query parameters, so without this any caller could
|
||||
name another workspace's id and read its variable groups
|
||||
(GHSA-893r-j92f-5q9x).
|
||||
"""
|
||||
for scope_type, key in ((SharedVariableGroup.SCOPE_WORKSPACE, 'workspace_id'),
|
||||
(SharedVariableGroup.SCOPE_PROJECT, 'project_id'),
|
||||
(SharedVariableGroup.SCOPE_ENVIRONMENT, 'environment_id')):
|
||||
scope_id = context.get(key)
|
||||
if scope_id in (None, ''):
|
||||
continue
|
||||
denied = _check_scope(scope_type, scope_id)
|
||||
if denied:
|
||||
return denied
|
||||
return None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- metadata
|
||||
|
||||
@shared_resources_bp.route('/resource-types', methods=['GET'])
|
||||
@@ -439,6 +497,9 @@ def resolved():
|
||||
resource_id = request.args.get('resource_id')
|
||||
if not resource_type or resource_id in (None, ''):
|
||||
return _bad('resource_type and resource_id are required')
|
||||
denied = _check_resource_read(resource_type, resource_id)
|
||||
if denied:
|
||||
return denied
|
||||
|
||||
variables = SharedResourceService.resolve_variables(
|
||||
resource_type, resource_id, mask_secrets=True
|
||||
@@ -477,6 +538,9 @@ def resolved_hierarchical():
|
||||
'project_id': request.args.get('project_id'),
|
||||
'environment_id': request.args.get('environment_id'),
|
||||
}
|
||||
denied = _check_resource_read(resource_type, resource_id) or _check_scope_context(context)
|
||||
if denied:
|
||||
return denied
|
||||
variables = SharedResourceService.resolve_hierarchical(
|
||||
resource_type, resource_id, context=context, mask_secrets=True
|
||||
)
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
"""GHSA-893r-j92f-5q9x: the resolved-variables routes took a resource id and
|
||||
scope ids from the query string and checked neither, so any account could
|
||||
read another workspace's variable groups.
|
||||
|
||||
Each refusal has its control: members still resolve their own.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
from factories import headers_for, make_application, make_user
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def variables(app):
|
||||
from app import db
|
||||
from app.services.shared_resource_service import SharedResourceService
|
||||
from app.services.workspace_service import WorkspaceService
|
||||
admin = make_user(db, 'v893_admin', 'admin')
|
||||
member = make_user(db, 'v893_member', 'developer')
|
||||
outsider = make_user(db, 'v893_outsider', 'developer')
|
||||
ws_a = WorkspaceService.create_workspace({'name': 'V893-A'}, admin.id)
|
||||
WorkspaceService.add_member(ws_a.id, member.id)
|
||||
ws_b = WorkspaceService.create_workspace({'name': 'V893-B'}, outsider.id)
|
||||
group = SharedResourceService.create_group('workspace', ws_a.id, 'tenant-a')
|
||||
SharedResourceService.set_variable(group.id, 'TENANT_A_PLAIN', 'visible-value-a')
|
||||
SharedResourceService.set_variable(group.id, 'TENANT_A_KEY', 'secret-a', is_secret=True)
|
||||
app_a = make_application(db, name='v893-a', user_id=member.id, workspace_id=ws_a.id)
|
||||
SharedResourceService.attach_group(group.id, 'application', app_a.id)
|
||||
app_b = make_application(db, name='v893-b', user_id=outsider.id, workspace_id=ws_b.id)
|
||||
return {'member': member.id, 'outsider': outsider.id, 'admin': admin.id,
|
||||
'ws_a': ws_a.id, 'ws_b': ws_b.id, 'app_a': app_a.id, 'app_b': app_b.id}
|
||||
|
||||
|
||||
def _resolved(client, user, path, **query):
|
||||
qs = '&'.join(f'{key}={value}' for key, value in query.items())
|
||||
return client.get(f'/api/v1/shared/{path}?{qs}', headers=headers_for(user))
|
||||
|
||||
|
||||
_NEEDLES = ('TENANT_A_PLAIN', 'TENANT_A_KEY', 'visible-value-a', 'secret-a', 'tenant-a')
|
||||
|
||||
|
||||
def test_outsider_cannot_layer_in_a_foreign_workspace(client, variables):
|
||||
"""The reported call: the caller's OWN app, another workspace's id."""
|
||||
resp = _resolved(client, variables['outsider'], 'resolved/hierarchical',
|
||||
resource_type='application', resource_id=variables['app_b'],
|
||||
workspace_id=variables['ws_a'])
|
||||
assert resp.status_code in (403, 404)
|
||||
assert not [n for n in _NEEDLES if n in resp.get_data(as_text=True)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', ['resolved', 'resolved/hierarchical'])
|
||||
def test_outsider_cannot_resolve_a_foreign_resource(client, variables, path):
|
||||
resp = _resolved(client, variables['outsider'], path,
|
||||
resource_type='application', resource_id=variables['app_a'])
|
||||
assert resp.status_code in (403, 404)
|
||||
assert not [n for n in _NEEDLES if n in resp.get_data(as_text=True)]
|
||||
|
||||
|
||||
@pytest.mark.parametrize('path', ['resolved', 'resolved/hierarchical'])
|
||||
def test_unknown_resource_types_fail_closed(client, variables, path):
|
||||
resp = _resolved(client, variables['outsider'], path,
|
||||
resource_type='nonsense', resource_id=1, workspace_id=variables['ws_b'])
|
||||
assert resp.status_code == 403
|
||||
|
||||
|
||||
@pytest.mark.parametrize('who', ['member', 'admin'])
|
||||
def test_members_still_resolve_their_own(client, variables, who):
|
||||
resp = _resolved(client, variables[who], 'resolved/hierarchical',
|
||||
resource_type='application', resource_id=variables['app_a'],
|
||||
workspace_id=variables['ws_a'])
|
||||
assert resp.status_code == 200
|
||||
keys = {row['key'] for row in resp.get_json()['variables']}
|
||||
assert {'TENANT_A_PLAIN', 'TENANT_A_KEY'} <= keys
|
||||
assert 'secret-a' not in resp.get_data(as_text=True) # still masked
|
||||
flat = _resolved(client, variables[who], 'resolved',
|
||||
resource_type='application', resource_id=variables['app_a'])
|
||||
assert flat.status_code == 200
|
||||
assert 'TENANT_A_PLAIN' in flat.get_data(as_text=True)
|
||||
|
||||
|
||||
def test_outsider_still_resolves_inside_their_own_workspace(client, variables):
|
||||
resp = _resolved(client, variables['outsider'], 'resolved/hierarchical',
|
||||
resource_type='application', resource_id=variables['app_b'],
|
||||
workspace_id=variables['ws_b'])
|
||||
assert resp.status_code == 200
|
||||
Reference in new issue
Block a user