Files
hermes-agent/gateway/session_operator.py
unsupportedpastels 021468f4b3 fix(approval): keep the execute_code gate for injected turns in a local session
The whole-script execute_code exemption keyed on the session, not on the
turn. Every turn that runs in a local session's FIFO had a local launch
policy, was no delegated child and had the turn's notifier registered, so a
plugin inject_message turn also skipped the gate: its
shutil.rmtree()/subprocess calls never pass through terminal() and ran with
no prompt, where the base asked.

The exemption now keys on the admission's origin. execute_admission binds the
live session in a new viewer_turn contextvar (beside admission_author) only
for a row the session's viewer submitted through session.submit
(is_viewer_input: the fields SessionAuthority.submit commits, not a hosted:
request). A local_automation_v1 admission (plugin injection, delegation
completions, background-process and watch notices, /loop, heartbeat), a
hosted-room task and every other producer row bind nothing, and the approval
check reads that through approval_context.is_viewer_submitted_turn(). Cron
and managed-worker turns return before the binding, so they keep the gate as
well. No protocol or schema change: the origin is read from the committed
row.

Tests: an injected turn's script that deletes a directory with subprocess
gets the whole-script prompt and, denied, never runs (failed on the base: the
script ran); a viewer submit, a plugin injection and a /loop tick run through
the real drain and only the viewer's turn skips the gate (failed on the base
for the injection and the tick).

Refs #134889 (T2)
Refs #139523
2026-10-16 19:42:20 -04:00

38 lines
1.9 KiB
Python

"""Private provenance for already-authorized operator input.
Operator access is profile-local, not account linking or a change of authorship.
It never claims unowned legacy history: that still requires the native migration
path. Only SessionAuthority.submit writes this envelope after authorization;
public submission parameters cannot supply it.
"""
from hermes_state_runtime import RuntimeStoreError
# Fields ``SessionAuthority.submit`` commits for a local session's input; producers commit their own envelope.
LOCAL_INPUT_FIELDS = frozenset({'text', 'attachments_v1', 'finite', 'unattended', 'surface_v1', 'display_v1',
'cli_turn_v1'})
def is_viewer_input(row):
"""A row a session viewer submitted through ``session.submit`` (a client's own prompt), not a producer
admission: ``local_automation_v1`` (plugin injection, delegation completions, process and watch notices,
/loop, heartbeat), a hosted-room task (``hosted:`` request id) or any other envelope."""
return (not str(row['request_id']).startswith('hosted:')
and {'text'} <= set(row['payload']) <= LOCAL_INPUT_FIELDS | {'local_operator_v1'})
def check_local_input(authority, ref, row, *, source=None):
payload = row['payload']
allowed = set(LOCAL_INPUT_FIELDS)
if 'local_operator_v1' in payload:
allowed.add('local_operator_v1')
if payload['local_operator_v1'] != {
'profile_id': authority.profile_id, 'session_id': ref.session_id,
'principal_id': row['principal_id']}:
raise RuntimeStoreError('permission_denied')
else:
source = source or authority.sessions[ref.session_id].source
if row['principal_id'] != source.user_id:
raise RuntimeStoreError('permission_denied')
if not {'text'} <= set(payload) <= allowed:
raise RuntimeStoreError('permission_denied')