forge-cw 0.1.20 → 0.1.21

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.
Files changed (2) hide show
  1. package/dist/index.js +96 -18
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -469,25 +469,81 @@ var init_action_params = __esm({
469
469
  });
470
470
 
471
471
  // ../core/dist/auth.js
472
+ import { createHash, timingSafeEqual } from "node:crypto";
473
+ function sameToken(expected, provided) {
474
+ if (!provided)
475
+ return false;
476
+ const a = createHash("sha256").update(expected).digest();
477
+ const b = createHash("sha256").update(provided).digest();
478
+ return timingSafeEqual(a, b);
479
+ }
480
+ function cookieValue(cookieHeader, name) {
481
+ if (!cookieHeader)
482
+ return void 0;
483
+ for (const part of cookieHeader.split(";")) {
484
+ const eq = part.indexOf("=");
485
+ if (eq === -1)
486
+ continue;
487
+ if (part.slice(0, eq).trim() === name) {
488
+ try {
489
+ return decodeURIComponent(part.slice(eq + 1).trim());
490
+ } catch {
491
+ return void 0;
492
+ }
493
+ }
494
+ }
495
+ return void 0;
496
+ }
497
+ function isAuthorized(token, authorization, cookieHeader) {
498
+ if (authorization?.startsWith("Bearer ") && sameToken(token, authorization.slice(7)))
499
+ return true;
500
+ return sameToken(token, cookieValue(cookieHeader, AUTH_COOKIE));
501
+ }
472
502
  function bearerAuth(token) {
473
503
  return async (c, next) => {
474
504
  if (c.req.path === "/api/health") {
475
505
  return next();
476
506
  }
477
- const authHeader = c.req.header("Authorization");
478
- if (!authHeader || !authHeader.startsWith("Bearer ")) {
479
- return c.json({ error: "Unauthorized" }, 401);
480
- }
481
- const provided = authHeader.slice(7);
482
- if (provided !== token) {
507
+ if (!isAuthorized(token, c.req.header("Authorization"), c.req.header("Cookie"))) {
483
508
  return c.json({ error: "Unauthorized" }, 401);
484
509
  }
485
510
  return next();
486
511
  };
487
512
  }
513
+ function mountAuthRoute(app, token) {
514
+ app.get("/auth", (c) => {
515
+ if (!sameToken(token, c.req.query("token"))) {
516
+ return c.text("Wrong or missing token. Open the link Forge printed when it started.", 401);
517
+ }
518
+ const https = new URL(c.req.url).protocol === "https:" || c.req.header("X-Forwarded-Proto") === "https";
519
+ const cookie = [
520
+ `${AUTH_COOKIE}=${encodeURIComponent(token)}`,
521
+ "Path=/",
522
+ "HttpOnly",
523
+ "SameSite=Strict",
524
+ "Max-Age=2592000",
525
+ ...https ? ["Secure"] : []
526
+ ].join("; ");
527
+ c.header("Set-Cookie", cookie);
528
+ c.header("Cache-Control", "no-store");
529
+ return c.redirect("/", 302);
530
+ });
531
+ }
532
+ function remoteAuthProblem(localOnly, token) {
533
+ if (localOnly)
534
+ return null;
535
+ if (!token)
536
+ return "Forge would be reachable from other machines (FORGE_HOST or team mode) and needs FORGE_AUTH_TOKEN. Set one, for example: FORGE_AUTH_TOKEN=$(openssl rand -hex 24)";
537
+ if (token.length < MIN_TOKEN_LENGTH)
538
+ return `FORGE_AUTH_TOKEN must be at least ${MIN_TOKEN_LENGTH} characters, for example: FORGE_AUTH_TOKEN=$(openssl rand -hex 24)`;
539
+ return null;
540
+ }
541
+ var AUTH_COOKIE, MIN_TOKEN_LENGTH;
488
542
  var init_auth = __esm({
489
543
  "../core/dist/auth.js"() {
490
544
  "use strict";
545
+ AUTH_COOKIE = "forge_token";
546
+ MIN_TOKEN_LENGTH = 16;
491
547
  }
492
548
  });
493
549
 
@@ -1354,7 +1410,7 @@ var init_usage_window = __esm({
1354
1410
 
1355
1411
  // ../core/dist/usage-claude.js
1356
1412
  import { execFile as execFile3 } from "node:child_process";
1357
- import { createHash } from "node:crypto";
1413
+ import { createHash as createHash2 } from "node:crypto";
1358
1414
  import { readFileSync as readFileSync4 } from "node:fs";
1359
1415
  import { homedir } from "node:os";
1360
1416
  import { join as join5 } from "node:path";
@@ -1389,7 +1445,7 @@ function claudeKeychainService(configDir) {
1389
1445
  const dir = configDir.replace(/\/+$/, "");
1390
1446
  if (dir === join5(homedir(), ".claude"))
1391
1447
  return KEYCHAIN_SERVICE;
1392
- return `${KEYCHAIN_SERVICE}-${createHash("sha256").update(dir).digest("hex").slice(0, 8)}`;
1448
+ return `${KEYCHAIN_SERVICE}-${createHash2("sha256").update(dir).digest("hex").slice(0, 8)}`;
1393
1449
  }
1394
1450
  async function readFromKeychain(service) {
1395
1451
  try {
@@ -3901,7 +3957,7 @@ function takeSession(reader, project, sessionDir, general) {
3901
3957
  const known = general?.get(sessionId);
3902
3958
  return known ? { session: known, isNew: true } : null;
3903
3959
  }
3904
- function createTerminalWss(manager, reader, { localOnly = true, general } = {}) {
3960
+ function createTerminalWss(manager, reader, { localOnly = true, authToken, general } = {}) {
3905
3961
  const wss = new WebSocketServer({ noServer: true });
3906
3962
  function wireWs(ws, sessionId, project, sessionDir) {
3907
3963
  const client = {
@@ -3970,6 +4026,10 @@ function createTerminalWss(manager, reader, { localOnly = true, general } = {})
3970
4026
  socket.destroy();
3971
4027
  return;
3972
4028
  }
4029
+ if (authToken && !isAuthorized(authToken, request.headers.authorization, request.headers.cookie)) {
4030
+ socket.end("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n");
4031
+ return;
4032
+ }
3973
4033
  const url = new URL(request.url ?? "/", "http://localhost");
3974
4034
  const parsed = parseTerminalUpgradeUrl(url.pathname);
3975
4035
  if (parsed) {
@@ -4004,6 +4064,7 @@ var init_pty_routes = __esm({
4004
4064
  "use strict";
4005
4065
  init_cw_routes();
4006
4066
  init_origin_guard();
4067
+ init_auth();
4007
4068
  REFUSED_CLOSE_CODE = 4404;
4008
4069
  }
4009
4070
  });
@@ -4267,7 +4328,7 @@ var init_liveframe_routes = __esm({
4267
4328
 
4268
4329
  // ../core/dist/cw-install.js
4269
4330
  import { execFile as execFile5 } from "node:child_process";
4270
- import { createHash as createHash2 } from "node:crypto";
4331
+ import { createHash as createHash3 } from "node:crypto";
4271
4332
  import { accessSync as accessSync2, constants as constants2, existsSync as existsSync11, readFileSync as readFileSync9, statSync as statSync3, writeFileSync as writeFileSync4 } from "node:fs";
4272
4333
  import { delimiter as delimiter2, join as join12 } from "node:path";
4273
4334
  function readCwVersion(file) {
@@ -4384,7 +4445,7 @@ var init_cw_install = __esm({
4384
4445
  MARKER_FILE = ".forge-cw.json";
4385
4446
  INSTALL_TIMEOUT_MS2 = 12e4;
4386
4447
  ANSI_RE3 = /\u001b\[[0-9;?]*[A-Za-z]/g;
4387
- sha256Of = (file) => createHash2("sha256").update(readFileSync9(file)).digest("hex");
4448
+ sha256Of = (file) => createHash3("sha256").update(readFileSync9(file)).digest("hex");
4388
4449
  runInstall = (script, args, env) => new Promise((resolve11) => {
4389
4450
  execFile5("bash", [script, ...args], { env, timeout: INSTALL_TIMEOUT_MS2, maxBuffer: 1024 * 1024 }, (err, stdout, stderr) => {
4390
4451
  const output = `${String(stdout)}${String(stderr)}`;
@@ -7819,9 +7880,9 @@ var init_module_host = __esm({
7819
7880
  });
7820
7881
 
7821
7882
  // ../core/dist/module-confirmations.js
7822
- import { createHash as createHash3, randomBytes } from "node:crypto";
7883
+ import { createHash as createHash4, randomBytes } from "node:crypto";
7823
7884
  function inputHash(input) {
7824
- return createHash3("sha256").update(stable(input)).digest("hex");
7885
+ return createHash4("sha256").update(stable(input)).digest("hex");
7825
7886
  }
7826
7887
  function stable(value) {
7827
7888
  if (Array.isArray(value))
@@ -8491,6 +8552,9 @@ import { join as join18, basename as basename5, resolve as resolve10 } from "nod
8491
8552
  import { readdirSync as readdirSync8, existsSync as existsSync15 } from "node:fs";
8492
8553
  function createForgeServer(options) {
8493
8554
  const { dataDir, db: externalDb, authToken, localOnly = true } = options;
8555
+ const authProblem = remoteAuthProblem(localOnly, authToken);
8556
+ if (authProblem)
8557
+ throw new Error(authProblem);
8494
8558
  const dbPath = join18(dataDir, "forge.db");
8495
8559
  const modulesDir = join18(dataDir, "modules");
8496
8560
  const db = externalDb ?? new ForgeDB(dbPath);
@@ -8506,6 +8570,7 @@ function createForgeServer(options) {
8506
8570
  });
8507
8571
  if (authToken) {
8508
8572
  app.use("/api/*", bearerAuth(authToken));
8573
+ mountAuthRoute(app, authToken);
8509
8574
  }
8510
8575
  const cwReader = new CWReader(options.cwDir);
8511
8576
  const loginManager = new LoginManager(resolveCwBin(cwReader.cwHome));
@@ -8531,7 +8596,7 @@ function createForgeServer(options) {
8531
8596
  });
8532
8597
  const ptyManager = new PTYManager(states);
8533
8598
  const generalSessions = new GeneralSessions(join18(dataDir, "general-sessions.json"));
8534
- const terminalWss = createTerminalWss(ptyManager, cwReader, { localOnly, general: generalSessions });
8599
+ const terminalWss = createTerminalWss(ptyManager, cwReader, { localOnly, authToken, general: generalSessions });
8535
8600
  app.route("/api/liveframe", liveframeRoutes());
8536
8601
  const kioku = new Kioku({ projects: () => cwReader.getProjects(), pluginInstalls: () => installsOf(cwReader, "kioku") });
8537
8602
  app.route("/api/kioku", kiokuRoutes(kioku, {
@@ -9054,6 +9119,7 @@ var init_port_in_use = __esm({
9054
9119
  var dist_exports = {};
9055
9120
  __export(dist_exports, {
9056
9121
  ACCOUNT_NAME_RE: () => ACCOUNT_NAME_RE,
9122
+ AUTH_COOKIE: () => AUTH_COOKIE,
9057
9123
  ActionRunner: () => ActionRunner,
9058
9124
  CWReader: () => CWReader,
9059
9125
  ForgeDB: () => ForgeDB,
@@ -9079,6 +9145,7 @@ __export(dist_exports, {
9079
9145
  ensureForgeDir: () => ensureForgeDir,
9080
9146
  getForgeDir: () => getForgeDir,
9081
9147
  installArgs: () => installArgs,
9148
+ isAuthorized: () => isAuthorized,
9082
9149
  kiokuRoutes: () => kiokuRoutes,
9083
9150
  liveframeRoutes: () => liveframeRoutes,
9084
9151
  pathWithCw: () => pathWithCw,
@@ -9087,6 +9154,7 @@ __export(dist_exports, {
9087
9154
  probePort: () => probePort,
9088
9155
  quietSqliteWarning: () => quietSqliteWarning,
9089
9156
  rememberPrompt: () => rememberPrompt,
9157
+ remoteAuthProblem: () => remoteAuthProblem,
9090
9158
  resolveKiokuBin: () => resolveKiokuBin,
9091
9159
  resolveListenOptions: () => resolveListenOptions,
9092
9160
  skillRoutes: () => skillRoutes,
@@ -9144,7 +9212,7 @@ var consoleDist = isCheckout ? join20(packageRoot, "../console/dist") : join20(p
9144
9212
  var forgeVersion = JSON.parse(readFileSync13(join20(packageRoot, "package.json"), "utf-8")).version;
9145
9213
  var bundledModulesDir = isCheckout ? join20(packageRoot, "../../modules") : join20(packageRoot, "modules");
9146
9214
  async function main() {
9147
- const { ensureForgeDir: ensureForgeDir2, createForgeServer: createForgeServer2, createDatabase: createDatabase2, resolveListenOptions: resolveListenOptions2, ensureCw: ensureCw2, pathWithCw: pathWithCw2, quietSqliteWarning: quietSqliteWarning2, waitForListening: waitForListening2, probePort: probePort2, portInUseMessage: portInUseMessage2 } = await Promise.resolve().then(() => (init_dist2(), dist_exports));
9215
+ const { ensureForgeDir: ensureForgeDir2, createForgeServer: createForgeServer2, createDatabase: createDatabase2, resolveListenOptions: resolveListenOptions2, remoteAuthProblem: remoteAuthProblem2, ensureCw: ensureCw2, pathWithCw: pathWithCw2, quietSqliteWarning: quietSqliteWarning2, waitForListening: waitForListening2, probePort: probePort2, portInUseMessage: portInUseMessage2 } = await Promise.resolve().then(() => (init_dist2(), dist_exports));
9148
9216
  quietSqliteWarning2();
9149
9217
  const args = parseArgs(process.argv.slice(2));
9150
9218
  const { forgeDir, created } = ensureForgeDir2();
@@ -9162,6 +9230,14 @@ async function main() {
9162
9230
  const authToken = process.env.FORGE_AUTH_TOKEN;
9163
9231
  const isTeam = !!dbUrl;
9164
9232
  const { host, localOnly } = resolveListenOptions2(isTeam);
9233
+ const authProblem = remoteAuthProblem2(localOnly, authToken);
9234
+ if (authProblem) {
9235
+ console.log(`
9236
+ ${authProblem}
9237
+ `);
9238
+ process.exit(1);
9239
+ }
9240
+ const remoteToken = localOnly ? void 0 : authToken;
9165
9241
  const db = await createDatabase2({
9166
9242
  mode: isTeam ? "team" : "local",
9167
9243
  dataDir: forgeDir,
@@ -9171,7 +9247,7 @@ async function main() {
9171
9247
  dataDir: forgeDir,
9172
9248
  port,
9173
9249
  db,
9174
- authToken: isTeam ? authToken : void 0,
9250
+ authToken: remoteToken,
9175
9251
  localOnly,
9176
9252
  bundledModulesDir,
9177
9253
  version: forgeVersion
@@ -9205,7 +9281,9 @@ async function main() {
9205
9281
  console.log(`
9206
9282
  Forge Console running at http://localhost:${port}
9207
9283
  Mode: ${isTeam ? "team (PostgreSQL)" : "local (SQLite)"}
9208
- ${authToken ? " Auth: bearer token required\n" : ""}
9284
+ ${remoteToken ? ` Auth: token required. Browsers sign in once at http://<this host>:${port}/auth?token=<FORGE_AUTH_TOKEN>;
9285
+ the CLI and scripts send Authorization: Bearer <FORGE_AUTH_TOKEN>
9286
+ ` : ""}
9209
9287
  Dashboard: http://localhost:${port}
9210
9288
  API: http://localhost:${port}/api/health
9211
9289
 
@@ -9214,7 +9292,7 @@ ${authToken ? " Auth: bearer token required\n" : ""}
9214
9292
  if (args.open && process.env.FORGE_NO_OPEN !== "1") {
9215
9293
  try {
9216
9294
  const open = (await import("open")).default;
9217
- await open(`http://localhost:${port}`);
9295
+ await open(remoteToken ? `http://localhost:${port}/auth?token=${encodeURIComponent(remoteToken)}` : `http://localhost:${port}`);
9218
9296
  } catch {
9219
9297
  }
9220
9298
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "forge-cw",
3
- "version": "0.1.20",
3
+ "version": "0.1.21",
4
4
  "description": "A visual dashboard for your coding agents on Claude Code, Codex, Pi or OpenCode. Installs CW for you.",
5
5
  "license": "MIT",
6
6
  "type": "module",