Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 9,924 Bytes
89af3b2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | """Run a guarded headless Chromium executor with credential recording disabled."""
import contextlib
import json
import logging
import os
import threading
from urllib.parse import urljoin, urlsplit
from browser_backend import BrowserRefusal
from core import login_origins, login_redact
_START_LOCK = threading.RLock()
_QUIET_LOCK = threading.RLock()
_DEBUG_KEYS = ("DEBUG", "PWDEBUG", "DEBUG_FILE", "PLAYWRIGHT_DEBUG_LOG",
"PW_TEST_TRACE", "PW_TEST_VIDEO", "PW_TEST_SCREENSHOT")
class _Discard:
def write(self, value):
return len(value)
def flush(self):
pass
@contextlib.contextmanager
def quiet_execution():
"""Discard executor and provider diagnostics rather than retaining credentials."""
# Logging and Python streams are process-wide, so nested callers share one lock.
with _QUIET_LOCK:
level = logging.root.manager.disable
saved_fds = []
sink = None
logging.disable(logging.CRITICAL)
try:
sink = os.open(os.devnull, os.O_WRONLY)
for fd in (1, 2):
saved_fds.append((fd, os.dup(fd)))
os.dup2(sink, fd)
with contextlib.redirect_stdout(_Discard()), contextlib.redirect_stderr(_Discard()):
yield
finally:
for fd, saved in reversed(saved_fds):
os.dup2(saved, fd)
os.close(saved)
if sink is not None:
os.close(sink)
logging.disable(level)
@contextlib.contextmanager
def _quiet_environment():
"""Clear inherited driver debug settings before starting the child process."""
with _START_LOCK:
saved = {name: os.environ.get(name) for name in _DEBUG_KEYS}
for name in _DEBUG_KEYS:
os.environ.pop(name, None)
os.environ["PWDEBUG"] = "0"
try:
yield
finally:
for name, value in saved.items():
if value is None:
os.environ.pop(name, None)
else:
os.environ[name] = value
class LocalBrowser:
"""Guard every request and fill against the saved Login origin."""
cost_bound_usd = 0
def __init__(self):
self._playwright = self._browser = self._context = self._page = None
self._handles = {}
self.blocked_origin = False
self._executor = False
@property
def landed_url(self):
return self._page.url if self._page is not None else ""
def _guard_url(self, url):
try:
parts = urlsplit(url)
if (parts.username is not None or parts.password is not None
or any(ord(ch) < 32 for ch in url) or "\\" in url):
raise ValueError()
clean = login_origins.normalize(parts.scheme + "://" + parts.netloc)
if clean != self._origin or self._origin_guard(clean) != clean:
raise ValueError()
except Exception:
self.blocked_origin = True
raise BrowserRefusal("blocked_origin") from None
return url
def _navigation_guard(self, route):
try:
self._guard_url(route.request.url)
if route.request.method not in ("GET", "HEAD") and not self._executor:
route.abort()
return
route.continue_()
except Exception:
self.blocked_origin = True
route.abort()
def _check_page(self):
if self.blocked_origin:
raise BrowserRefusal("blocked_origin")
self._guard_url(self.landed_url)
def open(self, origin, *, origin_guard):
self._origin, self._origin_guard = origin, origin_guard
try:
self._guard_url(origin)
with quiet_execution(), _quiet_environment():
from playwright.sync_api import sync_playwright
self._playwright = sync_playwright().start()
env = {key: value for key, value in os.environ.items() if key not in _DEBUG_KEYS}
env["PWDEBUG"] = "0"
self._browser = self._playwright.chromium.launch(
headless=True, env=env, args=["--disable-background-networking"])
# No tracing, video, HAR, screenshots, downloads or persistent profile.
self._context = self._browser.new_context(
service_workers="block", accept_downloads=False)
self._context.route("**/*", self._navigation_guard)
self._page = self._context.new_page()
self._page.set_default_timeout(5000)
self._page.set_default_navigation_timeout(5000)
self._page.on("dialog", lambda dialog: dialog.dismiss())
self._context.on("page", self._reject_popup)
return self
except Exception:
self.close()
raise BrowserRefusal("blocked_origin" if self.blocked_origin else "backend_unavailable") from None
def _reject_popup(self, page):
self.blocked_origin = True
page.close()
def goto(self, url):
try:
target = self._guard_url(urljoin(self._origin + "/", url))
self._page.goto(target, wait_until="domcontentloaded")
self._check_page()
except Exception:
raise BrowserRefusal("blocked_origin" if self.blocked_origin else "backend_unavailable") from None
def snapshot_text(self):
try:
self._check_page()
self._handles = {}
controls = []
nodes = self._page.locator("input, button, a[href]")
for index in range(nodes.count()):
node = nodes.nth(index)
if not node.is_visible():
continue
tag = node.evaluate("el => el.tagName.toLowerCase()")
kind, label = tag, ""
if tag == "input":
kind = node.get_attribute("type") or "text"
name = " ".join(node.get_attribute(key) or "" for key in
("name", "id", "autocomplete", "placeholder", "aria-label")).lower()
if kind == "password":
kind = "password"
elif any(word in name for word in ("code", "otp", "one-time")):
kind = "code"
elif any(word in name for word in ("username", "email", "user")):
kind = "username"
label = node.get_attribute("aria-label") or node.get_attribute("name") or kind
else:
label = node.inner_text()
handle = "h" + str(index)
self._handles[handle] = (node, kind)
controls.append({"handle": handle, "kind": kind, "label": label})
text = self._page.locator("body").inner_text()
return login_redact.scrub_for_model(json.dumps(
{"url": self.landed_url, "text": text, "controls": controls}))
except Exception:
raise BrowserRefusal("blocked_origin" if self.blocked_origin else "backend_unavailable") from None
def click(self, handle, *, executor=False):
try:
self._check_page()
node, kind = self._handles[handle]
if not executor:
if kind != "a" or node.get_attribute("download") is not None:
raise BrowserRefusal("read_only")
self._guard_url(urljoin(self.landed_url, node.get_attribute("href")))
elif kind != "button" or node.get_attribute("type") not in (None, "submit"):
raise BrowserRefusal("read_only")
if executor:
form = node.locator("xpath=ancestor::form")
if form.count() != 1:
raise BrowserRefusal("sign_in_failed")
self._guard_url(urljoin(self.landed_url, form.get_attribute("action") or self.landed_url))
self._executor = executor
try:
with quiet_execution():
node.click()
self._page.wait_for_load_state("domcontentloaded")
finally:
self._executor = False
self._check_page()
except BrowserRefusal:
raise
except Exception:
raise BrowserRefusal("blocked_origin" if self.blocked_origin else "sign_in_failed") from None
def type_handle(self, handle, text):
raise BrowserRefusal("read_only")
def fill_secret(self, handle, value, *, origin):
try:
self._check_page()
if origin != self._origin or login_origins.normalize(origin) != self._origin:
raise BrowserRefusal("blocked_origin")
node, kind = self._handles[handle]
if kind not in ("username", "password", "code"):
raise BrowserRefusal("sign_in_failed")
form = node.locator("xpath=ancestor::form")
if form.count() != 1:
raise BrowserRefusal("sign_in_failed")
self._guard_url(urljoin(self.landed_url, form.get_attribute("action") or self.landed_url))
with quiet_execution():
node.fill(value)
except BrowserRefusal:
raise
except Exception:
raise BrowserRefusal("sign_in_failed") from None
def close(self):
with quiet_execution():
for obj, method in ((self._context, "close"), (self._browser, "close"),
(self._playwright, "stop")):
if obj is not None:
try:
getattr(obj, method)()
except Exception:
pass
self._handles.clear()
self._playwright = self._browser = self._context = self._page = None
|