fix(ws,client): prevent Safe Mode from sticking on WebSocket PTY input after toggling off
This commit is contained in:
parent
9e8347876c
commit
2cbcde6d46
7 changed files with 227 additions and 21 deletions
|
|
@ -253,6 +253,152 @@ describe('Phase 3: Persistent PTYManager & SFTP File Manager + Inline Diff', ()
|
|||
expect(ctx.server.ptyManager.getScrollback(session.sessionId)).toBeNull();
|
||||
});
|
||||
|
||||
it('unblocks WS pty:input immediately on the same socket after disabling Safe Mode (mobile initialSafeMode regression)', async () => {
|
||||
const ctx = await setupServerWithSsh();
|
||||
|
||||
const created = await ctx.api('/api/terminal/sessions', {
|
||||
method: 'POST',
|
||||
body: { serverId: 's1', cols: 80, rows: 24 },
|
||||
});
|
||||
expect(created.status).toBe(201);
|
||||
const session = created.data.session as { sessionId: string };
|
||||
|
||||
// 1. Simulate mobile login where session.safeMode starts true before WS connects
|
||||
expect(
|
||||
(await ctx.api('/api/auth/safe-mode', { method: 'POST', body: { enabled: true } })).status,
|
||||
).toBe(200);
|
||||
|
||||
const { ws: wsMobile, frames: fMobile } = await openWs(ctx.wsUrl, {
|
||||
Cookie: ctx.jar.header(),
|
||||
Host: `127.0.0.1:${ctx.server.port}`,
|
||||
Origin: ctx.baseUrl,
|
||||
});
|
||||
try {
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
const hello = fMobile.find((f) => f.type === 'hello');
|
||||
expect(hello?.safeMode).toBe(true);
|
||||
|
||||
wsMobile.send(JSON.stringify({ type: 'sub', channels: [`pty:${session.sessionId}`] }));
|
||||
wsMobile.send(
|
||||
JSON.stringify({
|
||||
type: 'pty:input',
|
||||
sessionId: session.sessionId,
|
||||
data: 'echo mobile-blocked\r',
|
||||
}),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 60));
|
||||
const blockedErrorsBefore = fMobile.filter(
|
||||
(f) => f.type === 'error' && f.code === 'SAFE_MODE_ACTIVE',
|
||||
).length;
|
||||
expect(blockedErrorsBefore).toBe(1);
|
||||
|
||||
// Reproduce old race: WS safe-mode:false arrives BEFORE POST /api/auth/safe-mode:false
|
||||
wsMobile.send(JSON.stringify({ type: 'safe-mode', enabled: false }));
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
expect(
|
||||
(await ctx.api('/api/auth/safe-mode', { method: 'POST', body: { enabled: false } }))
|
||||
.status,
|
||||
).toBe(200);
|
||||
|
||||
// Sending input on the exact same WS connection (no reconnect) must now succeed
|
||||
wsMobile.send(
|
||||
JSON.stringify({
|
||||
type: 'pty:input',
|
||||
sessionId: session.sessionId,
|
||||
data: 'echo after-mobile-toggle\r',
|
||||
}),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
|
||||
const output1 = fMobile
|
||||
.filter((f) => f.type === 'pty:output' && f.sessionId === session.sessionId)
|
||||
.map((f) => String(f.data))
|
||||
.join('');
|
||||
expect(output1).toContain('after-mobile-toggle');
|
||||
expect(
|
||||
fMobile.filter((f) => f.type === 'error' && f.code === 'SAFE_MODE_ACTIVE').length,
|
||||
).toBe(blockedErrorsBefore);
|
||||
} finally {
|
||||
wsMobile.close();
|
||||
}
|
||||
|
||||
// 2. Pure HTTP toggle-off while WS is connected (without sending WS safe-mode frame)
|
||||
expect(
|
||||
(await ctx.api('/api/auth/safe-mode', { method: 'POST', body: { enabled: true } })).status,
|
||||
).toBe(200);
|
||||
|
||||
const { ws: wsHttpOnly, frames: fHttpOnly } = await openWs(ctx.wsUrl, {
|
||||
Cookie: ctx.jar.header(),
|
||||
Host: `127.0.0.1:${ctx.server.port}`,
|
||||
Origin: ctx.baseUrl,
|
||||
});
|
||||
try {
|
||||
wsHttpOnly.send(JSON.stringify({ type: 'sub', channels: [`pty:${session.sessionId}`] }));
|
||||
wsHttpOnly.send(
|
||||
JSON.stringify({
|
||||
type: 'pty:input',
|
||||
sessionId: session.sessionId,
|
||||
data: 'echo blocked-before-http\r',
|
||||
}),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 60));
|
||||
const httpOnlyErrorsBefore = fHttpOnly.filter(
|
||||
(f) => f.type === 'error' && f.code === 'SAFE_MODE_ACTIVE',
|
||||
).length;
|
||||
expect(httpOnlyErrorsBefore).toBe(1);
|
||||
|
||||
expect(
|
||||
(await ctx.api('/api/auth/safe-mode', { method: 'POST', body: { enabled: false } }))
|
||||
.status,
|
||||
).toBe(200);
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
|
||||
wsHttpOnly.send(
|
||||
JSON.stringify({
|
||||
type: 'pty:input',
|
||||
sessionId: session.sessionId,
|
||||
data: 'echo after-http-only-toggle\r',
|
||||
}),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 100));
|
||||
|
||||
const output2 = fHttpOnly
|
||||
.filter((f) => f.type === 'pty:output' && f.sessionId === session.sessionId)
|
||||
.map((f) => String(f.data))
|
||||
.join('');
|
||||
expect(output2).toContain('after-http-only-toggle');
|
||||
expect(
|
||||
fHttpOnly.filter((f) => f.type === 'error' && f.code === 'SAFE_MODE_ACTIVE').length,
|
||||
).toBe(httpOnlyErrorsBefore);
|
||||
|
||||
// 3. Security invariant: WS safe-mode:false cannot bypass active server session Safe Mode
|
||||
expect(
|
||||
(await ctx.api('/api/auth/safe-mode', { method: 'POST', body: { enabled: true } }))
|
||||
.status,
|
||||
).toBe(200);
|
||||
wsHttpOnly.send(JSON.stringify({ type: 'safe-mode', enabled: false }));
|
||||
wsHttpOnly.send(
|
||||
JSON.stringify({
|
||||
type: 'pty:input',
|
||||
sessionId: session.sessionId,
|
||||
data: 'echo must-stay-blocked\r',
|
||||
}),
|
||||
);
|
||||
await new Promise((r) => setTimeout(r, 80));
|
||||
|
||||
expect(
|
||||
fHttpOnly.filter((f) => f.type === 'error' && f.code === 'SAFE_MODE_ACTIVE').length,
|
||||
).toBe(httpOnlyErrorsBefore + 1);
|
||||
const output3 = fHttpOnly
|
||||
.filter((f) => f.type === 'pty:output' && f.sessionId === session.sessionId)
|
||||
.map((f) => String(f.data))
|
||||
.join('');
|
||||
expect(output3).not.toContain('must-stay-blocked');
|
||||
} finally {
|
||||
wsHttpOnly.close();
|
||||
}
|
||||
});
|
||||
|
||||
it('performs SFTP list, streaming upload/download, mkdir, rename, chmod, atomic text edit with 409 conflict guard, and streamed folder archive', async () => {
|
||||
const ctx = await setupServerWithSsh();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue