2026-10-05 14:26:43 +02:00
|
|
|
import html
|
|
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from openultrasast.search.oracles import BrowserExecutor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_browser_launch_failure_is_not_negative(monkeypatch):
|
|
|
|
|
def fail(*args, **kwargs):
|
|
|
|
|
return subprocess.CompletedProcess(args, 1, "", "failed")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(subprocess, "run", fail)
|
|
|
|
|
with pytest.raises(OSError):
|
2026-11-28 23:17:59 +01:00
|
|
|
BrowserExecutor(binary="/fake/chromium")('<script>alert("ousast-xss")</script>', "nonce")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_browser_error_names_the_first_line_of_chromium_output(monkeypatch):
|
|
|
|
|
# chromium prints its cause first ("No usable sandbox!"), then a stack trace and a register dump.
|
|
|
|
|
stderr = "FATAL:zygote_host_impl_linux.cc:144] No usable sandbox!\n" + "#0 0x0000000000000000\n" * 400 + "[end of stack trace]"
|
|
|
|
|
|
|
|
|
|
def run(*args, **kwargs):
|
|
|
|
|
return subprocess.CompletedProcess(args, 1, "", stderr)
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(subprocess, "run", run)
|
|
|
|
|
with pytest.raises(OSError, match="No usable sandbox"):
|
2026-10-05 14:26:43 +02:00
|
|
|
BrowserExecutor(binary="/fake/chromium")('<script>alert("ousast-xss")</script>', "nonce")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not (shutil.which("chromium") or shutil.which("chromium-browser")), reason="chromium not installed")
|
2026-10-14 07:54:09 +02:00
|
|
|
@pytest.mark.sandbox
|
2026-10-05 14:26:43 +02:00
|
|
|
def test_real_browser_executes_not_reflects():
|
|
|
|
|
browser = BrowserExecutor()
|
|
|
|
|
payload = '<script>alert("ousast-xss")</script>'
|
|
|
|
|
try:
|
|
|
|
|
observed = browser(payload, "nonce")
|
|
|
|
|
except OSError as exc:
|
|
|
|
|
# A present binary is insufficient on restricted hosts. Only known
|
|
|
|
|
# host permission denials skip; ordinary browser failures remain failures.
|
2026-11-24 20:16:23 +01:00
|
|
|
# oracles.py raises a fixed message `from` the launch error: the cause carries the evidence.
|
|
|
|
|
cause = exc.__cause__ or exc
|
|
|
|
|
evidence = " ".join(str(part) for part in (cause, getattr(cause, "stderr", None)))
|
|
|
|
|
denied = next((text for text in ("No usable sandbox", "Operation not permitted", "Permission denied") if text in evidence), None)
|
|
|
|
|
if denied:
|
|
|
|
|
pytest.skip("host denies browser isolation/socket setup: " + denied)
|
2026-10-05 14:26:43 +02:00
|
|
|
raise
|
|
|
|
|
assert observed
|
|
|
|
|
assert not browser(html.escape(payload), "nonce")
|
|
|
|
|
assert not browser("<p>nonce</p>", "nonce")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("executed", [False, True])
|
2026-10-14 07:54:09 +02:00
|
|
|
@pytest.mark.sandbox
|
2026-10-05 14:26:43 +02:00
|
|
|
def test_marker_is_dom_attribute_not_source_text(monkeypatch, executed):
|
|
|
|
|
import re
|
|
|
|
|
from urllib.parse import unquote
|
|
|
|
|
|
|
|
|
|
def run(command, **kwargs):
|
|
|
|
|
assert "--headless" in command and "--dump-dom" in command
|
|
|
|
|
assert "--unshare-all" in command and "--no-sandbox" not in command
|
|
|
|
|
document = unquote(command[-1].split(",", 1)[1])
|
|
|
|
|
attr, value = re.search(r'setAttribute\("([^"]+)","([^"]+)"\)', document).groups()
|
|
|
|
|
root = f'<html {attr}="{value}">' if executed else "<html>"
|
|
|
|
|
return subprocess.CompletedProcess(command, 0, root + document + "</html>", "")
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr(subprocess, "run", run)
|
|
|
|
|
assert BrowserExecutor(binary="/usr/bin/chromium")("<p>reflected nonce</p>", "nonce") is executed
|