Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/src/host/authz.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export function install(rt) {
}
}

// #3145: channel.js refuses a handler reached without this marker. It is set whichever way
// `authzEnforced()` answers — a single-user install (false) is a supported pair, not a
// missing one — so the marker and `authorize` are installed together and never separately.
rt.authorizePaired = true
rt.authorize = function authorize(actor, endpoint, body) {
if (!rt.authzEnforced()) return ALLOW
const ep = String(endpoint ?? '')
Expand Down
3 changes: 3 additions & 0 deletions core/src/host/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ export function install(rt) {
// Actor is resolved once from the member session token; body.actorId / payload.actorId
// grant nothing and are ignored (#48).
const actor = sessionToken ? await rt.authenticate(sessionToken) : null
// authorize() and this handler are a pair (#3145): the gate below is what makes the
// fail-open-looking ownership test in chats.search safe; mounted alone, that test is skipped.
if (typeof rt.authorize !== 'function' || rt.authorizePaired !== true) return fail('This host mounts the channel handler without its authz module; install both (#3145).', 'bot/internal')
const gate = rt.authorize(actor, endpoint, body)
if (!gate.ok) return fail(gate.message, gate.code)
switch (endpoint) {
Expand Down
3 changes: 3 additions & 0 deletions core/test/diagnostics-clear-endpoint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function channel() {
warn: () => {},
refresh: async () => {},
readOnly: () => undefined,
// This stub host reaches the channel without the real authz module; the pairing marker says
// so deliberately (#3145), as a host that installed both would carry.
authorize: () => ({ ok: true }),
authorizePaired: true,
load: async () => {},
diagnostics: { clear: async () => ({ leaf: 'truncated', rotated: 'kept' }) },
}
Expand Down
146 changes: 146 additions & 0 deletions core/test/host-pairing.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// `authorize` and the channel handler are installed as a pair (#3145). The ownership test
// inside `chats.search` is fail-open in shape (`if (rt.authzEnforced?.() && !rt.sessionOwnedBy?.(…))`),
// so mounting the handler without the authz module would silently index every Bot's chat.
// The handler now refuses a host whose `authorize` did not come with the pairing marker.
//
// Real module, a temporary home and the real /api/bot route, as core/test/chat-index-writers.test.js.
// The guard must fire on a missing pair only: a host where `authzEnforced()` answers false is a
// supported single-user install and must answer exactly as it did before.
import assert from 'node:assert/strict'
import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { createRuntime, provideFlyBot } from '../index.js'
import { fakeContext } from './fake-ctx.js'

const T0 = 1_760_000_000_000

const line = (seq, text) => ({
type: 'user/message',
seq,
time: T0 + seq * 1000,
data: { content: [{ type: 'text', text }], source: { kind: 'user' } },
})
const SECRET = 'owner-2 private SECRETNEEDLE words'
const HISTORY = { solo: [line(0, 'first line'), line(1, 'second line')] }

const seed = () => ({
version: 1,
mainBotIds: ['a'],
bots: {
a: { id: 'a', sessionId: 'solo', name: 'Solo', role: 'Dev', model: 'deepseek/v4', instructions: '', createdAt: 1 },
},
rooms: {},
exchanges: [],
exchangeTurns: {},
questions: {},
prefs: {},
revision: 1,
})

const scratch = (t, name = 'ds-bot-pairing-') => {
const home = mkdtempSync(join(tmpdir(), name))
t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5, retryDelay: 20 }))
return home
}

// One host over `home`, with the real channel route as `start()` in chat-index-writers.test.js
// builds it. `unpaired` reaches the handler without the authz module's pairing: the marker is
// removed and a permissive `authorize` stands in its place — exactly the "mounted alone, or
// with a different, weaker authorize" host the issue describes.
async function start(t, home, { unpaired = false } = {}) {
writeFileSync(join(home, 'state.json'), JSON.stringify(seed()), { mode: 0o600 })
const ctx = fakeContext()
const warnings = []
ctx.logger.warn = (...args) => warnings.push(args.map(part => String(part)).join(' '))
let route
const registered = ctx.connection.fetch.register
ctx.connection.fetch.register = (options) => { route = options; return registered(options) }
Object.assign(ctx, {
llm: { listProviders: () => [], listModels: async () => [], resolveModelInfo: async () => ({}) },
sessionController: {
create: async () => ({ sessionId: 'fresh' }),
resolveAgent: async sessionId => ({ agent: { session: { id: sessionId, seq: (HISTORY[sessionId]?.at(-1)?.seq ?? -1) + 1 }, followup() {} } }),
selectModel: async () => {},
page: async ({ address, beforeSeq, maxMessages } = {}) => {
const all = (HISTORY[address?.sessionId] ?? []).filter(event => beforeSeq === undefined || event.seq < beforeSeq)
const records = maxMessages === undefined ? all : all.slice(-maxMessages)
return { records: records.map(event => ({ event })), hasMore: records.length < all.length }
},
},
workspaceRegistry: { get: () => undefined, resolveByPath: async () => undefined, create: async path => ({ id: 'w1', path }), archiveSession: async () => {} },
sessionTitle: { rename: async () => {} },
sessions: { flush: async () => {}, messageProjections: [] },
})
const rt = createRuntime(ctx, { home })
provideFlyBot(ctx, rt.flybot)
await rt.load()
if (unpaired) {
delete rt.authorizePaired
rt.authorize = () => ({ ok: true })
}
const host = { home, ctx, rt, route, warnings, stop: () => ctx.dispose() }
t.after(() => host.stop())
return host
}

