upstream.test.ts 1.4 KB

1234567891011121314151617181920212223242526272829
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { CookieJar, UpstreamClient } from '../../src/server/upstream.js'
  3. describe('upstream request boundary', () => {
  4. it('rejects a path that URL normalization would escape', async () => {
  5. const fetchImpl = vi.fn<typeof fetch>()
  6. const client = new UpstreamClient(new URL('http://127.0.0.1:9119'), fetchImpl)
  7. await expect(client.request('/api/sessions/..', new CookieJar())).rejects.toThrow(/normalization/i)
  8. expect(fetchImpl).not.toHaveBeenCalled()
  9. })
  10. it('derives proxy scheme from public 8800 TLS and forwards only the trusted peer IP', async () => {
  11. let headers = new Headers()
  12. const fetchImpl = vi.fn<typeof fetch>(async (_input, init) => {
  13. headers = new Headers(init?.headers)
  14. return new Response('{}', {
  15. status: 200,
  16. headers: { 'content-type': 'application/json', 'set-cookie': 'session=test; Path=/; HttpOnly; SameSite=Lax' },
  17. })
  18. })
  19. const jar = new CookieJar()
  20. const client = new UpstreamClient(new URL('http://127.0.0.1:9119'), fetchImpl, true)
  21. await client.request('/api/status', jar, { clientAddress: '::ffff:192.168.1.22' })
  22. expect(headers.get('x-forwarded-proto')).toBe('https')
  23. expect(headers.get('x-forwarded-for')).toBe('192.168.1.22')
  24. expect(jar.browserCookies[0]).toContain('Secure')
  25. expect(jar.browserCookies[0]).not.toMatch(/Domain=/i)
  26. })
  27. })