Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: require hosted host join tokens (#5643)

Adam Eivy37aa976bc5c2
diff --git a/client/src/components/fableloom/LoomPlayPanel.jsx b/client/src/components/fableloom/LoomPlayPanel.jsx
index 37b96d95fa..697980669b 100644
--- a/client/src/components/fableloom/LoomPlayPanel.jsx
+++ b/client/src/components/fableloom/LoomPlayPanel.jsx
@@ -115,7 +115,7 @@ export default function LoomPlayPanel({ loom, episode: initialEpisode, onClose }
useEffect(() => {
if (!hostedSession?.id) return;
const socket = io('/fableloom-hosted', {
- auth: { sessionId: hostedSession.id, role: 'host' },
+ auth: { sessionId: hostedSession.id, token: hostedSession.token, role: 'host' },
transports: ['websocket', 'polling'],
});
hostedSocketRef.current = socket;
@@ -161,7 +161,7 @@ export default function LoomPlayPanel({ loom, episode: initialEpisode, onClose }
socket.disconnect();
hostedSocketRef.current = null;
};
- }, [hostedSession?.id]);
+ }, [hostedSession?.id, hostedSession?.token]);
// Sync playback phase & scene updates to hosted audience
useEffect(() => {
diff --git a/client/src/components/fableloom/LoomPlayPanel.test.jsx b/client/src/components/fableloom/LoomPlayPanel.test.jsx
index bacf6d169a..23d7de964a 100644
--- a/client/src/components/fableloom/LoomPlayPanel.test.jsx
+++ b/client/src/components/fableloom/LoomPlayPanel.test.jsx
@@ -4,8 +4,21 @@ import userEvent from '@testing-library/user-event';
vi.mock('../../services/api', () => ({ playLoomTurn: vi.fn() }));
vi.mock('../MediaImage', () => ({ default: (props) => <img alt="" {...props} /> }));
+vi.mock('socket.io-client', () => ({
+ io: vi.fn(() => ({ on: vi.fn(), emit: vi.fn(), disconnect: vi.fn() })),
+}));
+vi.mock('./LoomHostedSessionModal', () => ({
+ default: ({ onSessionCreated }) => (
+ <button type="button" onClick={() => onSessionCreated({ id: 'host-session' }, {
+ id: 'host-session', token: 'host-token', joinUrl: 'https://example.test/fableloom/join#session=host-session&token=host-token',
+ })}>
+ Start hosted session
+ </button>
+ ),
+}));
import { playLoomTurn } from '../../services/api';
+import { io } from 'socket.io-client';
import LoomPlayPanel from './LoomPlayPanel';
const loom = { id: 'loom-1', name: 'The Hollow Crown', episodes: [] };
@@ -31,6 +44,18 @@ const sendMessage = async (user, text) => {
beforeEach(() => vi.clearAllMocks());
describe('LoomPlayPanel', () => {
+ it('authenticates the host socket with the session token', async () => {
+ const user = userEvent.setup();
+ render(<LoomPlayPanel loom={loom} episode={episode} />);
+
+ await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
+ await user.click(screen.getByRole('button', { name: 'Start hosted session' }));
+
+ expect(io).toHaveBeenCalledWith('/fableloom-hosted', expect.objectContaining({
+ auth: { sessionId: 'host-session', token: 'host-token', role: 'host' },
+ }));
+ });
+
it('presents produced media as the main stage with the teleplay beside it', () => {
const onClose = vi.fn();
const producedEpisode = {
diff --git a/server/sockets/fableLoomHosted.js b/server/sockets/fableLoomHosted.js
index 76e1bbe405..842d0aad75 100644
--- a/server/sockets/fableLoomHosted.js
+++ b/server/sockets/fableLoomHosted.js
@@ -30,7 +30,8 @@ export function registerFableLoomHostedNamespace(io) {
const ns = io.of('/fableloom-hosted');
hostedNsInstance = ns;
- // Handshake authentication middleware for /fableloom-hosted
+ // `io.use(socketAuthGate)` only covers the default namespace, so every role
+ // here must authenticate with this session's join token.
ns.use(async (socket, next) => {
try {
const auth = socket.handshake.auth || {};
@@ -48,18 +49,15 @@ export function registerFableLoomHostedNamespace(io) {
return next(new Error('HOSTED_SESSION_NOT_FOUND_OR_EXPIRED'));
}
- if (role === 'host') {
- socket.hostedRole = 'host';
- socket.hostedSessionId = sessionId;
- return next();
- }
-
- // Audience role requires valid join token
if (!token || !verifyHostedToken(sessionId, token)) {
return next(new Error('HOSTED_SESSION_UNAUTHORIZED'));
}
- socket.hostedRole = 'audience';
+ if (role !== 'audience' && role !== 'host') {
+ return next(new Error('HOSTED_SESSION_ROLE_INVALID'));
+ }
+
+ socket.hostedRole = role;
socket.hostedSessionId = sessionId;
return next();
} catch (err) {
@@ -141,6 +139,7 @@ export function registerFableLoomHostedNamespace(io) {
// --- Audience actions ---
socket.on('hosted:mic:start', async () => {
+ if (socket.hostedRole !== 'audience') return;
micChunks = [];
try {
await startHostedListening(sessionId, { io });
@@ -150,6 +149,7 @@ export function registerFableLoomHostedNamespace(io) {
});
socket.on('hosted:mic:frame', (chunk) => {
+ if (socket.hostedRole !== 'audience') return;
if (!session || session.turnPhase !== 'listening') return;
if (chunk) {
micChunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
@@ -157,6 +157,7 @@ export function registerFableLoomHostedNamespace(io) {
});
socket.on('hosted:mic:stop', async (completeBuffer) => {
+ if (socket.hostedRole !== 'audience') return;
if (!session || session.turnPhase !== 'listening') return;
let finalAudio = null;
if (completeBuffer && (Buffer.isBuffer(completeBuffer) || ArrayBuffer.isView(completeBuffer))) {
@@ -174,6 +175,7 @@ export function registerFableLoomHostedNamespace(io) {
});
socket.on('hosted:turn:text', async (data) => {
+ if (socket.hostedRole !== 'audience') return;
if (!session || session.turnPhase !== 'listening') return;
try {
await processHostedUtterance(sessionId, { textMessage: data?.text, io });
diff --git a/server/sockets/fableLoomHosted.test.js b/server/sockets/fableLoomHosted.test.js
index 9999d64f08..9256344215 100644
--- a/server/sockets/fableLoomHosted.test.js
+++ b/server/sockets/fableLoomHosted.test.js
@@ -113,7 +113,7 @@ describe('fableLoomHosted Socket.IO namespace', () => {
expect(socket.hostedSessionId).toBe(session.id);
});
- it('allows host connection with valid sessionId', async () => {
+ it('rejects host connection without a token', async () => {
const { session } = await createHostedSession('loom-1', 'ep-1');
const socket = {
handshake: {
@@ -122,6 +122,33 @@ describe('fableLoomHosted Socket.IO namespace', () => {
};
const next = vi.fn();
await middleware(socket, next);
+ expect(next).toHaveBeenCalledWith(expect.any(Error));
+ expect(next.mock.calls[0][0].message).toBe('HOSTED_SESSION_UNAUTHORIZED');
+ });
+
+ it('rejects a host connection using another session token', async () => {
+ const { session } = await createHostedSession('loom-1', 'ep-1');
+ const { token: otherToken } = await createHostedSession('loom-1', 'ep-1');
+ const socket = {
+ handshake: {
+ auth: { sessionId: session.id, role: 'host', token: otherToken },
+ },
+ };
+ const next = vi.fn();
+ await middleware(socket, next);
+ expect(next).toHaveBeenCalledWith(expect.any(Error));
+ expect(next.mock.calls[0][0].message).toBe('HOSTED_SESSION_UNAUTHORIZED');
+ });
+
+ it('allows host connection with its session token', async () => {
+ const { session, token } = await createHostedSession('loom-1', 'ep-1');
+ const socket = {
+ handshake: {
+ auth: { sessionId: session.id, role: 'host', token },
+ },
+ };
+ const next = vi.fn();
+ await middleware(socket, next);
expect(next).toHaveBeenCalledWith();
expect(socket.hostedRole).toBe('host');
expect(socket.hostedSessionId).toBe(session.id);
@@ -129,6 +156,24 @@ describe('fableLoomHosted Socket.IO namespace', () => {
});
describe('socket event exchange', () => {
+ it('ignores audience mic controls from a host socket', async () => {
+ const { session } = await createHostedSession('loom-1', 'ep-1');
+ const listeners = {};
+ const socket = {
+ id: 'host-sock',
+ hostedRole: 'host',
+ hostedSessionId: session.id,
+ join: vi.fn(),
+ emit: vi.fn(),
+ on: vi.fn((event, fn) => { listeners[event] = fn; }),
+ removeAllListeners: vi.fn(),
+ };
+
+ connectionHandler(socket);
+ await listeners['hosted:mic:start']();
+ expect(getHostedSession(session.id).turnPhase).toBe('idle');
+ });
+
it('synchronizes session on connection and handles events', async () => {
const { session, token } = await createHostedSession('loom-1', 'ep-1');
const listeners = {};