test(profiles): prove registry single use against a profile that stays served

The single-use assertion reused a ticket against `keep`, which its first use
had just deleted, so the second request was refused whatever the ticket. It
now PATCHes the default profile (a display-name rename that never moves the
home) twice with one ticket: the reuse is 401 and a fresh ticket's identical
PATCH is 200, so only the reuse explains the refusal.

Smaller items from the same review: _redeem_http_ticket checks capabilities
against runtime_bootstrap._PURPOSE_CAPABILITIES instead of re-typing them, and
run_turn_runner drops its duplicate session_policy import.

Review finding by @dokterdok on #140970
Review finding by @kshitijk4poor on #140970
Refs #134889 (DB-5)
Refs #140719
This commit is contained in:
Teknium
2026-10-18 03:23:00 -07:00
parent b4a7225bfb
commit 4fe7ca4d71
3 changed files with 15 additions and 13 deletions

View File

@@ -1146,10 +1146,9 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
return final_response + "\n" + "\n".join(unique_tags)
def run_sync(self):
from gateway.session_policy import policy_for_source, policy_scope
from gateway.session_policy import bind_turn_workspace, policy_for_source, policy_scope
from gateway.session_api_turn import api_policy_scope
from gateway.session_authorities import active_authority
from gateway.session_policy import bind_turn_workspace
policy = policy_for_source(self._runner, self._ctx.source)
with policy_scope(policy, authority=active_authority(self._runner)), api_policy_scope():
bind_turn_workspace(policy, self._ctx.session_id)

View File

@@ -158,22 +158,21 @@ def _check_registry_scope(request, profile_id, launch_home):
def _redeem_http_ticket(store, ticket, request):
from gateway.runtime_bootstrap import _PURPOSE_CAPABILITIES
try:
grant = store.redeem(ticket, profile_id=None, purpose='native-http')
expected = frozenset({'http:owner'})
except PermissionError:
# Only these host-management operations accept a dedicated same-user grant.
# Wrong-purpose redemption does not consume a ticket; ordinary profile grants
# still reach _check_profile_scope and remain unable to create, move or delete profiles.
if request.method == 'POST' and request.url.path.rstrip('/') == '/api/profiles':
grant = store.redeem(ticket, profile_id=None, purpose='native-profile-create')
expected = frozenset({'profiles:create'})
elif _registry_target(request) is not None:
grant = store.redeem(ticket, profile_id=None, purpose='native-profile-registry')
expected = frozenset({'profiles:registry'})
else:
raise
if grant['capabilities'] != expected:
# The issuer's own purpose -> capability table, not a second copy of it.
if grant['capabilities'] != _PURPOSE_CAPABILITIES[grant['purpose']]:
raise PermissionError('invalid native grant')
return grant

View File

@@ -105,20 +105,24 @@ def test_registry_grant_renames_and_deletes_only_its_own_served_profile(tmp_path
response = client.delete('/api/profiles/beta', headers=grant('beta', registry))
assert response.status_code == 200, response.text
assert not (home / 'profiles' / 'beta').exists()
# Single use: the same ticket never authorizes a second write.
once = grant('keep', registry)
ready()
assert client.delete('/api/profiles/keep', headers=once).status_code == 200
assert client.patch('/api/profiles/delta', json={'new_name': 'again'},
headers=once).status_code == 401
assert client.delete('/api/profiles/keep', headers=grant('keep', registry)).status_code == 200
# The launch (default) home never moves: rename is its presentation-only display name.
served = ready()
assert home_of('default') in {p['profile_id'] for p in served['served_profiles']}
once = grant('default', registry)
response = client.patch('/api/profiles/default', json={'new_name': 'Main'}, headers=once)
assert response.status_code == 200, response.text
assert (response.json()['name'], response.json()['display_name']) == ('default', 'Main')
# Single use, against a profile that stays served (a display-name rename never moves
# the default home): the same ticket's identical second PATCH is refused, while a
# fresh ticket's identical PATCH succeeds, so only the reuse explains the 401.
ready()
assert client.patch('/api/profiles/default', json={'new_name': 'Main'},
headers=once).status_code == 401
response = client.patch('/api/profiles/default', json={'new_name': 'Main'},
headers=grant('default', registry))
assert response.status_code == 200, response.text
assert (response.json()['name'], response.json()['display_name']) == ('default', 'Main')
response = client.delete('/api/profiles/default', headers=grant('default', registry))
assert response.status_code == 403, response.text
assert (home / 'config.yaml').is_file()