app.test.ts 17 KB

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