castle-web-cli 0.4.139 → 0.4.140

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.
package/dist/agent.js CHANGED
@@ -16,6 +16,7 @@
16
16
  // --force. Claude support can slot in later behind runAgentCli.
17
17
  import { execFileSync, spawn } from 'child_process';
18
18
  import { IMPORTS_DIR } from './imports.js';
19
+ import { isSameOriginUpgrade } from './wsOrigin.js';
19
20
  import * as fs from 'fs';
20
21
  import * as os from 'os';
21
22
  import * as path from 'path';
@@ -4170,6 +4171,14 @@ export function createAgentServer(opts) {
4170
4171
  const url = new URL(req.url ?? '/', 'http://localhost');
4171
4172
  if (url.pathname !== AGENT_WS_PATH)
4172
4173
  return false;
4174
+ // The agent socket drives the coding agent (exec, file writes) — reject any upgrade
4175
+ // that isn't same-origin (a co-tenant reaching this port directly sends no browser
4176
+ // Origin). Consumed either way so a rejected upgrade never falls through.
4177
+ if (!isSameOriginUpgrade(req)) {
4178
+ socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
4179
+ socket.destroy();
4180
+ return true;
4181
+ }
4173
4182
  wss.handleUpgrade(req, socket, head, (ws) => attachClient(ws));
4174
4183
  return true;
4175
4184
  }
package/dist/ide.js CHANGED
@@ -12,6 +12,7 @@ import picomatch from 'picomatch';
12
12
  import { fileURLToPath } from 'url';
13
13
  import { createRequire } from 'module';
14
14
  import { spawn as spawnPty } from '@lydell/node-pty';
15
+ import { isSameOriginUpgrade } from './wsOrigin.js';
15
16
  import headlessPkg from '@xterm/headless';
16
17
  import { SerializeAddon } from '@xterm/addon-serialize';
17
18
  import { WebSocketServer } from 'ws';
@@ -1107,6 +1108,14 @@ export function createIdeServer(opts) {
1107
1108
  const url = new URL(req.url ?? '/', 'http://localhost');
1108
1109
  if (url.pathname !== PTY_WS_PATH)
1109
1110
  return false;
1111
+ // The PTY is a root login shell — reject any upgrade that isn't same-origin (a
1112
+ // co-tenant reaching this port directly sends no browser Origin). Consumed either
1113
+ // way so a rejected upgrade never falls through to another handler.
1114
+ if (!isSameOriginUpgrade(req)) {
1115
+ socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
1116
+ socket.destroy();
1117
+ return true;
1118
+ }
1110
1119
  wss.handleUpgrade(req, socket, head, (ws) => attachClient(ws));
1111
1120
  return true;
1112
1121
  }
package/dist/serve.js CHANGED
@@ -551,7 +551,11 @@ async function serveDetached(projectDir, options) {
551
551
  throw new Error(`Detached serve did not write ${serveJsonPath} within 15s; check ${logPath} for startup errors.`);
552
552
  }
553
553
  function startWSServer(port, projectDir, logFile, screenshotsDir, viteHolder) {
554
- const wss = new WebSocketServer({ port });
554
+ // Bind to loopback: the only reachers are same-container callers — the Vite proxy
555
+ // (`/__castle/ws` -> `ws://localhost:${port}`) and node-side CLI clients that dial
556
+ // `ws://localhost:${port}`. Binding 0.0.0.0 also exposed this control channel to
557
+ // co-tenant containers across the shared Docker bridge.
558
+ const wss = new WebSocketServer({ port, host: '127.0.0.1' });
555
559
  const clients = new Set();
556
560
  const broadcast = (msg) => {
557
561
  const data = JSON.stringify(msg);
@@ -0,0 +1,2 @@
1
+ import type { IncomingMessage } from 'http';
2
+ export declare function isSameOriginUpgrade(req: IncomingMessage): boolean;
@@ -0,0 +1,20 @@
1
+ // A WebSocket upgrade is same-origin when the browser-sent `Origin`'s host matches the
2
+ // `Host` the request arrived on. Browsers ALWAYS attach `Origin` to WS handshakes, so a
3
+ // missing (or foreign) Origin means a non-browser caller — e.g. a co-tenant container
4
+ // reaching this dev-server port directly across the shared Docker bridge, bypassing the
5
+ // sandbox proxy. The proxy applies the same guard at the edge; this is the in-container
6
+ // defense-in-depth for callers that skip it. Only browser clients (the shell's terminal
7
+ // and agent panels, loaded from `window.location.host`) legitimately open these sockets,
8
+ // so rejecting a missing Origin is safe.
9
+ export function isSameOriginUpgrade(req) {
10
+ const origin = req.headers.origin;
11
+ const host = req.headers.host;
12
+ if (!origin || !host)
13
+ return false;
14
+ try {
15
+ return new URL(origin).host === host;
16
+ }
17
+ catch {
18
+ return false;
19
+ }
20
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.139",
3
+ "version": "0.4.140",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "castle-web": "./dist/index.js"