From 6ebe706351137f05a79e0588944c7f473d997e44 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Nov 2026 09:19:03 +0000 Subject: [PATCH] demo: each session's transcript, kept 7 days in a write-only drop box (#192) Ruled 2026-11-21, "Write-only drop box": console.py writes each session's transcript, mode 0600, into /var/lib/starshipos-demo-transcripts (the owner's, group starshipos-demo, mode 1730: a session adds a file but cannot list or read the directory). transcripts-tidy.sh, run as root every 5 minutes by starshipos-demo-transcripts.timer, hands each file to the owner and deletes those over 7 days. starshipos-demo.service gets the directory writable and DEMO_TRANSCRIPTS; DEPLOY.md step 4 has the server steps and the check. "Nothing you do is kept" was no longer true. Ruled 2026-11-24, "Machine not kept; log 7 days": the session's banner, the Try it page and the old wiki page say nothing is kept on the machine and a transcript is kept for up to 7 days, then deleted, and the page says so before the console. demo/test_transcripts.py failed first (logs/20261121-demo-transcripts/before.txt) and passes its 10 checks (after.txt). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_017RzZgrf7V7Zk2iczBXDRbm --- demo/DEPLOY.md | 25 +++++++ demo/README.md | 7 ++ demo/console.py | 21 ++++++ demo/starshipos-demo-transcripts.service | 12 ++++ demo/starshipos-demo-transcripts.timer | 10 +++ demo/starshipos-demo.service | 3 + demo/start-session.sh | 3 +- demo/test_transcripts.py | 80 +++++++++++++++++++++++ demo/transcripts-tidy.sh | 12 ++++ demo/xwiki-try-it.xwiki | 3 +- logs/20261121-demo-transcripts/after.txt | 11 ++++ logs/20261121-demo-transcripts/before.txt | 8 +++ site/com/TryIt.WebHome.xwiki | 3 +- site/com/canvas/TryIt.dc.html | 6 +- 14 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 demo/starshipos-demo-transcripts.service create mode 100644 demo/starshipos-demo-transcripts.timer create mode 100644 demo/test_transcripts.py create mode 100755 demo/transcripts-tidy.sh create mode 100644 logs/20261121-demo-transcripts/after.txt create mode 100644 logs/20261121-demo-transcripts/before.txt diff --git a/demo/DEPLOY.md b/demo/DEPLOY.md index 85c24c1d..e6950982 100644 --- a/demo/DEPLOY.md +++ b/demo/DEPLOY.md @@ -59,6 +59,31 @@ ss -ltnp | grep 7681 # the ttyd of starshipos-demo, not The service expects the checkout's `demo/` at `/opt/starshipos-demo/demo` (step 1 put it there). +**The transcripts (#192, ruled 2026-11-21: "Write-only drop box").** Each session writes what the +visitor typed and what the machine answered to `/var/lib/starshipos-demo-transcripts`, one file per +session, mode 0600 (`console.py`). The directory is yours, group `starshipos-demo`, mode 1730: a +session can add a file, but cannot list the directory or read anything in it. A root timer hands each +file to you and deletes every one older than 7 days (`transcripts-tidy.sh`). The Try it page says so. +Before (re)starting the service, with `OWNER` your own login: + +```bash +OWNER= +install -d -o "$OWNER" -g starshipos-demo -m 1730 /var/lib/starshipos-demo-transcripts +sed "s/^Environment=OWNER=CHANGE-ME$/Environment=OWNER=$OWNER/" \ + demo/starshipos-demo-transcripts.service > /etc/systemd/system/starshipos-demo-transcripts.service +cp demo/starshipos-demo-transcripts.timer /etc/systemd/system/ +systemctl daemon-reload && systemctl enable --now starshipos-demo-transcripts.timer +systemctl restart starshipos-demo # its unit now writes the transcripts directory +systemctl list-timers starshipos-demo-transcripts --no-pager +``` + +Then the check: open a session, type a line, close it; within 5 minutes its file is in +`/var/lib/starshipos-demo-transcripts`, owned by you, mode `-rw-------`, and `sudo -u +starshipos-demo ls /var/lib/starshipos-demo-transcripts` is refused. A file older than 7 days +(`touch -d '8 days ago' FILE.log`) is gone at the next run (`systemctl start +starshipos-demo-transcripts` runs it at once). Apache must not serve the directory: nothing in +`apache-demo.conf` maps it, and nothing should. + ## 5. TLS and Apache for try.starshipos-f18.com (~10 min) DNS: `try.starshipos-f18.com` must point at this server (an A record) before certbot. diff --git a/demo/README.md b/demo/README.md index 484d039f..28f5b6fd 100644 --- a/demo/README.md +++ b/demo/README.md @@ -21,6 +21,9 @@ XWiki "Try it" page (iframe) -> Apache: TLS, websocket -> ttyd -> start-session. | `console.py` | Joins the visitor's terminal to the machine only once it runs, discarding keys typed while it loads | | `qemu-args.sh` | The machine, shared by the two above: a resumed machine must match the saved one exactly | | `starshipos-demo.service` | ttyd as a systemd service, on localhost; two guests at once, a busy message for the rest (#114, #116) | +| `transcripts-tidy.sh` | Hands each session's transcript to its owner, mode 0600, and deletes those over 7 days (#192) | +| `starshipos-demo-transcripts.service`, `.timer` | Runs `transcripts-tidy.sh` as root every 5 minutes (#192) | +| `test_transcripts.py` | Tests the transcript and the tidy, with a stand-in machine: `python3 demo/test_transcripts.py` | | `DEPLOY.md` | Putting it on the server, step by step | | `apache-demo.conf` | Example vhost: TLS, the websocket, framing allowed for the `.com` only (#115) | | `xwiki-try-it.xwiki` | The "Try it" page, its own page on the `.com` XWiki (`TryIt`), in the landing page's style | @@ -33,6 +36,10 @@ seccomp sandbox (no spawning, no privilege changes), as an unprivileged user, in that fences memory, CPU and the file system. A session lasts at most `DEMO_SESSION_LIMIT` seconds (1800). +What is kept: a transcript of each session -- what the visitor typed and what the machine answered +-- for up to 7 days, readable by the server's owner only, then deleted (#192; `DEPLOY.md`, step 4). +The Try it page and the session's first lines say so. + ## Setting it up From a checkout of the most recent green commit -- the latest that passed acceptance, `master` diff --git a/demo/console.py b/demo/console.py index dd87de21..8b696ed7 100755 --- a/demo/console.py +++ b/demo/console.py @@ -10,6 +10,13 @@ A resumed machine takes a moment to load. Anything typed meanwhile would reach a whose state is about to be replaced, and be lost half-way through a line ("3 4 + ." arriving as "4 + ."). So: wait until QEMU says the machine is running, throw away what was typed before, say so, and only then pass bytes both ways. Ends when either side closes. + +The transcript (#192, ruled 2026-11-13 and 2026-11-21): what the machine sends -- its prompt, the visitor's +line as it echoes it, what it prints -- is also written to DEMO_TRANSCRIPTS +(/var/lib/starshipos-demo-transcripts), one file a session, named by its UTC start and this process, mode 0600. +The directory is a drop box this user can write in but not list or read (DEPLOY.md); transcripts-tidy.sh +hands each file to its owner and deletes it after 7 days. The Try It page says so. If it cannot be written, +the session goes on without one. """ import json, os, select, socket, sys, termios, time, tty @@ -38,6 +45,14 @@ while True: # until the machine runs time.sleep(0.1) con = connect(con_path) +transcript = None +try: + tdir = os.environ.get("DEMO_TRANSCRIPTS", "/var/lib/starshipos-demo-transcripts") + name = os.path.join(tdir, time.strftime("%Y%m%d-%H%M%S", time.gmtime()) + "-%d.log" % os.getpid()) + transcript = os.open(name, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + os.write(transcript, ("# StarshipOS demo session, %s UTC\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())).encode()) +except OSError: + transcript = None # no transcript, the session as before stdin, stdout = sys.stdin.fileno(), sys.stdout.fileno() saved = None if os.isatty(stdin): @@ -56,7 +71,13 @@ try: data = con.recv(4096) if not data: break os.write(stdout, data) + if transcript is not None: + try: os.write(transcript, data) + except OSError: transcript = None except OSError: pass # the visitor's terminal went away finally: if saved: termios.tcsetattr(stdin, termios.TCSADRAIN, saved) + if transcript is not None: + os.write(transcript, ("\n# ended %s UTC\n" % time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())).encode()) + os.close(transcript) diff --git a/demo/starshipos-demo-transcripts.service b/demo/starshipos-demo-transcripts.service new file mode 100644 index 00000000..55633b5e --- /dev/null +++ b/demo/starshipos-demo-transcripts.service @@ -0,0 +1,12 @@ +# starshipos-demo-transcripts.service -- the demo's transcripts (#192, ruled 2026-11-21: "Write-only drop box"). +# Each session (console.py) writes its transcript, mode 0600, into /var/lib/starshipos-demo-transcripts: +# owned by Captain Bob, group starshipos-demo, mode 1730, so a session can add a file but not list or +# read the directory. This hands each file to the owner and deletes every one older than 7 days. +# Run by starshipos-demo-transcripts.timer. Set OWNER below to the owner's login (DEPLOY.md, step 4). +[Unit] +Description=StarshipOS demo transcripts: hand them to their owner, delete those over 7 days + +[Service] +Type=oneshot +Environment=OWNER=CHANGE-ME +ExecStart=/opt/starshipos-demo/demo/transcripts-tidy.sh /var/lib/starshipos-demo-transcripts ${OWNER} diff --git a/demo/starshipos-demo-transcripts.timer b/demo/starshipos-demo-transcripts.timer new file mode 100644 index 00000000..b96a80e9 --- /dev/null +++ b/demo/starshipos-demo-transcripts.timer @@ -0,0 +1,10 @@ +# starshipos-demo-transcripts.timer -- runs starshipos-demo-transcripts.service every 5 minutes (#192). +[Unit] +Description=StarshipOS demo transcripts, every 5 minutes + +[Timer] +OnBootSec=5min +OnUnitActiveSec=5min + +[Install] +WantedBy=timers.target diff --git a/demo/starshipos-demo.service b/demo/starshipos-demo.service index cff44643..b6a0f724 100644 --- a/demo/starshipos-demo.service +++ b/demo/starshipos-demo.service @@ -14,6 +14,8 @@ Group=starshipos-demo Environment=DEMO_STATE=/var/lib/starshipos-demo Environment=DEMO_SESSION_LIMIT=1800 Environment=DEMO_MAX_GUESTS=2 +# each session's transcript (console.py, #192): a write-only drop box, kept 7 days +Environment=DEMO_TRANSCRIPTS=/var/lib/starshipos-demo-transcripts # the guests' slot locks (start-session.sh): /run/starshipos-demo, made for this user, gone at stop RuntimeDirectory=starshipos-demo # --max-clients: the guests and a few more, so a visitor over the limit reaches the busy message @@ -30,6 +32,7 @@ ProtectHome=yes PrivateTmp=yes PrivateDevices=yes ReadOnlyPaths=/var/lib/starshipos-demo +ReadWritePaths=/var/lib/starshipos-demo-transcripts # Sized for this server (3.8 GiB, two cores; both wikis, Gitea, Nexus, CI). Since #151 an idle guest # halts (1% of a core at the prompt, was 100%), so a session costs CPU only while its visitor runs # something. All sessions together get 1.5 cores at the lowest priority (Captain Bob, 2026-11-08): diff --git a/demo/start-session.sh b/demo/start-session.sh index 17aa171e..3f27d56c 100755 --- a/demo/start-session.sh +++ b/demo/start-session.sh @@ -42,7 +42,8 @@ trap 'rm -rf "$tmp"' EXIT cp "$dir/vars.fd" "$tmp/vars.fd" demo_qemu_args "$dir" "$tmp/vars.fd" -printf '\r\nStarshipOS -- starting a throwaway machine, yours for up to %d minutes. Nothing you do is kept.\r\n' $((limit / 60)) +printf '\r\nStarshipOS -- starting a throwaway machine, yours for up to %d minutes. Nothing you do is kept on it.\r\n' $((limit / 60)) +printf 'A transcript of this session is kept for up to 7 days, then deleted.\r\n' printf 'Try: 3 4 + . : SQUARE DUP * ; 7 SQUARE .\r\n\r\n' # QEMU is a tracked child, not exec'd, so that whatever ends the session ends the machine: ttyd # hangs up (it signals this script only), the service stops, or the time runs out. Its console is diff --git a/demo/test_transcripts.py b/demo/test_transcripts.py new file mode 100644 index 00000000..05f976fc --- /dev/null +++ b/demo/test_transcripts.py @@ -0,0 +1,80 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: LicenseRef-Starship-1.0 +# Copyright (c) 2023-2026 Robert A. James +"""test_transcripts.py -- the demo's transcripts (#192, ruled 2026-11-13 and 2026-11-21): console.py writes each +session's console into DEMO_TRANSCRIPTS, one dated file a session, readable only by its writer; and +transcripts-tidy.sh hands each to the directory's owner and deletes those older than 7 days. No QEMU: a fake QMP +socket that says the machine runs, and a fake console that echoes. Prints PASS:/FAIL: and RESULT:.""" +import json, os, socket, subprocess, sys, tempfile, threading, time, glob + +HERE = os.path.dirname(os.path.abspath(__file__)) +ok = True +def check(what, cond): + global ok + print(("PASS: " if cond else "FAIL: ") + what) + ok = ok and bool(cond) + +def qmp_server(path): + s = socket.socket(socket.AF_UNIX); s.bind(path); s.listen(1) + def run(): + c, _ = s.accept(); f = c.makefile("rw") + f.write(json.dumps({"QMP": {}}) + "\n"); f.flush() + for line in f: + f.write(json.dumps({"return": {"status": "running"}} if "query-status" in line else {"return": {}}) + "\n"); f.flush() + threading.Thread(target=run, daemon=True).start() + +def console_server(path): + s = socket.socket(socket.AF_UNIX); s.bind(path); s.listen(1) + def run(): + c, _ = s.accept() + c.sendall(b"[zuse@Hera] ok> ") + while True: + d = c.recv(1024) + if not d: break + c.sendall(d) # the machine echoes + if b"\r" in d: c.sendall(b"7 ok\r\n[zuse@Hera] ok> ") + threading.Thread(target=run, daemon=True).start() + +with tempfile.TemporaryDirectory() as t: + tr = os.path.join(t, "transcripts"); os.mkdir(tr, 0o730) + qmp_server(os.path.join(t, "qmp.sock")); console_server(os.path.join(t, "con.sock")) + env = dict(os.environ, DEMO_TRANSCRIPTS=tr) + p = subprocess.Popen([sys.executable, "-I", os.path.join(HERE, "console.py"), os.path.join(t, "qmp.sock"), os.path.join(t, "con.sock")], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env) + time.sleep(1.0); p.stdin.write(b"3 4 + .\r"); p.stdin.flush(); time.sleep(1.0); p.stdin.close() + out = p.stdout.read(); p.wait(timeout=10) + files = glob.glob(os.path.join(tr, "*.log")) + check("the visitor still sees the session: %r" % out[-40:], b"7 ok" in out) + check("one transcript for the session (%d)" % len(files), len(files) == 1) + if files: + f = files[0]; text = open(f, "rb").read() + check("named by its date, UTC: %s" % os.path.basename(f), os.path.basename(f)[:8].isdigit()) + check("it holds what was typed, as the machine echoed it, and what it printed", b"3 4 + ." in text and b"7 ok" in text) + check("readable by its writer only (mode %o)" % (os.stat(f).st_mode & 0o777), os.stat(f).st_mode & 0o777 == 0o600) + # a directory it cannot write: the session goes on, without a transcript + ro = os.path.join(t, "nowhere") + env2 = dict(os.environ, DEMO_TRANSCRIPTS=ro) + os.unlink(os.path.join(t, "qmp.sock")); os.unlink(os.path.join(t, "con.sock")) + qmp_server(os.path.join(t, "qmp.sock")); console_server(os.path.join(t, "con.sock")) + p = subprocess.Popen([sys.executable, "-I", os.path.join(HERE, "console.py"), os.path.join(t, "qmp.sock"), os.path.join(t, "con.sock")], + stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env2) + time.sleep(1.0); p.stdin.write(b"3 4 + .\r"); p.stdin.flush(); time.sleep(1.0); p.stdin.close() + out = p.stdout.read(); p.wait(timeout=10) + check("with no directory to write in, the session still works", b"7 ok" in out) + + # transcripts-tidy.sh: handed to the owner, the old deleted + box = os.path.join(t, "box"); os.mkdir(box) + new, old = os.path.join(box, "20261121-000000-1.log"), os.path.join(box, "20261101-000000-2.log") + for f in (new, old): open(f, "w").write("x"); os.chmod(f, 0o600) + eight_days = time.time() - 8 * 86400; os.utime(old, (eight_days, eight_days)) + r = subprocess.run(["bash", os.path.join(HERE, "transcripts-tidy.sh"), box, "nobody"], capture_output=True, text=True) + check("the tidy succeeds: %s" % (r.stderr.strip() or "ok"), r.returncode == 0) + check("a transcript older than 7 days is deleted", not os.path.exists(old)) + check("a newer one is kept", os.path.exists(new)) + if os.path.exists(new): + import pwd + st = os.stat(new) + check("and handed to the directory's owner (%s), mode 600" % pwd.getpwuid(st.st_uid).pw_name, + pwd.getpwuid(st.st_uid).pw_name == "nobody" and st.st_mode & 0o777 == 0o600) +print("RESULT: " + ("PASS" if ok else "FAIL")) +sys.exit(0 if ok else 1) diff --git a/demo/transcripts-tidy.sh b/demo/transcripts-tidy.sh new file mode 100755 index 00000000..fbaaa6b5 --- /dev/null +++ b/demo/transcripts-tidy.sh @@ -0,0 +1,12 @@ +#!/bin/bash +# SPDX-License-Identifier: LicenseRef-Starship-1.0 +# Copyright (c) 2023-2026 Robert A. James +# transcripts-tidy.sh DIR OWNER -- the demo's transcripts (#192): each handed to OWNER, mode 0600, so only OWNER +# reads it (the session's user wrote it, and cannot list the directory); every one older than 7 days deleted. +# Run as root by starshipos-demo-transcripts.timer every 5 minutes. Tests: demo/test_transcripts.py. +set -euo pipefail +dir=${1:?usage: transcripts-tidy.sh DIR OWNER} +owner=${2:?usage: transcripts-tidy.sh DIR OWNER} +[ -d "$dir" ] || { echo "transcripts-tidy: no $dir" >&2; exit 1; } +find "$dir" -maxdepth 1 -type f -name '*.log' -mmin +10080 -delete # 7 days +find "$dir" -maxdepth 1 -type f -name '*.log' ! -user "$owner" -exec chown "$owner:" {} + -exec chmod 0600 {} + diff --git a/demo/xwiki-try-it.xwiki b/demo/xwiki-try-it.xwiki index 73e9183d..24616ed4 100644 --- a/demo/xwiki-try-it.xwiki +++ b/demo/xwiki-try-it.xwiki @@ -13,7 +13,8 @@ This is a real StarshipOS machine, started just for you and already at its promp simulation of StarshipOS and not a recording: the console below is the serial console of an amd64 machine running the same image the project publishes every night. You are logged in as **Zuse**, the superuser -- of a machine that exists only for your session. There is nothing to -break, and nothing you do is kept. Close the page and it is gone. +break, and nothing you do is kept on it. Close the page and it is gone. A transcript of your +session -- what you type and what the machine answers -- is kept for up to 7 days, then deleted. {{html clean="false"}}