2026-10-09 09:39:37 +00:00
|
|
|
"""Check that an application page can reach only its own files and its bridge.
|
|
|
|
|
|
|
|
|
|
The harness installs a test package, using the Counter demo's component and
|
|
|
|
|
the page in tests/frame/confinement, in a temporary store and launches it
|
|
|
|
|
through weft-appd with the real runtime and app shell on the nested desktop.
|
|
|
|
|
The page probes, and paints one band per probe:
|
|
|
|
|
|
|
|
|
|
- an image from its own UI directory loads;
|
|
|
|
|
- the session bridge reaches the component, which answers;
|
|
|
|
|
- an image just outside its UI directory (../outside.png) fails to load;
|
|
|
|
|
- an image elsewhere on disk (a file: URL) fails to load;
|
2026-10-09 10:03:30 +00:00
|
|
|
- an image reached through a symbolic link in the UI directory fails to load;
|
2026-10-09 09:39:37 +00:00
|
|
|
- an image from a local HTTP server fails to load;
|
|
|
|
|
- a no-cors fetch from that server is rejected;
|
2026-10-09 10:03:30 +00:00
|
|
|
- a WebSocket to a local server that completes the handshake fails;
|
2026-10-09 10:16:22 +00:00
|
|
|
- a worker does not fetch from that server (Servo does not start workers
|
|
|
|
|
for file: pages, which also keeps them from fetching);
|
2026-10-09 10:03:30 +00:00
|
|
|
- an about:srcdoc frame has no navigator.servo, which could change engine
|
|
|
|
|
preferences.
|
2026-10-09 09:39:37 +00:00
|
|
|
|
2026-10-09 10:03:30 +00:00
|
|
|
The page also imports a stylesheet, loads a font and sends a beacon to the
|
|
|
|
|
HTTP server, and frames an about:srcdoc document showing an image from it.
|
2026-10-09 09:39:37 +00:00
|
|
|
|
2026-10-09 10:03:30 +00:00
|
|
|
Each refused probe would succeed without the policy: the images are valid,
|
|
|
|
|
the fetches accept an opaque response and the WebSocket server accepts the
|
|
|
|
|
upgrade. Every band must be green, and the HTTP server and the WebSocket
|
|
|
|
|
server must see no connection at all, so the refusals happen in the
|
|
|
|
|
renderer, not after a request left it.
|
2026-10-09 09:39:37 +00:00
|
|
|
|
|
|
|
|
Requires Xvfb, xwd and ImageMagick's convert.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import argparse
|
2026-10-09 10:03:30 +00:00
|
|
|
import base64
|
|
|
|
|
import hashlib
|
2026-10-09 09:39:37 +00:00
|
|
|
import http.server
|
|
|
|
|
import shutil
|
|
|
|
|
import socket
|
|
|
|
|
import struct
|
|
|
|
|
import sys
|
|
|
|
|
import tempfile
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
import zlib
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
|
from check_counter import AppdClient, free_port, read_endpoint # noqa: E402
|
|
|
|
|
from session import ROOT, Desktop, SessionError, missing_tools, parse_ppm, pixel # noqa: E402
|
|
|
|
|
|
|
|
|
|
APP_ID = "org.weft.test.confinement"
|
2026-10-09 10:03:30 +00:00
|
|
|
PROBES = ["own-image", "bridge", "outside-image", "other-image", "linked-image", "http-image",
|
|
|
|
|
"http-fetch", "websocket", "worker-fetch", "srcdoc-internals"]
|
2026-10-09 09:39:37 +00:00
|
|
|
GREEN = (0, 192, 0)
|
|
|
|
|
# The app window's page area on the 1024x768 nested desktop.
|
|
|
|
|
PAGE_TOP, PAGE_HEIGHT, PAGE_X = 35, 600, 400
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def png():
|
|
|
|
|
"""A valid 1x1 PNG image."""
|
|
|
|
|
def chunk(kind, data):
|
|
|
|
|
body = kind + data
|
|
|
|
|
return struct.pack(">I", len(data)) + body + struct.pack(">I", zlib.crc32(body))
|
|
|
|
|
header = struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)
|
|
|
|
|
pixels = zlib.compress(b"\x00\x00\x00\xff")
|
|
|
|
|
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", pixels)
|
|
|
|
|
+ chunk(b"IEND", b""))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Counting(http.server.BaseHTTPRequestHandler):
|
2026-10-09 10:03:30 +00:00
|
|
|
requests = []
|
2026-10-09 09:39:37 +00:00
|
|
|
|
|
|
|
|
def do_GET(self):
|
2026-10-09 10:03:30 +00:00
|
|
|
Counting.requests.append(f"{self.command} {self.path}")
|
2026-10-09 09:39:37 +00:00
|
|
|
body = png()
|
|
|
|
|
self.send_response(200)
|
|
|
|
|
self.send_header("Content-Type", "image/png")
|
|
|
|
|
self.send_header("Access-Control-Allow-Origin", "*")
|
|
|
|
|
self.send_header("Content-Length", str(len(body)))
|
|
|
|
|
self.end_headers()
|
|
|
|
|
self.wfile.write(body)
|
|
|
|
|
|
2026-10-09 10:03:30 +00:00
|
|
|
do_POST = do_GET
|
|
|
|
|
|
2026-10-09 09:39:37 +00:00
|
|
|
def log_message(self, *args):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2026-10-09 10:03:30 +00:00
|
|
|
def websocket_server(connections, stop):
|
|
|
|
|
"""A listener that completes WebSocket handshakes, so a WebSocket that
|
|
|
|
|
reaches it opens. Returns its port."""
|
|
|
|
|
listener = socket.socket()
|
|
|
|
|
listener.bind(("127.0.0.1", 0))
|
|
|
|
|
listener.listen()
|
|
|
|
|
listener.settimeout(0.2)
|
|
|
|
|
|
|
|
|
|
def serve():
|
|
|
|
|
while not stop.is_set():
|
|
|
|
|
try:
|
|
|
|
|
connection = listener.accept()[0]
|
|
|
|
|
except OSError:
|
|
|
|
|
continue
|
|
|
|
|
connections.append(connection)
|
|
|
|
|
connection.settimeout(5)
|
|
|
|
|
try:
|
|
|
|
|
request = b""
|
|
|
|
|
while b"\r\n\r\n" not in request:
|
|
|
|
|
chunk = connection.recv(4096)
|
|
|
|
|
if not chunk:
|
|
|
|
|
break
|
|
|
|
|
request += chunk
|
|
|
|
|
key = next((line.split(b":", 1)[1].strip() for line in request.split(b"\r\n")
|
|
|
|
|
if line.lower().startswith(b"sec-websocket-key:")), b"")
|
|
|
|
|
accept = base64.b64encode(hashlib.sha1(
|
|
|
|
|
key + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11").digest())
|
|
|
|
|
connection.sendall(b"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\n"
|
|
|
|
|
b"Connection: Upgrade\r\nSec-WebSocket-Accept: " + accept
|
|
|
|
|
+ b"\r\n\r\n")
|
|
|
|
|
except OSError:
|
|
|
|
|
pass
|
|
|
|
|
listener.close()
|
|
|
|
|
|
|
|
|
|
threading.Thread(target=serve, daemon=True).start()
|
|
|
|
|
return listener.getsockname()[1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def make_package(store, http_port, ws_port, other_dir):
|
2026-10-09 09:39:37 +00:00
|
|
|
package = store / APP_ID
|
|
|
|
|
(package / "ui").mkdir(parents=True)
|
|
|
|
|
(package / "wapp.toml").write_text(
|
|
|
|
|
f'[package]\nid = "{APP_ID}"\nname = "Confinement"\nversion = "0.1.0"\n'
|
|
|
|
|
'[runtime]\nmodule = "app.wasm"\n[ui]\nentry = "ui/index.html"\n')
|
|
|
|
|
shutil.copy(ROOT / "examples/org.weft.demo.counter/app.wasm", package / "app.wasm")
|
2026-10-09 10:03:30 +00:00
|
|
|
fixture = Path(__file__).parent / "confinement"
|
|
|
|
|
other = other_dir / "other.png"
|
|
|
|
|
page = (fixture / "index.html").read_text()
|
|
|
|
|
page = (page.replace("@HTTP@", f"http://127.0.0.1:{http_port}/")
|
|
|
|
|
.replace("@WS@", f"ws://127.0.0.1:{ws_port}/")
|
2026-10-09 09:39:37 +00:00
|
|
|
.replace("@OTHER_URL@", other.as_uri()))
|
|
|
|
|
(package / "ui/index.html").write_text(page)
|
|
|
|
|
(package / "ui/own.png").write_bytes(png())
|
|
|
|
|
(package / "outside.png").write_bytes(png())
|
|
|
|
|
other.write_bytes(png())
|
2026-10-09 10:03:30 +00:00
|
|
|
(package / "ui/linked").symlink_to(other_dir)
|
fix(appd): verify package owner and signature before a session starts
weft-appd trusted an installed directory as installed, so content
changed after installation, or signed by a key since removed from the
trust store, still ran with the owner's app data. Before anything
starts, a package recorded as a publisher's must carry that
publisher's valid signature over its current content, with the key
still trusted; a package with no record must be signed by a trusted
key; development content, recorded by weft-pack install --dev, runs
unverified. Failures are refused with code 403.
The desktop checks trust the demo key, and the confinement check,
whose package deliberately holds a link, records it as development
content.
2026-10-09 14:29:14 +00:00
|
|
|
# The package holds a link, which weft-pack refuses to install, so it is
|
|
|
|
|
# planted directly and recorded as development content, which runs
|
|
|
|
|
# unverified: the renderer's own confinement is what this check probes.
|
|
|
|
|
record = store / "home/share/weft/owners" / APP_ID
|
|
|
|
|
record.parent.mkdir(parents=True)
|
|
|
|
|
record.write_text("development\n")
|
2026-10-09 09:39:37 +00:00
|
|
|
|
|
|
|
|
|
2026-10-09 10:03:30 +00:00
|
|
|
def run(args, desktop, store, other_dir):
|
2026-10-09 09:39:37 +00:00
|
|
|
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Counting)
|
|
|
|
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
|
|
|
stop = threading.Event()
|
2026-10-09 10:03:30 +00:00
|
|
|
connections = []
|
|
|
|
|
ws_port = websocket_server(connections, stop)
|
2026-10-09 09:39:37 +00:00
|
|
|
try:
|
2026-10-09 10:03:30 +00:00
|
|
|
make_package(store, server.server_port, ws_port, other_dir)
|
2026-10-09 09:39:37 +00:00
|
|
|
t = Path(args.target)
|
|
|
|
|
desktop.launch_client("appd", [t / "weft-appd"], {
|
|
|
|
|
"WEFT_RUNTIME_BIN": str(t / "weft-runtime"),
|
|
|
|
|
"WEFT_APP_SHELL_BIN": str(t / "weft-app-shell"),
|
|
|
|
|
"WEFT_APP_STORE": str(store),
|
fix(appd): verify package owner and signature before a session starts
weft-appd trusted an installed directory as installed, so content
changed after installation, or signed by a key since removed from the
trust store, still ran with the owner's app data. Before anything
starts, a package recorded as a publisher's must carry that
publisher's valid signature over its current content, with the key
still trusted; a package with no record must be signed by a trusted
key; development content, recorded by weft-pack install --dev, runs
unverified. Failures are refused with code 403.
The desktop checks trust the demo key, and the confinement check,
whose package deliberately holds a link, records it as development
content.
2026-10-09 14:29:14 +00:00
|
|
|
"HOME": str(store / "home"),
|
|
|
|
|
"XDG_DATA_HOME": str(store / "home/share"),
|
2026-10-09 09:39:37 +00:00
|
|
|
"WEFT_DISABLE_CGROUP": "1",
|
|
|
|
|
"WEFT_APPD_WS_PORT": str(free_port()),
|
|
|
|
|
})
|
|
|
|
|
port, token = read_endpoint(desktop.runtime, 30)
|
|
|
|
|
appd = AppdClient(port, token)
|
|
|
|
|
appd.send({"type": "LAUNCH_APP", "app_id": APP_ID, "surface_id": 0})
|
|
|
|
|
reply = appd.wait_for(lambda m: m.get("type") in ("APP_READY", "ERROR"), 120)
|
|
|
|
|
if reply.get("type") != "APP_READY":
|
|
|
|
|
raise AssertionError(f"the test app did not start: {reply}")
|
|
|
|
|
|
|
|
|
|
deadline = time.monotonic() + 15
|
|
|
|
|
while True:
|
|
|
|
|
width, _, pixels = parse_ppm(desktop.capture())
|
|
|
|
|
colours = {}
|
|
|
|
|
for i, name in enumerate(PROBES):
|
|
|
|
|
y = PAGE_TOP + int((i + 0.5) * PAGE_HEIGHT / len(PROBES))
|
|
|
|
|
colours[name] = pixel(width, pixels, PAGE_X, y)
|
|
|
|
|
if all(c == GREEN for c in colours.values()) or time.monotonic() > deadline:
|
|
|
|
|
break
|
|
|
|
|
time.sleep(0.5)
|
2026-10-09 10:16:22 +00:00
|
|
|
time.sleep(1) # let requests the page sent after the last band arrive
|
|
|
|
|
problems = []
|
2026-10-09 09:39:37 +00:00
|
|
|
failed = [name for name, c in colours.items() if c != GREEN]
|
|
|
|
|
if failed:
|
2026-10-09 10:16:22 +00:00
|
|
|
problems.append(f"probes not as required: {failed}")
|
2026-10-09 09:39:37 +00:00
|
|
|
if Counting.requests or connections:
|
2026-10-09 10:16:22 +00:00
|
|
|
problems.append(f"requests left the renderer: HTTP {sorted(Counting.requests)}, "
|
|
|
|
|
f"{len(connections)} WebSocket")
|
|
|
|
|
if problems:
|
|
|
|
|
raise AssertionError("; ".join(problems))
|
2026-10-09 09:39:37 +00:00
|
|
|
finally:
|
|
|
|
|
stop.set()
|
|
|
|
|
server.shutdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
|
|
|
parser.add_argument("--compositor", type=Path, default=ROOT / "target/debug/weft-compositor")
|
|
|
|
|
parser.add_argument("--target", type=Path, default=ROOT / "target/debug",
|
|
|
|
|
help="directory with weft-appd, weft-runtime and weft-app-shell")
|
|
|
|
|
parser.add_argument("--output", type=Path, default=ROOT / "target/confinement-check")
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
|
|
missing = missing_tools() + [str(p) for p in (args.compositor, args.target / "weft-appd",
|
|
|
|
|
args.target / "weft-runtime",
|
|
|
|
|
args.target / "weft-app-shell")
|
|
|
|
|
if not p.is_file()]
|
|
|
|
|
if missing:
|
|
|
|
|
print("missing: " + ", ".join(missing), file=sys.stderr)
|
|
|
|
|
return 2
|
|
|
|
|
store = Path(tempfile.mkdtemp(prefix="weft-store-"))
|
2026-10-09 10:03:30 +00:00
|
|
|
other_dir = Path(tempfile.mkdtemp(prefix="weft-other-"))
|
2026-10-09 09:39:37 +00:00
|
|
|
try:
|
|
|
|
|
with Desktop(args.compositor, args.output, outputs=["appd.log", "confinement.ppm"]) \
|
|
|
|
|
as desktop:
|
|
|
|
|
try:
|
2026-10-09 10:03:30 +00:00
|
|
|
run(args, desktop, store, other_dir)
|
2026-10-09 09:39:37 +00:00
|
|
|
except AssertionError:
|
|
|
|
|
(args.output / "confinement.ppm").write_bytes(desktop.capture())
|
|
|
|
|
raise
|
|
|
|
|
except (AssertionError, SessionError, TimeoutError) as failure:
|
|
|
|
|
print(f"confinement check failed: {failure}", file=sys.stderr)
|
|
|
|
|
print(f"logs and capture in {args.output}", file=sys.stderr)
|
|
|
|
|
return 1
|
|
|
|
|
finally:
|
|
|
|
|
shutil.rmtree(store, ignore_errors=True)
|
2026-10-09 10:03:30 +00:00
|
|
|
shutil.rmtree(other_dir, ignore_errors=True)
|
2026-10-09 09:39:37 +00:00
|
|
|
print("confinement check passed")
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
raise SystemExit(main())
|