@netnodeag/kraftwerk 0.105.0 → 0.105.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,17 @@
1
1
  import http from "node:http";
2
2
  import type { Duplex } from "node:stream";
3
+ /** A pass for the app `slug` of the workspace `workspace` (its slug here), as the path segment that carries it. */
4
+ export declare function vibeablePass(workspace: string, slug: string, now?: number): string;
5
+ /**
6
+ * Take the pass out of a preview path. `pathname` is the path without it
7
+ * either way; `valid` says whether it was there and holds for this app of
8
+ * this workspace, `slug` the app it names (undefined: not an app's path).
9
+ */
10
+ export declare function takeVibeablePass(workspace: string | undefined, pathname: string, now?: number): {
11
+ pathname: string;
12
+ slug?: string;
13
+ valid: boolean;
14
+ };
3
15
  /**
4
16
  * GET /vibeables/<slug>/<file>. Dot segments (.env, .git) are never served;
5
17
  * a directory answers with its index.html, and a directory url without the
@@ -1,3 +1,4 @@
1
+ import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
1
2
  import { createReadStream, promises as fs } from "node:fs";
2
3
  import http from "node:http";
3
4
  import net from "node:net";
@@ -58,7 +59,58 @@ const PREVIEW_HEADERS = {
58
59
  "cache-control": "no-store",
59
60
  "content-security-policy": VIBEABLE_CSP,
60
61
  "access-control-allow-origin": "*",
62
+ // A pass (below) is part of the address: it stays out of what the app tells other sites.
63
+ "referrer-policy": "same-origin",
61
64
  };
65
+ /* ---------- pass ---------- */
66
+ /**
67
+ * The sandboxed document has an opaque origin, so what it loads — its own
68
+ * style.css, app.js, images, fetch() — is a cross-site request to the
69
+ * browser: no SameSite cookie goes with it. Where a cookie is what lets the
70
+ * caller in (a signed-in user, a paired device, a member), the app's files
71
+ * would answer with the sign-in page and the app would show unstyled.
72
+ *
73
+ * So the first request, which does carry the cookie, is redirected to an
74
+ * address with a pass in it — /vibeables/<slug>/~<pass>/… — and the page's
75
+ * relative links stay under it. A pass opens one app's files of one
76
+ * workspace for PASS_TTL_MS and nothing else; it is signed with a secret
77
+ * this process made up, so none outlives a restart.
78
+ */
79
+ const PASS_TTL_MS = 12 * 60 * 60 * 1000;
80
+ const PASS_RE = /^~(\d{1,15})\.([A-Za-z0-9_-]{43})$/;
81
+ const passSecret = randomBytes(32);
82
+ const sign = (workspace, slug, exp) => createHmac("sha256", passSecret).update(`${workspace}\0${slug}\0${exp}`).digest("base64url");
83
+ /** A pass for the app `slug` of the workspace `workspace` (its slug here), as the path segment that carries it. */
84
+ export function vibeablePass(workspace, slug, now = Date.now()) {
85
+ const exp = now + PASS_TTL_MS;
86
+ return `~${exp}.${sign(workspace, slug, exp)}`;
87
+ }
88
+ /**
89
+ * Take the pass out of a preview path. `pathname` is the path without it
90
+ * either way; `valid` says whether it was there and holds for this app of
91
+ * this workspace, `slug` the app it names (undefined: not an app's path).
92
+ */
93
+ export function takeVibeablePass(workspace, pathname, now = Date.now()) {
94
+ const seg = pathname.split("/"); // ["", "vibeables", slug, maybe pass, ...rest]
95
+ let slug;
96
+ try {
97
+ slug = seg[2] ? safeVibeableSlug(decodeURIComponent(seg[2])) : undefined;
98
+ }
99
+ catch {
100
+ slug = undefined;
101
+ }
102
+ const m = PASS_RE.exec(seg[3] ?? "");
103
+ if (!m)
104
+ return { pathname, slug, valid: false };
105
+ seg.splice(3, 1);
106
+ const stripped = seg.length === 3 ? `${seg.join("/")}/` : seg.join("/");
107
+ const exp = Number(m[1]);
108
+ if (!slug || workspace === undefined || !(exp > now))
109
+ return { pathname: stripped, slug, valid: false };
110
+ const want = Buffer.from(sign(workspace, slug, exp));
111
+ const got = Buffer.from(m[2]);
112
+ return { pathname: stripped, slug, valid: want.length === got.length && timingSafeEqual(want, got) };
113
+ }
62
114
  /**
63
115
  * GET /vibeables/<slug>/<file>. Dot segments (.env, .git) are never served;
64
116
  * a directory answers with its index.html, and a directory url without the
@@ -8,8 +8,8 @@ import { stopMachineCloud } from "./cloud.js";
8
8
  import { disposeAllBackends, disposeWorkspaceBackends } from "../core/chat/sessions.js";
9
9
  import { markWorkspaceStopped, registerInstance, registerWorkspace, unregisterInstance } from "../core/instances.js";
10
10
  import { startGitSync, stopGitSync } from "../core/git.js";
11
- import { disposeAllDevs, disposeWorkspaceDevs } from "../core/vibeables.js";
12
- import { proxyUpgrade, serveVibeable } from "./preview.js";
11
+ import { disposeAllDevs, disposeWorkspaceDevs, safeVibeableSlug } from "../core/vibeables.js";
12
+ import { proxyUpgrade, serveVibeable, takeVibeablePass, vibeablePass } from "./preview.js";
13
13
  import { startRoutineScheduler, stopRoutineScheduler } from "../core/routines.js";
14
14
  import { startBoardWake, stopBoardWake } from "../core/board-wake.js";
15
15
  import { readWorkspaceDotenv } from "../core/env.js";
@@ -291,6 +291,14 @@ export async function startHub(opts) {
291
291
  return json(res, { error: `no workspace "${target.slug}" here` }, 404);
292
292
  const { url } = target;
293
293
  const prefix = prefixOf(req, target);
294
+ // An app's files under a pass (preview.ts): the pass is what lets the request in — the sandboxed page sends no cookie.
295
+ const isVibeable = url.pathname === "/vibeables" || url.pathname.startsWith("/vibeables/");
296
+ if (isVibeable) {
297
+ const pass = takeVibeablePass(open?.slug, url.pathname);
298
+ url.pathname = pass.pathname;
299
+ if (pass.valid && open)
300
+ return await runInHub(hub, () => open.ws.run(() => serveVibeable(req, res, url)));
301
+ }
294
302
  const cu = await cloudUsers(open, prefix);
295
303
  const gate = await withUser(req, await trustOf(req, url, trustOpts), cu);
296
304
  const { trust } = gate;
@@ -326,11 +334,27 @@ export async function startHub(opts) {
326
334
  return await run(() => apiRouter.handle(req, res, url, trust));
327
335
  }
328
336
  // /vibeables/<slug>/… is an app's own files, served for the preview pane — workspace data, so not for unpaired callers.
329
- if (url.pathname === "/vibeables" || url.pathname.startsWith("/vibeables/")) {
337
+ if (isVibeable) {
330
338
  if (trust.kind === "none")
331
339
  return json(res, { error: "pair this device first", pair: true }, 401);
332
340
  if (!open)
333
341
  return json(res, { error: "name a workspace: <slug>.localhost or /w/<slug>/" }, 404);
342
+ // Let in by a cookie (a signed-in user, a device, a member): on to the address with a pass, so the page's own files load too.
343
+ const app = url.pathname.split("/")[2];
344
+ if (app && (trust.kind !== "local" || trust.user) && (req.method === "GET" || req.method === "HEAD")) {
345
+ let slug;
346
+ try {
347
+ slug = safeVibeableSlug(decodeURIComponent(app));
348
+ }
349
+ catch {
350
+ /* not an app's name: serveVibeable says so */
351
+ }
352
+ if (slug) {
353
+ const rest = url.pathname.split("/").slice(3).join("/");
354
+ res.writeHead(302, { location: `${prefix}/vibeables/${encodeURIComponent(slug)}/${vibeablePass(open.slug, slug)}/${rest}${url.search}`, "cache-control": "no-store" });
355
+ return void res.end();
356
+ }
357
+ }
334
358
  return await run(() => serveVibeable(req, res, url));
