pairing.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { mkdtempSync, readFileSync, rmSync } 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 type { ApplicationRuntime } from '../../src/server/app.js'
  7. import type { ServerConfig } from '../../src/server/config.js'
  8. import { NodePairingStore } from '../../src/server/pairing.js'
  9. import { createAuthenticatedApplication as createApplication } from './authenticatedApplication.js'
  10. const homes: string[] = []
  11. const runtimes: ApplicationRuntime[] = []
  12. afterEach(() => {
  13. for (const runtime of runtimes.splice(0)) runtime.close()
  14. for (const home of homes.splice(0)) rmSync(home, { recursive: true, force: true })
  15. })
  16. function temporaryHome(): string {
  17. const home = mkdtempSync(join(tmpdir(), 'hermes-yaoyao-pairing-'))
  18. homes.push(home)
  19. return home
  20. }
  21. function config(home: string): ServerConfig {
  22. return {
  23. host: '127.0.0.1',
  24. port: 15300,
  25. upstream: new URL('http://127.0.0.1:9119'),
  26. allowedHosts: new Set(),
  27. home,
  28. mediaRoot: join(home, 'media'),
  29. attachmentsRoot: join(home, 'attachments'),
  30. imagesRoot: join(home, 'images'),
  31. mediaOwner: 'tester',
  32. allowInsecureLan: false,
  33. insecureLan: false,
  34. production: false,
  35. }
  36. }
  37. function cookies(response: request.Response): string {
  38. const values = response.headers['set-cookie'] as unknown as string[] | undefined
  39. return (values ?? []).map((value) => value.split(';', 1)[0]).join('; ')
  40. }
  41. describe('Hermes node pairing', () => {
  42. it('stores only encrypted delegated cookies and rejects replayed pairing secrets', () => {
  43. const home = temporaryHome()
  44. let now = 1_000
  45. const store = new NodePairingStore(home, () => now)
  46. const pairing = store.create('hermes_session_rt=refresh-secret', ['agents.read', 'history.read'])
  47. const claimed = store.claim({
  48. pairingID: pairing.id,
  49. secret: pairing.secret,
  50. deviceName: '测试 iPhone',
  51. })
  52. expect(store.authorize(claimed.device.id, claimed.token, 'history.read'))
  53. .toBe('hermes_session_rt=refresh-secret')
  54. expect(() => store.claim({
  55. pairingID: pairing.id,
  56. secret: pairing.secret,
  57. deviceName: '重放设备',
  58. })).toThrowError(/already used/i)
  59. expect(() => store.authorize(claimed.device.id, claimed.token, 'groups.execute'))
  60. .toThrowError(/lacks groups.execute/i)
  61. const persisted = readFileSync(join(home, 'paired-devices.json'), 'utf8')
  62. expect(persisted).not.toContain('refresh-secret')
  63. expect(persisted).not.toContain(claimed.token)
  64. now += 1
  65. const restored = new NodePairingStore(home, () => now)
  66. expect(restored.nodeID).toBe(store.nodeID)
  67. expect(restored.authorize(claimed.device.id, claimed.token, 'agents.read'))
  68. .toBe('hermes_session_rt=refresh-secret')
  69. })
  70. it('expires unclaimed QR secrets after two minutes', () => {
  71. const home = temporaryHome()
  72. let now = 10_000
  73. const store = new NodePairingStore(home, () => now)
  74. const pairing = store.create('hermes_session_at=access')
  75. now = pairing.expiresAt + 1
  76. expect(store.status(pairing.id)).toEqual({ state: 'expired' })
  77. expect(() => store.claim({
  78. pairingID: pairing.id,
  79. secret: pairing.secret,
  80. deviceName: '迟到的设备',
  81. })).toThrowError(/expired/i)
  82. })
  83. })