diff --git a/config/Service.pkl b/config/Service.pkl index 7132b2b923b6f6e5bc0826c78c6612bbee5e2ed4..6ab4e64926e60343647234ae20c5d1b27805e04f 100644 --- a/config/Service.pkl +++ b/config/Service.pkl @@ -88,6 +88,8 @@ class Container { env: Mapping = new {} /// Nomad template for environment values resolved after allocation. envTemplate: String? + /// Nomad templates rendered privately and mounted read-only at absolute container paths. + secretFiles: Mapping = new {} args: Listing = new {} capAdd: Listing = new {} devices: Listing = new {} diff --git a/service/knife/bundle.json b/service/knife/bundle.json index 6beb982f12b73115d26596e037c9fed41adced6c..2120faa436827e86642e7aca5123205149f1a497 100644 --- a/service/knife/bundle.json +++ b/service/knife/bundle.json @@ -1,5 +1,5 @@ { "url": "https://file.paperclover.net/shr/the-knife/knife-server-linux-amd64.tar.gz", - "sha256": "02d9dda61a4de554c5640e6039d2fe9bc5ebcb71d3967efa1930ffc953614102", - "image": "sha256:027c96783e914e8a7b4ab3351e89e2c2f75845e889bf81e19aa5b2e87c8a01a6" + "sha256": "c432d9e6565608367beaf6cbd98ea8ade498f26ecd2ac5becca6186615376e44", + "image": "sha256:6135e4adb01771ac33cb45b7d2874272883c5ecacf119b847cd7fc76a76349f1" } diff --git a/service/knife/prepare.py b/service/knife/prepare.py index 6e45e375082e6ea72d3f714d75cad7168e22b43e..6a377e67d901cd976456753c2ae2cda72fc9c158 100644 --- a/service/knife/prepare.py +++ b/service/knife/prepare.py @@ -1,12 +1,15 @@ #!/usr/bin/env python3 import hashlib import json +import os from pathlib import Path import shutil import subprocess +import sys import tarfile import tempfile import urllib.request +import uuid bundle = json.loads(Path(__file__).with_name("bundle.json").read_text()) @@ -29,3 +32,28 @@ if subprocess.run(["podman", "image", "exists", bundle["image"]]).returncode: shutil.copyfileobj(contents, target) subprocess.run(["podman", "load", "--quiet", "--input", str(image)], check=True) subprocess.run(["podman", "image", "exists", bundle["image"]], check=True) + +root = Path(json.load(sys.stdin)["hostRoot"]) +archives = root / "archives" +mount = subprocess.run( + ["findmnt", "-rn", "-o", "TARGET,FSTYPE", "--target", str(archives)], + check=True, capture_output=True, text=True, +).stdout.strip() +if root.resolve() != root or archives.resolve() != archives or mount != f"{root} zfs": + raise ValueError("Knife archives must be on the mounted service ZFS dataset") +marker = archives / ".knife-archive-store-id" +try: + with marker.open("x") as file: + file.write(str(uuid.uuid4()) + "\n") + os.fchmod(file.fileno(), 0o444) + file.flush() + os.fsync(file.fileno()) + directory = os.open(archives, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(directory) + finally: + os.close(directory) +except FileExistsError: + pass +if marker.is_symlink() or not marker.is_file() or not marker.read_text().strip(): + raise ValueError("Knife archive store identity must be a nonempty regular file") diff --git a/service/knife/service.pkl b/service/knife/service.pkl index 2422e4788f054cfa442ccb548fdc9c72037a2e9f..0ef5e90598f4cd3926c180b765d6250a44ebc11f 100644 --- a/service/knife/service.pkl +++ b/service/knife/service.pkl @@ -15,7 +15,6 @@ requirements { } stageIsolation = "fresh" prepare = "prepare.py" -setup = "setup.py" container { image = bundle.image @@ -26,7 +25,7 @@ container { "--" "/bin/sh" "-ec" - "node /opt/knife/scripts/container-runtime.ts; exec knife start --host 0.0.0.0 --port 3773 --no-browser" + "T3CODE_ARCHIVE_STORE_ID=$(cat /archives/.knife-archive-store-id); export T3CODE_ARCHIVE_STORE_ID; node /opt/knife/scripts/container-runtime.ts; exec knife start --host 0.0.0.0 --port 3773 --no-browser" } http { containerPort = 3773 @@ -46,10 +45,24 @@ container { ["HOME"] = "/home/node" ["KNIFE_UPDATE_URL"] = "https://file.paperclover.net/shr/the-knife/" ["T3CODE_HOME"] = "/data" + ["T3CODE_SSO_CONFIG"] = "/run/secrets/knife-oidc.json" ["T3CODE_ARCHIVE_DIR"] = "/archives" ["T3CODE_ARCHIVE_OFFLOAD"] = "true" ["T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD"] = "false" // Info-level startup logs include a live owner pairing credential. ["T3CODE_LOG_LEVEL"] = "Warn" } + secretFiles { + ["/run/secrets/knife-oidc.json"] = """ + {{ with nomadVar "nomad/jobs/\(module.id)/inputs/oidc" }} + { + "publicUrl": "https://\(module.container.http.hostname)", + "issuer": {{ .issuerUrl.Value | toJSON }}, + "clientId": {{ .clientId.Value | toJSON }}, + "clientSecret": {{ .clientSecret.Value | toJSON }}, + "ownerSub": "8c909fab-98b0-4472-9171-9606ccebe9d5" + } + {{ end }} + """ + } } diff --git a/service/knife/setup.py b/service/knife/setup.py deleted file mode 100644 index ec5115381302ce30667280bd462919499a492877..0000000000000000000000000000000000000000 --- a/service/knife/setup.py +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python3 -import json -import os -from pathlib import Path -import subprocess -import sys - - -data = json.load(sys.stdin) -allocations = json.loads(subprocess.check_output( - ["nomad", "job", "allocs", "-json", data["serviceId"]], - env={**os.environ, "NOMAD_TOKEN": Path("/var/lib/studio/nomad.token").read_text().strip()}, -)) -running = [item["ID"] for item in allocations if item["ClientStatus"] == "running" and item["DesiredStatus"] == "run"] -if len(running) != 1: - raise ValueError("Knife setup requires one running allocation") -subprocess.run([ - "podman", "exec", "app-" + running[0], "knife", "shared", "hub", "configure", "https://" + data["host"], -], check=True, capture_output=True) diff --git a/tools/knife-test.py b/tools/knife-test.py new file mode 100644 index 0000000000000000000000000000000000000000..a5a5e809b087fbf1f0c390832f6882c4eb6d4654 --- /dev/null +++ b/tools/knife-test.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +import io +import json +from pathlib import Path +import runpy +import subprocess +import tempfile +import unittest +from unittest.mock import patch +import uuid + + +class ArchiveIdentity(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory(prefix="knife-archive-test-") + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name).resolve() + self.archives = self.root / "archives" + self.archives.mkdir() + self.marker = self.archives / ".knife-archive-store-id" + + def prepare(self, mount=None): + def command(args, **kwargs): + if args[:3] == ["podman", "image", "exists"]: + return subprocess.CompletedProcess(args, 0) + self.assertEqual(args[0], "findmnt") + self.assertEqual(args[-1], str(self.archives)) + return subprocess.CompletedProcess(args, 0, stdout=mount or f"{self.root} zfs\n") + with patch("subprocess.run", side_effect=command), patch("sys.stdin", io.StringIO(json.dumps({"hostRoot": str(self.root)}))): + runpy.run_path(str(Path(__file__).resolve().parents[1] / "service/knife/prepare.py")) + + def test_create_on_confirmed_zfs_and_preserve_existing_identity(self): + self.prepare() + uuid.UUID(self.marker.read_text().strip()) + self.assertEqual(self.marker.stat().st_mode & 0o777, 0o444) + self.marker.unlink() + self.marker.write_text("existing-nas-identity\n") + self.prepare() + self.assertEqual(self.marker.read_bytes(), b"existing-nas-identity\n") + + def test_wrong_or_fallback_mount_cannot_create_marker(self): + for mount in ["/ ext4", f"{self.root} ext4", f"{self.archives} zfs"]: + with self.subTest(mount=mount), self.assertRaises(ValueError): + self.prepare(mount) + self.assertFalse(self.marker.exists()) + + def test_symlinked_archive_directory_cannot_create_marker(self): + self.archives.rmdir() + target = self.root / "elsewhere" + target.mkdir() + self.archives.symlink_to(target, target_is_directory=True) + with self.assertRaises(ValueError): + self.prepare() + self.assertFalse((target / self.marker.name).exists()) + + def test_invalid_existing_marker_is_preserved_and_rejected(self): + self.marker.write_text("") + with self.assertRaises(ValueError): + self.prepare() + self.assertEqual(self.marker.read_bytes(), b"") + self.marker.unlink() + target = self.root / "outside-marker" + target.write_text("outside\n") + self.marker.symlink_to(target) + with self.assertRaises(ValueError): + self.prepare() + self.assertEqual(target.read_text(), "outside\n") + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/knife.md b/tools/knife.md index 47f718a4018820a1013ffdf3bd639456879975bb..6a99d27fe21b2611262af09bf2018af41e8e2033 100644 --- a/tools/knife.md +++ b/tools/knife.md @@ -1,184 +1,88 @@ -# Knife setup and operation +# Knife sign-in and archive storage -`https://knife.paperclover.net` runs the private Knife authority, archive -receiver, and optional environment hub. Native single-use pairing controls -access; there is no public registration. The service's four directories live -on the managed NAS dataset `/srv/prod/knife`: `data`, `archives`, `home/node`, -and `workspaces`. Provider credentials can stay on each laptop. +Open https://knife.paperclover.net and sign in through Snow Globe. The server +accepts only the configured issuer and Clover's immutable owner subject. The NAS +environment is **snow globe**. Provider accounts can stay on each laptop; the NAS +needs none for archive storage or outbound environment routing. -## Pair the owner +On each machine, install the current Knife desktop release, choose its +self-hosted server connection, enter `https://knife.paperclover.net`, and complete +browser sign-in. The desktop returns through its proof-bound application callback; +only the server's HTTPS callback is registered with Snow Globe. Laptop connections +run outbound through Knife, with archive offload enabled automatically. No laptop +port forwarding, SSH pairing, bearer-token file, or manual hub enrollment is needed +for the SSO connection. -Run these commands in your terminal. SSH access to Zenith is the recovery -path; no owner credential is written to application or deployment logs. +Archive a disposable conversation first, reopen its cold transcript, then unarchive +it. Confirm that its source connection and archive queue report success before +relying on offload for existing history. A sleeping laptop cannot run interactive +agents; uploaded archives remain on the NAS. Successful offload reclaims local +SQLite pages after publication. Native provider history files remain on the laptop. + +## Access and recovery + +SSO access credentials last fifteen minutes, with rotating device credentials in +operating-system protected storage. Browser sign-in is required again after thirty +days. The server revalidates the issuer grant during renewal at least hourly; +issuer revocation can therefore take an hour plus fifteen minutes to fully apply. +Local device revocation disables renewal and closes active connections immediately. + +Existing manual pairing credentials survive the upgrade. Manage them separately +from SSO devices under access settings; do not share pairing links or bearer tokens. +The listener binds only to loopback behind HTTPS, with WebSocket proxying. Hosted +SSO cookies are Secure, HttpOnly, SameSite=Lax and protected against cross-origin +writes. + +For local recovery, derive the current container from Nomad rather than retaining +an allocation ID: ```sh ssh -p 22 root@10.0.0.1 export NOMAD_TOKEN="$(cat /var/lib/studio/nomad.token)" knife_allocation=$(nomad job allocs -json knife | python3 -c 'import json,sys; a=[r["ID"] for r in json.load(sys.stdin) if r["ClientStatus"]=="running" and r["DesiredStatus"]=="run"]; assert len(a)==1; print(a[0])') knife_container="app-$knife_allocation" -podman exec "$knife_container" knife auth pairing create \ - --ttl 5m --label 'Clover owner' --base-url https://knife.paperclover.net \ - --scope orchestration:read --scope orchestration:operate \ - --scope settings:write --scope providers:manage \ - --scope environment:maintain --scope preview:operate \ - --scope diagnostics:read --scope terminal:read --scope terminal:operate \ - --scope source-control:write --scope filesystem:read --scope filesystem:write \ - --scope relay:read --scope relay:write --scope access:read --scope access:write +podman exec "$knife_container" knife auth devices revoke-all ``` -Open the printed pairing link privately within five minutes. It is single-use. -An owner pairing needs the full scope list: supplying `--scope` replaces the -CLI defaults, and default pairing omits access management. The browser session -lasts 30 days. Repeat the command for another owner device or browser. - -In the desktop app, choose **Self-hosted server**, enter -`https://knife.paperclover.net`, and paste a fresh pairing code. The same entry -is under **Settings → Environments**. Choose this path to avoid T3 Connect. -A browser paired to the NAS controls the NAS environment; laptop transcripts -remain in their originating environments until those laptops are connected. - -The NAS needs no provider login for archive storage or hub routing. Configure -providers on the NAS only if agents should actually run there. Its writable -workspace is `/workspaces`; the personal file archive and host credentials are -not mounted into the container. - -## Enable laptop archive offload - -UI pairing and archive offload are separate connections. Repeat these steps for -each laptop, using a separate, revocable token and label. - -From the Mac, create a private local token file by capturing SSH output directly. -The current API requires orchestration read/operate and provider-management -permissions for offload plus the `shared connect` account-list check. These also -permit thread/provider operations on the NAS; keep the token private. No provider -accounts need to be imported. - -```sh -umask 077 -mkdir -p "$HOME/.config/knife" -ssh -p 22 root@10.0.0.1 'export NOMAD_TOKEN="$(cat /var/lib/studio/nomad.token)"; allocation=$(nomad job allocs -json knife | python3 -c '\''import json,sys; a=[r["ID"] for r in json.load(sys.stdin) if r["ClientStatus"]=="running" and r["DesiredStatus"]=="run"]; assert len(a)==1; print(a[0])'\''); podman exec "app-$allocation" knife auth session issue --ttl 30d --label "Clover Mac archive" --scope orchestration:read --scope orchestration:operate --scope providers:manage --token-only' > "$HOME/.config/knife/nas-token" -chmod 600 "$HOME/.config/knife/nas-token" -"$HOME/.t3/bin/t3" shared connect https://knife.paperclover.net \ - --token-file "$HOME/.config/knife/nas-token" -``` - -The packaged Knife desktop installs `~/.t3/bin/t3`. If a custom `T3CODE_HOME` -is used, use its `bin/t3` and pass the matching `--base-dir` to CLI commands. -The local Knife backend must already be running for `shared connect`. - -Quit Knife, then launch its executable with archive offload enabled: - -```sh -T3CODE_ARCHIVE_OFFLOAD=true '/Applications/The Knife.app/Contents/MacOS/The Knife' -``` - -The setting must reach the backend on every launch. For Finder launches, -`launchctl setenv T3CODE_ARCHIVE_OFFLOAD true` supplies it for this login session; -repeat after logging in again. Fully quit the existing app before relaunching. -Windows WSL backends currently do not forward this variable from the desktop; -enable it inside the WSL backend environment before using offload there. - -Check the connection and archive queue: - -```sh -"$HOME/.t3/bin/t3" shared status -"$HOME/.t3/bin/t3" shared archive status -``` - -Archive one disposable conversation first. Within roughly a minute, status -should report `enabled: true`, an increased `offloaded` count, `pending: 0`, -and no failures. Open its transcript and unarchive it to check retrieval and -restoration before relying on the connection for real history. - -Offload replaces archived T3 transcript bodies with local metadata after the -NAS acknowledges the object. Failed uploads retain the local bodies. Reading -cold history requires the NAS and its valid token; unarchive restores local -bodies. Existing SQLite files can retain allocated pages, and native Claude or -Codex history files remain: this feature does not compact all existing laptop -storage. Keep the NAS state and archive objects together in backups. - -The archive token expires after 30 days without automatic renewal. Issue a -replacement into the private file and run `shared connect` again before it -expires. `shared archive retry` retries failed uploads. Choose a longer `--ttl` -explicitly if monthly renewal is unsuitable; retain a separate label per laptop. - -## Route laptop environments through the NAS - -Archive offload only needs an outbound laptop connection to the NAS. Routing -interactive access through the hub additionally requires Zenith to reach the -laptop backend over a private LAN or VPN address. This fork has no reverse -connection that makes an unreachable laptop accessible through the hub. - -The NAS public hub URL is configured automatically during deployment. To enroll -a laptop, get its environment ID from its private -`/.well-known/t3/environment` descriptor. Issue a separate, labeled administrative -session on that laptop with `~/.t3/bin/t3 auth session issue --ttl 30d ---label "Clover NAS hub" --token-only`, capturing it into a mode-0600 file rather -than displaying it. -Transfer that file privately to Zenith. In the SSH session above: - -```sh -podman cp /root/knife-laptop-enrollment-token "$knife_container":/data/laptop-enrollment-token -podman exec "$knife_container" knife shared hub enroll \ - https://YOUR-PRIVATE-LAPTOP-ADDRESS ENVIRONMENT-ID \ - --token-file /data/laptop-enrollment-token -podman exec "$knife_container" rm /data/laptop-enrollment-token -rm /root/knife-laptop-enrollment-token -``` - -Use a private endpoint already secured by Knife authentication; Tailscale Serve -is one option. No public laptop port is required. Do not disable certificate -verification. The hub retains this administrative credential in its private -state to delegate later connections; removing the transfer files does not revoke -it. Renew and re-enroll before its 30-day expiry, or revoke the labeled laptop -session to withdraw hub access. A sleeping or unreachable laptop cannot serve -an interactive session. Its uploaded archive objects remain on the NAS. - -## Revoke access and update the service - -The owner session can manage pairing links and clients in **Settings → This machine → Authorized clients**. -SSH recovery also works without a live browser session: - -```sh -podman exec "$knife_container" knife auth session list -podman exec "$knife_container" knife auth session revoke SESSION-ID -podman exec "$knife_container" knife auth pairing list -podman exec "$knife_container" knife auth pairing revoke PAIRING-ID -``` - -Keep pairing links and archive tokens private. Access is possession-based: -anyone holding a live code or token has its granted scopes. The proxy adds -`Secure` to browser cookies; the app supplies `HttpOnly` and `SameSite=Lax`. -The container's published port binds only to loopback. - -As requested, each container restart checks the release feed and starts its -newest compatible server/UI/assets after SHA-256 verification. The running -process keeps its current code until restarted. This quiet startup path does -not prefetch in the background. A temporary feed outage starts the installed -runtime; corrupt downloads or a release requiring a newer base image stop -startup instead of executing unverified or incompatible code. - -For a new base image, review the Linux bundle, update -`service/knife/bundle.json` with its checksum and image ID, and use the normal -stage → main → prod workflow. The loader rejects changed bytes at the existing -bundle URL until its recorded checksum is updated. Application code cached -under `/data/runtime/knife-container` survives image/infra rollback; restoring -an older runtime requires an explicit application recovery choice. Code -rollback does not roll back state or archive objects. - -## 2026-10-16 verification - -Zenith fresh preview verified anonymous project/archive/pairing denial, -single-use pairing, hostile cookie Origin rejection, session revocation, -hash-checked archive upload, identical download, byte ranges, and corrupt-object -rejection. A disposable local client imported a two-message synthetic Claude -conversation, offloaded it to the NAS, restarted, read both cold transcript -items, and restored both bodies on unarchive. Temporary client credentials -were revoked and local fixture stores removed. - -Browser verification covered first run, self-hosted connection entry, settings, -access management, archive/history controls, keyboard navigation, and reload -persistence. Real Caddy checks verified Secure on multiple cookies and logout -responses while leaving default routes unchanged. Warn-level startup emitted -no pairing credential. All four service mounts used disposable managed preview -storage with the assigned service UID. +This revokes SSO devices and sessions without contacting the issuer. It preserves +the configured owner, identity-provider account, archives, and manual pairing +credentials. Inspect/revoke those separately with `knife auth session list`, +`knife auth session revoke SESSION-ID`, `knife auth pairing list`, and +`knife auth pairing revoke PAIRING-ID` in the same container. + +## Persistent configuration + +The existing managed ZFS dataset `/srv/prod/knife` holds `data`, `archives`, +`home/node`, and `workspaces`. Keep state and archives together in backups. Preserve +its assigned service UID and environment identity across image upgrades. + +Nomad owns the confidential client secret at `nomad/jobs/knife/inputs/oidc`. +The allocation renders the private SSO JSON and mounts it read-only at +`/run/secrets/knife-oidc.json`, readable by the assigned service UID. Tracked +configuration contains the public issuer, callback, and immutable owner subject, +with references to the private Nomad values. Preview allocations use their own +registered clients and disposable state. + +Preparation creates `archives/.knife-archive-store-id` exclusively on the confirmed +service ZFS mount, preserving any existing marker. Startup supplies its value as +`T3CODE_ARCHIVE_STORE_ID`. Missing or mismatched identity blocks archive publication +and local payload eviction; never replace the marker to silence that failure. + +Each restart follows the release feed and applies the newest compatible, +checksum-verified runtime. The quiet startup path does not prefetch in the +background. Feed outages retain the installed runtime; incompatible base-image +requirements or corrupt downloads fail startup. For a new image, review the bundle +and update `service/knife/bundle.json`, then use the normal stage → main → prod +workflow. Runtime downloads under `data/runtime/knife-container` survive an infra +image rollback; rolling back code does not restore state or archive objects. + +## 2026-10-16 SSO verification + +A fresh Zenith preview completed Clover's real Snow Globe sign-in, verified the +issuer/subject account binding, renewed its browser session, and opened an +authenticated WebSocket through HTTPS. Anonymous access, missing CSRF, invalid +OIDC/native transactions, and corrupt archive objects were rejected. A synthetic +object uploaded and downloaded identically with working byte ranges; temporarily +removing the preview's archive marker returned 503 and its original marker was +restored unchanged. The private allocation JSON matched its separate Nomad client, +used mode 0400 and UID 3132, and its intended bind mount rejected writes. diff --git a/tools/studio-secret-files-test.py b/tools/studio-secret-files-test.py new file mode 100644 index 0000000000000000000000000000000000000000..613cad961bd91717aab5fa2b4199be69a3fa85d0 --- /dev/null +++ b/tools/studio-secret-files-test.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python3 +import json +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +import studio + + +class SecretFiles(unittest.TestCase): + @classmethod + def setUpClass(cls): + with tempfile.TemporaryDirectory(prefix="studio-secret-template-") as temporary: + fixture = Path(temporary) / "fixture.pkl" + fixture.write_text('amends ' + json.dumps((studio.REPO / "config/Service.pkl").as_uri()) + '\nmeta { name = "Fixture" }\ncontainer { image = "localhost/fixture:pinned" }\n') + result = subprocess.run([os.environ.get("PKL", "pkl"), "eval", "-p", "uid=3132", "-p", "serviceId=fixture", str(fixture)], check=True, capture_output=True, text=True) + cls.fixture = json.loads(result.stdout) + cls.fixture.update(uid=3132, sourceId="fixture", inputs={}) + cls.fixture.pop("requirements") + + def setUp(self): + self.data = json.loads(json.dumps(self.fixture)) + self.task = self.data["containers"]["app"] + self.task["secretFiles"] = {"/run/secrets/oidc.json": '{{ with nomadVar "nomad/jobs/fixture/inputs/oidc" }}{"clientSecret":{{ .clientSecret.Value | toJSON }}}{{ end }}'} + + def test_private_allocation_template_and_read_only_file_mount(self): + rendered = studio.render(self.data, {"fixture": self.data}) + self.assertIn('"secrets/oidc.json:/run/secrets/oidc.json:ro"', rendered) + self.assertIn('destination = "secrets/oidc.json"', rendered) + self.assertIn('perms = "0400"', rendered) + self.assertIn('uid = 3132', rendered) + self.assertIn('gid = 3132', rendered) + self.assertIn('error_on_missing_key = true', rendered) + self.assertIn(self.task["secretFiles"]["/run/secrets/oidc.json"], rendered) + self.assertNotIn('env = true', rendered) + self.assertNotIn('change_mode =', rendered) + + def test_services_without_files_gain_no_template_or_mount(self): + self.task["secretFiles"] = {} + rendered = studio.render(self.data, {"fixture": self.data}) + self.assertNotIn('template {', rendered) + self.assertNotIn('secrets/', rendered) + + def test_unsafe_and_colliding_targets_are_rejected(self): + for target in ("relative.json", "/run/../secret.json", "/run/./secret.json", "/run//secret.json", "/run/secrets/", "/secrets/file", "/secrets", "/run/app.env", "/run/app-secret.env", "/run/nomad_token", "/run/line\nbreak"): + with self.subTest(target=target): + self.task["secretFiles"] = {target: "{}"} + with self.assertRaisesRegex(ValueError, "invalid secret file target"): + studio.render(self.data, {"fixture": self.data}) + self.task["secretFiles"] = {"/one/config.json": "{}", "/two/config.json": "{}"} + with self.assertRaisesRegex(ValueError, "invalid secret file target"): + studio.render(self.data, {"fixture": self.data}) + self.task["secretFiles"] = {"/run/secrets/oidc.json": "{}"} + self.task["volumes"] = {"/run/secrets/oidc.json": {}} + with self.assertRaisesRegex(ValueError, "invalid secret file target"): + studio.render(self.data, {"fixture": self.data}) + + def test_image_user_and_heredoc_injection_are_rejected(self): + self.task["imageUser"] = True + with self.assertRaisesRegex(ValueError, "service-owned"): + studio.render(self.data, {"fixture": self.data}) + self.task["imageUser"] = False + for body in ("EOF", "{}\nEOF\nmalicious", "{}\nEOF\r\nmalicious"): + self.task["secretFiles"] = {"/run/secrets/oidc.json": body} + with self.assertRaisesRegex(ValueError, "invalid secret template delimiter"): + studio.render(self.data, {"fixture": self.data}) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/studio.py b/tools/studio.py index a1b379ed9f37729f9dcf6c56ab78bb0e0c536469..55477e09d230c8a1c19eaaabd835043efdc80510 100644 --- a/tools/studio.py +++ b/tools/studio.py @@ -397,6 +397,22 @@ def render(data, definitions): if values: lines.append(f" {field} = {q(values)}") volumes = [] + secret_files = task.get("secretFiles", {}) + filenames = set() + for target, template in secret_files.items(): + filename = Path(target).name + if (not re.fullmatch(r"/[A-Za-z0-9_./-]+", target) or target.endswith("/") + or any(part in {".", "..", ""} for part in target[1:].split("/")) + or filename in filenames + or filename in {task_name + ".env", task_name + "-secret.env", "nomad_token"} + or target in task["volumes"] or target == "/secrets" or target.startswith("/secrets/")): + raise ValueError(f"invalid secret file target: {target}") + if task["imageUser"]: + raise ValueError("secret files require a service-owned container user") + if re.search(r"(?m)^EOF\r?$", template): + raise ValueError("invalid secret template delimiter") + filenames.add(filename) + volumes.append(f"secrets/{filename}:{target}:ro") for target, volume in task["volumes"].items(): if not target.startswith("/") or ":" in target: raise ValueError(f"invalid volume target: {target}") @@ -460,6 +476,11 @@ def render(data, definitions): if "\nEOF\n" in task["envTemplate"]: raise ValueError("invalid env template delimiter") lines += [" template {", " data = <