const ask = (host, endpoint, payload, token) => host.route.fetch(new Request('http://localhost/api/bot', {
method: 'POST',
headers: token === undefined ? {} : { 'x-flybot-member-session': token },
body: JSON.stringify({ endpoint, payload }),
})).then(response => response.json())

test('a channel handler mounted without its authz pairing answers no data (#3145)', async (t) => {
const home = scratch(t)
const host = await start(t, home, { unpaired: true })
assert.notEqual(host.route, undefined, 'the channel registered its /api/bot route')

// No tree, no rows, and no successful answer: the loud refusal is the point, not its code.
const state = await ask(host, 'state', {})
assert.equal(state.ok, false, JSON.stringify(state))
assert.equal(state.value, undefined, JSON.stringify(state))

const search = await ask(host, 'chats.search', { query: 'first' })
assert.equal(search.ok, false, JSON.stringify(search))
assert.equal(search.value, undefined, JSON.stringify(search))

// The leak the pairing protects against: the private line must not appear anywhere.
assert.doesNotMatch(JSON.stringify(search), /SECRETNEEDLE/)
})

// A host that never got the authz module at all: `authorize` is absent rather than weak. The
// handler must still answer no data instead of falling through to its switch.
test('a channel handler with no authorize at all answers no data (#3145)', async (t) => {
const home = scratch(t)
const host = await start(t, home)
delete host.rt.authorize
delete host.rt.authorizePaired

const state = await ask(host, 'state', {})
assert.equal(state.ok, false, JSON.stringify(state))
assert.equal(state.value, undefined, JSON.stringify(state))

const search = await ask(host, 'chats.search', { query: 'first' })
assert.equal(search.ok, false, JSON.stringify(search))
assert.equal(search.value, undefined, JSON.stringify(search))
assert.doesNotMatch(JSON.stringify(search), new RegExp(SECRET))
})

