fusiontest 0.2.6__tar.gz → 0.2.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {fusiontest-0.2.6 → fusiontest-0.2.7}/PKG-INFO +1 -1
- fusiontest-0.2.7/fusiontest/core/netguard.py +230 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/desktop/playwright_adapter.py +19 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/discovery/goal_generator.py +6 -3
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/PKG-INFO +1 -1
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/SOURCES.txt +1 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/pyproject.toml +1 -1
- {fusiontest-0.2.6 → fusiontest-0.2.7}/README.md +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/cli.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/action_model.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/goal_verifier.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/replay.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/runner.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/screen_parser.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/secrets.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/core/tokens.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/desktop/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/desktop/macos_adapter.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/desktop/windows_adapter.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/discovery/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/guardrails/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/guardrails/engine.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/mobile/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/mobile/android_adapter.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/mobile/ios_adapter.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/recording/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/recording/recorder.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/reporting/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/reporting/reporter.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/training/__init__.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/training/data_collector.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/training/dataset_builder.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest/training/trainer.py +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/dependency_links.txt +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/entry_points.txt +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/not-zip-safe +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/requires.txt +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/fusiontest.egg-info/top_level.txt +0 -0
- {fusiontest-0.2.6 → fusiontest-0.2.7}/setup.cfg +0 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
"""
|
|
2
|
+
fusiontest/core/netguard.py
|
|
3
|
+
|
|
4
|
+
SSRF protection for server-side runs: keep user-supplied URLs (test targets,
|
|
5
|
+
discovery, webhooks) away from private networks — localhost, the VPC, and
|
|
6
|
+
link-local addresses such as the ECS task-metadata/credentials endpoint
|
|
7
|
+
(169.254.170.2) or cloud instance metadata (169.254.169.254).
|
|
8
|
+
|
|
9
|
+
- ``check_public_url`` validates a URL up front (API endpoints, webhooks).
|
|
10
|
+
- ``EgressProxy`` is a small forward proxy the browser is forced through. It
|
|
11
|
+
resolves every host itself, refuses private addresses, and connects to the
|
|
12
|
+
address it checked — so redirects, subresources and DNS rebinding can't
|
|
13
|
+
reach a private network. (Playwright's route interception doesn't see
|
|
14
|
+
redirect targets, so it can't be the enforcement point.)
|
|
15
|
+
|
|
16
|
+
The local CLI doesn't use this: testing apps on localhost is a normal use.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
import asyncio
|
|
22
|
+
import ipaddress
|
|
23
|
+
import logging
|
|
24
|
+
import socket
|
|
25
|
+
import threading
|
|
26
|
+
from urllib.parse import urlsplit
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger("fusiontest.netguard")
|
|
29
|
+
|
|
30
|
+
_ALLOWED_SCHEMES = {"http", "https"}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class BlockedAddressError(ValueError):
|
|
34
|
+
"""The URL or host points at a private/internal network (or can't be resolved)."""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def is_public_address(address: str) -> bool:
|
|
38
|
+
"""True if an IP address is publicly routable (not private, loopback, link-local…)."""
|
|
39
|
+
ip = ipaddress.ip_address(address.split("%", 1)[0]) # drop IPv6 zone id
|
|
40
|
+
if isinstance(ip, ipaddress.IPv6Address) and ip.ipv4_mapped is not None:
|
|
41
|
+
ip = ip.ipv4_mapped
|
|
42
|
+
return ip.is_global and not ip.is_multicast
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _getaddrinfo(host: str, port: int) -> list[str]:
|
|
46
|
+
infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
|
|
47
|
+
return sorted({info[4][0] for info in infos})
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Indirection so tests can resolve without real DNS.
|
|
51
|
+
_resolve = _getaddrinfo
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def public_addresses(host: str, port: int) -> list[str]:
|
|
55
|
+
"""Resolve a host; raise BlockedAddressError unless every address is public."""
|
|
56
|
+
if not host:
|
|
57
|
+
raise BlockedAddressError("URL has no host")
|
|
58
|
+
try:
|
|
59
|
+
addresses = _resolve(host, port)
|
|
60
|
+
except OSError as exc:
|
|
61
|
+
raise BlockedAddressError(f"could not resolve {host!r}") from exc
|
|
62
|
+
if not addresses:
|
|
63
|
+
raise BlockedAddressError(f"could not resolve {host!r}")
|
|
64
|
+
private = [a for a in addresses if not is_public_address(a)]
|
|
65
|
+
if private:
|
|
66
|
+
raise BlockedAddressError(f"{host!r} resolves to a private or internal address ({private[0]})")
|
|
67
|
+
return addresses
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def check_public_url(url: str) -> None:
|
|
71
|
+
"""Raise BlockedAddressError unless `url` is http(s) and its host is public."""
|
|
72
|
+
parts = urlsplit(url.strip())
|
|
73
|
+
if parts.scheme not in _ALLOWED_SCHEMES:
|
|
74
|
+
raise BlockedAddressError("only http:// and https:// URLs are allowed")
|
|
75
|
+
try:
|
|
76
|
+
port = parts.port or (443 if parts.scheme == "https" else 80)
|
|
77
|
+
except ValueError as exc:
|
|
78
|
+
raise BlockedAddressError("invalid port in URL") from exc
|
|
79
|
+
public_addresses(parts.hostname or "", port)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# ── Egress proxy ──────────────────────────────────────────────────────────────
|
|
83
|
+
|
|
84
|
+
_HEADER_LIMIT = 64 * 1024
|
|
85
|
+
_HOP_BY_HOP = {"proxy-connection", "proxy-authorization", "connection", "keep-alive"}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class EgressProxy:
|
|
89
|
+
"""
|
|
90
|
+
HTTP(S) forward proxy that only connects to public addresses. Runs on its
|
|
91
|
+
own thread and event loop, so it keeps serving while the caller's loop is
|
|
92
|
+
idle. Use: ``port = proxy.start()`` … ``proxy.stop()``.
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(self) -> None:
|
|
96
|
+
self._loop: asyncio.AbstractEventLoop | None = None
|
|
97
|
+
self._server: asyncio.base_events.Server | None = None
|
|
98
|
+
self._thread: threading.Thread | None = None
|
|
99
|
+
self.port = 0
|
|
100
|
+
|
|
101
|
+
def start(self) -> int:
|
|
102
|
+
ready = threading.Event()
|
|
103
|
+
|
|
104
|
+
def _serve() -> None:
|
|
105
|
+
self._loop = asyncio.new_event_loop()
|
|
106
|
+
asyncio.set_event_loop(self._loop)
|
|
107
|
+
self._server = self._loop.run_until_complete(
|
|
108
|
+
asyncio.start_server(self._handle, "127.0.0.1", 0)
|
|
109
|
+
)
|
|
110
|
+
self.port = self._server.sockets[0].getsockname()[1]
|
|
111
|
+
ready.set()
|
|
112
|
+
self._loop.run_forever()
|
|
113
|
+
|
|
114
|
+
self._thread = threading.Thread(target=_serve, name="egress-proxy", daemon=True)
|
|
115
|
+
self._thread.start()
|
|
116
|
+
if not ready.wait(timeout=10):
|
|
117
|
+
raise RuntimeError("egress proxy failed to start")
|
|
118
|
+
return self.port
|
|
119
|
+
|
|
120
|
+
def stop(self) -> None:
|
|
121
|
+
loop = self._loop
|
|
122
|
+
if loop is None:
|
|
123
|
+
return
|
|
124
|
+
|
|
125
|
+
async def _shutdown() -> None:
|
|
126
|
+
if self._server is not None:
|
|
127
|
+
self._server.close()
|
|
128
|
+
for task in asyncio.all_tasks():
|
|
129
|
+
if task is not asyncio.current_task():
|
|
130
|
+
task.cancel()
|
|
131
|
+
|
|
132
|
+
asyncio.run_coroutine_threadsafe(_shutdown(), loop).result(timeout=5)
|
|
133
|
+
loop.call_soon_threadsafe(loop.stop)
|
|
134
|
+
if self._thread is not None:
|
|
135
|
+
self._thread.join(timeout=5)
|
|
136
|
+
loop.close()
|
|
137
|
+
self._loop = None
|
|
138
|
+
|
|
139
|
+
# ── Connection handling ───────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
async def _handle(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
|
|
142
|
+
try:
|
|
143
|
+
head = await reader.readuntil(b"\r\n\r\n")
|
|
144
|
+
except (asyncio.IncompleteReadError, asyncio.LimitOverrunError, ConnectionError):
|
|
145
|
+
writer.close()
|
|
146
|
+
return
|
|
147
|
+
if len(head) > _HEADER_LIMIT:
|
|
148
|
+
await self._refuse(writer, 431, "headers too large")
|
|
149
|
+
return
|
|
150
|
+
|
|
151
|
+
lines = head.decode("latin-1").split("\r\n")
|
|
152
|
+
try:
|
|
153
|
+
method, target, version = lines[0].split(" ", 2)
|
|
154
|
+
except ValueError:
|
|
155
|
+
await self._refuse(writer, 400, "bad request")
|
|
156
|
+
return
|
|
157
|
+
|
|
158
|
+
if method.upper() == "CONNECT":
|
|
159
|
+
host, _, port_text = target.rpartition(":")
|
|
160
|
+
host = host.strip("[]")
|
|
161
|
+
port = int(port_text) if port_text.isdigit() else 443
|
|
162
|
+
upstream = await self._connect(writer, host, port)
|
|
163
|
+
if upstream is None:
|
|
164
|
+
return
|
|
165
|
+
writer.write(b"HTTP/1.1 200 Connection Established\r\n\r\n")
|
|
166
|
+
await writer.drain()
|
|
167
|
+
await self._pipe_both(reader, writer, *upstream)
|
|
168
|
+
return
|
|
169
|
+
|
|
170
|
+
parts = urlsplit(target)
|
|
171
|
+
if parts.scheme != "http" or not parts.hostname:
|
|
172
|
+
await self._refuse(writer, 400, "only absolute http:// URLs can be proxied")
|
|
173
|
+
return
|
|
174
|
+
upstream = await self._connect(writer, parts.hostname, parts.port or 80)
|
|
175
|
+
if upstream is None:
|
|
176
|
+
return
|
|
177
|
+
up_reader, up_writer = upstream
|
|
178
|
+
path = parts.path or "/"
|
|
179
|
+
if parts.query:
|
|
180
|
+
path += "?" + parts.query
|
|
181
|
+
headers = [h for h in lines[1:] if h and h.split(":", 1)[0].strip().lower() not in _HOP_BY_HOP]
|
|
182
|
+
request = "\r\n".join([f"{method} {path} {version}", *headers, "Connection: close", "", ""])
|
|
183
|
+
up_writer.write(request.encode("latin-1"))
|
|
184
|
+
await up_writer.drain()
|
|
185
|
+
await self._pipe_both(reader, writer, up_reader, up_writer)
|
|
186
|
+
|
|
187
|
+
async def _connect(self, writer: asyncio.StreamWriter, host: str, port: int):
|
|
188
|
+
"""Resolve, check and connect to the checked address; refuse private targets."""
|
|
189
|
+
try:
|
|
190
|
+
addresses = await asyncio.get_running_loop().run_in_executor(
|
|
191
|
+
None, public_addresses, host, port
|
|
192
|
+
)
|
|
193
|
+
except BlockedAddressError as exc:
|
|
194
|
+
logger.warning("Blocked browser request to %s:%s — %s", host, port, exc)
|
|
195
|
+
await self._refuse(writer, 403, f"blocked: {exc}")
|
|
196
|
+
return None
|
|
197
|
+
try:
|
|
198
|
+
return await asyncio.open_connection(addresses[0], port)
|
|
199
|
+
except OSError as exc:
|
|
200
|
+
await self._refuse(writer, 502, f"could not connect: {exc}")
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
@staticmethod
|
|
204
|
+
async def _refuse(writer: asyncio.StreamWriter, status: int, reason: str) -> None:
|
|
205
|
+
body = reason.encode()
|
|
206
|
+
writer.write(
|
|
207
|
+
f"HTTP/1.1 {status} Blocked\r\nContent-Type: text/plain\r\n"
|
|
208
|
+
f"Content-Length: {len(body)}\r\nConnection: close\r\n\r\n".encode() + body
|
|
209
|
+
)
|
|
210
|
+
try:
|
|
211
|
+
await writer.drain()
|
|
212
|
+
finally:
|
|
213
|
+
writer.close()
|
|
214
|
+
|
|
215
|
+
@staticmethod
|
|
216
|
+
async def _pipe_both(client_r, client_w, up_r, up_w) -> None:
|
|
217
|
+
async def pipe(src: asyncio.StreamReader, dst: asyncio.StreamWriter) -> None:
|
|
218
|
+
try:
|
|
219
|
+
while chunk := await src.read(65536):
|
|
220
|
+
dst.write(chunk)
|
|
221
|
+
await dst.drain()
|
|
222
|
+
except (ConnectionError, asyncio.CancelledError):
|
|
223
|
+
pass
|
|
224
|
+
finally:
|
|
225
|
+
try:
|
|
226
|
+
dst.close()
|
|
227
|
+
except Exception: # noqa: BLE001
|
|
228
|
+
pass
|
|
229
|
+
|
|
230
|
+
await asyncio.gather(pipe(client_r, up_w), pipe(up_r, client_w))
|
|
@@ -20,6 +20,7 @@ import os
|
|
|
20
20
|
import re
|
|
21
21
|
|
|
22
22
|
from fusiontest.core.action_model import Action
|
|
23
|
+
from fusiontest.core.netguard import EgressProxy
|
|
23
24
|
from fusiontest.core.screen_parser import ScreenParser, UIElement
|
|
24
25
|
|
|
25
26
|
logger = logging.getLogger("fusiontest.desktop.playwright")
|
|
@@ -91,12 +92,18 @@ class PlaywrightAdapter:
|
|
|
91
92
|
slow_mo: int = 100,
|
|
92
93
|
viewport: dict | None = None,
|
|
93
94
|
electron_app_path: str | None = None,
|
|
95
|
+
block_private_networks: bool = False,
|
|
94
96
|
):
|
|
97
|
+
"""block_private_networks: route all browser traffic through an
|
|
98
|
+
EgressProxy that refuses private/internal addresses (server-side runs
|
|
99
|
+
on user-supplied URLs). Off for local runs, which test localhost apps."""
|
|
95
100
|
self.browser_type = browser_type
|
|
96
101
|
self.headless = headless
|
|
97
102
|
self.slow_mo = slow_mo
|
|
98
103
|
self.viewport = viewport or {"width": 1280, "height": 800}
|
|
99
104
|
self.electron_app_path = electron_app_path
|
|
105
|
+
self.block_private_networks = block_private_networks
|
|
106
|
+
self._egress_proxy: EgressProxy | None = None
|
|
100
107
|
|
|
101
108
|
self._playwright = None
|
|
102
109
|
self._browser = None
|
|
@@ -176,10 +183,19 @@ class PlaywrightAdapter:
|
|
|
176
183
|
"--disable-dev-shm-usage", "--disable-gpu"]
|
|
177
184
|
if (self.headless or _in_container) else []
|
|
178
185
|
)
|
|
186
|
+
launch_options: dict = {}
|
|
187
|
+
if self.block_private_networks:
|
|
188
|
+
self._egress_proxy = EgressProxy()
|
|
189
|
+
port = self._egress_proxy.start()
|
|
190
|
+
# "<-loopback>" removes Chromium's implicit proxy bypass for
|
|
191
|
+
# localhost, so every request — including 127.0.0.1 — is checked.
|
|
192
|
+
launch_options["proxy"] = {"server": f"http://127.0.0.1:{port}", "bypass": "<-loopback>"}
|
|
193
|
+
_docker_args = [*_docker_args, "--force-webrtc-ip-handling-policy=disable_non_proxied_udp"]
|
|
179
194
|
self._browser = await browser_launcher.launch(
|
|
180
195
|
headless=self.headless,
|
|
181
196
|
slow_mo=self.slow_mo,
|
|
182
197
|
args=_docker_args,
|
|
198
|
+
**launch_options,
|
|
183
199
|
)
|
|
184
200
|
context = await self._browser.new_context(viewport=self.viewport)
|
|
185
201
|
self._page = await context.new_page()
|
|
@@ -212,6 +228,9 @@ class PlaywrightAdapter:
|
|
|
212
228
|
await closer()
|
|
213
229
|
except Exception as exc: # noqa: BLE001 — already closed / crashed
|
|
214
230
|
logger.debug("Ignoring error while stopping Playwright: %s", exc)
|
|
231
|
+
proxy, self._egress_proxy = self._egress_proxy, None
|
|
232
|
+
if proxy is not None:
|
|
233
|
+
proxy.stop()
|
|
215
234
|
logger.info("PlaywrightAdapter stopped")
|
|
216
235
|
|
|
217
236
|
# ── Screen reading ─────────────────────────────────────────────────────────
|
|
@@ -77,13 +77,16 @@ class SiteSnapshot:
|
|
|
77
77
|
|
|
78
78
|
# ── Site snapshot ─────────────────────────────────────────────────────────────
|
|
79
79
|
|
|
80
|
-
def snapshot_site(
|
|
81
|
-
|
|
80
|
+
def snapshot_site(
|
|
81
|
+
url: str, config: GoalGeneratorConfig | None = None, block_private_networks: bool = False,
|
|
82
|
+
) -> SiteSnapshot:
|
|
83
|
+
"""Capture the start page and pages linked from its navigation.
|
|
84
|
+
block_private_networks: server-side use — see PlaywrightAdapter."""
|
|
82
85
|
from fusiontest.desktop.playwright_adapter import PlaywrightAdapter # noqa: PLC0415
|
|
83
86
|
|
|
84
87
|
cfg = config or GoalGeneratorConfig()
|
|
85
88
|
snapshot = SiteSnapshot(url=url)
|
|
86
|
-
adapter = PlaywrightAdapter(headless=True)
|
|
89
|
+
adapter = PlaywrightAdapter(headless=True, block_private_networks=block_private_networks)
|
|
87
90
|
try:
|
|
88
91
|
adapter.start(url)
|
|
89
92
|
snapshot.pages.append((url, _page_text(adapter, cfg)))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|