app.test.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import request from 'supertest'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { createApplication, type ApplicationRuntime } from '../../src/server/app.js'
  7. import type { ServerConfig } from '../../src/server/config.js'
  8. interface RecordedRequest {
  9. path: string
  10. method: string
  11. search: URLSearchParams
  12. headers: Headers
  13. body?: unknown
  14. }
  15. const runtimes: ApplicationRuntime[] = []
  16. const homes: string[] = []
  17. afterEach(() => {
  18. for (const runtime of runtimes.splice(0)) runtime.close()
  19. for (const home of homes.splice(0)) rmSync(home, { recursive: true, force: true })
  20. })
  21. function makeConfig(upstream = 'http://127.0.0.1:9119'): ServerConfig {
  22. const home = mkdtempSync(join(tmpdir(), 'hermes-yaoyao-server-'))
  23. const mediaRoot = join(home, 'media')
  24. const attachmentsRoot = join(home, 'attachments')
  25. const imagesRoot = join(home, 'images')
  26. mkdirSync(mediaRoot)
  27. mkdirSync(attachmentsRoot)
  28. mkdirSync(imagesRoot)
  29. homes.push(home)
  30. return {
  31. host: '127.0.0.1',
  32. port: 8800,
  33. upstream: new URL(upstream),
  34. allowedHosts: new Set(),
  35. home,
  36. mediaRoot,
  37. attachmentsRoot,
  38. imagesRoot,
  39. mediaOwner: 'samien',
  40. allowInsecureLan: false,
  41. insecureLan: false,
  42. production: false,
  43. }
  44. }
  45. function jsonResponse(value: unknown, init: ResponseInit = {}): Response {
  46. return new Response(JSON.stringify(value), {
  47. ...init,
  48. headers: { 'content-type': 'application/json', ...init.headers },
  49. })
  50. }
  51. function fakeGateway(records: RecordedRequest[]): typeof fetch {
  52. return (async (input: string | URL | Request, init?: RequestInit) => {
  53. const url = new URL(input instanceof Request ? input.url : String(input))
  54. const headers = new Headers(init?.headers)
  55. const rawBody = typeof init?.body === 'string' ? init.body : undefined
  56. const recorded: RecordedRequest = {
  57. path: url.pathname,
  58. method: init?.method ?? 'GET',
  59. search: url.searchParams,
  60. headers,
  61. body: rawBody ? JSON.parse(rawBody) : undefined,
  62. }
  63. records.push(recorded)
  64. switch (url.pathname) {
  65. case '/api/status':
  66. return jsonResponse({ auth_required: true }, {
  67. headers: { 'set-cookie': 'hermes_session_at=access-cookie; Path=/; HttpOnly; SameSite=Lax' },
  68. })
  69. case '/api/auth/me':
  70. return jsonResponse({
  71. user_id: 'user-1', display_name: '测试用户', provider: 'basic',
  72. })
  73. case '/api/profiles':
  74. return jsonResponse({ profiles: [{ name: 'default', is_default: true }] })
  75. case '/api/plugins/yaoyao/profiles':
  76. return jsonResponse({ profiles: [{ name: 'default', agentName: '夭夭', isDefault: true }] })
  77. case '/api/auth/providers':
  78. return jsonResponse({ providers: [{ name: 'basic', supports_password: true }] })
  79. case '/auth/password-login':
  80. return jsonResponse({ ok: true })
  81. case '/api/profiles/sessions':
  82. case '/api/sessions':
  83. return jsonResponse({ sessions: [] })
  84. case '/api/sessions/session-1/messages':
  85. return jsonResponse({ messages: [], pagination: { returned: 0 } })
  86. case '/api/plugins/yaoyao/v1/rooms/11111111-1111-4111-8111-111111111111/messages':
  87. return jsonResponse({ accepted: true })
  88. default:
  89. return jsonResponse({ ok: true })
  90. }
  91. }) as typeof fetch
  92. }
  93. function cookieHeader(response: request.Response): string {
  94. const values = response.headers['set-cookie'] as unknown as string[]
  95. return values.map((value) => value.split(';', 1)[0]).join('; ')
  96. }
  97. describe('8800 BFF', () => {
  98. it('keeps existing CSRF tokens valid across an 8800 restart', async () => {
  99. const config = makeConfig()
  100. const first = createApplication({ config, fetchImpl: fakeGateway([]) })
  101. const bootstrap = await request(first.app.callback())
  102. .get('/api/app/bootstrap').set('Host', '127.0.0.1:8800').expect(200)
  103. const cookies = cookieHeader(bootstrap)
  104. first.close()
  105. const restarted = createApplication({ config, fetchImpl: fakeGateway([]) })
  106. runtimes.push(restarted)
  107. expect(restarted.csrf.verify(cookies, bootstrap.body.csrfToken)).toBe(true)
  108. })
  109. it('serves authenticated historical media only from the configured root', async () => {
  110. const config = makeConfig()
  111. writeFileSync(join(config.mediaRoot, '报告.txt'), '历史文件内容')
  112. const runtime = createApplication({ config, fetchImpl: fakeGateway([]) })
  113. runtimes.push(runtime)
  114. const bootstrap = await request(runtime.app.callback())
  115. .get('/api/app/bootstrap').set('Host', '127.0.0.1:8800').expect(200)
  116. const cookies = cookieHeader(bootstrap)
  117. const response = await request(runtime.app.callback())
  118. .get('/Users/samien/Agents/报告.txt').set('Host', '127.0.0.1:8800').set('Cookie', cookies).expect(200)
  119. expect(response.text).toBe('历史文件内容')
  120. expect(response.headers['content-disposition']).toContain('inline')
  121. await request(runtime.app.callback())
  122. .get('/Users/samien/Agents/../state.db').set('Host', '127.0.0.1:8800').set('Cookie', cookies).expect(404)
  123. })
  124. it('serves persisted Hermes attachment references from the restricted attachment root', async () => {
  125. const config = makeConfig()
  126. writeFileSync(join(config.attachmentsRoot, '报告.docx'), 'document bytes')
  127. const runtime = createApplication({ config, fetchImpl: fakeGateway([]) })
  128. runtimes.push(runtime)
  129. const bootstrap = await request(runtime.app.callback())
  130. .get('/api/app/bootstrap').set('Host', '127.0.0.1:8800').expect(200)
  131. const response = await request(runtime.app.callback())
  132. .get('/Users/samien/.hermes/attachments/报告.docx')
  133. .set('Host', '127.0.0.1:8800').set('Cookie', cookieHeader(bootstrap)).expect(200)
  134. expect(response.headers['content-length']).toBe(String(Buffer.byteLength('document bytes')))
  135. expect(response.headers['content-disposition']).toContain('inline')
  136. await request(runtime.app.callback())
  137. .get('/attachments/报告.docx')
  138. .set('Host', '127.0.0.1:8800').set('Cookie', cookieHeader(bootstrap)).expect(200)
  139. await request(runtime.app.callback())
  140. .get('/attachments/../state.db')
  141. .set('Host', '127.0.0.1:8800').set('Cookie', cookieHeader(bootstrap)).expect(404)
  142. })
  143. it('serves persisted Hermes image references from the restricted images root', async () => {
  144. const config = makeConfig()
  145. writeFileSync(join(config.imagesRoot, '照片.png'), 'image bytes')
  146. const runtime = createApplication({ config, fetchImpl: fakeGateway([]) })
  147. runtimes.push(runtime)
  148. const bootstrap = await request(runtime.app.callback())
  149. .get('/api/app/bootstrap').set('Host', '127.0.0.1:8800').expect(200)
  150. const response = await request(runtime.app.callback())
  151. .get('/Users/samien/.hermes/images/照片.png')
  152. .set('Host', '127.0.0.1:8800').set('Cookie', cookieHeader(bootstrap)).expect(200)
  153. expect(response.body.toString()).toBe('image bytes')
  154. await request(runtime.app.callback())
  155. .get('/Users/samien/.hermes/images/../state.db')
  156. .set('Host', '127.0.0.1:8800').set('Cookie', cookieHeader(bootstrap)).expect(404)
  157. })
  158. it('bootstraps sequentially without exposing the upstream address', async () => {
  159. const records: RecordedRequest[] = []
  160. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway(records) })
  161. runtimes.push(runtime)
  162. const response = await request(runtime.app.callback())
  163. .get('/api/app/bootstrap')
  164. .set('Host', '127.0.0.1:8800')
  165. .expect(200)
  166. expect(records.map((entry) => entry.path)).toEqual([
  167. '/api/status', '/api/auth/me', '/api/profiles', '/api/plugins/yaoyao/profiles',
  168. ])
  169. expect(response.body).toMatchObject({
  170. authRequired: true,
  171. authenticated: true,
  172. user: { user_id: 'user-1' },
  173. profiles: [{ name: 'default', agentName: '夭夭' }],
  174. groupUploadsEnabled: true,
  175. })
  176. expect(response.body.csrfToken).toEqual(expect.any(String))
  177. expect(response.body.status).toEqual({
  178. state: 'unknown',
  179. gatewayRunning: false,
  180. })
  181. expect(JSON.stringify(response.body)).not.toContain('9119')
  182. expect(response.headers['content-security-policy']).toContain("default-src 'self'")
  183. })
  184. it('omits opener isolation on plaintext LAN origins but keeps it on loopback', async () => {
  185. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway([]) })
  186. runtimes.push(runtime)
  187. const lan = await request(runtime.app.callback())
  188. .get('/healthz')
  189. .set('Host', '192.168.153.155:8800')
  190. .expect(200)
  191. expect(lan.headers['cross-origin-opener-policy']).toBeUndefined()
  192. const loopback = await request(runtime.app.callback())
  193. .get('/healthz')
  194. .set('Host', '127.0.0.1:8800')
  195. .expect(200)
  196. expect(loopback.headers['cross-origin-opener-policy']).toBe('same-origin')
  197. })
  198. it('degrades unsupported Gateway unread endpoints without browser 404 responses', async () => {
  199. const records: RecordedRequest[] = []
  200. const baseGateway = fakeGateway(records)
  201. const fetchImpl = (async (input: string | URL | Request, init?: RequestInit) => {
  202. const url = new URL(input instanceof Request ? input.url : String(input))
  203. if (url.pathname.startsWith('/api/session-unread')) {
  204. records.push({
  205. path: url.pathname,
  206. method: init?.method ?? 'GET',
  207. search: url.searchParams,
  208. headers: new Headers(init?.headers),
  209. })
  210. const status = (init?.method ?? 'GET') === 'PATCH' ? 405 : 404
  211. return jsonResponse({ detail: status === 405 ? 'Method Not Allowed' : 'Not Found' }, { status })
  212. }
  213. return baseGateway(input, init)
  214. }) as typeof fetch
  215. const runtime = createApplication({ config: makeConfig(), fetchImpl })
  216. runtimes.push(runtime)
  217. const unread = await request(runtime.app.callback())
  218. .get('/api/app/sessions/unread?profile=yaoer')
  219. .set('Host', '127.0.0.1:8800')
  220. .expect(200)
  221. expect(unread.body).toEqual({ profile: 'yaoer', total_unread: 0, sessions: [], supported: false })
  222. const bootstrap = await request(runtime.app.callback())
  223. .get('/api/app/bootstrap')
  224. .set('Host', '127.0.0.1:8800')
  225. .expect(200)
  226. const marked = await request(runtime.app.callback())
  227. .patch('/api/app/sessions/unread/session-1?profile=yaoer')
  228. .set('Host', '127.0.0.1:8800')
  229. .set('Origin', 'http://127.0.0.1:8800')
  230. .set('Cookie', cookieHeader(bootstrap))
  231. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  232. .send({ readMessageCount: 12 })
  233. .expect(200)
  234. expect(marked.body).toEqual({ ok: true, supported: false })
  235. })
  236. it('requires exact Origin and CSRF on login, then sequences the Gateway login', async () => {
  237. const records: RecordedRequest[] = []
  238. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway(records) })
  239. runtimes.push(runtime)
  240. const bootstrap = await request(runtime.app.callback())
  241. .get('/api/app/bootstrap')
  242. .set('Host', '127.0.0.1:8800')
  243. .expect(200)
  244. const cookies = cookieHeader(bootstrap)
  245. await request(runtime.app.callback())
  246. .post('/api/app/login')
  247. .set('Host', '127.0.0.1:8800')
  248. .set('Cookie', cookies)
  249. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  250. .send({ username: 'user', password: 'secret' })
  251. .expect(403)
  252. records.length = 0
  253. const response = await request(runtime.app.callback())
  254. .post('/api/app/login')
  255. .set('Host', '127.0.0.1:8800')
  256. .set('Origin', 'http://127.0.0.1:8800')
  257. .set('Cookie', cookies)
  258. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  259. .send({ username: 'user', password: 'secret' })
  260. .expect(200)
  261. expect(records.map((entry) => entry.path)).toEqual([
  262. '/api/auth/providers',
  263. '/auth/password-login',
  264. '/api/status',
  265. '/api/auth/me',
  266. '/api/profiles',
  267. '/api/plugins/yaoyao/profiles',
  268. ])
  269. expect(records[1]?.body).toEqual({ provider: 'basic', username: 'user', password: 'secret', next: '' })
  270. expect(response.body.authenticated).toBe(true)
  271. expect(JSON.stringify(response.body)).not.toContain('secret')
  272. })
  273. it('forces visibility and compressed-history query invariants', async () => {
  274. const records: RecordedRequest[] = []
  275. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway(records) })
  276. runtimes.push(runtime)
  277. await request(runtime.app.callback())
  278. .get('/api/app/sessions?exclude_sources=evil')
  279. .set('Host', '127.0.0.1:8800')
  280. .expect(200)
  281. expect(records.at(-1)?.path).toBe('/api/profiles/sessions')
  282. expect(records.at(-1)?.search.get('exclude_sources')).toBe('cron,ios_group')
  283. await request(runtime.app.callback())
  284. .get('/api/app/sessions/session-1/messages?offset=3&limit=50&order=oldest&include_compacted=false&profile=default')
  285. .set('Host', '127.0.0.1:8800')
  286. .expect(200)
  287. const history = records.at(-1)!
  288. expect(history.search.get('offset')).toBe('3')
  289. expect(history.search.get('limit')).toBe('50')
  290. expect(history.search.get('order')).toBe('latest')
  291. expect(history.search.get('include_compacted')).toBe('true')
  292. expect(history.search.get('profile')).toBe('default')
  293. })
  294. it('proxies topic pagination and forwards topic-scoped message queries', async () => {
  295. const records: RecordedRequest[] = []
  296. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway(records) })
  297. runtimes.push(runtime)
  298. const roomID = '11111111-1111-4111-8111-111111111111'
  299. const topicID = '22222222-2222-4222-8222-222222222222'
  300. await request(runtime.app.callback())
  301. .get(`/api/app/groups/rooms/${roomID}/topics?limit=25&cursor=next-topic&ignored=drop-me`)
  302. .set('Host', '127.0.0.1:8800')
  303. .expect(200)
  304. const topics = records.at(-1)!
  305. expect(topics.path).toBe(`/api/plugins/yaoyao/v1/rooms/${roomID}/topics`)
  306. expect(topics.search.get('limit')).toBe('25')
  307. expect(topics.search.get('cursor')).toBe('next-topic')
  308. expect(topics.search.has('ignored')).toBe(false)
  309. const bootstrap = await request(runtime.app.callback())
  310. .get('/api/app/bootstrap')
  311. .set('Host', '127.0.0.1:8800')
  312. .expect(200)
  313. await request(runtime.app.callback())
  314. .patch(`/api/app/groups/rooms/${roomID}/topics/${topicID}`)
  315. .set('Host', '127.0.0.1:8800')
  316. .set('Origin', 'http://127.0.0.1:8800')
  317. .set('Cookie', cookieHeader(bootstrap))
  318. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  319. .send({ requestId: '33333333-3333-4333-8333-333333333333', title: '新标题' })
  320. .expect(200)
  321. const renamed = records.at(-1)!
  322. expect(renamed.method).toBe('PATCH')
  323. expect(renamed.path).toBe(`/api/plugins/yaoyao/v1/rooms/${roomID}/topics/${topicID}`)
  324. expect(renamed.body).toEqual({ requestId: '33333333-3333-4333-8333-333333333333', title: '新标题' })
  325. await request(runtime.app.callback())
  326. .patch(`/api/app/groups/rooms/${roomID}/topics/${topicID}/read`)
  327. .set('Host', '127.0.0.1:8800')
  328. .set('Origin', 'http://127.0.0.1:8800')
  329. .set('Cookie', cookieHeader(bootstrap))
  330. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  331. .send({ requestId: '44444444-4444-4444-8444-444444444444', throughSeq: 42 })
  332. .expect(200)
  333. const markedRead = records.at(-1)!
  334. expect(markedRead.method).toBe('PATCH')
  335. expect(markedRead.path).toBe(`/api/plugins/yaoyao/v1/rooms/${roomID}/topics/${topicID}/read`)
  336. expect(markedRead.body).toEqual({ requestId: '44444444-4444-4444-8444-444444444444', throughSeq: 42 })
  337. await request(runtime.app.callback())
  338. .get(`/api/app/groups/rooms/${roomID}/messages?topicId=${topicID}&beforeSeq=40&limit=20`)
  339. .set('Host', '127.0.0.1:8800')
  340. .expect(200)
  341. const messages = records.at(-1)!
  342. expect(messages.path).toBe(`/api/plugins/yaoyao/v1/rooms/${roomID}/messages`)
  343. expect(messages.search.get('topicId')).toBe(topicID)
  344. expect(messages.search.get('beforeSeq')).toBe('40')
  345. expect(messages.search.get('limit')).toBe('20')
  346. })
  347. it('keeps unknown API routes out of the SPA fallback', async () => {
  348. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway([]) })
  349. runtimes.push(runtime)
  350. const response = await request(runtime.app.callback())
  351. .get('/api/anything')
  352. .set('Host', '127.0.0.1:8800')
  353. .expect(404)
  354. expect(response.body.code).toBe('not_found')
  355. })
  356. it('reuses a valid CSRF token across tabs instead of invalidating the first tab', async () => {
  357. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway([]) })
  358. runtimes.push(runtime)
  359. const first = await request(runtime.app.callback())
  360. .get('/api/app/bootstrap')
  361. .set('Host', '127.0.0.1:8800')
  362. .expect(200)
  363. const second = await request(runtime.app.callback())
  364. .get('/api/app/bootstrap')
  365. .set('Host', '127.0.0.1:8800')
  366. .set('Cookie', cookieHeader(first))
  367. .expect(200)
  368. expect(second.body.csrfToken).toBe(first.body.csrfToken)
  369. })
  370. it('refuses anonymous group uploads before reading a multipart body', async () => {
  371. const fetchImpl = (async (input: string | URL | Request) => {
  372. const url = new URL(input instanceof Request ? input.url : String(input))
  373. if (url.pathname === '/api/status') return jsonResponse({ auth_required: true })
  374. if (url.pathname === '/api/auth/me') return jsonResponse({ detail: 'Unauthorized' }, { status: 401 })
  375. return jsonResponse({ profiles: [] })
  376. }) as typeof fetch
  377. const runtime = createApplication({ config: makeConfig(), fetchImpl })
  378. runtimes.push(runtime)
  379. const bootstrap = await request(runtime.app.callback())
  380. .get('/api/app/bootstrap')
  381. .set('Host', '127.0.0.1:8800')
  382. .expect(200)
  383. await request(runtime.app.callback())
  384. .post('/api/app/group-uploads')
  385. .set('Host', '127.0.0.1:8800')
  386. .set('Origin', 'http://127.0.0.1:8800')
  387. .set('Cookie', cookieHeader(bootstrap))
  388. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  389. .attach('files', Buffer.from('hello'), { filename: 'hello.txt', contentType: 'text/plain' })
  390. .expect(401)
  391. })
  392. it('forces active file content to download instead of executing on the 8800 origin', async () => {
  393. const fetchImpl = (async () => new Response('<script>window.pwned=true</script>', {
  394. status: 200,
  395. headers: { 'content-type': 'text/html; charset=utf-8' },
  396. })) as typeof fetch
  397. const runtime = createApplication({ config: makeConfig(), fetchImpl })
  398. runtimes.push(runtime)
  399. const response = await request(runtime.app.callback())
  400. .get('/api/app/files/1/preview')
  401. .set('Host', '127.0.0.1:8800')
  402. .expect(200)
  403. expect(response.headers['content-type']).toMatch(/^application\/octet-stream/)
  404. expect(response.headers['content-disposition']).toContain('attachment')
  405. })
  406. it('stores group files opaquely and injects the local path only upstream', async () => {
  407. const records: RecordedRequest[] = []
  408. const runtime = createApplication({ config: makeConfig(), fetchImpl: fakeGateway(records) })
  409. runtimes.push(runtime)
  410. const bootstrap = await request(runtime.app.callback())
  411. .get('/api/app/bootstrap')
  412. .set('Host', '127.0.0.1:8800')
  413. .expect(200)
  414. const cookies = cookieHeader(bootstrap)
  415. const upload = await request(runtime.app.callback())
  416. .post('/api/app/group-uploads')
  417. .set('Host', '127.0.0.1:8800')
  418. .set('Origin', 'http://127.0.0.1:8800')
  419. .set('Cookie', cookies)
  420. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  421. .attach('files', Buffer.from('hello'), { filename: 'hello.txt', contentType: 'text/plain' })
  422. .expect(201)
  423. expect(upload.body.files[0]).toMatchObject({ name: 'hello.txt', size: 5 })
  424. expect(upload.body.files[0].path).toBeUndefined()
  425. await request(runtime.app.callback())
  426. .post('/api/app/groups/rooms/11111111-1111-4111-8111-111111111111/messages')
  427. .set('Host', '127.0.0.1:8800')
  428. .set('Origin', 'http://127.0.0.1:8800')
  429. .set('Cookie', cookies)
  430. .set('X-CSRF-Token', bootstrap.body.csrfToken)
  431. .send({
  432. requestId: '22222222-2222-4222-8222-222222222222',
  433. clientMessageId: '33333333-3333-4333-8333-333333333333',
  434. content: '请看文件',
  435. mentionAgentIds: [],
  436. uploadIds: [upload.body.files[0].id],
  437. })
  438. .expect(200)
  439. const sent = records.at(-1)?.body as { content: string; uploadIds?: unknown }
  440. expect(sent.uploadIds).toBeUndefined()
  441. expect(sent.content).toContain('请看文件')
  442. expect(sent.content).toContain(runtime.uploads.uploadRoot)
  443. })
  444. })