// The boundary: `authzEnforced() === false` is a supported single-user install, not a
// missing pair. It carries the pairing marker and answers `state`/`chats.search` as today.
test('a single-user host carries the pair and answers state and chats.search as today (#3145)', async (t) => {
const home = scratch(t)
const host = await start(t, home)
assert.equal(host.rt.authzEnforced(), false, 'this case is the single-user install, or it proves nothing')
assert.equal(host.rt.authorizePaired, true, 'the pairing marker is set whether or not authz is enforced')

const state = await ask(host, 'state', {})
assert.equal(state.ok, true, JSON.stringify(state))
assert.deepEqual(state.value.bots.map(bot => bot.id), ['a'], JSON.stringify(state.value.bots))

const search = await ask(host, 'chats.search', { query: 'first' })
assert.equal(search.ok, true, JSON.stringify(search))
assert.deepEqual(search.value.rows.map(row => row.chat), ['a'], JSON.stringify(search.value))
assert.match(search.value.rows[0].text, /first line/)
})
2 changes: 1 addition & 1 deletion core/test/preview-fence-words.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test('the host room preview on the state answer keeps them too', async () => {
state,
ctx: { effect: (fn) => { fn(); return () => {} }, connection: { fetch: { register: (r) => { route = r; return () => {} } } } },
warn: () => {}, noteFailure: () => {},
readOnly: () => undefined, authenticate: async () => null, authorize: () => ({ ok: true }),
readOnly: () => undefined, authenticate: async () => null, authorize: () => ({ ok: true }), authorizePaired: true,
bootstrap: async () => {}, catalog: async () => [], loadSecrets: async () => {},
cachedCatalog: () => [], cachedDefault: () => null,
chatOf: id => `chat-${id}`, ownedLive: full => full, ownedView: full => full, load: async () => {}, refresh: async () => {},
Expand Down
2 changes: 1 addition & 1 deletion docs/subsystems/core-host.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ has lines (#2790, `core/test/room-read-floor-clamp.test.js`).

- The client's command palette searched each chat's `turnOutline` projection, whose prompt and response previews stop at 50/120 characters, so a hit past that budget — most of any long reply — found nothing. The index this file already keeps holds the whole text, so the search became a host read: `chat-index.js` lifts the term/any-term core of `searchOwnChat` into `matchedRows` (both reads share it; `searchOwnChat` answers byte for byte as before) and adds `rt.searchHomeChat(sessionIds, query, { limit })` — raw rows (`text, session, seq, time, who, turn`), best first, over the sessions the caller names. Each named session is caught up bare first, exactly as a Bot's parts are; no `endedAt` is known there, so nothing seals. Without an index there are no rows and the palette keeps its outline search.
- Every indexed line carries the turn it belongs to by number: `lines.turn` (and `marks.turnNo`, what the next catch-up resumes from), tracked from the number a `turn/start` event carries, `INDEX_VERSION` 4 → 5 — a bump rides the same drop-and-rebuild path as #2711's. A line written before any turn, or after a `turn/start` that carried no number, keeps `-1`, which the client reads as no turn to jump to.
- The channel endpoint is `chats.search` (`{ query }` → `{ rows: [{ chat, turn, text }], more }`, `text` an `excerpt` around the first term the row holds): the sessions are taken from `view(actor)` — the same narrowing the `state` answer carries — but each Bot's parts map to their chat id only while `sessionOwnedBy` passes, and every member/owned room's parts map unconditionally. The view keeps an other-owner Bot so a shared room renders, and that Bot's own chat is not room content, so the ownership test is what stops a signed-in member from reading another member's Bot chats here, while a room's own parts stay searchable because the room is shared. `authz.js` allows the endpoint for any signed-in actor the way `schedules` is allowed (the handler scopes), and the channel's `READS` whitelist carries it, so a second read-only window answers it too.
- The channel endpoint is `chats.search` (`{ query }` → `{ rows: [{ chat, turn, text }], more }`, `text` an `excerpt` around the first term the row holds): the sessions are taken from `view(actor)` — the same narrowing the `state` answer carries — but each Bot's parts map to their chat id only while `sessionOwnedBy` passes, and every member/owned room's parts map unconditionally. The view keeps an other-owner Bot so a shared room renders, and that Bot's own chat is not room content, so the ownership test is what stops a signed-in member from reading another member's Bot chats here, while a room's own parts stay searchable because the room is shared. `authorize` and this channel handler are installed as a pair (`authz.js` defines `rt.authorize`; `channel.js:326` calls it before its switch), and mounting the handler alone is fail-open — `channel.js:370` then skips every ownership test — so the two are never reused separately; a host whose `authzEnforced()` answers `false` is a supported single-user install, not a missing pair. `authz.js` allows the endpoint for any signed-in actor the way `schedules` is allowed (the handler scopes), and the channel's `READS` whitelist carries it, so a second read-only window answers it too.
- Covered by `core/test/chat-index-writers.test.js` (a line whose match sits 300 characters past the preview budget, with its turn; the endpoint scoped to the signed-in member through the real `/api/bot` route; and a shared room where the other member's Bot own chat stays unreadable while the room's own Session is found, with `earlier` and `sessionOwnedBy` still refusing it) and `core/test/authz.test.js` (allowed for a signed-in actor, refused without one).

### Out-of-app delivery, first half (#355)
Expand Down
5 changes: 4 additions & 1 deletion patches/ds-bot/0005-social-seams.patch
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ diff --git a/src/host/channel.js b/src/host/channel.js

function setPrefs(body) {
if (body.theme !== undefined) {
@@ -97,28 +97,72 @@ export function install(rt) {
@@ -97,28 +97,75 @@ export function install(rt) {
else if (body.motion === 'quiet' || body.motion === 'lively') state.prefs.motion = body.motion
else throw invalid('Motion must be quiet, normal or lively')
}
Expand Down Expand Up @@ -595,6 +595,9 @@ diff --git a/src/host/channel.js b/src/host/channel.js
+ // Actor is resolved once from the member session token; body.actorId / payload.actorId
+ // grant nothing and are ignored (#48).
+ const actor = sessionToken ? await rt.authenticate(sessionToken) : null
+ // authorize() and this handler are a pair (#3145): the gate below is what makes the
+ // fail-open-looking ownership test in chats.search safe; mounted alone, that test is skipped.
+ if (typeof rt.authorize !== 'function' || rt.authorizePaired !== true) return fail('This host mounts the channel handler without its authz module; install both (#3145).', 'bot/internal')
+ const gate = rt.authorize(actor, endpoint, body)
+ if (!gate.ok) return fail(gate.message, gate.code)
switch (endpoint) {
Expand Down
4 changes: 4 additions & 0 deletions patches/ds-bot/files/src/host/authz.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ export function install(rt) {
}
}

// #3145: channel.js refuses a handler reached without this marker. It is set whichever way
// `authzEnforced()` answers — a single-user install (false) is a supported pair, not a
// missing one — so the marker and `authorize` are installed together and never separately.
rt.authorizePaired = true
rt.authorize = function authorize(actor, endpoint, body) {
if (!rt.authzEnforced()) return ALLOW
const ep = String(endpoint ?? '')
Expand Down
3 changes: 3 additions & 0 deletions patches/ds-bot/files/test/diagnostics-clear-endpoint.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ function channel() {
warn: () => {},
refresh: async () => {},
readOnly: () => undefined,
// This stub host reaches the channel without the real authz module; the pairing marker says
// so deliberately (#3145), as a host that installed both would carry.
authorize: () => ({ ok: true }),
authorizePaired: true,
load: async () => {},
diagnostics: { clear: async () => ({ leaf: 'truncated', rotated: 'kept' }) },
}
Expand Down
Loading
Loading