This lands the clue collection line on top of origin/dev (v0.32.1, DSH 0.2.2-rc.2). The 14 commits that carried it are squashed into this one because every one of them overlapped upstream's own changes (version, 19 dictionaries, CHANGELOG), and each intermediate commit would have failed to compile against rc.2's menu API. The pre-integration head is kept at backup/clues-uiux-refresh-pre-rebase. What the feature is: - a Clues tab that reads local files, Feishu tasks/docs and GitHub issues/pulls through one source registry, with a scope row, shared search, filters, paging and stable [[dsh-clue:...]] references; - one window host for detail, pack management, source configuration, new pack and sharing (preview and edit are separate paths: a read is never saved over); - .cluepack export/import with per-row resolvability previews; - a source runtime that owns dependency installs and connection authorization (gh, lark-cli device code) and only probes on an explicit read; - the public betterSidebarClues service and the windows capability (features gains clues / windowHost). Merged with upstream rather than over it: - the settings section keeps upstream's start-page takeover group and this branch's clue sources group; navigation is now six groups; - the dictionaries keep both sides' new keys (1211 each, verified equal across all 19), the external-plugin guide and CHANGELOG keep both histories, and service features list both capability sets. Adapted to 0.2.2-rc.2: - menu call sites use anchorPoint instead of getAnchorRect; - the shared SelectMenu mounts the host menu only while open, because rc.2's Menu reads the DOM during render and broke server-rendered settings surfaces; - the submenu flip workaround stays deleted (the host positions submenus now), pinned by an updated layout-css assertion. Version 0.33.0 (three synced spots) with a new 未发布 / Unreleased changelog section; package.json regains the ./clues export, lib/clues.js and the uqr dependency that upstream never had. Gates: typecheck, lint, 5410 unit tests, build, check:consumer-types and the full mount lane (35 passed) against dsh 0.2.2-rc.2 in Chromium.
277 lines
12 KiB
TypeScript
277 lines
12 KiB
TypeScript
/** The sidebar must use native Connection admission, never a reconstructed trust list. */
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { apply } from '../src/index.ts'
|
|
import type { SidebarHttpRequest, SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts'
|
|
|
|
/** No retired webRuntime service: rc.2 and newer both expose Connection admission. */
|
|
function mount(initialRejection: 401 | 403 | undefined = undefined) {
|
|
let rejection = initialRejection
|
|
const connection = { requestRejection: vi.fn((_request: SidebarHttpRequest) => rejection) }
|
|
const routes: SidebarWebRoute[] = []
|
|
const upgrades: SidebarWebUpgradeRoute[] = []
|
|
const effects: Array<() => void> = []
|
|
const injections: Array<{ deps: readonly string[]; activate: (ctx: unknown) => void }> = []
|
|
apply({
|
|
connection,
|
|
webServer: {
|
|
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
|
|
registerUpgrade: (route: SidebarWebUpgradeRoute) => { upgrades.push(route); return () => {} },
|
|
// The approved loopback read route compares the peer socket's local port
|
|
// with the real bound port.
|
|
port: 3080,
|
|
},
|
|
sessions: { get: () => undefined },
|
|
tools: { register: () => () => {} },
|
|
effect: (fn: () => void | (() => void)) => {
|
|
const cleanup = fn()
|
|
if (typeof cleanup === 'function') effects.push(cleanup)
|
|
},
|
|
inject: (deps: readonly string[], callback: (ctx: unknown) => void) => {
|
|
injections.push({ deps, activate: callback })
|
|
return () => {}
|
|
},
|
|
on: () => () => {},
|
|
get: () => undefined,
|
|
} as never)
|
|
return {
|
|
connection,
|
|
routes,
|
|
upgrades,
|
|
setRejection: (next: 401 | 403 | undefined) => { rejection = next },
|
|
/** Fire the child fiber that waits for `connection.authentication` (0.2.3+). */
|
|
activateProvider: (providerConnection: unknown = connection) => {
|
|
for (const injection of injections) {
|
|
if (injection.deps.includes('connection.authentication')) injection.activate({ connection: providerConnection })
|
|
}
|
|
},
|
|
cleanup: () => { for (const cleanup of effects) cleanup() },
|
|
}
|
|
}
|
|
|
|
function request(url: string, headers: SidebarHttpRequest['headers'] = { host: '127.0.0.1:3080' }) {
|
|
let reads = 0
|
|
const req: SidebarHttpRequest = {
|
|
method: 'POST',
|
|
url,
|
|
headers,
|
|
[Symbol.asyncIterator]: async function* () {
|
|
reads += 1
|
|
yield JSON.stringify({ sessionId: 'test-session' })
|
|
},
|
|
}
|
|
return { req, reads: () => reads }
|
|
}
|
|
|
|
function response() {
|
|
const out = { status: 0, body: '' }
|
|
return {
|
|
out,
|
|
res: {
|
|
writeHead: (status: number) => { out.status = status },
|
|
end: (body?: string | Uint8Array) => { out.body += String(body ?? '') },
|
|
} as never,
|
|
}
|
|
}
|
|
|
|
describe('native Connection admission', () => {
|
|
it('accepts the native verdict without webRuntime and preserves the request and method receiver', async () => {
|
|
const mounted = mount()
|
|
try {
|
|
const { req } = request('/sidebar/api/session.cwd', {
|
|
host: 'example.com',
|
|
origin: 'https://example.com',
|
|
cookie: 'native-authenticated-session',
|
|
})
|
|
const { res, out } = response()
|
|
await mounted.routes.find(route => route.path === '/sidebar/api')!.handler(req, res)
|
|
expect(out.status).toBe(200)
|
|
expect(mounted.connection.requestRejection).toHaveBeenCalledExactlyOnceWith(req)
|
|
expect(mounted.connection.requestRejection.mock.contexts[0]).toBe(mounted.connection)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it.each([401, 403] as const)('refuses native %s on every HTTP route and WebSocket before consuming data', async (rejection) => {
|
|
const mounted = mount(rejection)
|
|
try {
|
|
expect(mounted.routes.map(route => route.path)).toEqual([
|
|
// S7 share surface (four exact POST routes, fenced per request).
|
|
'/sidebar/clues/share/export-preview', '/sidebar/clues/share/export',
|
|
'/sidebar/clues/share/import-preview', '/sidebar/clues/share/import',
|
|
// Two approved exact loopback POST exceptions (not a generic API bypass).
|
|
'/sidebar/agent/clues/read', '/sidebar/agent/clues/commit',
|
|
'/sidebar/api', '/sidebar/sidechat-image', '/sidebar/upload', '/sidebar/write', '/sidebar/bundle',
|
|
'/sidebar/archive', '/sidebar/file', '/sidebar/html',
|
|
])
|
|
expect(mounted.upgrades.map(route => route.path)).toEqual([
|
|
'/sidebar/ws/agent-opens', '/sidebar/ws/fs-watch', '/sidebar/ws/sidechat',
|
|
])
|
|
const agentPaths = ['/sidebar/agent/clues/read', '/sidebar/agent/clues/commit']
|
|
for (const route of mounted.routes) {
|
|
if (agentPaths.includes(route.path)) continue
|
|
const input = request(route.path)
|
|
const { res, out } = response()
|
|
await route.handler(input.req, res)
|
|
expect(out.status, route.path).toBe(403)
|
|
expect(input.reads(), route.path).toBe(0)
|
|
expect(mounted.connection.requestRejection).toHaveBeenLastCalledWith(input.req)
|
|
}
|
|
// Both exact routes reject a non-loopback peer before reading a body.
|
|
for (const path of agentPaths) {
|
|
const input = request(path)
|
|
const { res, out } = response()
|
|
await mounted.routes.find(route => route.path === path)!.handler(input.req, res)
|
|
expect(out.status).toBe(403)
|
|
expect(input.reads()).toBe(0)
|
|
expect(mounted.connection.requestRejection).not.toHaveBeenCalledWith(input.req)
|
|
}
|
|
for (const route of mounted.upgrades) {
|
|
const input = request(route.path)
|
|
const socket = { destroy: vi.fn() }
|
|
await route.handler(input.req, socket, new Uint8Array())
|
|
expect(socket.destroy, route.path).toHaveBeenCalledOnce()
|
|
expect(input.reads(), route.path).toBe(0)
|
|
expect(mounted.connection.requestRejection).toHaveBeenLastCalledWith(input.req)
|
|
}
|
|
// One admission check per FENCED HTTP route and per WebSocket upgrade.
|
|
expect(mounted.connection.requestRejection).toHaveBeenCalledTimes(mounted.routes.length + mounted.upgrades.length - agentPaths.length)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it.each([null, false, 0, ''])('does not treat an unexpected falsy verdict (%s) as admission', async (verdict) => {
|
|
const mounted = mount()
|
|
try {
|
|
mounted.connection.requestRejection.mockReturnValue(verdict as never)
|
|
for (const route of mounted.routes) {
|
|
if (['/sidebar/agent/clues/read', '/sidebar/agent/clues/commit'].includes(route.path)) continue
|
|
const input = request(route.path)
|
|
const { res, out } = response()
|
|
await route.handler(input.req, res)
|
|
expect(out.status, route.path).toBe(403)
|
|
expect(input.reads(), route.path).toBe(0)
|
|
}
|
|
// The loopback route ignores the login verdict entirely; without a real
|
|
// peer socket its own gate still refuses it.
|
|
for (const path of ['/sidebar/agent/clues/read', '/sidebar/agent/clues/commit']) {
|
|
const input = request(path)
|
|
const { res, out } = response()
|
|
await mounted.routes.find(route => route.path === path)!.handler(input.req, res)
|
|
expect(out.status).toBe(403)
|
|
expect(input.reads()).toBe(0)
|
|
}
|
|
for (const route of mounted.upgrades) {
|
|
const input = request(route.path)
|
|
const socket = { destroy: vi.fn() }
|
|
await route.handler(input.req, socket, new Uint8Array())
|
|
expect(socket.destroy, route.path).toHaveBeenCalledOnce()
|
|
expect(input.reads(), route.path).toBe(0)
|
|
}
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it('has no old-host fallback when native admission is missing', async () => {
|
|
const mounted = mount()
|
|
try {
|
|
Reflect.deleteProperty(mounted.connection, 'requestRejection')
|
|
const input = request('/sidebar/api/session.cwd')
|
|
const { res, out } = response()
|
|
await expect(mounted.routes.find(route => route.path === '/sidebar/api')!.handler(input.req, res)).rejects.toThrow(TypeError)
|
|
expect(out.status).toBe(0)
|
|
expect(input.reads()).toBe(0)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it('falls back to native admit once requestRejection is gone', async () => {
|
|
const mounted = mount()
|
|
try {
|
|
let rejection: 401 | 403 | undefined
|
|
const admit = vi.fn(() => (rejection === undefined ? {} : { rejection }))
|
|
Reflect.deleteProperty(mounted.connection, 'requestRejection')
|
|
Object.assign(mounted.connection as unknown as Record<string, unknown>, { admit })
|
|
const route = mounted.routes.find(candidate => candidate.path === '/sidebar/api')!
|
|
for (const [verdict, expected] of [[undefined, 200], [401, 403], [403, 403]] as const) {
|
|
rejection = verdict
|
|
const { res, out } = response()
|
|
await route.handler(request('/sidebar/api/session.cwd').req, res)
|
|
expect(out.status).toBe(expected)
|
|
}
|
|
expect(admit).toHaveBeenCalledTimes(3)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it('admits through the injected authentication provider when admit reads the nested service', async () => {
|
|
const mounted = mount()
|
|
try {
|
|
let rejection: 401 | 403 | undefined
|
|
const providerAdmit = vi.fn(() => (rejection === undefined ? { peer: {} } : { rejection }))
|
|
// 0.2.3 host: the plugin's own context exposes `admit`, but cordis refuses the
|
|
// nested `connection.authentication` read unless the provider fiber declared it.
|
|
Reflect.deleteProperty(mounted.connection, 'requestRejection')
|
|
Object.assign(mounted.connection as unknown as Record<string, unknown>, {
|
|
admit: () => { throw new Error('cannot get property "connection.authentication" without inject') },
|
|
})
|
|
mounted.activateProvider({ admit: providerAdmit })
|
|
const route = mounted.routes.find(candidate => candidate.path === '/sidebar/api')!
|
|
for (const [verdict, expected] of [[undefined, 200], [401, 403], [403, 403]] as const) {
|
|
rejection = verdict
|
|
const { res, out } = response()
|
|
await route.handler(request('/sidebar/api/session.cwd').req, res)
|
|
expect(out.status).toBe(expected)
|
|
}
|
|
expect(providerAdmit).toHaveBeenCalledTimes(3)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it('does not bypass native refusals for former desktop, image, or port exceptions', async () => {
|
|
const mounted = mount(403)
|
|
try {
|
|
for (const headers of [
|
|
{ host: '127.0.0.1:3080', origin: 'dsh-app://app' },
|
|
{ host: '127.0.0.1:3080', origin: 'http://127.0.0.1' },
|
|
{
|
|
host: '127.0.0.1:3080', 'sec-fetch-site': 'cross-site',
|
|
'sec-fetch-mode': 'no-cors', 'sec-fetch-dest': 'image', referer: 'http://127.0.0.1:3080/',
|
|
},
|
|
]) {
|
|
const { req } = request('/sidebar/file', headers)
|
|
req.method = 'GET'
|
|
const { res, out } = response()
|
|
await mounted.routes.find(route => route.path === '/sidebar/file')!.handler(req, res)
|
|
expect(out.status).toBe(403)
|
|
expect(mounted.connection.requestRejection).toHaveBeenLastCalledWith(req)
|
|
expect(req.headers).toBe(headers)
|
|
}
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
|
|
it('reads native admission on every request, including authentication expiry', async () => {
|
|
const mounted = mount()
|
|
try {
|
|
const route = mounted.routes.find(route => route.path === '/sidebar/api')!
|
|
const { req } = request('/sidebar/api/session.cwd')
|
|
for (const [rejection, expected] of [[undefined, 200], [401, 403], [403, 403], [undefined, 200]] as const) {
|
|
mounted.setRejection(rejection)
|
|
const { res, out } = response()
|
|
await route.handler(req, res)
|
|
expect(out.status).toBe(expected)
|
|
}
|
|
expect(mounted.connection.requestRejection).toHaveBeenCalledTimes(4)
|
|
} finally {
|
|
mounted.cleanup()
|
|
}
|
|
})
|
|
})
|