2026-09-06 00:44:08 +02:00
|
|
|
import runpy
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
import unittest
|
|
|
|
|
from urllib.parse import urlencode
|
2026-11-02 02:42:57 +01:00
|
|
|
launcher=runpy.run_path(str(Path(__file__).resolve().parents[1]/'scripts/operator-browser/xdg-open'))
|
|
|
|
|
allowed=launcher['allowed']
|
|
|
|
|
launch_argv=launcher['launch_argv']
|
2026-09-06 00:44:08 +02:00
|
|
|
class BrowserBoundary(unittest.TestCase):
|
|
|
|
|
def test_approved_flow(self):
|
|
|
|
|
self.assertTrue(allowed('https://kc.coulomb.social/authorize?'+urlencode({'redirect_uri':'http://localhost:8250/oidc/callback','state':'test'})))
|
|
|
|
|
def test_other_destination_rejected(self):
|
|
|
|
|
for host in ['evil.example','kc.coulomb.social.evil.example','user@kc.coulomb.social']:
|
|
|
|
|
self.assertFalse(allowed('https://'+host+'/?'+urlencode({'redirect_uri':'http://localhost:8250/oidc/callback'})))
|
|
|
|
|
def test_unapproved_callback_rejected(self):
|
|
|
|
|
self.assertFalse(allowed('https://kc.coulomb.social/?'+urlencode({'redirect_uri':'https://evil.example'})))
|
|
|
|
|
def test_duplicate_callback_rejected(self):
|
|
|
|
|
self.assertFalse(allowed('https://kc.coulomb.social/?redirect_uri=http://localhost:8250/oidc/callback&redirect_uri=https://evil.example'))
|
2026-11-02 02:42:57 +01:00
|
|
|
def test_launch_uses_empty_profile(self):
|
|
|
|
|
url='https://kc.coulomb.social/authorize?'+urlencode({'redirect_uri':'http://localhost:8250/oidc/callback'})
|
2026-11-02 02:59:51 +01:00
|
|
|
browser='/cache/ms-playwright/chromium-1234/chrome-linux64/chrome'
|
|
|
|
|
argv=launch_argv(browser, '/tmp/fresh-profile', url)
|
|
|
|
|
self.assertEqual(argv[0], browser)
|
|
|
|
|
self.assertIn('--user-data-dir=/tmp/fresh-profile', argv)
|
2026-11-02 02:42:57 +01:00
|
|
|
self.assertEqual(argv[-1], url)
|
2026-11-02 02:59:51 +01:00
|
|
|
self.assertNotIn('--headless', argv)
|
|
|
|
|
joined=' '.join(argv)
|
|
|
|
|
for banned in ('explorer.exe', 'powershell', 'msedge', 'firefox', '/mnt/c/'):
|
|
|
|
|
self.assertNotIn(banned, joined)
|
2026-11-02 19:28:28 +01:00
|
|
|
def test_contained_home_does_not_move_the_browser_cache(self):
|
|
|
|
|
self.assertEqual(
|
|
|
|
|
launcher["playwright_cache"]("/tmp/warden-session", "/home/operator"),
|
|
|
|
|
Path("/home/operator/.cache/ms-playwright"),
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-02 02:59:51 +01:00
|
|
|
def test_chromium_comes_from_the_playwright_pin(self):
|
|
|
|
|
import json
|
|
|
|
|
import tempfile
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
with tempfile.TemporaryDirectory() as raw:
|
|
|
|
|
root=Path(raw)
|
|
|
|
|
spec=root/'browsers.json'
|
|
|
|
|
spec.write_text(json.dumps({'browsers':[{'name':'chromium','revision':'1234'},{'name':'firefox','revision':'1538'}]}))
|
|
|
|
|
binary=root/'chromium-1234'/'chrome-linux64'/'chrome'
|
|
|
|
|
binary.parent.mkdir(parents=True)
|
|
|
|
|
binary.write_text('')
|
|
|
|
|
binary.chmod(0o755)
|
|
|
|
|
self.assertEqual(launcher['chromium_executable'](spec, root), str(binary))
|
|
|
|
|
self.assertEqual(launcher['chromium_executable'](root/'missing.json', root), '')
|
2026-11-02 10:08:36 +01:00
|
|
|
def test_wsl_cmd_start_is_the_login_opener(self):
|
|
|
|
|
url='https://kc.coulomb.social/authorize?'+urlencode({'redirect_uri':'http://127.0.0.1:8250/oidc/callback','state':'test'})
|
|
|
|
|
escaped=url.replace('&','^&')
|
|
|
|
|
self.assertEqual(launcher['classify_cmd'](['/c','start',escaped]), ('open', url))
|
|
|
|
|
self.assertEqual(launcher['classify_cmd'](['/c','start','https://evil.example/']), ('reject', ''))
|
|
|
|
|
self.assertEqual(launcher['classify_cmd'](['/c','dir']), ('delegate', ''))
|