security.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { randomBytes } from 'node:crypto'
  2. import type Koa from 'koa'
  3. import { describe, expect, it } from 'vitest'
  4. import { loadServerConfig, type ServerConfig } from '../../src/server/config.js'
  5. import {
  6. CsrfProtection,
  7. isAllowedHostHeader,
  8. isExactOrigin,
  9. } from '../../src/server/security.js'
  10. function config(overrides: Partial<ServerConfig> = {}): ServerConfig {
  11. return {
  12. host: '127.0.0.1',
  13. port: 8800,
  14. upstream: new URL('http://127.0.0.1:9119'),
  15. allowedHosts: new Set(),
  16. home: '/tmp/hermes-yaoyao-test',
  17. allowInsecureLan: false,
  18. insecureLan: false,
  19. production: false,
  20. ...overrides,
  21. }
  22. }
  23. describe('server security boundary', () => {
  24. it('accepts loopback/private hosts and configured DNS only', () => {
  25. const value = config({ allowedHosts: new Set(['yaoyao.internal']) })
  26. expect(isAllowedHostHeader('127.0.0.1:8800', value)).toBe(true)
  27. expect(isAllowedHostHeader('[::1]:8800', value)).toBe(true)
  28. expect(isAllowedHostHeader('192.168.10.8:8800', value)).toBe(true)
  29. expect(isAllowedHostHeader('yaoyao.internal:8800', value)).toBe(true)
  30. expect(isAllowedHostHeader('127.0.0.1:9999', value)).toBe(false)
  31. expect(isAllowedHostHeader('127.0.0.1', value)).toBe(true)
  32. expect(isAllowedHostHeader('192.168.10.8:443', value)).toBe(true)
  33. expect(isAllowedHostHeader('yaoyao.internal', value)).toBe(true)
  34. expect(isAllowedHostHeader('attacker.example:8800', value)).toBe(false)
  35. expect(isAllowedHostHeader('127.0.0.1@attacker.example', value)).toBe(false)
  36. })
  37. it('accepts the configured production reverse-proxy domain on public ports', () => {
  38. const value = config({ allowedHosts: new Set(['yaoyao-lc.samien.cn']) })
  39. expect(isAllowedHostHeader('yaoyao-lc.samien.cn', value)).toBe(true)
  40. expect(isAllowedHostHeader('yaoyao-lc.samien.cn:443', value)).toBe(true)
  41. expect(isAllowedHostHeader('other.samien.cn', value)).toBe(false)
  42. })
  43. it('accepts an HTTPS browser Origin behind an HTTP TLS-terminating proxy', () => {
  44. expect(isExactOrigin('https://yaoyao.internal', 'yaoyao.internal', false)).toBe(true)
  45. expect(isExactOrigin('https://attacker.example', 'yaoyao.internal', false)).toBe(false)
  46. })
  47. it('requires an exact scheme and host Origin', () => {
  48. expect(isExactOrigin('http://127.0.0.1:8800', '127.0.0.1:8800', false)).toBe(true)
  49. expect(isExactOrigin('https://127.0.0.1:8800', '127.0.0.1:8800', false)).toBe(true)
  50. expect(isExactOrigin('http://127.0.0.1:8801', '127.0.0.1:8800', false)).toBe(false)
  51. expect(isExactOrigin('null', '127.0.0.1:8800', false)).toBe(false)
  52. })
  53. it('uses a signed double-submit CSRF token', () => {
  54. const csrf = new CsrfProtection(randomBytes(32), false)
  55. const headers: Record<string, unknown> = {}
  56. let incomingCookie = ''
  57. const ctx = {
  58. response: { headers },
  59. set(name: string, value: unknown) { headers[name.toLowerCase()] = value },
  60. get(name: string) { return name.toLowerCase() === 'cookie' ? incomingCookie : '' },
  61. } as unknown as Koa.Context
  62. const token = csrf.issue(ctx)
  63. const setCookie = (headers['set-cookie'] as string[])[0]!
  64. const cookie = setCookie.split(';', 1)[0]!
  65. incomingCookie = cookie
  66. expect(csrf.verify(cookie, token)).toBe(true)
  67. expect(csrf.issue(ctx)).toBe(token)
  68. expect(csrf.issue(ctx, true)).not.toBe(token)
  69. expect(csrf.verify(cookie, `${token}x`)).toBe(false)
  70. expect(csrf.verify(cookie.replace(/.$/, 'x'), token)).toBe(false)
  71. })
  72. it('refuses production LAN HTTP unless explicitly enabled', () => {
  73. expect(() => loadServerConfig({
  74. NODE_ENV: 'production',
  75. HERMES_YAOYAO_HOST: '0.0.0.0',
  76. })).toThrow(/ALLOW_INSECURE_LAN/)
  77. expect(loadServerConfig({
  78. NODE_ENV: 'production',
  79. HERMES_YAOYAO_HOST: '0.0.0.0',
  80. HERMES_YAOYAO_ALLOW_INSECURE_LAN: '1',
  81. }).insecureLan).toBe(true)
  82. })
  83. })