"""Trusted environment setup. Run before exposing screenshots/actions to a model.""" import json import os from pathlib import Path import subprocess import xml.etree.ElementTree as ET # Restore navigation packages from trusted, offline Debian assets before guarding commands. if Path('/opt/gui-only/nonexistent-debs').exists(): subprocess.run(['dpkg','-i',*[str(p) for p in Path('/opt/gui-only/nonexistent-debs').glob('*.deb')]],check=True,stdout=subprocess.DEVNULL) BLOCKED = 'xterm uxterm lxterm koi8rxterm x-terminal-emulator xfce4-terminal gnome-terminal gnome-terminal-server konsole mate-terminal lxterminal qterminal terminator tilix sakura rxvt urxvt zutty xfce4-appfinder xfrun4 exo-desktop-item-edit xfce4-settings-editor xfce4-settings-manager thunar Thunar mousepad xfce4-keyboard-settings xfce4-mime-settings'.split() stub = Path('/usr/local/bin/gui-only-denied') stub.write_text('''#!/bin/sh mkdir -p /logs/artifacts printf '%s %s\\n' "$(date -u +%FT%TZ)" "$0" >> /logs/artifacts/gui-only-denied.log exit 126 ''') stub.chmod(0o755) for name in BLOCKED: for directory in ['/usr/bin','/usr/local/bin','/bin']: p = Path(directory)/name if directory == '/bin' and Path('/bin').resolve() == Path('/usr/bin'):continue if p.exists() or p.is_symlink():p.unlink() p.symlink_to(stub) # Remove shell launch surfaces without removing native editor file dialogs. for root in ['/root/Desktop','/usr/share/applications','/usr/local/share/applications','/root/.local/share/applications']: for p in Path(root).glob('*.desktop'): text=p.read_text(errors='replace').lower() if any(x in text for x in ['terminal','appfinder','xfrun','settings-editor','keyboard-settings','mime-settings','desktop-item-edit','text-editor','mousepad','chromium','web-browser']):p.unlink() # Only the harness launches the editor; disable duplicate session autostarts. for root in ['/root/.config/autostart','/etc/xdg/autostart']: for p in Path(root).glob('*.desktop'): if any(x in p.read_text(errors='replace').lower() for x in ['shotcut','filmcraft','x11vnc']): p.write_text('[Desktop Entry]\nType=Application\nHidden=true\n') for root in ['/etc/xdg/xfce4/xfconf/xfce-perchannel-xml','/root/.config/xfce4/xfconf/xfce-perchannel-xml']: p=Path(root)/'xfce4-keyboard-shortcuts.xml' if p.exists(): tree=ET.parse(p) for parent in tree.iter(): for child in list(parent): if any(x in child.get('value','').lower() for x in ['terminal','appfinder','xfrun']):parent.remove(child) tree.write(p,encoding='unicode') kiosk=Path('/etc/xdg/xfce4/kiosk/kioskrc');kiosk.parent.mkdir(parents=True,exist_ok=True) kiosk.write_text('[xfce4-panel]\nCustomizePanel=NONE\n[xfdesktop]\nCustomizeDesktopMenu=NONE\nCustomizeWindowlist=NONE\nUserMenu=ALL\n[xfce4-session]\nCustomizeCompatibility=NONE\nCustomizeSecurity=NONE\n') # Custom file-manager commands can launch shells. Keep file navigation/native dialogs. actions=Path('/root/.config/Thunar/uca.xml');actions.parent.mkdir(parents=True,exist_ok=True);actions.write_text('\n') for name in BLOCKED: r=subprocess.run(['/usr/bin/'+name,'--version'],capture_output=True,timeout=5) if r.returncode !=126:raise RuntimeError('GUI-only guard failed: '+name) Path('/logs/artifacts').mkdir(parents=True,exist_ok=True) Path('/logs/artifacts/gui-only-policy.json').write_text(json.dumps({'version':2,'os_navigation':True,'terminal_tools':False,'blocked_launchers':BLOCKED,'self_check':'passed'},indent=2)) print('GUI_ONLY_SELF_CHECK_PASSED') # Fixed GUI file browser and app launchers do not execute arbitrary commands. p=Path('/root/Desktop/Files.desktop');p.write_text('[Desktop Entry]\nType=Application\nName=Files\nExec=/usr/bin/python3 /opt/gui-only/files.py\nIcon=system-file-manager\nTerminal=false\n');p.chmod(0o755) for app,command in [('FilmCraft','/usr/local/bin/filmcraft --graphics software /root/Desktop/task-01/start.fcproj')]: if not Path(command.split()[0]).exists():continue p=Path('/root/Desktop')/(app+'.desktop');p.write_text('[Desktop Entry]\nType=Application\nName='+app+'\nExec='+command+'\nIcon=applications-multimedia\nTerminal=false\n');p.chmod(0o755)