security.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { randomBytes } from 'node:crypto'
  2. import { mkdtempSync, rmSync } from 'node:fs'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import type Koa from 'koa'
  6. import { afterEach, describe, expect, it } from 'vitest'
  7. import { loadServerConfig, type ServerConfig } from '../../src/server/config.js'
  8. import {
  9. CsrfProtection,
  10. isAllowedHostHeader,
  11. isExactOrigin,
  12. applySecurityHeaders,
  13. } from '../../src/server/security.js'
  14. const configHomes: string[] = []
  15. afterEach(() => {
  16. for (const home of configHomes.splice(0)) rmSync(home, { recursive: true, force: true })
  17. })
  18. function isolatedEnv(overrides: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv {
  19. const home = mkdtempSync(join(tmpdir(), 'hermes-yaoyao-security-config-'))
  20. configHomes.push(home)
  21. return { HERMES_YAOYAO_HOME: home, ...overrides }
  22. }
  23. function config(overrides: Partial<ServerConfig> = {}): ServerConfig {
  24. return {
  25. host: '127.0.0.1',
  26. port: 8800,
  27. upstream: new URL('http://127.0.0.1:9119'),
  28. allowedHosts: new Set(),
  29. home: '/tmp/hermes-yaoyao-test',
  30. allowInsecureLan: false,
  31. insecureLan: false,
  32. production: false,
  33. ...overrides,
  34. }
  35. }
  36. describe('server security boundary', () => {
  37. it('accepts loopback/private hosts and configured DNS only', () => {
  38. const value = config({ allowedHosts: new Set(['yaoyao.internal']) })
  39. expect(isAllowedHostHeader('127.0.0.1:8800', value)).toBe(true)
  40. expect(isAllowedHostHeader('[::1]:8800', value)).toBe(true)
  41. expect(isAllowedHostHeader('192.168.10.8:8800', value)).toBe(true)
  42. expect(isAllowedHostHeader('yaoyao.internal:8800', value)).toBe(true)
  43. expect(isAllowedHostHeader('127.0.0.1:9999', value)).toBe(false)
  44. expect(isAllowedHostHeader('127.0.0.1', value)).toBe(true)
  45. expect(isAllowedHostHeader('192.168.10.8:443', value)).toBe(true)
  46. expect(isAllowedHostHeader('yaoyao.internal', value)).toBe(true)
  47. expect(isAllowedHostHeader('attacker.example:8800', value)).toBe(false)
  48. expect(isAllowedHostHeader('127.0.0.1@attacker.example', value)).toBe(false)
  49. })
  50. it('accepts the configured production reverse-proxy domain on public ports', () => {
  51. const value = config({ allowedHosts: new Set(['yaoyao-lc.samien.cn']) })
  52. expect(isAllowedHostHeader('yaoyao-lc.samien.cn', value)).toBe(true)
  53. expect(isAllowedHostHeader('yaoyao-lc.samien.cn:443', value)).toBe(true)
  54. expect(isAllowedHostHeader('other.samien.cn', value)).toBe(false)
  55. })
  56. it('accepts an HTTPS browser Origin behind an HTTP TLS-terminating proxy', () => {
  57. expect(isExactOrigin('https://yaoyao.internal', 'yaoyao.internal', false)).toBe(true)
  58. expect(isExactOrigin('https://attacker.example', 'yaoyao.internal', false)).toBe(false)
  59. })
  60. it('accepts a configured public Origin when the reverse proxy rewrites Host internally', () => {
  61. const allowed = new Set(['yaoyao-lc.samien.cn'])
  62. expect(isExactOrigin('http://yaoyao-lc.samien.cn', '10.1.5.100', false, allowed)).toBe(true)
  63. expect(isExactOrigin('http://attacker.example', '10.1.5.100', false, allowed)).toBe(false)
  64. })
  65. it('adds configured reverse-proxy domains to the WebSocket CSP', () => {
  66. const headers: Record<string, string> = {}
  67. const ctx = { host: '10.1.5.100', set(name: string, value: string) { headers[name] = value } } as unknown as Koa.Context
  68. applySecurityHeaders(ctx, false, new Set(['yaoyao-lc.samien.cn']))
  69. expect(headers['Content-Security-Policy']).toContain('ws://yaoyao-lc.samien.cn')
  70. expect(headers['Content-Security-Policy']).toContain('wss://yaoyao-lc.samien.cn')
  71. })
  72. it('requires an exact scheme and host Origin', () => {
  73. expect(isExactOrigin('http://127.0.0.1:8800', '127.0.0.1:8800', false)).toBe(true)
  74. expect(isExactOrigin('https://127.0.0.1:8800', '127.0.0.1:8800', false)).toBe(true)
  75. expect(isExactOrigin('http://127.0.0.1:8801', '127.0.0.1:8800', false)).toBe(false)
  76. expect(isExactOrigin('null', '127.0.0.1:8800', false)).toBe(false)
  77. })
  78. it('uses a signed double-submit CSRF token', () => {
  79. const csrf = new CsrfProtection(randomBytes(32), false)
  80. const headers: Record<string, unknown> = {}
  81. let incomingCookie = ''
  82. const ctx = {
  83. response: { headers },
  84. set(name: string, value: unknown) { headers[name.toLowerCase()] = value },
  85. get(name: string) { return name.toLowerCase() === 'cookie' ? incomingCookie : '' },
  86. } as unknown as Koa.Context
  87. const token = csrf.issue(ctx)
  88. const setCookie = (headers['set-cookie'] as string[])[0]!
  89. const cookie = setCookie.split(';', 1)[0]!
  90. incomingCookie = cookie
  91. expect(csrf.verify(cookie, token)).toBe(true)
  92. expect(csrf.issue(ctx)).toBe(token)
  93. expect(csrf.issue(ctx, true)).not.toBe(token)
  94. expect(csrf.verify(cookie, `${token}x`)).toBe(false)
  95. expect(csrf.verify(cookie.replace(/.$/, 'x'), token)).toBe(false)
  96. })
  97. it('refuses production LAN HTTP unless explicitly enabled', () => {
  98. expect(() => loadServerConfig(isolatedEnv({
  99. NODE_ENV: 'production',
  100. HERMES_YAOYAO_HOST: '0.0.0.0',
  101. }))).toThrow(/ALLOW_INSECURE_LAN/)
  102. expect(loadServerConfig(isolatedEnv({
  103. NODE_ENV: 'production',
  104. HERMES_YAOYAO_HOST: '0.0.0.0',
  105. HERMES_YAOYAO_ALLOW_INSECURE_LAN: '1',
  106. })).insecureLan).toBe(true)
  107. })
  108. it('defaults the YaoYao Web listener to loopback', () => {
  109. const value = loadServerConfig(isolatedEnv())
  110. expect(value.host).toBe('127.0.0.1')
  111. expect(value.insecureLan).toBe(false)
  112. expect(value.yaoyaoPluginSource).toBe(
  113. 'https://git.samien.cn/samien/hermes-yaoyao.git#hermes-plugins/yaoyao',
  114. )
  115. expect(value.releaseSource).toBe('https://git.samien.cn/samien/hermes-yaoyao.git')
  116. expect(value.allowRemoteUpdate).toBe(false)
  117. })
  118. it('rejects credentials embedded in the configured Yaoyao plugin source', () => {
  119. expect(() => loadServerConfig(isolatedEnv({
  120. HERMES_YAOYAO_PLUGIN_SOURCE: 'https://user:secret@git.example/yaoyao.git#hermes-plugins/yaoyao',
  121. }))).toThrow(/must not contain credentials/)
  122. })
  123. it('requires a credential-free HTTPS or SSH system release source', () => {
  124. expect(() => loadServerConfig(isolatedEnv({
  125. HERMES_YAOYAO_RELEASE_SOURCE: 'http://git.example/hermes-yaoyao.git',
  126. }))).toThrow(/HTTPS or SSH/)
  127. expect(() => loadServerConfig(isolatedEnv({
  128. HERMES_YAOYAO_RELEASE_SOURCE: 'https://user:secret@git.example/hermes-yaoyao.git',
  129. }))).toThrow(/must not contain credentials/)
  130. })
  131. })