Keep backup hooks and app creation from viewers

GHSA-vrwv-j8w5-p94q: POST /apps took any authenticated account,
so a read-only viewer could create an app, own it, and set its
pre-backup hook, which the panel runs as a shell command on its
own host.

Creating a service now needs a developer or admin. Changing a
hook needs an admin, and the service refuses it by default, so
a new caller that forwards request data cannot hand out shell
access. Sending a hook back unchanged is not a change, so a
policy read by its owner still round-trips.
This commit is contained in:
Juan Denis committed 2026-11-22 02:30:11 -05:00
1 parent f605c6b95f
commit 60030b0b4e
6 files changed
+106 -7

No files matched your search

+9 -1
View File
@@ -930,6 +930,11 @@ _HOST_BINDING_FIELDS = ('root_path', 'compose_file', 'systemd_unit', 'managed_by
@jwt_required()
def create_app():
user = get_current_user()
# Creating a service is a write. A read-only (viewer) account owning an
# app would hold write access to it — and to everything an owner can
# configure (GHSA-vrwv-j8w5-p94q).
if not user.is_developer:
raise PermissionDeniedError('Your role is read-only and cannot create services')
current_user_id = user.id
data = json_object()
@@ -2229,7 +2234,10 @@ def update_app_backup_policy(app_id):
app = require_app(app_id, write=True)
policy = BackupPolicyService.get_or_create_policy('application', app_id)
try:
BackupPolicyService.update_policy(policy, json_object())
# Write access to the app is not host access: only an admin may set
# the hooks, which run as shell commands on the panel host.
BackupPolicyService.update_policy(
policy, json_object(), allow_host_commands=get_current_user().is_admin)
except BackupPolicyError as e:
return jsonify({'error': str(e)}), 400
return jsonify(BackupPolicyService.serialize_policy_view(policy))
+2 -1
View File
@@ -205,7 +205,8 @@ def update_target_policy(target_type, target_id):
try:
BackupPolicyService.validate_target_type(target_type)
policy = BackupPolicyService.get_or_create_policy(target_type, target_id, subtype, meta)
BackupPolicyService.update_policy(policy, data)
# Admin-only route: an admin may set the policy's backup hooks.
BackupPolicyService.update_policy(policy, data, allow_host_commands=True)
except BackupPolicyError as e:
return jsonify({'error': str(e)}), 400
return jsonify(BackupPolicyService.serialize_policy_view(policy)), 200
+3 -1
View File
@@ -879,7 +879,9 @@ def protect_managed_database(managed_id):
if not managed:
raise not_found('database')
data = json_object()
policy = ManagedDatabaseService.protect(managed, fields=data.get('policy'))
# Admin-only route: an admin may set the policy's backup hooks.
policy = ManagedDatabaseService.protect(managed, fields=data.get('policy'),
allow_host_commands=True)
return jsonify({'policy': policy.to_dict()}), 201
+19 -2
View File
@@ -132,10 +132,27 @@ class BackupPolicyService:
'pre_backup_hook', 'post_backup_hook',
)
# A hook is a shell command the panel runs as its own user on every backup:
# setting one is host command execution, whatever the target belongs to.
_HOST_COMMAND_FIELDS = ('pre_backup_hook', 'post_backup_hook')
@classmethod
def update_policy(cls, policy, fields):
"""Apply a partial update, persist, and re-sync the ScheduledJob."""
def update_policy(cls, policy, fields, *, allow_host_commands=False):
"""Apply a partial update, persist, and re-sync the ScheduledJob.
Changing a hook is refused unless the caller says the actor is an admin
(``allow_host_commands``) — default-deny, so a new caller that forwards
request data cannot hand shell access to whoever may edit the target
(GHSA-vrwv-j8w5-p94q). Sending a hook back unchanged is not a change,
so a policy read by a non-admin still round-trips.
"""
clean = {k: v for k, v in (fields or {}).items() if k in cls._MUTABLE}
if not allow_host_commands:
changed = [key for key in cls._HOST_COMMAND_FIELDS
if key in clean and (clean[key] or None) != (getattr(policy, key) or None)]
if changed:
from app.exceptions import PermissionDeniedError
raise PermissionDeniedError('Only an admin can set backup hooks')
cls.validate_fields(clean)
for key, value in clean.items():
if key in ('retention_count', 'retention_days', 'full_every_n_days') and value is not None:
@@ -170,7 +170,7 @@ class ManagedDatabaseService:
}
@classmethod
def protect(cls, managed, fields=None):
def protect(cls, managed, fields=None, allow_host_commands=False):
"""Create/refresh a BackupPolicy for this managed DB with a **real FK**
(``target_id = managed.id``) plus a ``managed`` marker so the executor
resolves the descriptor from the row (not a free-floating JSON blob)."""
@@ -184,7 +184,8 @@ class ManagedDatabaseService:
'host': managed.host},
)
if fields:
policy = BackupPolicyService.update_policy(policy, fields)
policy = BackupPolicyService.update_policy(
policy, fields, allow_host_commands=allow_host_commands)
return policy
@staticmethod
+70
View File
@@ -0,0 +1,70 @@
"""GHSA-vrwv-j8w5-p94q: a viewer created an app, then set a backup hook the
panel runs as a shell command on its own host.
Each refusal has its control: the fix must not lock out the people who are
supposed to be able to do the thing.
"""
import pytest
from factories import headers_for, make_user
def _create(client, user, name):
return client.post('/api/v1/apps', json={'name': name, 'app_type': 'static'},
headers=headers_for(user))
def test_viewer_cannot_create_a_service(client, app):
from app import db
from app.models.application import Application
viewer = make_user(db, 'vrwv_viewer', 'viewer')
assert _create(client, viewer, 'vrwv-viewer-app').status_code == 403
assert Application.query.filter_by(name='vrwv-viewer-app').first() is None
developer = make_user(db, 'vrwv_dev', 'developer')
assert _create(client, developer, 'vrwv-dev-app').status_code == 201
def test_only_an_admin_can_set_a_backup_hook(client, app):
from app import db
from app.models.backup_policy import BackupPolicy
developer = make_user(db, 'vrwv_owner', 'developer')
admin = make_user(db, 'vrwv_admin', 'admin')
app_id = _create(client, developer, 'vrwv-owned').get_json()['app']['id']
url = f'/api/v1/apps/{app_id}/backup-policy'
def policy():
return BackupPolicy.query.filter_by(target_type='application', target_id=app_id).one()
for field in ('pre_backup_hook', 'post_backup_hook'):
resp = client.put(url, json={field: 'id > /tmp/pwned'}, headers=headers_for(developer))
assert resp.status_code == 403, field
assert getattr(policy(), field) in (None, '')
# The owner still manages the rest of the policy...
resp = client.put(url, json={'retention_count': 9}, headers=headers_for(developer))
assert resp.status_code == 200 and policy().retention_count == 9
# ...an admin sets the hook...
resp = client.put(url, json={'pre_backup_hook': 'echo ok'}, headers=headers_for(admin))
assert resp.status_code == 200 and policy().pre_backup_hook == 'echo ok'
# ...and the owner can send the policy back unchanged (a round trip of what
# GET returned) without that counting as setting a hook, but not alter it.
resp = client.put(url, json={'pre_backup_hook': 'echo ok', 'retention_count': 4},
headers=headers_for(developer))
assert resp.status_code == 200 and policy().retention_count == 4
resp = client.put(url, json={'pre_backup_hook': 'echo changed'},
headers=headers_for(developer))
assert resp.status_code == 403 and policy().pre_backup_hook == 'echo ok'
resp = client.put(url, json={'pre_backup_hook': ''}, headers=headers_for(developer))
assert resp.status_code == 403 and policy().pre_backup_hook == 'echo ok'
def test_hook_changes_are_denied_by_default_in_the_service(app):
"""A new caller that forwards request data gets the refusal, not a shell."""
from app.exceptions import PermissionDeniedError
from app.services.backup_policy_service import BackupPolicyService
policy = BackupPolicyService.get_or_create_policy('application', 987654)
with pytest.raises(PermissionDeniedError):
BackupPolicyService.update_policy(policy, {'post_backup_hook': 'id'})
assert policy.post_backup_hook in (None, '')