Files
VoiceStudio/tests/cli/network_guard/sitecustomize.py
mostafa-ismail-2004 ff1aa131f6 fix(cli): address review findings on the terminal command
- Background launches keep the launcher's switches, so Snap's
  --no-sandbox reaches Electron and the app can start confined.
- A crashed backend fails fast instead of waiting out the startup budget.
- The AUR launcher runs the renamed executable, and package-managed
  installs (VOICESTUDIO_DISABLE_UPDATER=1) are told to update through
  their package manager instead of opening a disabled updater.
- Windows: PATH is edited in the registry as stored (unexpanded entries,
  same value type); the warning never blocks a silent install; an
  update keeps the PATH entry in case it rolls back.
- Relative command links are resolved against their own folder, and
  bash users with ~/.bash_profile get the PATH entry there.
- Settings shows a translated, actionable message when PATH setup fails.
- Tests: the network guard also blocks connect_ex and sendto; the lock
  tests synchronize on events instead of sleeps and kill the holder to
  test a real crash; a Windows path assertion is separator-agnostic.
2026-11-05 19:40:27 +02:00

50 lines
1.3 KiB
Python

"""Subprocess test guard: CLI tests may contact loopback servers only."""
import ipaddress
import socket
original_connect = socket.socket.connect
original_connect_ex = socket.socket.connect_ex
original_sendto = socket.socket.sendto
original_getaddrinfo = socket.getaddrinfo
def allowed(host):
if host == 'localhost':
return True
try:
return ipaddress.ip_address(host).is_loopback
except ValueError:
return False
def check(sock, address):
if sock.family in {socket.AF_INET, socket.AF_INET6} and not allowed(address[0]):
raise OSError('External network access forbidden in CLI tests')
def connect(self, address):
check(self, address)
return original_connect(self, address)
def connect_ex(self, address):
check(self, address)
return original_connect_ex(self, address)
def sendto(self, data, *args):
check(self, args[-1]) # sendto(data, address) or sendto(data, flags, address)
return original_sendto(self, data, *args)
def getaddrinfo(host, *args, **kwargs):
if not allowed(host):
raise OSError('External DNS access forbidden in CLI tests')
return original_getaddrinfo(host, *args, **kwargs)
socket.socket.connect = connect
socket.socket.connect_ex = connect_ex
socket.socket.sendto = sendto
socket.getaddrinfo = getaddrinfo