cast's review server checks host, origin, and input, and writes atomically
Any web page could approve a workflow through a cross-site POST, and a rebound host could read the state. The server now takes only a loopback Host and its own Origin, caps bodies at 1 MB, and rejects malformed workflows and decisions with a 400. state.write replaces files through a temp file, so the page never reads a half-written run.json. The checks follow claudeweaver's server. Co-Authored-By: Claude Opus 5.5
@@ -3,10 +3,12 @@ import json
33 import pathlib
44 import sys
55
6-from state import latest, read, versions
6+from state import latest, read, versions, write
77
88 root = pathlib.Path(sys.argv[1]).resolve()
99 page = pathlib.Path(__file__).with_name("review.html")
10+actions = {"approve", "refine", "discard", "pause", "continue", "cancel"}
11+modes = {"all", "step", "task"}
1012
1113
1214 def state():
@@ -23,19 +25,47 @@ def state():
2325 }
2426
2527
28+def valid_workflow(wf):
29+ return isinstance(wf, dict) and isinstance(wf.get("tasks"), list) and len(wf["tasks"]) > 0 and all(
30+ isinstance(t, dict) and isinstance(t.get("id"), str) and isinstance(t.get("step"), int) for t in wf["tasks"]
31+ )
32+
33+
34+def valid_decision(d):
35+ return isinstance(d, dict) and d.get("action") in actions and d.get("mode", "all") in modes and isinstance(d.get("feedback", ""), str)
36+
37+
2638 class Handler(http.server.BaseHTTPRequestHandler):
2739 def send(self, code, body, kind="application/json"):
2840 data = body if isinstance(body, bytes) else json.dumps(body).encode()
2941 self.send_response(code)
3042 self.send_header("Content-Type", kind)
3143 self.send_header("Cache-Control", "no-store")
44+ self.send_header("X-Content-Type-Options", "nosniff")
3245 self.end_headers()
3346 self.wfile.write(data)
3447
48+ def allowed(self):
49+ port = self.server.server_address[1]
50+ hosts = {f"127.0.0.1:{port}", f"localhost:{port}"}
51+ origin = self.headers.get("Origin")
52+ if self.headers.get("Host") in hosts and (origin is None or origin.removeprefix("http://") in hosts):
53+ return True
54+ self.send(403, {"error": "forbidden host or origin"})
55+ return False
56+
3557 def body(self):
36- return json.loads(self.rfile.read(int(self.headers.get("Content-Length", 0))) or b"null")
58+ size = int(self.headers.get("Content-Length") or 0)
59+ if size > 1 << 20:
60+ return None
61+ try:
62+ return json.loads(self.rfile.read(size))
63+ except ValueError:
64+ return None
3765
3866 def do_GET(self):
67+ if not self.allowed():
68+ return
3969 if self.path == "/":
4070 return self.send(200, page.read_bytes(), "text/html; charset=utf-8")
4171 if self.path == "/state":
@@ -47,16 +77,28 @@ class Handler(http.server.BaseHTTPRequestHandler):
4777 self.send(404, {"error": "not found"})
4878
4979 def do_PUT(self):
80+ if not self.allowed():
81+ return
82+ if self.path != "/workflow":
83+ return self.send(404, {"error": "not found"})
5084 s = state()
51- if self.path != "/workflow" or s["run"]:
85+ if s["run"]:
5286 return self.send(409, {"error": "this version already ran, refine it into a new version"})
53- (root / f"v{s['version']}.json").write_text(json.dumps(self.body(), indent=2))
87+ wf = self.body()
88+ if not valid_workflow(wf):
89+ return self.send(400, {"error": "a workflow needs a list of tasks, each with a string id and an integer step"})
90+ write(root / f"v{s['version']}.json", wf)
5491 self.send(200, {"ok": True})
5592
5693 def do_POST(self):
94+ if not self.allowed():
95+ return
5796 if self.path != "/decide":
5897 return self.send(404, {"error": "not found"})
59- (root / "decision.json").write_text(json.dumps(self.body()))
98+ d = self.body()
99+ if not valid_decision(d):
100+ return self.send(400, {"error": f"action must be one of {sorted(actions)}, and mode one of {sorted(modes)}"})
101+ write(root / "decision.json", d)
60102 self.send(200, {"ok": True})
61103
62104 def log_message(self, *args):
@@ -1,7 +1,9 @@
11 import datetime
22 import json
3+import os
34 import pathlib
45 import re
6+import tempfile
57
68
79 def versions(root):
@@ -14,7 +16,10 @@ def read(path):
1416
1517 def write(path, data):
1618 path.parent.mkdir(parents=True, exist_ok=True)
17- path.write_text(json.dumps(data, indent=2))
19+ fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.")
20+ with os.fdopen(fd, "w") as f:
21+ json.dump(data, f, indent=2)
22+ os.replace(tmp, path)
1823
1924
2025 def latest(root):