335
359
  }
336
360
  // A daemon's own address lists its workspaces; anywhere else, the web UI's shell (public: an unpaired browser needs it to pair).
@@ -352,6 +376,15 @@ export async function startHub(opts) {
352
376
  if (target.slug !== undefined && !open)
353
377
  return void socket.end("HTTP/1.1 404 Not Found\r\nconnection: close\r\n\r\n");
354
378
  const { url } = target;
379
+ // A dev server's HMR socket under a pass (preview.ts), opened by the sandboxed page: no cookie comes with it.
380
+ if (url.pathname.startsWith("/vibeables/")) {
381
+ const pass = takeVibeablePass(open?.slug, url.pathname);
382
+ url.pathname = pass.pathname;
383
+ if (pass.valid && open) {
384
+ req.url = url.pathname + url.search;
385
+ return runInHub(hub, () => open.ws.run(() => proxyUpgrade(req, socket, head)));
386
+ }
387
+ }
355
388
  const cu = await cloudUsers(open, prefixOf(req, target));
356
389
  const gate = await withUser(req, await trustOf(req, url, trustOpts, true), cu);
357
390
  const { trust } = gate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netnodeag/kraftwerk",
3
- "version": "0.105.0",
3
+ "version": "0.105.1",
4
4
  "description": "Open-source agentic workspace for teams — where people and AI agents get work done together: agents (Claude Code, Codex, Pi), skills, shared knowledge, repeatable workflows, and deterministic verification",
5
5
  "keywords": [
6
6
  "agents",