diff --git a/CONFORMANCE.md b/CONFORMANCE.md index fa2df5f..6102296 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -44,6 +44,26 @@ Adopts the covenant scale (LBTAS, v2 floor) and carries its own record instance. The reference economy. Its operator/federation model is the named reference for the substrate's frontend-as-operator design target. +Private application chat uses the verified backend account `userId` as an +immutable conversation owner. Conversation/message routes and attachment +downloads enforce that boundary; shared integration keys supply no participant +identity. Legacy ownerless rows remain inaccessible pending explicit operator +assignment. Bindings: `backend/middleware/chatAuth.js`, +`conversationOwner.js`, `attachmentOwner.js`, the conditional conversation +model writes, and `backend/__tests__/conversationAccess.test.js`. This does not +promote private chat to a sealed commons dialog or change the in-review ledger +claims above. SSE uses the same verified owner identity before stream headers, +implemented in `backend/lib/conversationStreams.js` and checked by +`backend/__tests__/conversationStreams.test.js`. The unrestricted global feed +is retired. Streams expire with the account token and close on local parent +deletion after retryable message cleanup. Shared request/capacity guards bind +to the verified account; Redis is required across replicas. Bindings include +`backend/middleware/streamConcurrency.js`, `backend/__tests__/streamCapacity.test.js` +and the optional real-Redis `streamSharedCapacity.test.js`. Shared capacity does +not provide cross-replica event delivery or deletion invalidation. See +`backend/docs/conversation-ownership.md` for migration and client +integration limits. + The application message API supports optional retry receipts, scoped to the authenticated caller and conversation. These are private operational metadata, not sealed dialog or commons entries; they store hashes and message references, @@ -68,14 +88,3 @@ binding changes, and the in-review statuses above remain in review. dates, adjacent-crop conflicts, resize impact, portable round-tripping, invalid imports, and storage errors. This is feature verification, not a claim that the delegated JFA invariants are now enforced by this repository. -Private application chat uses the verified backend account `userId` as an -immutable conversation owner. Conversation/message routes and attachment -downloads enforce that boundary; shared integration keys supply no participant -identity. Legacy ownerless rows remain inaccessible pending explicit operator -assignment. Bindings: `backend/middleware/chatAuth.js`, -`conversationOwner.js`, `attachmentOwner.js`, the conditional conversation -model writes, and `backend/__tests__/conversationAccess.test.js`. This does not -promote private chat to a sealed commons dialog or change the in-review ledger -claims above. Legacy SSE endpoints remain retired with HTTP 410 until the -companion authenticated-stream change lands. See `backend/docs/conversation-ownership.md` for migration and client -integration limits. diff --git a/README.md b/README.md index f481f28..b042209 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ Additional chat interface endpoints (e.g., `/conversations`, `/messages/:id`, `/ structures are documented inline in the frontend components [`Sidebar.jsx`](frontend/chat-ui/src/components/Sidebar.jsx) and [`ChatWindow.jsx`](frontend/chat-ui/src/components/ChatWindow.jsx). +Private chat requires the existing backend account JWT, including for SSE and +attachments. Shared API keys do not identify a chat owner. Legacy ownerless +chats remain inaccessible pending explicit operator assignment. The host +application supplies its login token to `setAccountToken`; the chat UI does +not provide a complete onboarding flow. See +[conversation ownership](backend/docs/conversation-ownership.md) and +[authenticated streams](backend/docs/authenticated-streams.md). The global +`/events` feed is retired; use an authorized `/stream/:conversationId` request. ### User Registration #### POST /userRegistration ``` diff --git a/backend/__tests__/communication_controller.test.js b/backend/__tests__/communication_controller.test.js index b5fb7e2..ef403db 100644 --- a/backend/__tests__/communication_controller.test.js +++ b/backend/__tests__/communication_controller.test.js @@ -100,7 +100,7 @@ test('agrinet responder intercepts Agrinet-specific questions', async () => { assert.strictEqual(agrinetCalled, true, 'Agrinet guard should evaluate'); const messageBroadcasts = broadcastEvents.filter((evt) => evt.payload.type === 'message'); assert.strictEqual(messageBroadcasts.length, 2, 'both messages should be broadcast'); - assert.strictEqual(emitted.length, 2, 'emitMessage should fire for both messages'); + assert.strictEqual(emitted.length, 0, 'legacy alias must not duplicate canonical broadcasts'); } finally { agrinetResponder.shouldHandle = originalShouldHandle; agrinetResponder.generateResponse = originalGenerateResponse; diff --git a/backend/__tests__/controllers/communication_controller.test.js b/backend/__tests__/controllers/communication_controller.test.js index f974202..3f1fcdc 100644 --- a/backend/__tests__/controllers/communication_controller.test.js +++ b/backend/__tests__/controllers/communication_controller.test.js @@ -102,7 +102,8 @@ test('sendMessage streams tokens and persists ai message', async () => { assert.equal(jsonResponse.ai.id, aiMessage.id); assert.equal(tokenEvents.length, 3); - assert.deepEqual(directTokens, ['Hello', ' ', 'world!']); + assert.deepEqual(directTokens, [], 'legacy alias must not duplicate canonical tokens'); + assert.equal(messageEvents.length, 2, 'one event per persisted message'); assert(tokenEvents.every((event) => event.id === aiMessage.id)); assert.ok( diff --git a/backend/__tests__/conversationStreams.test.js b/backend/__tests__/conversationStreams.test.js new file mode 100644 index 0000000..71aad89 --- /dev/null +++ b/backend/__tests__/conversationStreams.test.js @@ -0,0 +1,180 @@ +const jwt = require('jsonwebtoken'); +const mockConversations = new Map(); +let mockCleanupFailure = false; +let mockHoldRead; +jest.mock('../lib/dynamodbClient', () => ({ + get: ({ Key }) => ({ promise: async () => { + const Item = mockConversations.get(Key.id); + const hold = mockHoldRead; + mockHoldRead = undefined; + if (hold) { hold.started(); await hold.wait; } + return { Item }; + } }), + delete: ({ Key }) => ({ promise: async () => { mockConversations.delete(Key.id); return {}; } }), + query: () => ({ promise: async () => { + if (mockCleanupFailure) throw new Error('fixture cleanup failure'); + return { Items: [] }; + } }), +})); +jest.mock('../bull/pingJobs', () => ({ addPingJob: jest.fn() })); +jest.mock('../bull/smsQueue', () => ({ enqueueSms: jest.fn() })); +jest.mock('../federation/federationSyncJob', () => jest.fn()); + +describe('production conversation SSE boundary', () => { + const originalEnv = { ...process.env }; + const originalGlobals = Object.fromEntries(['broadcast', 'emitToken', 'emitMessage'].map(key => [key, global[key]])); + let server; + let base; + const controllers = []; + const bearer = (userId, seconds = 60) => `Bearer ${jwt.sign({ userId }, process.env.JWT_SECRET, { expiresIn: seconds })}`; + async function open(path, headers = {}) { + const controller = new AbortController(); + controllers.push(controller); + return fetch(base + path, { headers, signal: AbortSignal.any([controller.signal, AbortSignal.timeout(4000)]) }); + } + beforeAll(async () => { + process.env.MINIMAL_SERVER = 'false'; + process.env.JWT_SECRET = 'stream-fixture-secret'; + process.env.API_KEY = 'stream-fixture-key'; + ({ server } = require('../server')); + await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); + base = `http://127.0.0.1:${server.address().port}`; + }); + beforeEach(() => { + mockCleanupFailure = false; + mockHoldRead = undefined; + mockConversations.clear(); + mockConversations.set('mine', { id: 'mine', ownerId: 'alice' }); + mockConversations.set('foreign', { id: 'foreign', ownerId: 'bob' }); + mockConversations.set('legacy', { id: 'legacy' }); + }); + afterEach(() => { for (const controller of controllers.splice(0)) controller.abort(); }); + afterAll(async () => { + server.closeAllConnections(); + await new Promise(resolve => server.close(resolve)); + process.env = originalEnv; + for (const [key, value] of Object.entries(originalGlobals)) { + if (value === undefined) delete global[key]; else global[key] = value; + } + }); + + test.each([ + ['anonymous', {}, ''], + ['shared key', { 'x-api-key': 'stream-fixture-key' }, ''], + ['invalid bearer and shared key', { Authorization: 'Bearer invalid', 'x-api-key': 'stream-fixture-key' }, ''], + ['query key', {}, '?x-api-key=stream-fixture-key'], + ])('rejects %s before opening stream headers', async (_name, headers, query) => { + let response; + try { response = await open('/stream/mine' + query, headers); } catch { response = { status: 'open stream', headers: new Headers() }; } + expect(response.status).toBe(401); + expect(response.headers.get('content-type')).not.toContain('text/event-stream'); + }); + test.each(['foreign', 'legacy', 'missing'])('conceals %s from an authenticated non-owner', async id => { + let response; + try { response = await open(`/stream/${id}`, { Authorization: bearer('alice') }); } catch { response = { status: 'open stream' }; } + expect(response.status).toBe(404); + expect(await response.json()).toEqual({ error: 'Not found' }); + }); + test('query bearer is not a substitute for an Authorization header', async () => { + const token = bearer('alice').slice(7); + let response; + try { response = await open(`/stream/mine?access_token=${token}`); } catch { response = { status: 'open stream' }; } + expect(response.status).toBe(401); + }); + test('retires the unrestricted global feed rather than exposing transaction events to every account', async () => { + const response = await open('/events', { Authorization: bearer('alice') }); + expect(response.status).toBe(410); + expect(response.headers.get('content-type')).not.toContain('text/event-stream'); + }); + test('owner receives only conversation-scoped events and deletion ends the stream', async () => { + const response = await open('/stream/mine', { Authorization: bearer('alice') }); + expect(response.status).toBe(200); + expect(response.headers.get('cache-control')).toContain('no-store'); + const reader = response.body.getReader(); + global.emitMessage('foreign', { content: 'Bobs secret' }); + global.emitMessage('mine', { content: 'Alice answer' }); + const chunk = new TextDecoder().decode((await reader.read()).value); + expect(chunk).toContain('Alice answer'); + expect(chunk).not.toContain('Bobs secret'); + const deletion = await fetch(base + '/conversations/mine', { method: 'DELETE', headers: { Authorization: bearer('alice') } }); + expect(deletion.status).toBe(204); + expect((await reader.read()).done).toBe(true); + }); + test('a connection closes when its account token expires', async () => { + const response = await open('/stream/mine', { Authorization: `Bearer ${jwt.sign({ userId: 'alice', exp: Math.ceil(Date.now() / 1000) + 1 }, process.env.JWT_SECRET)}` }); + expect(response.status).toBe(200); + const read = await response.body.getReader().read(); + expect(read.done).toBe(true); + expect((await open('/stream/mine', { Authorization: bearer('alice', -1) })).status).toBe(401); + }); + + test('failed message cleanup preserves ownership for retry and committed deletion closes streams', async () => { + const response = await open('/stream/mine', { Authorization: bearer('alice') }); + const reader = response.body.getReader(); + mockCleanupFailure = true; + const expectedError = jest.spyOn(console, 'error').mockImplementation(() => {}); + const deletion = await fetch(base + '/conversations/mine', { method: 'DELETE', headers: { Authorization: bearer('alice') } }); + expectedError.mockRestore(); + expect(deletion.status).toBe(500); + expect(mockConversations.has('mine')).toBe(true); + mockCleanupFailure = false; + const retry = await fetch(base + '/conversations/mine', { method: 'DELETE', headers: { Authorization: bearer('alice') } }); + expect(retry.status).toBe(204); + expect(mockConversations.has('mine')).toBe(false); + expect((await reader.read()).done).toBe(true); + }); + + test('deletion revokes a stream whose ownership lookup is still in flight', async () => { + let release; + let started; + const reading = new Promise(resolve => { started = resolve; }); + mockHoldRead = { started, wait: new Promise(resolve => { release = resolve; }) }; + const pending = open('/stream/mine', { Authorization: bearer('alice') }); + await reading; + const deletion = await fetch(base + '/conversations/mine', { method: 'DELETE', headers: { Authorization: bearer('alice') } }); + expect(deletion.status).toBe(204); + release(); + const response = await pending; + expect(response.status).toBe(404); + expect(response.headers.get('content-type')).not.toContain('text/event-stream'); + }); + test('retired global feed returns 410 without account credentials', async () => { + const response = await open('/events'); + expect(response.status).toBe(410); + }); + test('production stream route shares reservations and reuses released capacity', async () => { + const { EventEmitter } = require('node:events'); + const { createConcurrentStreamLimit } = require('../middleware/streamConcurrency'); + const reserve = createConcurrentStreamLimit({ maximum: 5 }); + const userId = 'shared-capacity-owner'; + mockConversations.set('capacity-room', { id: 'capacity-room', ownerId: userId }); + const held = Array.from({ length: 5 }, () => new EventEmitter()); + try { + for (const res of held) { + await reserve({ user: { userId, exp: Math.ceil(Date.now() / 1000) + 60 } }, res, () => {}); + } + const blocked = await open('/stream/capacity-room', { Authorization: bearer(userId) }); + expect(blocked.status).toBe(429); + held[0].emit('close'); + await new Promise(resolve => setImmediate(resolve)); + const admitted = await open('/stream/capacity-room', { Authorization: bearer(userId) }); + expect(admitted.status).toBe(200); + expect(admitted.headers.get('content-type')).toContain('text/event-stream'); + } finally { + for (const res of held) res.emit('close'); + } + }); + test('stream opening requests have an account budget before ownership reads', async () => { + const auth = { Authorization: bearer('stream-rate-fixture') }; + const first = await open('/stream/missing', auth); + expect(first.status).toBe(404); + expect(first.headers.get('ratelimit-limit')).not.toBeNull(); + const limit = Number(first.headers.get('ratelimit-limit')); + for (let i = 1; i < limit; i++) expect((await open('/stream/missing', auth)).status).toBe(404); + const blocked = await open('/stream/missing', auth); + expect(blocked.status).toBe(429); + expect(Number(blocked.headers.get('retry-after'))).toBeGreaterThan(0); + expect((await open('/stream/missing', { Authorization: bearer('other-stream-user') })).status).toBe(404); + }); + +}); diff --git a/backend/__tests__/sseDelivery.test.js b/backend/__tests__/sseDelivery.test.js new file mode 100644 index 0000000..9a1eb6a --- /dev/null +++ b/backend/__tests__/sseDelivery.test.js @@ -0,0 +1,61 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { EventEmitter } = require('node:events'); +const streams = require('../lib/conversationStreams'); +const Conversation = require('../models/conversation'); +const Message = require('../models/message'); +const dynamo = require('../lib/dynamodbClient'); +const controller = require('../controllers/communication_controller'); +const { setClient, resetClient } = require('../lib/openaiClient'); + +function streamResponse(frames) { + const res = new EventEmitter(); + res.status = () => res; + res.set = () => res; + res.flushHeaders = () => {}; + res.write = frame => frames.push(frame); + res.end = () => { res.writableEnded = true; res.emit('close'); }; + return res; +} + +async function checkDelivery(t, canonical) { + const originalOwner = Conversation.getOwned; + const originalGlobals = Object.fromEntries(['broadcast', 'emitToken', 'emitMessage'].map(key => [key, global[key]])); + const frames = []; + const saved = []; + const id = canonical ? 'canonical-delivery' : 'legacy-delivery'; + t.after(() => { + streams.close(id); + Conversation.getOwned = originalOwner; + Message.setDocClient(dynamo); + resetClient(); + for (const [key, value] of Object.entries(originalGlobals)) { + if (value === undefined) delete global[key]; else global[key] = value; + } + }); + Conversation.getOwned = async () => ({ id, ownerId: 'alice' }); + Message.setDocClient({ + put: ({ Item }) => ({ promise: async () => { saved.push(Item); return {}; } }), + query: () => ({ promise: async () => ({ Items: saved }) }), + }); + setClient({ chat: { completions: { create: async () => (async function* () { + for (const content of ['Hello', ' ', 'world!']) yield { choices: [{ delta: { content } }] }; + }()) } } }); + global.emitToken = (conversationId, responseId, token) => streams.send(conversationId, 'token', { id: responseId, token }); + global.emitMessage = (conversationId, message) => streams.send(conversationId, 'message', { message }); + if (canonical) global.broadcast = (event, data, conversationId) => streams.send(conversationId, event, data); + else delete global.broadcast; + await streams.open({ user: { userId: 'alice', exp: Math.ceil(Date.now() / 1000) + 60 }, params: { conversationId: id } }, streamResponse(frames)); + const result = { status() { return this; }, json(body) { this.body = body; } }; + await controller.sendMessage({ params: { conversationId: id }, body: { from: 'user', content: 'How are you?', type: 'text' } }, result); + const tokens = frames.filter(frame => frame.startsWith('event: token\n')).map(frame => JSON.parse(frame.split('\n')[1].slice(6))); + const messages = frames.filter(frame => frame.startsWith('event: message\n')).map(frame => JSON.parse(frame.split('\n')[1].slice(6)).message.id); + assert.deepEqual(tokens.map(token => token.token), ['Hello', ' ', 'world!'], 'each streamed token must have one SSE frame'); + assert(tokens.every(token => token.id === result.body.ai.id)); + assert.deepEqual(messages, [result.body.message.id, result.body.ai.id], 'each persisted message must have one SSE frame'); +} + +test('canonical and legacy aliases produce one frame per token and message', async t => { + await t.test('canonical broadcast present', t => checkDelivery(t, true)); + await t.test('legacy aliases only', t => checkDelivery(t, false)); +}); diff --git a/backend/__tests__/streamCapacity.test.js b/backend/__tests__/streamCapacity.test.js new file mode 100644 index 0000000..e459ed5 --- /dev/null +++ b/backend/__tests__/streamCapacity.test.js @@ -0,0 +1,107 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { EventEmitter } = require('node:events'); +const Conversation = require('../models/conversation'); +const registry = require('../lib/conversationStreams'); +const { createConcurrentStreamLimit } = require('../middleware/streamConcurrency'); +const reserve = createConcurrentStreamLimit({ maximum: 5 }); +const streams = { + ...registry, + async open(req, res) { + try { return await reserve(req, res, () => registry.open(req, res)); } + catch (error) { res.end(); throw error; } + }, +}; + +function response() { + return Object.assign(new EventEmitter(), { + statusCode: 200, + status(code) { this.statusCode = code; return this; }, + json(body) { this.body = body; this.end(); return this; }, + set() { return this; }, flushHeaders() {}, write() {}, + end() { this.writableEnded = true; this.emit('close'); }, + }); +} +function request(id, userId = 'alice') { + return { params: { conversationId: id }, user: { userId, exp: Math.ceil(Date.now() / 1000) + 60 } }; +} + +test('stream capacity is shared across conversations and renewed tokens, isolated by account, and reusable after close', async () => { + Conversation.setDocClient({ get: ({ Key }) => ({ promise: async () => ({ Item: { id: Key.id, ownerId: Key.id.startsWith('bob') ? 'bob' : 'alice' } }) }) }); + const connections = []; + try { + for (let i = 0; i < 5; i++) { + const res = response(); connections.push(res); + await streams.open(request(`alice-${i}`), res); + assert.equal(res.statusCode, 200); + } + const denied = response(); connections.push(denied); + await streams.open(request('sixth'), denied); + assert.equal(denied.statusCode, 429); + const bob = response(); connections.push(bob); + await streams.open(request('bob-1', 'bob'), bob); + assert.equal(bob.statusCode, 200); + connections[0].end(); + connections[0].emit('error', new Error('duplicate cleanup')); + const replacement = response(); connections.push(replacement); + await streams.open(request('replacement'), replacement); + assert.equal(replacement.statusCode, 200); + const stillFull = response(); connections.push(stillFull); + await streams.open(request('still-full'), stillFull); + assert.equal(stillFull.statusCode, 429); + } finally { for (const res of connections) res.end(); } +}); + +test('pending ownership reads reserve capacity and deletion releases it before the database returns', async () => { + let release; + const wait = new Promise(resolve => { release = resolve; }); + Conversation.setDocClient({ get: ({ Key }) => ({ promise: async () => { await wait; return { Item: { id: Key.id, ownerId: 'alice' } }; } }) }); + const connections = Array.from({ length: 5 }, response); + const pending = connections.map((res, index) => streams.open(request(`pending-${index}`), res)); + try { + const denied = response(); connections.push(denied); + await Promise.race([streams.open(request('pending-sixth'), denied), new Promise(resolve => setTimeout(resolve, 100))]); + assert.equal(denied.statusCode, 429); + streams.close('pending-0'); + const replacement = response(); connections.push(replacement); + const next = streams.open(request('replacement'), replacement); + release(); + await Promise.all([...pending, next]); + assert.equal(connections[0].statusCode, 404); + assert.equal(replacement.statusCode, 200); + } finally { release(); await Promise.allSettled(pending); for (const res of connections) res.end(); } +}); + +test('failed ownership checks never consume account stream capacity', async () => { + let fail = true; + Conversation.setDocClient({ get: () => ({ promise: async () => { if (fail) throw new Error('database unavailable'); return {}; } }) }); + for (let i = 0; i < 6; i++) await assert.rejects(streams.open(request('failed'), response()), /database unavailable/); + fail = false; + for (let i = 0; i < 6; i++) { + const res = response(); + await streams.open(request('missing'), res); + assert.equal(res.statusCode, 404); + } +}); + +test('token expiry closes pending reads and frees slots for a renewed token before database recovery', async () => { + let release; + const wait = new Promise(resolve => { release = resolve; }); + Conversation.setDocClient({ get: ({ Key }) => ({ promise: async () => { + if (Key.id !== 'renewed') await wait; + return { Item: { id: Key.id, ownerId: 'alice' } }; + } }) }); + const exp = Math.ceil(Date.now() / 1000) + 1; + const connections = Array.from({ length: 5 }, response); + const pending = connections.map((res, index) => streams.open({ ...request(`expiring-${index}`), user: { userId: 'alice', exp } }, res)); + try { + await new Promise(resolve => setTimeout(resolve, exp * 1000 - Date.now() + 30)); + assert.ok(connections.every(res => res.writableEnded && res.statusCode === 401)); + const renewed = response(); connections.push(renewed); + await streams.open(request('renewed'), renewed); + assert.equal(renewed.statusCode, 200); + release(); + await Promise.all(pending); + assert.ok(connections.slice(0, 5).every(res => res.statusCode === 401)); + } finally { release(); await Promise.allSettled(pending); for (const res of connections) res.end(); } +}); diff --git a/backend/__tests__/streamSharedCapacity.test.js b/backend/__tests__/streamSharedCapacity.test.js new file mode 100644 index 0000000..fcd9561 --- /dev/null +++ b/backend/__tests__/streamSharedCapacity.test.js @@ -0,0 +1,79 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { spawn } = require('node:child_process'); +const { once } = require('node:events'); +const { randomUUID, randomBytes } = require('node:crypto'); +const path = require('node:path'); +const jwt = require('jsonwebtoken'); + +test('separate stream servers share capacity across conversations and renewed tokens', { + skip: !process.env.STREAM_LIMIT_REDIS_URL, + timeout: 30000, +}, async t => { + const redisUrl = new URL(process.env.STREAM_LIMIT_REDIS_URL); + assert.ok(['127.0.0.1', 'localhost', '[::1]'].includes(redisUrl.hostname)); + const secret = randomBytes(32).toString('hex'); + const owner = randomUUID(); + const workers = []; + const controllers = []; + t.after(async () => { + for (const controller of controllers) controller.abort(); + await Promise.all(workers.map(async child => { + if (child.exitCode === null) { + const exited = once(child, 'exit'); + child.kill(); + await exited; + } + })); + }); + const source = ` + const Conversation = require('./models/conversation'); + Conversation.setDocClient({ get: ({ Key }) => ({ promise: async () => ({ + Item: { id: Key.id, ownerId: Key.id.split(':')[0] } + }) }) }); + const { server } = require('./server'); + const redis = require('./middleware/rateLimit').getRedisClient(); + const listen = () => server.listen(0, '127.0.0.1', () => process.send(server.address().port)); + if (redis.status === 'ready') listen(); else redis.once('ready', listen); + `; + const ports = []; + for (let i = 0; i < 2; i++) { + const child = spawn(process.execPath, ['-e', source], { + cwd: path.resolve(__dirname, '..'), + env: { ...process.env, MINIMAL_SERVER: 'true', REDIS_URL: redisUrl.href, + RATE_LIMIT_REDIS_REQUIRED: 'true', JWT_SECRET: secret }, + stdio: ['ignore', 'ignore', 'inherit', 'ipc'], + }); + workers.push(child); + ports.push((await once(child, 'message'))[0]); + } + async function open(server, conversation, userId = owner) { + const controller = new AbortController(); + controllers.push(controller); + const token = jwt.sign({ userId }, secret, { expiresIn: '1h', jwtid: randomUUID() }); + return fetch(`http://127.0.0.1:${ports[server]}/stream/${userId}:${conversation}`, { + headers: { Authorization: `Bearer ${token}` }, + signal: AbortSignal.any([controller.signal, AbortSignal.timeout(10000)]), + }); + } + for (let i = 0; i < 5; i++) { + const response = await open(i % 2, `room-${i}`); + assert.equal(response.status, 200); + assert.match(response.headers.get('content-type'), /text\/event-stream/); + } + const denied = await open(1, 'sixth'); + assert.equal(denied.status, 429, 'switching servers, conversations, or tokens must not reset capacity'); + await denied.arrayBuffer(); + assert.equal((await open(0, 'other-account', randomUUID())).status, 200); + controllers[0].abort(); + // Disconnect cleanup crosses the HTTP and Redis boundaries asynchronously. + let replacement; + for (let attempt = 0; attempt < 20; attempt++) { + replacement = await open(1, 'replacement'); + if (replacement.status === 200) break; + assert.equal(replacement.status, 429); + await replacement.arrayBuffer(); + await new Promise(resolve => setTimeout(resolve, 10)); + } + assert.equal(replacement.status, 200, 'disconnect must free capacity on the other server'); +}); diff --git a/backend/controllers/communication_controller.js b/backend/controllers/communication_controller.js index 2d1299e..5e25140 100644 --- a/backend/controllers/communication_controller.js +++ b/backend/controllers/communication_controller.js @@ -15,9 +15,7 @@ function broadcastMessage(conversationId, msg, options = {}) { tokens.forEach((token) => { global.broadcast('token', { id: msg.id, token }, conversationId); }); - } - - if (emitTokens && global.emitToken) { + } else if (emitTokens && global.emitToken) { tokens.forEach((token) => { global.emitToken(conversationId, msg.id, token); }); @@ -25,8 +23,7 @@ function broadcastMessage(conversationId, msg, options = {}) { if (global.broadcast) { global.broadcast('message', { type: 'message', message: msg }, conversationId); - } - if (global.emitMessage) { + } else if (global.emitMessage) { global.emitMessage(conversationId, msg); } } diff --git a/backend/docs/authenticated-streams.md b/backend/docs/authenticated-streams.md new file mode 100644 index 0000000..5f2bd94 --- /dev/null +++ b/backend/docs/authenticated-streams.md @@ -0,0 +1,101 @@ +# Authenticated conversation streams + +Conversation ownership (#191) and security hardening (#197) are merged into +this branch. Deploy their protections with this authenticated stream wiring. +The existing backend account JWT must be supplied as +`Authorization: Bearer `; its verified `userId` must own the requested +conversation. Tokens must have a finite future expiry, as backend login tokens +already do. API keys and credentials in query strings grant no stream access. + +`GET /stream/:conversationId` authorizes before sending SSE headers or +subscribing to live events. Foreign, missing and ownerless conversations have +the same 404 response. Successful streams preserve the existing `token` and +`message` payloads, disable caching, and request that reverse proxies disable +buffering. Authorization errors are JSON responses, never open SSE connections. + +The unrestricted `GET /events` feed is retired: all requests receive 410 +without account authentication. No current chat +component uses it. Generic transaction broadcast producers remain callable, +but their events are no longer exposed to arbitrary subscribers. Reintroducing +transaction events needs a specific recipient-authorization contract; this PR +does not infer one from a shared key or broad account membership. + +Streams stop on socket closure/error and token expiry. The emitter rechecks +expiry before each write so delayed timers do not send post-expiry frames. +Parent conversation deletion revokes ready and pending connections on the +serving process immediately after the conditional database delete. Message +cleanup finishes first, preserving the parent and its owner if cleanup fails +so the operation can be retried. Pending registration precedes its strongly +consistent owner lookup; a concurrent delete therefore cannot be missed by +a late ownership-read response. This is a process-local stream registry, not +a distributed invalidation system: another replica's deletion or manual store +changes are not pushed to existing sockets here. Tokens remain the existing +signed bearer capability until expiry; no server-wide JWT revocation system +is introduced. + +## Resource limits + +Stream opening uses the existing chat-route pattern: 300 pre-authentication +attempts per minute per IP, then 60 per minute per verified account before the +ownership lookup. Open streams are separately capped at five per +account across all conversations, including pending ownership reads. Renewing a +token for the same account does not reset either budget. Excess attempts return +429. Idempotent cleanup releases capacity on disconnect, errors, expiry, +ownership failure, and deletion (including pending connections). The route uses +`createConcurrentStreamLimit({ maximum: 5 })` after authentication and the +account request limiter; it no longer keeps a second process-local count. + +Set `REDIS_URL` and `RATE_LIMIT_REDIS_REQUIRED=true` for multiple replicas. +Request counters and stream reservations then share the existing Redis store; +without Redis, the fallback is suitable only for a single server process. +Capacity-store failures return 503 before stream opening. A disconnect during +acquisition releases its late reservation without opening the stream. + +The existing helper expires abandoned reservations using their JWT expiry and +an eight-day Redis key TTL; it does not implement short renewable leases. +Failed release or process loss can retain slots until expiration. Redis reset +or key expiry can discard reservations, so this is not durable admission state. +Shared capacity does not add cross-replica event delivery or deletion revocation. +Keep `CORS_ALLOWED_ORIGINS` set to the exact deployed chat origin(s), as required +by the security deployment guide; this change selects no new deployment origin. + +## Browser transport + +Native EventSource cannot attach Authorization headers. The chat adapter now +uses streaming fetch and keeps the subset ChatWindow uses: add/remove event +listeners and `close()`. It parses UTF-8 SSE token/message frames across chunk +and CRLF boundaries. It retries transient network disconnects after one second, +with fresh account authorization, and stops on failed HTTP authorization or an +invalid content type. There is no replay guarantee or Last-Event-ID contract. +Buffering has a defensive 1 MiB text limit; oversized responses are cancelled +and stop permanently rather than entering a reconnect loop. + +Call #191's `setAccountToken(token)` after the host application's existing login, +and `setAccountToken(null)` on logout. Every account change closes existing +streams immediately and remounts private chat state. Old frames are not +dispatched after closure. Credentials stay in headers and memory, never URLs, +browser storage or build-time account-token configuration. A shared +`VITE_API_KEY` alone is insufficient. Full browser onboarding remains outside +these two changes; the integration limits in `conversation-ownership.md` apply. + +## Verification + +`cd backend && npm test` exercises the production server using real HTTP +connections, verified account JWTs and isolated persistence/queue fixtures. +Coverage includes anonymous/shared-key/query-key/invalid-token denial, foreign +and legacy IDs, owner-only payloads, token expiry, retry after failed child +cleanup, and deletion racing a pending ownership lookup. + +From `backend`, run the cross-process integration against disposable loopback +Redis with `STREAM_LIMIT_REDIS_URL=redis://127.0.0.1:PORT node --test __tests__/streamSharedCapacity.test.js`. +It opens five streams across two real server processes, rejects the sixth +despite renewed tokens and different conversations, isolates another account, +and verifies that disconnect releases a slot on the other server. Without the +explicit service URL this optional test skips; the default hosted backend +workflow does not provision Redis or establish this integration result. + +`cd frontend/chat-ui && npm test` checks header credentials, fragmented SSE +frames, cancellation, account changes and terminal authorization errors. +`npm run build` verifies the browser bundle. These are private application +events; no Mycelium, covenant, escrow, protocol-wire or federation semantics +are changed, and the existing conformance stand-ins remain labeled. diff --git a/backend/docs/conversation-ownership.md b/backend/docs/conversation-ownership.md index 586da61..d8b6793 100644 --- a/backend/docs/conversation-ownership.md +++ b/backend/docs/conversation-ownership.md @@ -49,12 +49,14 @@ that conversation partition, including continuation pages. No new index or table is required. New files use UUID names, safe extensions and exclusive writes; original names remain in message metadata. -This branch includes the merged account/federation security baseline. Legacy -`/events` and `/stream/:conversationId` return 410; keep that protection until -#192 installs owner-authenticated streaming. #191 restores only authenticated, -rate-limited, owner-checked attachment access. It can merge while streams remain -disabled. Restore streaming only after reviewing #192 against this branch and -its shared concurrent-stream reservation helper. +This branch includes merged conversation ownership (#191) and account/federation +security hardening (#197). The global `/events` feed remains retired with 410. +`/stream/:conversationId` now requires an account token, account-keyed throttling, +a shared concurrent-stream reservation and conversation ownership before sending +stream headers. Attachment access retains its independent authentication, rate +limits and ownership checks. Deploy the complete reviewed protections together; +see `authenticated-streams.md` for Redis configuration and remaining replica +delivery/revocation limits. Operators can count ownerless conversations without changing records: diff --git a/backend/lib/conversationStreams.js b/backend/lib/conversationStreams.js new file mode 100644 index 0000000..9eff376 --- /dev/null +++ b/backend/lib/conversationStreams.js @@ -0,0 +1,108 @@ +const streams = new Map(); +const MAX_TIMER_MS = 2 ** 31 - 1; + +function validateExpiry(user) { + const expiresAt = user.exp * 1000; + if (!Number.isFinite(expiresAt) || expiresAt <= Date.now()) { + return { status: 401, error: 'Expiring account token required' }; + } +} + +function register(id, expiresAt, res) { + if (!streams.has(id)) streams.set(id, new Set()); + const clients = streams.get(id); + let timer; + const client = { res, expiresAt, ready: false, revoked: false, cleaned: false }; + const cleanup = () => { + if (!client.cleaned) { + client.cleaned = true; + clearTimeout(timer); + clients.delete(client); + if (!clients.size && streams.get(id) === clients) streams.delete(id); + } + }; + client.cleanup = cleanup; + const expire = () => { + const remaining = expiresAt - Date.now(); + if (remaining <= 0) { + cleanup(); + if (client.ready) res.end(); + else res.status(401).json({ error: 'Expiring account token required' }); + } + else timer = setTimeout(expire, Math.min(remaining, MAX_TIMER_MS)); + }; + res.on('close', cleanup); + res.on('error', cleanup); + clients.add(client); + expire(); + return client; +} + +async function open(req, res) { + const denial = validateExpiry(req.user); + if (denial) return res.status(denial.status).json({ error: denial.error }); + const expiresAt = req.user.exp * 1000; + const ownerId = req.user.userId; + const id = req.params.conversationId; + const client = register(id, expiresAt, res); + const cleanup = client.cleanup; + // Register the pending connection BEFORE reading ownership, so a concurrent + // committed delete can revoke it even if the database response is in flight. + const conversation = await readOwnership(client, id, ownerId); + if (res.destroyed || res.writableEnded) { cleanup(); return; } + if (!conversation || client.revoked) { + cleanup(); + return res.status(404).json({ error: 'Not found' }); + } + if (client.cleaned) return; + if (expiresAt <= Date.now()) { + cleanup(); + return res.status(401).json({ error: 'Expiring account token required' }); + } + client.ready = true; + res.status(200).set({ + 'Content-Type': 'text/event-stream', + 'Cache-Control': 'private, no-store', + Connection: 'keep-alive', + 'X-Accel-Buffering': 'no', + }); + res.flushHeaders(); +} + +async function readOwnership(client, id, ownerId) { + try { return await require('../models/conversation').getOwned(id, ownerId); } + catch (error) { + const ended = client.cleaned; + client.cleanup(); + if (!ended) throw error; + } +} + +function send(id, event, data) { + const clients = streams.get(id); + if (!clients) return; + const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; + for (const client of clients) { + if (!client.ready || client.revoked) continue; + // Check at emission too: an overdue timer must not leak post-expiry data. + if (client.expiresAt <= Date.now() || client.res.destroyed || client.res.writableEnded) { + client.res.end(); + continue; + } + client.res.write(payload); + } +} + +function close(id) { + const clients = streams.get(id); + if (!clients) return; + for (const client of clients) { + client.revoked = true; + client.cleanup(); + if (client.ready) client.res.end(); + else client.res.status(404).json({ error: 'Not found' }); + } + streams.delete(id); +} + +module.exports = { open, send, close }; diff --git a/backend/middleware/__tests__/streamConcurrency.test.js b/backend/middleware/__tests__/streamConcurrency.test.js index 59fbf20..6143407 100644 --- a/backend/middleware/__tests__/streamConcurrency.test.js +++ b/backend/middleware/__tests__/streamConcurrency.test.js @@ -40,4 +40,50 @@ describe('concurrent stream limits', () => { await middleware({ user: { userId: 'missing-exp' } }, res, () => { throw new Error('next must not run'); }); expect(res.statusCode).toBe(401); }); + + test('a disconnect during Redis acquisition releases the late reservation without opening a stream', async () => { + let resume; + let started; + const acquiring = new Promise(resolve => { started = resolve; }); + const paused = new Promise(resolve => { resume = resolve; }); + const active = new Set(); + let firstAcquire = true; + // Redis is the external boundary; the real reservation middleware runs unchanged. + const redis = { eval: async (script, _count, _key, ...args) => { + if (script.includes('ZADD')) { + if (firstAcquire) { firstAcquire = false; started(); await paused; } + if (active.size >= args[1]) return 0; + active.add(args[3]); + } else active.delete(args[0]); + return 1; + } }; + let createLimit; + jest.isolateModules(() => { + jest.doMock('../rateLimit', () => ({ getRedisClient: () => redis })); + createLimit = require('../streamConcurrency').createConcurrentStreamLimit; + }); + const limit = createLimit({ maximum: 1 }); + const req = { user: { userId: 'late-reservation', exp: Math.ceil(Date.now() / 1000) + 60 } }; + const first = response(); + const second = response(); + let opened = false; + const pending = limit(req, first, () => { opened = true; }); + try { + await acquiring; + first.destroyed = true; + first.emit('close'); + resume(); + await pending; + expect(opened).toBe(false); + let replacementOpened = false; + await limit(req, second, () => { replacementOpened = true; }); + expect(replacementOpened).toBe(true); + } finally { + resume(); + await pending; + first.emit('close'); + second.emit('close'); + jest.dontMock('../rateLimit'); + } + }); }); diff --git a/backend/middleware/streamConcurrency.js b/backend/middleware/streamConcurrency.js index 2489581..db0ed97 100644 --- a/backend/middleware/streamConcurrency.js +++ b/backend/middleware/streamConcurrency.js @@ -89,7 +89,12 @@ function createConcurrentStreamLimit({ maximum = 5 } = {}) { res.once('close', cleanup); res.once('finish', cleanup); res.once('error', cleanup); - return next(); + // A disconnected response may have closed while the Redis acquire was pending. + if (res.destroyed || res.writableEnded) { + cleanup(); + } else { + return next(); + } }; } diff --git a/backend/models/conversation.js b/backend/models/conversation.js index bb0314c..92bc41e 100644 --- a/backend/models/conversation.js +++ b/backend/models/conversation.js @@ -78,6 +78,7 @@ async function remove(id, ownerId) { if (err.code === 'ConditionalCheckFailedException') return false; throw err; } + require('../lib/conversationStreams').close(id); return true; } diff --git a/backend/server.js b/backend/server.js index d77b676..92a289b 100644 --- a/backend/server.js +++ b/backend/server.js @@ -10,7 +10,8 @@ let dotenv; try { dotenv = require('dotenv'); } catch { dotenv = { config: () => {} }; } dotenv.config(); const authMiddleware = require('./middleware/authMiddleware'); -const { apiRateLimit, streamRateLimit, locationRateLimit } = require('./middleware/rateLimit'); +const { apiRateLimit, locationRateLimit } = require('./middleware/rateLimit'); +const { createConcurrentStreamLimit } = require('./middleware/streamConcurrency'); const minimal = process.env.MINIMAL_SERVER === 'true'; const app = express(); const allowedOrigins = (process.env.CORS_ALLOWED_ORIGINS || 'https://www.ntari.org').split(',').map(origin => origin.trim()).filter(Boolean); @@ -20,22 +21,32 @@ const tryMount = (route, modPath) => { try { app.use(route, require(modPath)); } if (!minimal) tryMount('/deposit', './routes/depositRoutes'); app.get('/health', (_req, res) => res.status(200).send('OK')); const server = http.createServer(app); -// Fail closed until owner-authenticated streaming (#192) is merged. -app.get('/events', (_req, res) => res.status(410).json({ error: 'Global event stream retired; use authenticated conversation streams' })); -app.get('/stream/:conversationId', streamRateLimit, (_req, res) => res.status(410).json({ error: 'Conversation streaming is temporarily unavailable pending owner-authenticated stream support' })); -const conversationStreams = new Map(); -function sendConversationEvent(conversationId, event, data) { const clients = conversationStreams.get(conversationId); if (!clients) return; const payload = 'event: ' + event + '\ndata: ' + JSON.stringify(data) + '\n\n'; clients.forEach(res => res.write(payload)); } -function broadcast(event, data, conversationId) { if (conversationId) sendConversationEvent(conversationId, event, data); } +// Account-scoped streams share the same owner check as message reads/sends. +const conversationStreams = require('./lib/conversationStreams'); +app.get('/events', (_req, res) => { + res.status(410).json({ error: 'Global event feed retired; use an owned conversation stream' }); +}); +app.get('/stream/:conversationId', + rateLimit(require('./middleware/chatRateLimit').options(300, 'stream-attempt')), chatAuth, + rateLimit(require('./middleware/chatRateLimit').options(60, 'stream')), + createConcurrentStreamLimit({ maximum: 5 }), asyncHandler(conversationStreams.open)); + +function broadcast(event, data, conversationId) { + if (conversationId) conversationStreams.send(conversationId, event, data); + // Generic transaction broadcasts have no recipient authorization contract. + // Keep producers callable, but never expose them through a global feed. +} global.broadcast = broadcast; -global.emitToken = (conversationId, id, token) => sendConversationEvent(conversationId, 'token', { id, token }); -global.emitMessage = (conversationId, message) => sendConversationEvent(conversationId, 'message', { message }); +global.emitToken = (conversationId, id, token) => conversationStreams.send(conversationId, 'token', { id, token }); +global.emitMessage = (conversationId, message) => conversationStreams.send(conversationId, 'message', { message }); + // Federation authenticates with per-peer HMAC credentials, independently of browser API keys. if (!minimal) tryMount('/federation', './federation/federationRoutes'); // Public account creation/login have their own IP throttles and do not require the browser integration key. if (!minimal) tryMount('/api/auth', './routes/authRoutes'); app.use(authMiddleware); app.use(apiRateLimit); -// Private uploads are disabled until conversation-owner authorization is deployed (#191). +// Private uploads retain the merged conversation-owner authorization boundary. app.use('/uploads', chatAuth, rateLimit(require('./middleware/chatRateLimit').options(60, 'uploads')), asyncHandler(require('./middleware/attachmentOwner')), express.static(path.join(__dirname, 'uploads'), { dotfiles: 'deny', index: false, setHeaders(res) { diff --git a/backend/services/openAIResponder.js b/backend/services/openAIResponder.js index 7ca7d2c..0630f12 100644 --- a/backend/services/openAIResponder.js +++ b/backend/services/openAIResponder.js @@ -16,11 +16,10 @@ async function generateResponse({ conversationId, message, chatHistory }) { if (!token) continue; streamedTokens = true; content += token; - if (global.emitToken) { - global.emitToken(conversationId, responseId, token); - } if (global.broadcast) { global.broadcast('token', { id: responseId, token }, conversationId); + } else if (global.emitToken) { + global.emitToken(conversationId, responseId, token); } } } catch (error) { diff --git a/frontend/chat-ui/ARCHITECTURE.md b/frontend/chat-ui/ARCHITECTURE.md index d7119f7..f43ed34 100644 --- a/frontend/chat-ui/ARCHITECTURE.md +++ b/frontend/chat-ui/ARCHITECTURE.md @@ -30,7 +30,7 @@ This document describes the component-level architecture and user-experience flo ## 4. Sidebar (Left Panel) - **Component**: `Sidebar` with nested `ChatList` entries. -- **API notes**: Comments in [`src/components/Sidebar.jsx`](src/components/Sidebar.jsx) outline conversation/message endpoints, expected JSON, and `x-api-key` usage. +- **API notes**: Comments in [`src/components/Sidebar.jsx`](src/components/Sidebar.jsx) outline conversation/message endpoints and expected JSON. Private chat uses a backend account bearer supplied through `setAccountToken`; shared keys grant no private-chat access. SSE uses streaming fetch with Authorization headers, not native EventSource query credentials. See [`authenticated-streams.md`](../../backend/docs/authenticated-streams.md) for lifecycle and integration limits. - **New Chat**: Clears `messages[]` and establishes a new conversation identifier. - **History**: Loaded from `/conversations` and displayed as `ConversationCard` items with lazy‑loaded pagination. - **Pinned Chats**: Items marked with `pinned: true` render in a dedicated section. diff --git a/frontend/chat-ui/authenticatedStream.test.js b/frontend/chat-ui/authenticatedStream.test.js new file mode 100644 index 0000000..9cfcbff --- /dev/null +++ b/frontend/chat-ui/authenticatedStream.test.js @@ -0,0 +1,81 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { once } = require('node:events'); + +test('stream authenticates by header and parses fragmented CRLF token/message events', async () => { + const { createAccountClient } = await import('./src/accountClient.mjs'); + const { authenticatedStream } = await import('./src/authenticatedStream.mjs'); + const client = createAccountClient({ baseUrl: 'https://backend.example' }); + client.setToken('alice-token'); + let headers; + let destination; + const encoder = new TextEncoder(); + const stream = authenticatedStream(client, '/stream/mine', { fetchImpl: async (url, options) => { + headers = options.headers; + destination = url; + return new Response(new ReadableStream({ start(controller) { + for (const chunk of ['event: to', 'ken\r', '\ndata: {"token":"hello"}\r\n\r', '\nevent: message\ndata: {"message":{"id":"answer"}}\n\n']) controller.enqueue(encoder.encode(chunk)); + } }), { headers: { 'Content-Type': 'text/event-stream' } }); + } }); + const tokens = []; + stream.addEventListener('token', event => tokens.push(JSON.parse(event.data).token)); + const [message] = await once(stream, 'message'); + assert.deepEqual(tokens, ['hello']); + assert.equal(JSON.parse(message.data).message.id, 'answer'); + assert.equal(headers.Authorization, 'Bearer alice-token'); + assert.equal(destination, 'https://backend.example/stream/mine'); + stream.close(); +}); + +test('account change aborts and closes the old stream before later frames dispatch', async () => { + const { createAccountClient } = await import('./src/accountClient.mjs'); + const { authenticatedStream } = await import('./src/authenticatedStream.mjs'); + const client = createAccountClient(); + client.setToken('alice-token'); + let signal; + let cancelled = false; + const stream = authenticatedStream(client, '/stream/mine', { fetchImpl: async (_url, options) => { + signal = options.signal; + return new Response(new ReadableStream({ cancel() { cancelled = true; } }), { headers: { 'Content-Type': 'text/event-stream' } }); + } }); + await once(stream, 'open'); + client.setToken('bob-token'); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(signal.aborted, true); + assert.equal(cancelled, true); + assert.equal(stream.readyState, 2); +}); + +test('authorization failures stop reconnecting and no token means no request', async () => { + const { createAccountClient } = await import('./src/accountClient.mjs'); + const { authenticatedStream } = await import('./src/authenticatedStream.mjs'); + const client = createAccountClient(); + let requests = 0; + const fetchImpl = async () => { requests++; return new Response('denied', { status: 401 }); }; + const anonymous = authenticatedStream(client, '/stream/mine', { fetchImpl }); + await once(anonymous, 'error'); + assert.equal(requests, 0); + client.setToken('account-token'); + const denied = authenticatedStream(client, '/stream/mine', { fetchImpl }); + await once(denied, 'error'); + assert.equal(denied.readyState, 2); + assert.equal(requests, 1); +}); + +test('oversized frames close permanently instead of repeatedly reconnecting', async () => { + const { createAccountClient } = await import('./src/accountClient.mjs'); + const { authenticatedStream } = await import('./src/authenticatedStream.mjs'); + const client = createAccountClient(); + client.setToken('alice'); + let requests = 0; + const stream = authenticatedStream(client, '/stream/mine', { retryMs: 1, fetchImpl: async () => { + requests++; + return new Response('data: ' + 'x'.repeat(1024 * 1024), { headers: { 'Content-Type': 'text/event-stream' } }); + } }); + try { + await once(stream, 'error'); + await new Promise(resolve => setTimeout(resolve, 20)); + assert.equal(stream.readyState, 2); + assert.equal(requests, 1); + } finally { stream.close(); } +}); diff --git a/frontend/chat-ui/src/api.js b/frontend/chat-ui/src/api.js index 90aacb6..40c83cd 100644 --- a/frontend/chat-ui/src/api.js +++ b/frontend/chat-ui/src/api.js @@ -1,15 +1,11 @@ import { createAccountClient } from './accountClient.mjs'; +import { authenticatedStream } from './authenticatedStream.mjs'; export const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || ''; const API_KEY = import.meta.env.VITE_API_KEY || ''; export const accountClient = createAccountClient({ baseUrl: API_BASE_URL, apiKey: API_KEY }); export const setAccountToken = accountClient.setToken; -function buildUrl(path) { - const clean = path.startsWith('/') ? path : `/${path}`; - return API_BASE_URL ? `${API_BASE_URL.replace(/\/$/, '')}${clean}` : clean; -} - async function request(method, path, body) { return accountClient.request(method, path.startsWith('/') ? path : `/${path}`, body); } @@ -29,15 +25,9 @@ export const post = (path, body) => request('POST', path, body); export const put = (path, body) => request('PUT', path, body); export const del = (path) => request('DELETE', path); -// Create an EventSource for SSE. Many servers don't accept custom headers for SSE, -// so we append the API key as a query parameter named `x-api-key` when present. +// Native EventSource cannot carry Authorization. Use streaming fetch instead. export function stream(path) { - let url = buildUrl(path); - if (API_KEY) { - const hasQuery = url.includes('?'); - url = `${url}${hasQuery ? '&' : '?'}x-api-key=${encodeURIComponent(API_KEY)}`; - } - return new EventSource(url); + return authenticatedStream(accountClient, path); } const api = { get, post, put, del, stream }; diff --git a/frontend/chat-ui/src/authenticatedStream.mjs b/frontend/chat-ui/src/authenticatedStream.mjs new file mode 100644 index 0000000..b5a29f4 --- /dev/null +++ b/frontend/chat-ui/src/authenticatedStream.mjs @@ -0,0 +1,61 @@ +import { readFrames } from './sseFrames.mjs'; + +// EventSource-shaped subset used by ChatWindow, with bearer headers and an +// abortable lifetime. No credential is ever appended to a stream URL. +export function authenticatedStream(client, path, { fetchImpl = (...args) => fetch(...args), retryMs = 1000 } = {}) { + const events = new EventTarget(); + const controller = new AbortController(); + const state = { reader: null, retry: null, closed: false }; + events.readyState = 0; + events.close = () => { + state.closed = true; + events.readyState = 2; + clearTimeout(state.retry); + controller.abort(); + if (state.reader) void state.reader.cancel().catch(() => {}); + client.changes.removeEventListener('change', events.close); + }; + client.changes.addEventListener('change', events.close); + + const connection = { client, path, fetchImpl, retryMs, events, controller, state }; + queueMicrotask(() => connect(connection)); + return events; +} + +async function connect(connection) { + const { client, path, fetchImpl, retryMs, events, controller, state } = connection; + if (state.closed) return; + try { + if (!client.getToken()) { + events.close(); + events.dispatchEvent(new Event('error')); + return; + } + const response = await fetchImpl(client.url(path), { + headers: { ...client.headers(), Accept: 'text/event-stream' }, + signal: controller.signal, + redirect: 'error', + }); + if (state.closed) { await response.body?.cancel(); return; } + if (!response.ok || !response.headers.get('content-type')?.startsWith('text/event-stream')) { + await response.body?.cancel(); + if (response.status === 401) client.setToken(null); + events.close(); + events.dispatchEvent(new Event('error')); + return; + } + state.reader = response.body.getReader(); + events.readyState = 1; + events.dispatchEvent(new Event('open')); + await readFrames(state.reader, events, () => state.closed); + } catch (error) { + if (error instanceof RangeError) { events.close(); events.dispatchEvent(new Event('error')); } + else if (!state.closed) events.dispatchEvent(new Event('error')); + } finally { + if (state.reader) { await state.reader.cancel().catch(() => {}); state.reader = null; } + if (!state.closed) { + events.readyState = 0; + state.retry = setTimeout(() => connect(connection), retryMs); + } + } +} diff --git a/frontend/chat-ui/src/components/ChatWindow.jsx b/frontend/chat-ui/src/components/ChatWindow.jsx index ec5c9bb..18b10f2 100644 --- a/frontend/chat-ui/src/components/ChatWindow.jsx +++ b/frontend/chat-ui/src/components/ChatWindow.jsx @@ -5,7 +5,7 @@ * token -> { "id": string, "token": string } * message -> { "message": { id, role, content } } * - * Assumes server-side auth (no 'x-api-key' header sent). + * Streaming fetch carries the current backend account bearer in a header. * Errors while parsing events are logged to the console. */ import React, { useEffect, useRef } from 'react'; diff --git a/frontend/chat-ui/src/components/Sidebar.jsx b/frontend/chat-ui/src/components/Sidebar.jsx index 9a41bf7..313c65d 100644 --- a/frontend/chat-ui/src/components/Sidebar.jsx +++ b/frontend/chat-ui/src/components/Sidebar.jsx @@ -5,7 +5,7 @@ * GET /messages/{conversationId} -> [{ id, role, content }] * POST /conversations/{id}/pin -> body: { pinned: boolean } * - * All requests include an 'x-api-key' header and expect JSON responses. + * Private chat requests use the current backend account bearer and expect JSON. * Errors are caught and logged to the console without user-facing feedback. */ import React, { useEffect, useState } from 'react'; diff --git a/frontend/chat-ui/src/sseFrames.mjs b/frontend/chat-ui/src/sseFrames.mjs new file mode 100644 index 0000000..56bafc3 --- /dev/null +++ b/frontend/chat-ui/src/sseFrames.mjs @@ -0,0 +1,43 @@ +export async function readFrames(reader, events, isClosed) { + const decoder = new TextDecoder(); + let buffer = ''; + const frame = { type: 'message', data: [], size: 0 }; + while (!isClosed()) { + const result = await reader.read(); + if (isClosed() || result.done) break; + buffer += decoder.decode(result.value, { stream: true }); + buffer = consumeLines(buffer, frame, events, isClosed); + checkSize(buffer.length + frame.size); + } +} + + +function parseLine(frame, line, events) { + if (!line) { + if (frame.data.length) events.dispatchEvent(new MessageEvent(frame.type, { data: frame.data.join('\n') })); + frame.type = 'message'; frame.data = []; frame.size = 0; + } else if (line.startsWith('event:')) frame.type = line.slice(6).replace(/^ /, '') || 'message'; + else if (line.startsWith('data:')) { frame.data.push(line.slice(5).replace(/^ /, '')); frame.size += line.length; } +} + +function lineEnd(buffer) { + const end = buffer.search(/[\r\n]/); + return end < 0 || (buffer[end] === '\r' && end === buffer.length - 1) ? -1 : end; +} + +function consumeLines(buffer, frame, events, isClosed) { + while (!isClosed()) { + const end = lineEnd(buffer); + if (end < 0) break; + const line = buffer.slice(0, end); + checkSize(frame.size + line.length); + const width = buffer[end] === '\r' && buffer[end + 1] === '\n' ? 2 : 1; + buffer = buffer.slice(end + width); + parseLine(frame, line, events); + } + return buffer; +} + +function checkSize(size) { + if (size > 1024 * 1024) throw new RangeError('Stream frame too large'); +} diff --git a/frontend/chat-ui/sseFrames.test.js b/frontend/chat-ui/sseFrames.test.js new file mode 100644 index 0000000..5d38c71 --- /dev/null +++ b/frontend/chat-ui/sseFrames.test.js @@ -0,0 +1,31 @@ +const { test } = require('node:test'); +const assert = require('node:assert/strict'); + +// A network chunk may contain several independent, individually valid frames. +test('coalesced frames each below the size limit are delivered even when their transport chunk exceeds it', async () => { + const { readFrames } = await import('./src/sseFrames.mjs'); + const first = 'a'.repeat(600000); + const second = 'b'.repeat(600000); + const bytes = new TextEncoder().encode(`data: ${first}\n\ndata: ${second}\n\n`); + const body = new ReadableStream({ start(controller) { controller.enqueue(bytes); controller.close(); } }); + const events = new EventTarget(); + const messages = []; + events.addEventListener('message', event => messages.push(event.data)); + await readFrames(body.getReader(), events, () => false); + assert.deepEqual(messages, [first, second]); +}); + +test('one frame exceeding the limit across separate data lines and chunks is rejected before delivery', async () => { + const { readFrames } = await import('./src/sseFrames.mjs'); + const encoder = new TextEncoder(); + const body = new ReadableStream({ start(controller) { + controller.enqueue(encoder.encode('data: ' + 'a'.repeat(600000) + '\n')); + controller.enqueue(encoder.encode('data: ' + 'b'.repeat(600000) + '\n\n')); + controller.close(); + } }); + const events = new EventTarget(); + let messages = 0; + events.addEventListener('message', () => messages++); + await assert.rejects(readFrames(body.getReader(), events, () => false), /Stream frame too large/); + assert.equal(messages, 0); +});