Merge branch 'worktree-agent-ac54c8919e940163a'

# Conflicts:
#	scripts/setup-native.py
This commit is contained in:
2026-11-10 01:07:12 +00:00
34 changed files with 1825 additions and 66 deletions
+16 -8
View File
@@ -27,6 +27,9 @@ from urllib.parse import urlsplit
REPO = Path(__file__).resolve().parents[1]
APP = REPO / "app"
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
_signing_spec = importlib.util.spec_from_file_location("pipelines_update_signing", Path(__file__).with_name("update_signing.py"))
update_signing = importlib.util.module_from_spec(_signing_spec)
_signing_spec.loader.exec_module(update_signing)
def default_cache():
return Path(os.environ.get("PIPELINES_APP_CACHE") or Path.home() / ".cache/pipelines/app-builds")
@@ -346,7 +349,10 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin
server = server_url or phone_server_url(manifest)
# The build number is the source key's build date, so the same sources
# reuse one APK and every rebuild is newer than the last.
arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}",)
# The feed is signed and the app embeds the keys it trusts (see
# scripts/update_signing.py), so a server cannot hand phones another APK.
signer = update_signing.Signer()
arguments = (f"--dart-define=PIPELINES_SERVER_URL={server}", *update_signing.dart_defines())
host_flutter, sdk = native_flutter(flutter), android_sdk_dir()
use_native = native if native is not None else bool(host_flutter and sdk)
if use_native:
@@ -365,9 +371,9 @@ def build_apk(prefix, server_url=None, notes="", runner=subprocess.run, log=prin
target.mkdir(parents=True, exist_ok=True)
shutil.copyfile(output / "pipelines.apk", target / "pipelines.apk.next")
os.replace(target / "pipelines.apk.next", target / "pipelines.apk")
(target / "version.json").write_text(json.dumps({
update_signing.publish_feed(target, {
"versionCode": code, "versionName": f"0.1.{code}", "apk": "pipelines.apk", "notes": notes,
"signingCertificateSha256": fingerprint, "sha256": apk_sha256}) + "\n")
"signingCertificateSha256": fingerprint, "sha256": apk_sha256}, signer)
log(f"APK ready: {target / 'pipelines.apk'} (build {code}, server {server}).")
log("Phones get it from Settings → App updates → Check for updates, or download "
f"{server.replace('wss://', 'https://').replace('ws://', 'http://').removesuffix('/connect')}/updates/pipelines.apk")
@@ -380,9 +386,10 @@ def print_web_urls(manifest, lan_host=None):
if not web.get("port"):
return
host = web.get("host", "0.0.0.0")
local = "localhost" if host in ("0.0.0.0", "127.0.0.1") else host
local = "localhost" if host in ("0.0.0.0", "127.0.0.1", "tailnet") else host
urls = [f"http://{local}:{web['port']}/"]
if host == "0.0.0.0":
# The LAN URL only where a LAN browser can sign in (see web_access.py).
if host == "0.0.0.0" or (host == "tailnet" and web.get("lan") is True and web.get("lan_plain_login") is True):
lan_host = lan_host or urlsplit(manifest.get("public_url", "")).hostname
if lan_host and lan_host not in ("localhost", "127.0.0.1", "0.0.0.0"):
urls.append(f"http://{lan_host}:{web['port']}/")
@@ -416,13 +423,14 @@ def main(argv=None):
bundle, archive = desktop.build_desktop(args.kind, app=APP, cache=default_cache(), runner=subprocess.run,
source_digest=digest, docker_build=build, docker_ready=docker_available,
server_url=args.server_url, flutter=args.flutter, output=args.output,
prune_cache=prune)
prune_cache=prune, update_keys=update_signing.dart_defines())
if args.publish:
manifest = load_manifest(args.prefix)
updates = (manifest.get("web") or {}).get("updates")
if not updates:
raise ValueError("This installation has no app update directory; rerun ./install")
desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes)
desktop.publish_desktop(args.kind, bundle, archive, Path(updates), notes=args.notes,
signer=update_signing.Signer())
return
if args.kind == "apk":
build_apk(args.prefix, args.server_url, args.notes, flutter=args.flutter, native=args.native)
@@ -434,7 +442,7 @@ def main(argv=None):
web = manifest.get("web") or {}
if not web.get("root"):
raise ValueError("This installation does not serve the app; rerun ./install")
install_tree(build("web"), Path(web["root"]))
install_tree(build("web", arguments=update_signing.dart_defines()), Path(web["root"]))
print(f"Web app installed at {web['root']}; reload the page.")
print_web_urls(manifest)
+10 -5
View File
@@ -120,8 +120,8 @@ def _macos_build(*, app, cache, source_digest, runner, arguments, flutter, log):
def build_desktop(kind, *, app, cache, runner, source_digest, docker_build,
docker_ready, server_url=None, flutter=None, output=None, log=print,
prune_cache=None):
arguments = _arguments(server_url)
prune_cache=None, update_keys=()):
arguments = _arguments(server_url) + tuple(update_keys)
if kind == "macos":
entry, bundle, archive = _macos_build(app=app, cache=cache, runner=runner,
source_digest=source_digest, arguments=arguments, flutter=flutter, log=log)
@@ -154,8 +154,10 @@ def build_desktop(kind, *, app, cache, runner, source_digest, docker_build,
return bundle, archive
def publish_desktop(kind, bundle, archive, updates, *, notes="", log=print):
"""Publish archive before its feed; versioned names preserve in-flight reads."""
def publish_desktop(kind, bundle, archive, updates, *, signer, notes="", log=print):
"""Publish archive before its feed; versioned names preserve in-flight reads.
[signer] returns the detached `version.json.sig` for the feed bytes."""
entry = bundle.parent if kind == "macos" else bundle
complete = json.loads((entry / ".complete").read_text())
code = int(complete["built"]) // 60
@@ -176,8 +178,11 @@ def publish_desktop(kind, bundle, archive, updates, *, notes="", log=print):
previous = json.loads(feed.read_text())
if previous.get("versionCode", 0) > code:
raise ValueError("Refusing to publish an older build over a newer update")
data = (json.dumps(metadata) + "\n").encode()
# The signature lands before the feed it covers, after the archive.
for destination, content in ((target / filename, archive.read_bytes()),
(feed, (json.dumps(metadata) + "\n").encode())):
(target / "version.json.sig", signer(data)),
(feed, data)):
descriptor, temporary = tempfile.mkstemp(prefix=destination.name + ".", dir=target)
try:
with os.fdopen(descriptor, "wb") as output:
+18 -4
View File
@@ -52,10 +52,24 @@ def mdns_args(config):
"""
web = config.get("web") or {}
if web.get("port") and web.get("host") not in (None, "", "127.0.0.1", "localhost", "::1"):
return ["-mdns-port", str(web["port"])]
# Loopback + tailnet: the advert carries only the tailnet/public routes.
lan = web.get("host") != "tailnet" or web.get("lan") is True
return ["-mdns-port", str(web["port"])] + ([] if lan else ["-mdns-lan=false"])
return []
def web_listen_args(web):
"""The proxy's bind and access flags. "tailnet" (the installer default)
binds the wildcard and admits only loopback and tailnet connections, plus
the LAN when web.lan is set; sign-in over plain-HTTP LAN additionally needs
web.lan_plain_login. A literal address is bound as before, unfiltered."""
if web.get("host") != "tailnet":
return ["--host", web["host"]]
lan = web.get("lan") is True
return (["--host", "::", "--allow", "loopback,tailnet" + (",lan" if lan else "")]
+ (["--lan-plain-login"] if lan and web.get("lan_plain_login") is True else []))
def load(prefix):
prefix = Path(prefix).expanduser().resolve()
path = prefix / MARKER
@@ -224,7 +238,7 @@ def start(config):
wait_health(f"http://127.0.0.1:{config['server_port']}/healthz", timeout=75)
if config.get("web"):
web = config["web"]
host = "127.0.0.1" if web["host"] == "0.0.0.0" else web["host"]
host = "127.0.0.1" if web["host"] in ("0.0.0.0", "tailnet") else web["host"]
end = time.monotonic() + 20
while True:
try:
@@ -325,7 +339,7 @@ def report(config):
if web:
public = (config.get("opencode_env") or {}).get("PIPELINES_WEB_PUBLIC_URL")
print("== Access")
address = public or f"http://{web['host']}:{web['port']}"
address = public or f"http://{'127.0.0.1' if web['host'] == 'tailnet' else web['host']}:{web['port']}"
print(f" {'Web':<10} {address} (password in {web['auth_file']})")
root = Path(web["root"]) if web.get("root") else None
built = root is not None and (root / "index.html").is_file()
@@ -440,7 +454,7 @@ def supervise(config):
with (root / "logs/web.log").open("a") as web_log:
web_child = subprocess.Popen(
[web["node"], web["script"], "--port", str(web["port"]),
"--host", web["host"], "--pipelines", f"127.0.0.1:{config['server_port']}",
*web_listen_args(web), "--pipelines", f"127.0.0.1:{config['server_port']}",
*(["--root", web["root"]] if web.get("root") else []),
*(["--updates", web["updates"]] if web.get("updates") else [])],
env=web_env, stdout=web_log, stderr=web_log)
+30 -9
View File
@@ -44,6 +44,7 @@ lan = module("lan_address", "lan-address.py")
release_lookup = module("resolve_releases", "resolve-releases.py")
tokens = module("save_ntfy_token", "save-ntfy-token.py")
app_build = module("app_build", "app_build.py")
web_access = module("web_access", "web_access.py")
DEFAULT_PREFIX = Path.home() / ".local/share/pipelines"
MARKER = "pipelines-install.json"
@@ -310,10 +311,12 @@ class Setup:
saved = {"port": existing.get("server_port"),
"ntfy_port": existing.get("ntfy_port"),
"bind_address": existing.get("bind_address"),
"web_port": web.get("port"), "web_host": web.get("host")}
"web_port": web.get("port"), "web_host": web.get("host"),
"web_lan": web.get("lan"), "web_lan_plain_login": web.get("lan_plain_login")}
supplied = {argument.split("=", 1)[0] for argument in args.argv}
for name, value in saved.items():
if value is not None and "--" + name.replace("_", "-") not in supplied:
flag = name.replace("_", "-")
if value is not None and not {"--" + flag, "--no-" + flag} & supplied:
setattr(args, name, value)
# --- prefix layout ---------------------------------------------------
@@ -368,6 +371,7 @@ class Setup:
return {"node": node, "script": str(target / "serve.mjs"),
"auth_file": str(auth), "host": args.web_host, "port": args.web_port,
"server_token_file": str(server_token),
"lan": args.web_lan, "lan_plain_login": args.web_lan_plain_login,
"root": self.install_app_web(prefix, args, web_build), "updates": str(updates)}
def ensure_server_token(self, path):
@@ -416,7 +420,8 @@ class Setup:
raise ValueError("Docker is not running, so the web app cannot be built. The server is never "
"installed without it; start Docker and rerun ./install.")
try:
built = app_build.build("web", runner=self.runner)
built = app_build.build("web", runner=self.runner,
arguments=app_build.update_signing.dart_defines(self.environ))
except RuntimeError as error:
raise ValueError(f"The web app build failed ({error}); nothing was installed") from None
if not (Path(built) / "index.html").is_file():
@@ -822,9 +827,11 @@ class Setup:
raise ValueError("Web port must be between 1024 and 65535 and differ from server/ntfy ports")
try:
ipaddress.IPv4Address(args.bind_address)
ipaddress.IPv4Address(args.web_host)
except ValueError:
raise ValueError("Invalid API/web bind address; use an IPv4 address such as 0.0.0.0 or 127.0.0.1") from None
raise ValueError("Invalid API bind address; use an IPv4 address such as 127.0.0.1") from None
listen, web_warnings = web_access.resolve(args.web_host, args.web_lan, args.web_lan_plain_login,
{argument.split("=", 1)[0] for argument in args.argv})
args.web_host, args.web_lan, args.web_lan_plain_login = listen["host"], listen["lan"], listen["lan_plain_login"]
allow_authoring = self.authoring_role(prefix, args.allow_authoring)
web_public_url = self.environ.get("PIPELINES_WEB_PUBLIC_URL", "")
previous = prefix / MARKER
@@ -886,7 +893,8 @@ class Setup:
if self.environ.get(key):
opencode_env[key] = self.environ[key]
opencode_env["PIPELINES_DEFAULT_MODEL"] = args.model or pipeline_model
opencode_env["PIPELINES_WEB_PUBLIC_URL"] = web_public_url or f"http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}"
web_url = f"http://{web_access.default_url_host(web, lan_ip, web_access.tailnet_ipv4(self.runner))}:{args.web_port}"
opencode_env["PIPELINES_WEB_PUBLIC_URL"] = web_public_url or web_url
path_value = str(prefix / "bin") + os.pathsep + self.environ.get("PATH", "")
manifest = {
"format": 1,
@@ -943,8 +951,13 @@ class Setup:
self.serve_on_tailnet(opencode_env["PIPELINES_WEB_PUBLIC_URL"], args.web_port)
print(f"ntfy is configured for {base_url}; server, web proxy and ntfy have login autostart ({service_path}).")
print("Phone notifications: subscribe to the 'pipelines' topic as phone-reader with the web password.")
print(f"Web: http://{lan_ip if args.web_host == '0.0.0.0' else args.web_host}:{args.web_port}/ "
f"(sign-in password in {web['auth_file']}; existing credentials retained).")
print(f"Web: {web_url}/ (sign-in password in {web['auth_file']}; existing credentials retained).")
print("Web proxy access: " + ("loopback + tailnet" + (" + LAN" if args.web_lan else "")
if args.web_host == web_access.TAILNET else f"bound to {args.web_host}"))
for note in (*web_warnings, web_access.public_url_warning(opencode_env["PIPELINES_WEB_PUBLIC_URL"], web),
web_access.firewall_note(self.runner)):
if note:
print(note)
print("Login autostart is already enabled. To enable/start it again, run:\n "
+ shlex.join([str(prefix / "bin/pipelines"), "service", "enable"]))
print("Android app (optional, slower): ./install --apk builds it in Docker and offers it at "
@@ -968,7 +981,15 @@ def arguments(argv=None):
parser.add_argument("--timezone", help="Override the detected timezone (fallback: UTC)")
parser.add_argument("--port", type=int, default=DEFAULT_SERVER_PORT, help="Pipelines server port")
parser.add_argument("--web-port", type=int, default=8082, help="Authenticated web proxy port (default 8082)")
parser.add_argument("--web-host", default="0.0.0.0", help="Web proxy bind address (default 0.0.0.0)")
parser.add_argument("--web-host", help="Web proxy access: 'tailnet' (default for new installs: loopback and "
"this machine's Tailscale addresses only) or an IPv4 bind address such as 0.0.0.0 "
"(plain HTTP on every interface); reinstalls keep the saved value")
parser.add_argument("--web-lan", action=argparse.BooleanOptionalAction, default=None,
help="Also accept plain-HTTP connections from the LAN (with --web-host tailnet); "
"sign-in over them still needs --web-lan-plain-login")
parser.add_argument("--web-lan-plain-login", action=argparse.BooleanOptionalAction, default=None,
help="Allow the password and signed-in pages over plain-HTTP LAN connections "
"(unencrypted; only on a trusted network)")
parser.add_argument("--bind-address", default=DEFAULT_BIND_ADDRESS,
help="Unauthenticated server bind address (default 127.0.0.1; expose only the web proxy)")
parser.add_argument("--ntfy-port", type=int, default=DEFAULT_NTFY_PORT, help="ntfy listen port")
+5
View File
@@ -83,6 +83,11 @@ class AppBuildTests(unittest.TestCase):
feed = json.loads((updates / "version.json").read_text())
self.assertEqual(feed["signingCertificateSha256"], "ab" * 32)
self.assertEqual(feed["sha256"], app_build.hashlib.sha256((updates / "pipelines.apk").read_bytes()).hexdigest())
# Signed with the installation key generated (0600) in the scratch home.
key = Path(self.home.name) / ".config/pipelines/update-signing-key.pem"
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
signature = json.loads((updates / "version.json.sig").read_text())
self.assertEqual(signature["publicKey"], app_build.update_signing.public_key(key))
(output / "signing-certificate-sha256.txt").write_text("invalid")
with patch.object(app_build, "load_manifest", return_value=manifest), patch.object(app_build, "build", return_value=output):
with self.assertRaisesRegex(ValueError, "exactly one"):
+15 -3
View File
@@ -160,24 +160,36 @@ class DesktopBuildTests(unittest.TestCase):
def test_linux_publication_has_matching_hash_and_rejects_downgrade(self):
bundle, archive = self.build("linux")
updates = self.root / "updates"
metadata = desktop.publish_desktop("linux", bundle, archive, updates, log=lambda *_: None)
signed = []
signer = lambda data: signed.append(data) or b"signature document\n"
metadata = desktop.publish_desktop("linux", bundle, archive, updates, signer=signer, log=lambda *_: None)
target = updates / "linux-x64"
self.assertEqual(json.loads((target / "version.json").read_text()), metadata)
# The detached signature covers the exact published feed bytes.
self.assertEqual(signed, [(target / "version.json").read_bytes()])
self.assertEqual((target / "version.json.sig").read_bytes(), b"signature document\n")
self.assertEqual(metadata["architectures"], ["x64"])
self.assertEqual(metadata["sha256"], desktop.hashlib.sha256((target / metadata["file"]).read_bytes()).hexdigest())
metadata["versionCode"] += 1
(target / "version.json").write_text(json.dumps(metadata))
with self.assertRaisesRegex(ValueError, "older build"):
desktop.publish_desktop("linux", bundle, archive, updates)
desktop.publish_desktop("linux", bundle, archive, updates, signer=signer)
@patch.object(desktop.platform, "system", return_value="Darwin")
def test_macos_feed_uses_actual_architectures_and_native_build_stamp(self, _):
bundle, archive = self.build("macos", flutter="/fixture/flutter")
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates", log=lambda *_: None)
metadata = desktop.publish_desktop("macos", bundle, archive, self.root / "updates",
signer=lambda data: b"{}\n", log=lambda *_: None)
self.assertTrue((self.root / "updates/macos/version.json.sig").is_file())
self.assertEqual(metadata["architectures"], ["arm64", "x64"])
command = next(c for c in self.runner.calls if c[1:3] == ["build", "macos"])
self.assertIn(f"--dart-define=PIPELINES_NATIVE_BUILD_CODE={metadata['versionCode']}", command)
def test_desktop_builds_embed_the_trusted_update_keys(self):
self.build("linux", update_keys=("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc",))
run = next(c for c in self.runner.calls if c[:2] == ["docker", "run"])
self.assertIn("--dart-define=PIPELINES_UPDATE_PUBLIC_KEY=abc", run)
def test_desktop_server_prefill_rejects_embedded_credentials(self):
with self.assertRaisesRegex(ValueError, "embedded credentials"):
self.build("linux", server_url="wss://secret@example.com/connect")
+46 -2
View File
@@ -463,7 +463,11 @@ class SetupNativeTests(unittest.TestCase):
self.assertEqual(config["opencode"], "/usr/bin/opencode")
self.assertEqual(config["pi"], "")
self.assertEqual(config["opencode_env"]["PIPELINES_DEFAULT_MODEL"], "fixture/host-default")
self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://192.168.1.20:8082")
# A fresh install's proxy admits loopback and tailnet only, so the
# default web URL is not the LAN address.
self.assertEqual(config["opencode_env"]["PIPELINES_WEB_PUBLIC_URL"], "http://127.0.0.1:8082")
self.assertEqual((config["web"]["host"], config["web"]["lan"], config["web"]["lan_plain_login"]),
("tailnet", False, False))
host = json.loads(installer.harness_config_path().read_text())["profiles"]["opencode"]
self.assertEqual(host["model"], "fixture/host-default")
self.assertEqual(host["pipeline_model"], "fixture/host-default")
@@ -509,11 +513,51 @@ class SetupNativeTests(unittest.TestCase):
self.assertEqual((prefix / "secrets/publisher-token").read_text(), before)
self.assertEqual(manage.load(prefix)["web"]["port"], 8096)
self.assertEqual(output.getvalue().splitlines()[-4:], [
" http://localhost:8096/", " http://192.168.1.20:8096/",
" http://localhost:8096/", " http://127.0.0.1:8096/",
"OpenCode is required (default stage and chat harness) and is set up by ./install.",
"Next: set up your agent harnesses: pipelines harness setup claude|codex|opencode|pi"])
self.assertFalse(any("token" in call for call in recorder.calls), "reinstall re-created the token")
def test_upgrade_keeps_an_all_interfaces_web_host_with_a_warning_and_lan_is_opt_in(self):
with tempfile.TemporaryDirectory() as directory:
installer = self.installer(directory, Recorder())
prefix = Path(directory) / "prefix"
def install(argv):
with redirect_stdout(io.StringIO()) as output:
installer.run(setup_native.arguments(argv))
return manage.load(prefix)["web"], output.getvalue()
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()), \
patch.object(setup_native.lan, "detect_lan", return_value="192.168.1.20"), \
patch.object(setup_native.configure, "host_timezone", return_value="Europe/London"):
# An install from before: the proxy on every interface.
install(["--web-host", "0.0.0.0"])
web, text = install([])
self.assertEqual(web["host"], "0.0.0.0")
self.assertIn("WARNING: the web proxy listens on 0.0.0.0 over plain HTTP", text)
self.assertIn("./install --web-host tailnet", text)
self.assertIn(" http://192.168.1.20:8082/", text)
# Opting into the LAN listener selects the new model; sign-in
# over the plain-HTTP LAN stays off until asked for.
web, text = install(["--web-lan"])
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, False))
self.assertNotIn("WARNING: the web proxy listens", text)
self.assertIn("Web proxy access: loopback + tailnet + LAN", text)
# The URL recorded by the old install is kept, and flagged.
self.assertIn("WARNING: the recorded web URL http://192.168.1.20:8082 is a plain-HTTP LAN", text)
web, text = install(["--web-lan-plain-login"])
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", True, True))
self.assertIn("WARNING: web.lan_plain_login is on", text)
web, _ = install(["--no-web-lan"])
self.assertEqual((web["host"], web["lan"], web["lan_plain_login"]), ("tailnet", False, False))
web, _ = install([])
self.assertEqual((web["host"], web["lan"]), ("tailnet", False))
with self.assertRaisesRegex(ValueError, "--web-lan applies to --web-host tailnet"):
installer.run(setup_native.arguments(["--web-host", "0.0.0.0", "--web-lan"]))
with self.assertRaisesRegex(ValueError, "Invalid --web-host"):
installer.run(setup_native.arguments(["--web-host", "everywhere"]))
def test_ntfy_is_built_from_the_pinned_module(self):
with tempfile.TemporaryDirectory() as directory:
recorder = Recorder()
+117
View File
@@ -0,0 +1,117 @@
import base64
import importlib.util
import json
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from unittest.mock import patch
def load(name):
spec = importlib.util.spec_from_file_location(name, Path(__file__).with_name(name + ".py"))
result = importlib.util.module_from_spec(spec)
spec.loader.exec_module(result)
return result
signing = load("update_signing")
FIXTURE = Path(__file__).resolve().parents[1] / "app/test/features/updates/fixtures/signed_feed"
def verify(tool, public, data, signature):
"""Independent check with `openssl pkeyutl -verify` against the raw key."""
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
(root / "pub.der").write_bytes(signing.SPKI_PREFIX + base64.b64decode(public))
(root / "data").write_bytes(data)
(root / "sig").write_bytes(signature)
result = subprocess.run([tool, "pkeyutl", "-verify", "-pubin", "-keyform", "DER", "-inkey",
str(root / "pub.der"), "-rawin", "-in", str(root / "data"),
"-sigfile", str(root / "sig")], capture_output=True)
return result.returncode == 0
class UpdateSigningTests(unittest.TestCase):
def setUp(self):
self.home = tempfile.TemporaryDirectory()
self.addCleanup(self.home.cleanup)
patcher = patch.object(signing.Path, "home", return_value=Path(self.home.name))
patcher.start()
self.addCleanup(patcher.stop)
self.environ = {}
self.tool = signing.openssl(self.environ)
def test_key_is_generated_once_private_under_the_config_directory(self):
key = signing.ensure_key(self.environ)
self.assertEqual(key, Path(self.home.name) / ".config/pipelines/update-signing-key.pem")
self.assertEqual(key.stat().st_mode & 0o777, 0o600)
self.assertEqual(key.parent.stat().st_mode & 0o777, 0o700)
before = key.read_bytes()
self.assertEqual(signing.ensure_key(self.environ), key)
self.assertEqual(key.read_bytes(), before)
self.assertEqual([p.name for p in key.parent.iterdir()], [key.name])
def test_readable_or_symlinked_key_is_refused(self):
key = signing.ensure_key(self.environ)
os.chmod(key, 0o644)
with self.assertRaisesRegex(ValueError, "chmod 600"):
signing.ensure_key(self.environ)
os.chmod(key, 0o600)
link = key.with_name("link.pem")
link.symlink_to(key)
with self.assertRaisesRegex(ValueError, "regular file"):
signing.ensure_key({"PIPELINES_UPDATE_SIGNING_KEY": str(link)})
def test_openssl_without_ed25519_is_skipped_or_reported(self):
missing = {"PIPELINES_OPENSSL": "/nonexistent/openssl"}
self.assertEqual(signing.openssl(missing), self.tool)
with patch.object(signing, "CANDIDATES", ()), \
patch.object(signing.shutil, "which", return_value="/usr/bin/false"):
with self.assertRaisesRegex(ValueError, "OpenSSL 3"):
signing.openssl({})
def test_signature_verifies_and_tampering_does_not(self):
key = signing.ensure_key(self.environ)
data = b'{"versionCode": 7}\n'
document = json.loads(signing.signature_document(data, key, self.environ))
self.assertEqual(document["algorithm"], "ed25519")
public = document["publicKey"]
signature = base64.b64decode(document["signature"])
self.assertEqual((len(base64.b64decode(public)), len(signature)), (32, 64))
self.assertTrue(verify(self.tool, public, data, signature))
self.assertFalse(verify(self.tool, public, b'{"versionCode": 8}\n', signature))
def test_publish_writes_feed_and_detached_signature(self):
target = Path(self.home.name) / "updates"
target.mkdir()
data = signing.publish_feed(target, {"versionCode": 3}, signing.Signer(environ=self.environ))
self.assertEqual((target / "version.json").read_bytes(), data)
document = json.loads((target / "version.json.sig").read_text())
self.assertTrue(verify(self.tool, document["publicKey"], data, base64.b64decode(document["signature"])))
self.assertEqual((target / "version.json").stat().st_mode & 0o777, 0o644)
def test_build_embeds_own_key_first_then_listed_trusted_keys(self):
own = signing.public_key(signing.ensure_key(self.environ))
other = base64.b64encode(bytes(range(32))).decode()
listed = Path(self.home.name) / ".config/pipelines/update-trusted-keys"
listed.write_text(f"# tank\n{other}\n\n{own}\n")
self.assertEqual(signing.dart_defines(self.environ),
(f"--dart-define=PIPELINES_UPDATE_PUBLIC_KEY={own},{other}",))
listed.write_text("not-a-key\n")
with self.assertRaisesRegex(ValueError, "update-trusted-keys:1"):
signing.trusted_keys(self.environ)
def test_committed_dart_fixture_is_a_valid_python_signature(self):
# The Dart verifier test reads this fixture; publish_feed produced it
# with a throwaway key that was never kept.
public = (FIXTURE / "public_key.txt").read_text().strip()
document = json.loads((FIXTURE / "version.json.sig").read_text())
self.assertEqual(document["publicKey"], public)
self.assertTrue(verify(self.tool, public, (FIXTURE / "version.json").read_bytes(),
base64.b64decode(document["signature"])))
if __name__ == "__main__":
unittest.main()
+98
View File
@@ -0,0 +1,98 @@
import importlib.util
from pathlib import Path
import subprocess
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location("web_access", Path(__file__).with_name("web_access.py"))
web_access = importlib.util.module_from_spec(spec)
spec.loader.exec_module(web_access)
manage_spec = importlib.util.spec_from_file_location("manage_web", Path(__file__).with_name("manage.py"))
manage = importlib.util.module_from_spec(manage_spec)
manage_spec.loader.exec_module(manage)
def reply(stdout="", code=0):
return subprocess.CompletedProcess([], code, stdout=stdout, stderr="")
class ResolveTests(unittest.TestCase):
def test_fresh_install_defaults_to_loopback_and_tailnet(self):
self.assertEqual(web_access.resolve(None, None, None, set()),
({"host": "tailnet", "lan": False, "lan_plain_login": False}, []))
def test_an_existing_all_interfaces_host_is_kept_with_a_warning(self):
listen, warnings = web_access.resolve("0.0.0.0", None, None, set())
self.assertEqual(listen["host"], "0.0.0.0")
self.assertEqual(len(warnings), 1)
self.assertIn("cross it unencrypted", warnings[0])
self.assertIn("--web-host tailnet", warnings[0])
self.assertEqual(web_access.resolve("127.0.0.1", None, None, set())[1], [])
def test_lan_is_opt_in_and_plain_login_needs_lan(self):
listen, warnings = web_access.resolve("0.0.0.0", True, None, {"--web-lan"})
self.assertEqual(listen, {"host": "tailnet", "lan": True, "lan_plain_login": False})
self.assertEqual(warnings, [])
listen, warnings = web_access.resolve("tailnet", True, True, set())
self.assertTrue(listen["lan_plain_login"])
self.assertIn("lan_plain_login is on", warnings[0])
self.assertFalse(web_access.resolve("tailnet", False, True, set())[0]["lan_plain_login"])
with self.assertRaisesRegex(ValueError, "applies to --web-host tailnet"):
web_access.resolve("0.0.0.0", True, None, {"--web-host", "--web-lan"})
with self.assertRaisesRegex(ValueError, "Invalid --web-host"):
web_access.resolve("0.0.0.0\nX=1", None, None, {"--web-host"})
def test_urls_follow_where_a_browser_can_sign_in(self):
tailnet = {"host": "tailnet", "lan": False}
self.assertEqual(web_access.default_url_host(tailnet, "192.168.1.20", "100.85.25.33"), "100.85.25.33")
self.assertEqual(web_access.default_url_host(tailnet, "192.168.1.20", None), "127.0.0.1")
self.assertEqual(web_access.default_url_host({"host": "tailnet", "lan": True, "lan_plain_login": True},
"192.168.1.20", None), "192.168.1.20")
self.assertEqual(web_access.default_url_host({"host": "0.0.0.0"}, "192.168.1.20", None), "192.168.1.20")
self.assertIsNotNone(web_access.public_url_warning("http://192.168.1.20:8083", tailnet))
for url in ("https://box.tail1.ts.net:8443", "http://100.85.25.33:8083", "http://127.0.0.1:8083", ""):
self.assertIsNone(web_access.public_url_warning(url, tailnet), url)
self.assertIsNone(web_access.public_url_warning("http://192.168.1.20:8083", {"host": "0.0.0.0"}))
def test_tailnet_address_and_firewall_are_read_only_probes(self):
calls = []
def runner(command, **_):
calls.append(command)
return reply("100.85.25.33\n") if command[-1] == "-4" else reply("Firewall is disabled. (State = 0)\n")
with patch.object(web_access.shutil, "which", return_value="/usr/bin/tailscale"):
self.assertEqual(web_access.tailnet_ipv4(runner), "100.85.25.33")
self.assertIsNone(web_access.tailnet_ipv4(lambda *a, **k: reply("10.0.0.5\n")))
self.assertIsNone(web_access.tailnet_ipv4(lambda *a, **k: reply("", 1)))
with patch.object(web_access.shutil, "which", return_value=None):
self.assertIsNone(web_access.tailnet_ipv4(runner))
with patch.object(web_access.platform, "system", return_value="Darwin"), \
patch.object(web_access, "FIREWALL", Path(__file__)):
self.assertIn("firewall is off", web_access.firewall_note(runner))
self.assertIsNone(web_access.firewall_note(lambda *a, **k: reply("Firewall is enabled. (State = 1)\n")))
self.assertEqual(calls[-1], [__file__, "--getglobalstate"])
class ManageWebTests(unittest.TestCase):
def test_proxy_flags_follow_the_manifest(self):
self.assertEqual(manage.web_listen_args({"host": "0.0.0.0"}), ["--host", "0.0.0.0"])
self.assertEqual(manage.web_listen_args({"host": "tailnet"}),
["--host", "::", "--allow", "loopback,tailnet"])
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan": True}),
["--host", "::", "--allow", "loopback,tailnet,lan"])
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan": True, "lan_plain_login": True}),
["--host", "::", "--allow", "loopback,tailnet,lan", "--lan-plain-login"])
self.assertEqual(manage.web_listen_args({"host": "tailnet", "lan_plain_login": True}),
["--host", "::", "--allow", "loopback,tailnet"])
def test_mdns_omits_the_lan_route_when_the_proxy_refuses_the_lan(self):
port = {"port": 8083}
self.assertEqual(manage.mdns_args({"web": {**port, "host": "tailnet"}}), ["-mdns-port", "8083", "-mdns-lan=false"])
self.assertEqual(manage.mdns_args({"web": {**port, "host": "tailnet", "lan": True}}), ["-mdns-port", "8083"])
self.assertEqual(manage.mdns_args({"web": {**port, "host": "0.0.0.0"}}), ["-mdns-port", "8083"])
self.assertEqual(manage.mdns_args({"web": {**port, "host": "127.0.0.1"}}), [])
if __name__ == "__main__":
unittest.main()
+189
View File
@@ -0,0 +1,189 @@
"""Ed25519 signing for the app update feed.
Each installation owns one private key, generated once into a 0600 file under
~/.config/pipelines/ (never in the repository). App builds embed the public key
(`--dart-define=PIPELINES_UPDATE_PUBLIC_KEY`, comma separated) together with any
other servers' keys listed one per line in
~/.config/pipelines/update-trusted-keys. Publishing writes `version.json` and a
detached `version.json.sig` over its exact bytes; the app verifies that
signature with an embedded key before it trusts the artifact sha256 inside.
The OpenSSL 3 command line does the cryptography (the system Python has no
`cryptography` package and macOS's LibreSSL has no Ed25519), so the tool is
looked up explicitly: PIPELINES_OPENSSL, Homebrew's openssl@3, then PATH.
"""
import base64
import json
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
KEY_NAME = "update-signing-key.pem"
TRUSTED_NAME = "update-trusted-keys"
SIGNATURE_SUFFIX = ".sig"
DEFINE = "PIPELINES_UPDATE_PUBLIC_KEY"
CANDIDATES = ("/opt/homebrew/opt/openssl@3/bin/openssl", "/usr/local/opt/openssl@3/bin/openssl")
# DER SubjectPublicKeyInfo for Ed25519 is this 12-byte prefix plus the raw key.
SPKI_PREFIX = bytes.fromhex("302a300506032b6570032100")
def config_dir():
return Path.home() / ".config/pipelines"
def key_path(environ=None):
environ = os.environ if environ is None else environ
explicit = environ.get("PIPELINES_UPDATE_SIGNING_KEY")
return Path(explicit).expanduser() if explicit else config_dir() / KEY_NAME
def _supports_ed25519(tool):
try:
result = subprocess.run([tool, "genpkey", "-algorithm", "ed25519"],
capture_output=True, timeout=30)
except (OSError, subprocess.TimeoutExpired):
return False
return result.returncode == 0 and b"PRIVATE KEY" in result.stdout
def openssl(environ=None):
"""An OpenSSL executable with Ed25519 support, or a clear error."""
environ = os.environ if environ is None else environ
found = [environ.get("PIPELINES_OPENSSL"), *CANDIDATES, shutil.which("openssl")]
for tool in dict.fromkeys(t for t in found if t):
if _supports_ed25519(tool):
return tool
raise ValueError("Update signing needs OpenSSL 3 with Ed25519 (LibreSSL lacks it): "
"install openssl@3 (brew install openssl@3) or set PIPELINES_OPENSSL")
def _check_private(path):
if path.is_symlink() or not path.is_file():
raise ValueError(f"Update signing key must be a regular file: {path}")
if path.stat().st_mode & 0o077:
raise ValueError(f"Update signing key must be private (chmod 600): {path}")
def ensure_key(environ=None):
"""The installation's signing key, generated once (0600) when absent."""
path = key_path(environ)
if path.exists() or path.is_symlink():
_check_private(path)
return path
tool = openssl(environ)
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
descriptor, temporary = tempfile.mkstemp(prefix=".update-key-", dir=path.parent)
os.close(descriptor)
try:
os.chmod(temporary, 0o600)
result = subprocess.run([tool, "genpkey", "-algorithm", "ed25519", "-out", temporary],
capture_output=True, text=True, timeout=30)
if result.returncode:
raise ValueError("Could not generate the update signing key: " + result.stderr.strip())
os.chmod(temporary, 0o600)
# Never replace a key another process created meanwhile.
os.link(temporary, path)
finally:
Path(temporary).unlink(missing_ok=True)
_check_private(path)
return path
def public_key(key, environ=None):
"""The raw 32-byte public key, base64 encoded."""
result = subprocess.run([openssl(environ), "pkey", "-in", str(key), "-pubout", "-outform", "DER"],
capture_output=True, timeout=30)
der = result.stdout
if result.returncode or len(der) != 44 or not der.startswith(SPKI_PREFIX):
raise ValueError(f"Could not read the Ed25519 public key from {key}")
return base64.b64encode(der[len(SPKI_PREFIX):]).decode()
def valid_public_key(text):
try:
return len(base64.b64decode(text, validate=True)) == 32
except ValueError:
return False
def trusted_keys(environ=None):
"""This installation's key first, then other servers' keys it trusts."""
keys = [public_key(ensure_key(environ), environ)]
listed = config_dir() / TRUSTED_NAME
if listed.is_file():
for number, line in enumerate(listed.read_text().splitlines(), 1):
line = line.split("#", 1)[0].strip()
if not line:
continue
if not valid_public_key(line):
raise ValueError(f"{listed}:{number} is not a base64 Ed25519 public key")
keys.append(line)
return list(dict.fromkeys(keys))
def dart_defines(environ=None):
"""The build argument embedding the trusted update keys."""
return (f"--dart-define={DEFINE}={','.join(trusted_keys(environ))}",)
def sign(data, key, environ=None):
"""A raw 64-byte Ed25519 signature over [data]."""
with tempfile.NamedTemporaryFile(prefix="pipelines-feed-") as message:
message.write(data)
message.flush()
result = subprocess.run([openssl(environ), "pkeyutl", "-sign", "-rawin", "-inkey", str(key),
"-in", message.name], capture_output=True, timeout=30)
if result.returncode or len(result.stdout) != 64:
raise ValueError("Could not sign the update feed: " + result.stderr.decode(errors="replace").strip())
return result.stdout
def signature_document(data, key, environ=None):
"""The detached `version.json.sig` content for feed bytes [data]."""
return (json.dumps({"algorithm": "ed25519", "publicKey": public_key(key, environ),
"signature": base64.b64encode(sign(data, key, environ)).decode()}) + "\n").encode()
class Signer:
"""Signs feeds with one key file; the installer's key unless given."""
def __init__(self, key=None, environ=None):
self.environ = environ
self.key = Path(key) if key else ensure_key(environ)
_check_private(self.key)
def __call__(self, data):
return signature_document(data, self.key, self.environ)
def write_atomic(path, content, mode=0o644):
descriptor, temporary = tempfile.mkstemp(prefix=path.name + ".", dir=path.parent)
try:
with os.fdopen(descriptor, "wb") as output:
output.write(content)
output.flush()
os.fsync(output.fileno())
os.chmod(temporary, mode)
os.replace(temporary, path)
finally:
Path(temporary).unlink(missing_ok=True)
def publish_feed(directory, metadata, signer):
"""Writes the signature, then the feed it covers, both atomically.
A reader racing a publish may see a new signature with the old feed for a
moment; that verification fails and the next check succeeds."""
data = (json.dumps(metadata) + "\n").encode()
signature = signer(data)
write_atomic(directory / ("version.json" + SIGNATURE_SUFFIX), signature)
write_atomic(directory / "version.json", data)
return data
if __name__ == "__main__":
# Prints this installation's public key, for another server's
# update-trusted-keys file (generating the key on first use).
print(public_key(ensure_key()))
+113
View File
@@ -0,0 +1,113 @@
"""Where the authenticated web proxy listens, as the native installer decides it.
`web.host` in the installation manifest is either "tailnet" (the default for
new installs: the proxy admits only loopback and this machine's Tailscale
addresses, see web/src/listenPolicy.js) or a literal IPv4 bind address (the
behaviour before; existing installs keep it, with a warning when it reaches
beyond loopback). A plain-HTTP LAN listener is opt-in with `web.lan`, and
signing in over it additionally needs `web.lan_plain_login`.
"""
import ipaddress
import platform
import shutil
import subprocess
from pathlib import Path
from urllib.parse import urlsplit
TAILNET = "tailnet"
FIREWALL = Path("/usr/libexec/ApplicationFirewall/socketfilterfw")
def resolve(host, lan, lan_plain_login, supplied):
"""The web settings for this install and the warnings to print.
host/lan/lan_plain_login are the parsed flags after the saved settings were
applied (None when neither gave a value); supplied is the set of flag names
on the command line."""
if lan and "--web-lan" in supplied and "--web-host" not in supplied:
host = TAILNET # opting into the LAN listener selects the new model
host = host or TAILNET
if host != TAILNET:
try:
ipaddress.IPv4Address(host)
except ValueError:
raise ValueError("Invalid --web-host; use 'tailnet' (loopback + Tailscale) "
"or an IPv4 address such as 127.0.0.1") from None
if lan and "--web-lan" in supplied:
raise ValueError("--web-lan applies to --web-host tailnet; a literal --web-host binds that address only")
lan = bool(lan) and host == TAILNET
plain = bool(lan_plain_login) and lan
warnings = []
if host != TAILNET and not ipaddress.IPv4Address(host).is_loopback:
warnings.append(
f"WARNING: the web proxy listens on {host} over plain HTTP, so on an untrusted network "
"(cafe or hotel Wi-Fi) the web password and the 30-day session cookie cross it unencrypted. "
"Recommended: ./install --web-host tailnet (loopback and Tailscale only; add --web-lan "
"for a trusted LAN).")
if plain:
warnings.append(
"WARNING: web.lan_plain_login is on: the web password and session cookie are accepted over "
"plain HTTP on the LAN. Use it only on a network you trust; prefer the tailnet HTTPS address.")
return {"host": host, "lan": lan, "lan_plain_login": plain}, warnings
def lan_sign_in(web):
"""Whether a browser on the LAN can sign in over plain HTTP."""
if web.get("host") == TAILNET:
return web.get("lan") is True and web.get("lan_plain_login") is True
return not ipaddress.IPv4Address(web["host"]).is_loopback
def tailnet_ipv4(runner=subprocess.run):
"""This machine's Tailscale IPv4 address, or None."""
tailscale = shutil.which("tailscale")
if not tailscale:
return None
try:
result = runner([tailscale, "ip", "-4"], capture_output=True, text=True, timeout=10)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode:
return None
for line in (result.stdout or "").split():
try:
if ipaddress.IPv4Address(line) in ipaddress.IPv4Network("100.64.0.0/10"):
return line
except ValueError:
continue
return None
def default_url_host(web, lan_ip, tailnet_ip):
"""The host of the default web URL when PIPELINES_WEB_PUBLIC_URL is unset."""
if web["host"] == TAILNET:
return lan_ip if lan_sign_in(web) else (tailnet_ip or "127.0.0.1")
return lan_ip if web["host"] == "0.0.0.0" else web["host"]
def public_url_warning(url, web):
"""A recorded plain-HTTP LAN web URL that the proxy now refuses."""
parsed = urlsplit(url or "")
try:
address = ipaddress.ip_address(parsed.hostname or "")
except ValueError:
return None
if (parsed.scheme == "http" and not address.is_loopback
and address not in ipaddress.ip_network("100.64.0.0/10") and not lan_sign_in(web)):
return (f"WARNING: the recorded web URL {url} is a plain-HTTP LAN address that no longer accepts "
"sign-in. Set PIPELINES_WEB_PUBLIC_URL to the tailnet HTTPS address and rerun ./install.")
return None
def firewall_note(runner=subprocess.run):
"""A read-only report of the macOS application firewall (never changed)."""
if platform.system() != "Darwin" or not FIREWALL.exists():
return None
try:
result = runner([str(FIREWALL), "--getglobalstate"], capture_output=True, text=True, timeout=10)
except (OSError, subprocess.SubprocessError):
return None
if "disabled" in (result.stdout or "").lower():
return ("Note: the macOS firewall is off. The installer does not change it; consider turning it on "
"(System Settings > Network > Firewall) so other local services are not exposed either.")
return None