mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
A fetch grant for a host name, or for any host, let a component reach this machine and the local network whenever the name resolved there. A granted name could therefore be pointed by DNS at loopback, link-local metadata or private services. The runtime now resolves each destination itself. It passes only the allowed addresses to the HTTP client, so the address checked is the one connected to. Names and the wildcard reach only public unicast addresses. An IPv4 address named in a grant is still reached as declared. Credentials in a URL are refused. tests/runtime/check_grants.py runs the real component and checks these refusals: the local server through the wildcard, the name localhost, and credentials in a URL. The server records that none of the refused requests reached it.
210 lines
9 KiB
Python
210 lines
9 KiB
Python
"""Check that weft-runtime enforces the grants weft-appd passes it.
|
|
|
|
The harness runs the real runtime (built with wasmtime-runtime and
|
|
net-fetch) on the test component in tests/components/grants-probe, which
|
|
calls every grant-controlled operation once and prints its outcome. Each
|
|
scenario passes a different set of `--grant` and `--preopen` arguments:
|
|
|
|
- no grants: no /data, and every host import reports that its capability
|
|
is not granted;
|
|
- read-only data: /data can be read, but creating, overwriting, renaming
|
|
and removing files and creating directories all fail and the host
|
|
directory is unchanged;
|
|
- read-write data: the write reaches the host directory;
|
|
- notifications and clipboard read: those imports are attempted (they may
|
|
still fail for lack of a desktop service) while clipboard write stays
|
|
denied;
|
|
- a host-specific fetch grant: requests to that host return their real
|
|
status (200, 404, and 302 without following the redirect to another
|
|
host); requests to another host, credentials in the URL, a forged
|
|
`Host` header, a method that injects a request line and a `file:` URL
|
|
are refused;
|
|
- a wildcard fetch grant, and a grant for the name `localhost`: the local
|
|
server is refused, since only an address named in a grant reaches this
|
|
machine or a local network; the connection is never made.
|
|
|
|
It also checks that the runtime refuses, with a specific error, filesystem
|
|
capabilities passed as `--grant`, unknown capabilities and preopens
|
|
without an access mode.
|
|
|
|
Requires cargo with the wasm32-wasip2 target.
|
|
"""
|
|
|
|
import argparse
|
|
import http.server
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
PROBE_DIR = ROOT / "tests/components/grants-probe"
|
|
APP_ID = "org.weft.test.grants"
|
|
NOT_GRANTED = "is not granted"
|
|
|
|
|
|
# Paths the test server was asked for.
|
|
REQUESTS = []
|
|
|
|
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
REQUESTS.append(self.path)
|
|
if self.path == "/ok":
|
|
self.send_response(200)
|
|
elif self.path == "/redirect":
|
|
self.send_response(302)
|
|
self.send_header("Location", f"http://localhost:{self.server.server_port}/ok")
|
|
else:
|
|
self.send_response(404)
|
|
self.send_header("Content-Length", "0")
|
|
self.end_headers()
|
|
|
|
def log_message(self, *args):
|
|
pass
|
|
|
|
|
|
def build_probe():
|
|
subprocess.run(["cargo", "build", "-q", "--release", "--target", "wasm32-wasip2"],
|
|
cwd=PROBE_DIR, check=True)
|
|
return PROBE_DIR / "target/wasm32-wasip2/release/app.wasm"
|
|
|
|
|
|
class Runner:
|
|
def __init__(self, runtime, work, port):
|
|
self.runtime = runtime
|
|
self.work = work
|
|
self.module = work / "app.wasm"
|
|
self.targets = work / "targets"
|
|
self.targets.mkdir()
|
|
(self.targets / "targets.txt").write_text(
|
|
f"fetch-ok http://127.0.0.1:{port}/ok\n"
|
|
f"fetch-missing http://127.0.0.1:{port}/missing\n"
|
|
f"fetch-redirect http://127.0.0.1:{port}/redirect\n"
|
|
f"fetch-other-host http://localhost:{port}/ok\n"
|
|
f"fetch-credentials http://user:secret@127.0.0.1:{port}/ok\n")
|
|
|
|
def run(self, *arguments, expect_success=True):
|
|
command = [str(self.runtime), APP_ID, "1", "--module", str(self.module),
|
|
"--preopen", f"{self.targets}::/probe::ro", *arguments]
|
|
result = subprocess.run(command, capture_output=True, text=True, timeout=60,
|
|
env={"PATH": "/usr/bin:/bin", "RUST_LOG": "warn"})
|
|
if expect_success and result.returncode != 0:
|
|
raise AssertionError(f"runtime failed: {' '.join(arguments)}\n{result.stderr}")
|
|
probes = {}
|
|
for line in result.stdout.splitlines():
|
|
if line.startswith("PROBE "):
|
|
name, outcome = line[6:].split(" ", 1)
|
|
probes[name] = outcome
|
|
return result, probes
|
|
|
|
|
|
def expect(probes, name, ok, contains=None, absent=None):
|
|
outcome = probes.get(name)
|
|
if outcome is None:
|
|
raise AssertionError(f"{name}: no result")
|
|
if outcome.startswith("ok") != ok:
|
|
raise AssertionError(f"{name}: expected {'success' if ok else 'failure'}, got {outcome!r}")
|
|
if contains is not None and contains not in outcome:
|
|
raise AssertionError(f"{name}: expected {contains!r} in {outcome!r}")
|
|
if absent is not None and absent in outcome:
|
|
raise AssertionError(f"{name}: did not expect {absent!r} in {outcome!r}")
|
|
|
|
|
|
def check(runner, data):
|
|
_, probes = runner.run()
|
|
expect(probes, "data-read", False)
|
|
expect(probes, "data-write", False)
|
|
for name in ("notifications", "clipboard-read", "clipboard-write",
|
|
"fetch-ok", "fetch-other-host"):
|
|
expect(probes, name, False, contains=NOT_GRANTED)
|
|
|
|
(data / "seed.txt").write_text("seed")
|
|
_, probes = runner.run("--preopen", f"{data}::/data::ro")
|
|
expect(probes, "data-read", True, contains="seed")
|
|
for name in ("data-write", "data-overwrite", "data-mkdir", "data-rename", "data-remove"):
|
|
expect(probes, name, False)
|
|
if sorted(p.name for p in data.iterdir()) != ["seed.txt"] or \
|
|
(data / "seed.txt").read_text() != "seed":
|
|
raise AssertionError("read-only grant allowed the host directory to change")
|
|
|
|
_, probes = runner.run("--preopen", f"{data}::/data::rw")
|
|
for name in ("data-write", "data-overwrite", "data-mkdir", "data-rename"):
|
|
expect(probes, name, True)
|
|
if (data / "written.txt").read_text() != "probe" or \
|
|
(data / "moved.txt").read_text() != "changed" or not (data / "made").is_dir():
|
|
raise AssertionError("read-write grant did not change the host directory")
|
|
|
|
_, probes = runner.run("--grant", "sys:notifications", "--grant", "sys:clipboard:read")
|
|
expect(probes, "notifications", probes.get("notifications", "").startswith("ok"),
|
|
absent=NOT_GRANTED)
|
|
expect(probes, "clipboard-read", probes.get("clipboard-read", "").startswith("ok"),
|
|
absent=NOT_GRANTED)
|
|
expect(probes, "clipboard-write", False, contains=NOT_GRANTED)
|
|
|
|
_, probes = runner.run("--grant", "net:fetch:127.0.0.1")
|
|
expect(probes, "fetch-ok", True, contains="200")
|
|
expect(probes, "fetch-missing", True, contains="404")
|
|
expect(probes, "fetch-redirect", True, contains="302")
|
|
expect(probes, "fetch-other-host", False, contains=NOT_GRANTED)
|
|
expect(probes, "fetch-credentials", False, contains="credentials")
|
|
expect(probes, "fetch-host-header", False, contains="set by the runtime")
|
|
expect(probes, "fetch-bad-method", False, contains="unsupported HTTP method")
|
|
expect(probes, "fetch-file-scheme", False, contains="unsupported URL scheme")
|
|
|
|
local = "local network"
|
|
REQUESTS.clear()
|
|
_, probes = runner.run("--grant", "net:fetch:*")
|
|
expect(probes, "fetch-other-host", False, contains=local)
|
|
expect(probes, "fetch-ok", False, contains=local)
|
|
_, probes = runner.run("--grant", "net:fetch:localhost")
|
|
expect(probes, "fetch-other-host", False, contains=local)
|
|
if REQUESTS:
|
|
raise AssertionError(f"refused fetches reached the server: {REQUESTS}")
|
|
|
|
for arguments, message in (
|
|
(["--grant", "fs:rw:app-data"], "is not granted through --grant"),
|
|
(["--preopen", f"{data}::/data"], "--preopen expects HOST::GUEST::ro|rw"),
|
|
(["--preopen", f"{data}::/data::wx"], "--preopen mode must be ro or rw"),
|
|
(["--grant", "sys:everything"], "unknown capability 'sys:everything'")):
|
|
result, _ = runner.run(*arguments, expect_success=False)
|
|
if result.returncode == 0 or message not in result.stderr:
|
|
raise AssertionError(f"runtime did not refuse {' '.join(arguments)} with "
|
|
f"{message!r}:\n{result.stderr}")
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__,
|
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument("--runtime", type=Path, default=ROOT / "target/debug/weft-runtime",
|
|
help="weft-runtime built with wasmtime-runtime and net-fetch")
|
|
parser.add_argument("--probe", type=Path, help="prebuilt probe component")
|
|
args = parser.parse_args(argv)
|
|
if not args.runtime.is_file():
|
|
print(f"missing: {args.runtime}", file=sys.stderr)
|
|
return 2
|
|
|
|
probe = args.probe or build_probe()
|
|
server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler)
|
|
threading.Thread(target=server.serve_forever, daemon=True).start()
|
|
work = Path(tempfile.mkdtemp(prefix="weft-grants-"))
|
|
try:
|
|
runner = Runner(args.runtime, work, server.server_port)
|
|
shutil.copy(probe, runner.module)
|
|
data = work / "data"
|
|
data.mkdir()
|
|
check(runner, data)
|
|
except AssertionError as failure:
|
|
print(f"grants check failed: {failure}", file=sys.stderr)
|
|
return 1
|
|
finally:
|
|
server.shutdown()
|
|
shutil.rmtree(work, ignore_errors=True)
|
|
print("grants check passed")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|