Create the server token at install and document the security boundary
This commit is contained in:
@@ -38,6 +38,12 @@ def server_address(config):
|
||||
return f"{config.get('bind_address') or '127.0.0.1'}:{config['server_port']}"
|
||||
|
||||
|
||||
def server_token_env(config):
|
||||
"""PIPELINES_SERVER_TOKEN_FILE for the server, the web proxy and the CLI."""
|
||||
path = (config.get("web") or {}).get("server_token_file")
|
||||
return {"PIPELINES_SERVER_TOKEN_FILE": path} if path else {}
|
||||
|
||||
|
||||
def mdns_args(config):
|
||||
"""The LAN port the server advertises over mDNS.
|
||||
|
||||
@@ -416,6 +422,9 @@ def supervise(config):
|
||||
PIPELINES_NTFY_PUBLIC_URL=config["public_url"],
|
||||
PIPELINES_NTFY_TOKEN_FILE=str(root / "secrets/publisher-token"))
|
||||
env.pop("PIPELINES_NTFY_TOKEN", None)
|
||||
# The server requires, and the proxy and every child CLI send, the
|
||||
# shared token in this file (installs from before it have none).
|
||||
env.update(server_token_env(config))
|
||||
server_command = [str(root / "bin/pipelines-server"), "-addr",
|
||||
server_address(config), "-db", str(root / "data/pipelines.db")]
|
||||
if config.get("allow_authoring") is True:
|
||||
@@ -520,6 +529,7 @@ def main():
|
||||
env.update(config.get("opencode_env", {}))
|
||||
env.update(PIPELINES_URL=f"ws://127.0.0.1:{config['server_port']}/connect",
|
||||
PIPELINES_OPENCODE=config["opencode"], PATH=config["path"])
|
||||
env.update(server_token_env(config))
|
||||
os.execve(binary, [binary, *(forwarded or ["tui"])], env)
|
||||
return
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
|
||||
@@ -359,6 +359,7 @@ class Setup:
|
||||
auth.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
self._write_atomic(auth, (json.dumps({"password": password,
|
||||
"secret": secrets.token_urlsafe(48)}) + "\n").encode(), 0o600)
|
||||
server_token = self.ensure_server_token(auth.parent / "server-token")
|
||||
target = prefix / "lib/web"
|
||||
shutil.copytree(Path(__file__).resolve().parents[1] / "web", target, dirs_exist_ok=True,
|
||||
ignore=shutil.ignore_patterns("test", "node_modules", ".DS_Store"))
|
||||
@@ -366,8 +367,24 @@ class Setup:
|
||||
updates.mkdir(parents=True, exist_ok=True)
|
||||
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),
|
||||
"root": self.install_app_web(prefix, args, web_build), "updates": str(updates)}
|
||||
|
||||
def ensure_server_token(self, path):
|
||||
"""The proxy/CLI ↔ server shared secret (docs/notes/security-boundary.md).
|
||||
|
||||
Created once (0600, beside the web auth file) and kept across reinstalls."""
|
||||
if path.is_symlink() or (path.exists() and not path.is_file()):
|
||||
raise ValueError(f"Refusing a server token that is not a regular file: {path}")
|
||||
if path.exists():
|
||||
if path.stat().st_mode & 0o077 or path.stat().st_uid != os.getuid():
|
||||
raise ValueError(f"Server token must be owned by you and private (chmod 600): {path}")
|
||||
if len(path.read_text().strip()) >= 32:
|
||||
return path
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
self._write_atomic(path, (secrets.token_urlsafe(48) + "\n").encode(), 0o600)
|
||||
return path
|
||||
|
||||
def choose_password(self):
|
||||
"""The web sign-in password for a new computer, chosen by the operator.
|
||||
|
||||
|
||||
@@ -281,6 +281,7 @@ class ManageNativeTests(unittest.TestCase):
|
||||
config = self.config(root)
|
||||
config["web"] = {"node": "/bin/node", "script": "/prefix/lib/web/serve.mjs",
|
||||
"host": "0.0.0.0", "port": 8082, "auth_file": "/private/auth.json",
|
||||
"server_token_file": "/private/server-token",
|
||||
"root": str(root / "lib/app-web"), "updates": "/prefix/lib/app-updates"}
|
||||
ntfy, server, web = MagicMock(), MagicMock(), MagicMock()
|
||||
ntfy.poll.return_value = None
|
||||
@@ -299,6 +300,10 @@ class ManageNativeTests(unittest.TestCase):
|
||||
"--updates", "/prefix/lib/app-updates"])
|
||||
self.assertNotIn("PIPELINES_WEB_AUTH", call.kwargs["env"])
|
||||
self.assertEqual(call.kwargs["env"]["PIPELINES_WEB_AUTH_FILE"], "/private/auth.json")
|
||||
# The server requires and the proxy sends the shared server token.
|
||||
for spawned in popen.call_args_list:
|
||||
self.assertEqual(spawned.kwargs["env"]["PIPELINES_SERVER_TOKEN_FILE"], "/private/server-token")
|
||||
self.assertEqual(manage.server_token_env({"web": None}), {})
|
||||
web.terminate.assert_called_once()
|
||||
server.terminate.assert_called_once()
|
||||
ntfy.terminate.assert_called_once()
|
||||
|
||||
@@ -257,6 +257,24 @@ class SetupNativeTests(unittest.TestCase):
|
||||
self.assertTrue((prefix / "lib/web/src/proxySecurity.js").is_file())
|
||||
self.assertEqual(web["port"], 8082)
|
||||
|
||||
def test_server_token_is_created_private_once_beside_the_web_auth(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
installer = self.installer(directory, Recorder())
|
||||
prefix = Path(directory) / "prefix"
|
||||
installer.prepare_layout(prefix)
|
||||
with patch.object(setup_native.shutil, "which", side_effect=self.which_all()):
|
||||
web = installer.configure_web(prefix, setup_native.arguments([]))
|
||||
token = Path(web["server_token_file"])
|
||||
before = token.read_bytes()
|
||||
installer.configure_web(prefix, setup_native.arguments([]))
|
||||
self.assertEqual(token.parent, Path(web["auth_file"]).parent)
|
||||
self.assertEqual(token.read_bytes(), before)
|
||||
self.assertEqual(token.stat().st_mode & 0o777, 0o600)
|
||||
self.assertGreaterEqual(len(before.strip()), 32)
|
||||
token.chmod(0o644)
|
||||
with self.assertRaises(ValueError):
|
||||
installer.ensure_server_token(token)
|
||||
|
||||
def test_new_web_password_must_be_chosen(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
installer = self.installer(directory, Recorder())
|
||||
|
||||
Reference in New Issue
Block a user