AI Engineering Workspace
PartialChat, files, terminal and preview in one place. Chat sessions run on a durable Postgres-backed queue (chat only: no tools or shell). Live models need an API key; the terminal is unavailable.
Project files
read-onlyapp/src/lib/tlsgate.ts
1import tls from "node:tls";2import { headers } from "next/headers";3import { resolveConfig } from "./config";45// Credentials are accepted only on a connection that is PROVABLY trusted. Three independent facts must hold:6// 1. the edge (which overwrites every x-zcs-* header, so clients cannot forge them) says this TLS session was served with the publicly trusted,7// unexpired certificate selected by SNI, and the Host header names that same SNI host;8// 2. the host is the explicitly configured canonical host (never merely derived from the request);9// 3. a real handshake against the edge for that host validates against the system CA store with hostname verification (cached briefly).10// This is objective evidence, not a typed attestation, and it never depends on the self-signed preview certificate.11type Verified = { ok: boolean; reason?: string; validTo?: string; issuer?: string };12const cache = new Map<string, { at: number; v: Verified }>();13const TTL = 5 * 60 * 1000;1415export function verifyChain(host: string, port = Number(process.env.STUDIO_HTTPS_PORT || 8443)): Promise<Verified> {16 const hit = cache.get(host + ":" + port); if (hit && Date.now() - hit.at < TTL) return Promise.resolve(hit.v);17 return new Promise(resolve => {18 let done = false; const fin = (v: Verified) => { if (done) return; done = true; if (v.ok) cache.set(host + ":" + port, { at: Date.now(), v }); resolve(v); };19 const s = tls.connect({ host: "127.0.0.1", port, servername: host, rejectUnauthorized: true, minVersion: "TLSv1.2" }, () => {20 const c = s.getPeerCertificate(); const left = Date.parse(c.valid_to) - Date.now();21 s.end(); fin(left > 24 * 3600 * 1000 ? { ok: true, validTo: c.valid_to, issuer: String((c.issuer as any)?.O ?? "") } : { ok: false, reason: "certificate expires within 24 hours" });22 });23 s.setTimeout(5000, () => { s.destroy(); fin({ ok: false, reason: "handshake timeout" }); }); s.on("error", e => fin({ ok: false, reason: "chain/hostname verification failed: " + e.message.slice(0, 80) }));24 });25}2627export async function trustedConnection(): Promise<{ ok: boolean; reason?: string; host?: string; validTo?: string }> {28 const h = await headers(); const no = (reason: string) => ({ ok: false, reason });29 if (h.get("x-zcs-proto") !== "https") return no("not HTTPS");30 if (h.get("x-zcs-cert") !== "trusted") return no("served with the self-signed preview certificate");31 const host = (h.get("host") || "").replace(/:\d+$/, "").toLowerCase(); const sni = h.get("x-zcs-sni") || "";32 if (!host || host !== sni) return no("Host does not match the TLS server name");33 const pub = (await resolveConfig()).PUBLIC_BASE_URL;34 if (pub.source === "derived" || !pub.value) return no("canonical public URL is not configured");35 let canon = ""; try { canon = new URL(pub.value).hostname.toLowerCase(); } catch { return no("invalid canonical URL"); }36 if (canon !== host) return no("not the canonical host");37 const v = await verifyChain(host); return v.ok ? { ok: true, host, validTo: v.validTo } : no(v.reason ?? "verification failed");38}39
Conversation
PartialSign in to start AI sessions.
Task checklist
- Application shell deployed
- Persistent projects
- Agent runtime
- Sandboxed terminal
Execution activity
No agent sessions. Git HEAD 692573d on master.
Approval requests
None pending.