castle-web-cli 0.4.151 → 0.4.152
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/serve.js +67 -12
- package/dist/serveSecurity.d.ts +4 -0
- package/dist/serveSecurity.js +31 -0
- package/package.json +1 -1
package/dist/serve.js
CHANGED
|
@@ -81,11 +81,12 @@ function listenLocalContentServer(upstreamHost, upstreamPort, role, security, re
|
|
|
81
81
|
});
|
|
82
82
|
server.on('upgrade', (req, socket, head) => {
|
|
83
83
|
const multiplayer = isDevMultiplayerUpgrade(req);
|
|
84
|
-
|
|
84
|
+
const legacyContent = isLegacyContentSocketUpgrade(req);
|
|
85
|
+
if (!isViteClientUpgrade(req) && !multiplayer && !legacyContent) {
|
|
85
86
|
socket.destroy();
|
|
86
87
|
return;
|
|
87
88
|
}
|
|
88
|
-
if (multiplayer && !isSameOriginUpgrade(req)) {
|
|
89
|
+
if ((multiplayer || legacyContent) && !isSameOriginUpgrade(req)) {
|
|
89
90
|
socket.destroy();
|
|
90
91
|
return;
|
|
91
92
|
}
|
|
@@ -162,6 +163,15 @@ function isDevMultiplayerUpgrade(req) {
|
|
|
162
163
|
return false;
|
|
163
164
|
}
|
|
164
165
|
}
|
|
166
|
+
const LEGACY_CONTENT_WS_PATH = '/__castle/ws-legacy';
|
|
167
|
+
function isLegacyContentSocketUpgrade(req) {
|
|
168
|
+
try {
|
|
169
|
+
return new URL(req.url || '/', 'http://localhost').pathname === LEGACY_CONTENT_WS_PATH;
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
165
175
|
async function startLocalContentServers(upstreamHost, upstreamPort, security, reserved) {
|
|
166
176
|
const servers = [];
|
|
167
177
|
const ports = {};
|
|
@@ -334,8 +344,23 @@ window.__castleShellOrigin=(function(){
|
|
|
334
344
|
res.end(JSON.stringify({ ticket: security.issueTicket(endpoint) }));
|
|
335
345
|
return;
|
|
336
346
|
}
|
|
347
|
+
// SDK <=0.4.16 discovers a direct socket here. Admit it through a
|
|
348
|
+
// one-use role-bound path so old editors keep saving while the play
|
|
349
|
+
// capability retains the same no-write boundary as the modern bridge.
|
|
350
|
+
if (reqPath === '/__castle/ws-port') {
|
|
351
|
+
if (!contentRole || req.method !== 'GET') {
|
|
352
|
+
res.writeHead(404).end();
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const ticket = security.issueLegacyContentTicket(contentRole);
|
|
356
|
+
res.writeHead(200, {
|
|
357
|
+
'content-type': 'application/json',
|
|
358
|
+
'cache-control': 'no-store',
|
|
359
|
+
});
|
|
360
|
+
res.end(JSON.stringify({ path: `${LEGACY_CONTENT_WS_PATH}?ticket=${ticket}` }));
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
337
363
|
if (reqPath === '/__castle/auth' ||
|
|
338
|
-
reqPath === '/__castle/ws-port' ||
|
|
339
364
|
reqPath === '/__castle/context') {
|
|
340
365
|
res.writeHead(404).end();
|
|
341
366
|
return;
|
|
@@ -478,6 +503,13 @@ const DEFAULT_PORT = 5757;
|
|
|
478
503
|
// them (an import listing goes on to the browser's castle-www host and its
|
|
479
504
|
// GraphQL scans); an unanswered id just sits in the map until this expires.
|
|
480
505
|
const PENDING_REQUEST_TTL_MS = 30_000;
|
|
506
|
+
const BRIDGED_CONTENT_MESSAGES = new Set([
|
|
507
|
+
'log',
|
|
508
|
+
'screenshot_response',
|
|
509
|
+
'write_file',
|
|
510
|
+
'castle_command',
|
|
511
|
+
]);
|
|
512
|
+
const LEGACY_CONTENT_MESSAGES = new Set([...BRIDGED_CONTENT_MESSAGES, 'ping']);
|
|
481
513
|
function readServeJson(projectDir) {
|
|
482
514
|
try {
|
|
483
515
|
const raw = fs.readFileSync(path.join(projectDir, '.castle', 'serve.json'), 'utf8');
|
|
@@ -938,29 +970,47 @@ async function serveDetached(projectDir, options) {
|
|
|
938
970
|
}
|
|
939
971
|
function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security, multiplayer) {
|
|
940
972
|
// The control socket is upgraded directly on Vite's HTTP server. Browser
|
|
941
|
-
// clients use the named path and a one-use ticket
|
|
973
|
+
// shell clients use the named path and a one-use ticket; pre-bridge SDKs use
|
|
974
|
+
// a separate ticket carrying their content role. The root path is accepted
|
|
942
975
|
// only from loopback clients with no Origin so older CLI versions can read
|
|
943
976
|
// wsPort from a new serve.json and continue to connect during rollout.
|
|
944
977
|
const wss = new WebSocketServer({
|
|
945
978
|
noServer: true,
|
|
946
979
|
maxPayload: 12 * 1024 * 1024,
|
|
947
980
|
});
|
|
981
|
+
const legacyRoles = new WeakMap();
|
|
948
982
|
const handleUpgrade = (req, socket, head) => {
|
|
949
983
|
const pathname = new URL(req.url || '/', 'http://localhost').pathname;
|
|
950
984
|
const hasOrigin = Boolean(req.headers.origin);
|
|
951
985
|
const isNamedPath = pathname === '/__castle/ws';
|
|
952
986
|
const isLegacyCliPath = pathname === '/' && !hasOrigin;
|
|
953
|
-
|
|
987
|
+
const isLegacyContentPath = pathname === LEGACY_CONTENT_WS_PATH;
|
|
988
|
+
if (!isNamedPath && !isLegacyCliPath && !isLegacyContentPath)
|
|
954
989
|
return false;
|
|
955
990
|
const loopback = req.socket.remoteAddress === '127.0.0.1' || req.socket.remoteAddress === '::1';
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
991
|
+
let legacyRole = null;
|
|
992
|
+
let authorized;
|
|
993
|
+
if (isLegacyContentPath) {
|
|
994
|
+
legacyRole = security.consumeLegacyContentTicket(req);
|
|
995
|
+
const assertedRole = security.verifyContentRequest(req);
|
|
996
|
+
authorized =
|
|
997
|
+
legacyRole !== null &&
|
|
998
|
+
(assertedRole === legacyRole || (assertedRole === null && isSameOriginUpgrade(req)));
|
|
999
|
+
}
|
|
1000
|
+
else {
|
|
1001
|
+
authorized = hasOrigin
|
|
1002
|
+
? isNamedPath && isSameOriginUpgrade(req) && security.consumeTicket(req, 'serve')
|
|
1003
|
+
: loopback;
|
|
1004
|
+
}
|
|
959
1005
|
if (!authorized) {
|
|
960
1006
|
socket.destroy();
|
|
961
1007
|
return true;
|
|
962
1008
|
}
|
|
963
|
-
wss.handleUpgrade(req, socket, head, (ws) =>
|
|
1009
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
1010
|
+
if (legacyRole)
|
|
1011
|
+
legacyRoles.set(ws, legacyRole);
|
|
1012
|
+
wss.emit('connection', ws, req);
|
|
1013
|
+
});
|
|
964
1014
|
return true;
|
|
965
1015
|
};
|
|
966
1016
|
const clients = new Set();
|
|
@@ -985,6 +1035,7 @@ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security
|
|
|
985
1035
|
wss.on('connection', (ws, req) => {
|
|
986
1036
|
clients.add(ws);
|
|
987
1037
|
const multiplayerUrl = devMultiplayerUrl(req);
|
|
1038
|
+
const legacyRole = legacyRoles.get(ws) ?? null;
|
|
988
1039
|
ws.on('message', (raw) => {
|
|
989
1040
|
try {
|
|
990
1041
|
const text = Array.isArray(raw)
|
|
@@ -996,6 +1047,8 @@ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security
|
|
|
996
1047
|
let bridgeRole = null;
|
|
997
1048
|
let bridgeContentOrigin = null;
|
|
998
1049
|
if (msg.type === 'deck_bridge') {
|
|
1050
|
+
if (legacyRole)
|
|
1051
|
+
return;
|
|
999
1052
|
if ((msg.role !== 'edit' && msg.role !== 'play') ||
|
|
1000
1053
|
!msg.message ||
|
|
1001
1054
|
typeof msg.message !== 'object')
|
|
@@ -1003,10 +1056,12 @@ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security
|
|
|
1003
1056
|
bridgeRole = msg.role;
|
|
1004
1057
|
bridgeContentOrigin = trustedBridgeContentOrigin(security, req, bridgeRole, msg.contentOrigin);
|
|
1005
1058
|
msg = msg.message;
|
|
1006
|
-
|
|
1007
|
-
if (!allowed.has(String(msg.type)))
|
|
1059
|
+
if (!BRIDGED_CONTENT_MESSAGES.has(String(msg.type)))
|
|
1008
1060
|
return;
|
|
1009
1061
|
}
|
|
1062
|
+
else if (legacyRole && !LEGACY_CONTENT_MESSAGES.has(String(msg.type))) {
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1010
1065
|
// Liveness. A client asks so it can tell a live connection from one that
|
|
1011
1066
|
// died without a close -- the state a sleeping laptop or a dropped
|
|
1012
1067
|
// network leaves behind, which `readyState` reports as OPEN forever.
|
|
@@ -1110,7 +1165,7 @@ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security
|
|
|
1110
1165
|
}
|
|
1111
1166
|
}
|
|
1112
1167
|
if (msg.type === 'write_file') {
|
|
1113
|
-
if (bridgeRole === 'play') {
|
|
1168
|
+
if ((bridgeRole ?? legacyRole) === 'play') {
|
|
1114
1169
|
ws.send(JSON.stringify({
|
|
1115
1170
|
type: 'write_file_response',
|
|
1116
1171
|
requestId: msg.requestId,
|
package/dist/serveSecurity.d.ts
CHANGED
|
@@ -25,7 +25,11 @@ export declare class ServeSecurity {
|
|
|
25
25
|
authorizeHttp(req: http.IncomingMessage): boolean;
|
|
26
26
|
rejectHttpAuth(res: http.ServerResponse): void;
|
|
27
27
|
issueTicket(endpoint: SocketEndpoint): string;
|
|
28
|
+
/** One-use admission for an SDK that predates the shell's MessagePort bridge. */
|
|
29
|
+
issueLegacyContentTicket(role: ContentRole): string;
|
|
28
30
|
consumeTicket(req: http.IncomingMessage, endpoint: SocketEndpoint): boolean;
|
|
31
|
+
/** Consume and recover the edit/play role bound to a legacy content ticket. */
|
|
32
|
+
consumeLegacyContentTicket(req: http.IncomingMessage): ContentRole | null;
|
|
29
33
|
private pruneTickets;
|
|
30
34
|
shellBootstrap(req: http.IncomingMessage): string;
|
|
31
35
|
contentOrigins(req: http.IncomingMessage): {
|
package/dist/serveSecurity.js
CHANGED
|
@@ -88,6 +88,17 @@ export class ServeSecurity {
|
|
|
88
88
|
this.tickets.set(ticket, { endpoint, expiresAt: Date.now() + 30_000 });
|
|
89
89
|
return ticket;
|
|
90
90
|
}
|
|
91
|
+
/** One-use admission for an SDK that predates the shell's MessagePort bridge. */
|
|
92
|
+
issueLegacyContentTicket(role) {
|
|
93
|
+
this.pruneTickets();
|
|
94
|
+
const ticket = randomBytes(16).toString('base64url');
|
|
95
|
+
this.tickets.set(ticket, {
|
|
96
|
+
endpoint: 'legacy-content',
|
|
97
|
+
role,
|
|
98
|
+
expiresAt: Date.now() + 30_000,
|
|
99
|
+
});
|
|
100
|
+
return ticket;
|
|
101
|
+
}
|
|
91
102
|
consumeTicket(req, endpoint) {
|
|
92
103
|
this.pruneTickets();
|
|
93
104
|
let ticket = '';
|
|
@@ -103,6 +114,26 @@ export class ServeSecurity {
|
|
|
103
114
|
this.tickets.delete(ticket);
|
|
104
115
|
return record.endpoint === endpoint && record.expiresAt > Date.now();
|
|
105
116
|
}
|
|
117
|
+
/** Consume and recover the edit/play role bound to a legacy content ticket. */
|
|
118
|
+
consumeLegacyContentTicket(req) {
|
|
119
|
+
this.pruneTickets();
|
|
120
|
+
let ticket = '';
|
|
121
|
+
try {
|
|
122
|
+
ticket = new URL(req.url || '/', 'http://localhost').searchParams.get('ticket') || '';
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const record = this.tickets.get(ticket);
|
|
128
|
+
if (!record)
|
|
129
|
+
return null;
|
|
130
|
+
this.tickets.delete(ticket);
|
|
131
|
+
return record.endpoint === 'legacy-content' &&
|
|
132
|
+
(record.role === 'edit' || record.role === 'play') &&
|
|
133
|
+
record.expiresAt > Date.now()
|
|
134
|
+
? record.role
|
|
135
|
+
: null;
|
|
136
|
+
}
|
|
106
137
|
pruneTickets() {
|
|
107
138
|
const now = Date.now();
|
|
108
139
|
for (const [ticket, record] of this.tickets) {
|