web-document.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { afterEach, expect, it, vi } from 'vitest'
  2. import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { join } from 'node:path'
  5. import { authenticateWebHost, forwardWebRequest, serveWebDocument } from '../src/web-document.ts'
  6. const roots: string[] = []
  7. afterEach(async () => {
  8. vi.unstubAllGlobals()
  9. await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })))
  10. })
  11. it('serves the Web entry and assets without starting or contacting a Host', async () => {
  12. const root = await mkdtemp(join(tmpdir(), 'desktop-web-'))
  13. roots.push(root)
  14. await mkdir(join(root, 'assets'))
  15. await writeFile(join(root, 'index.html'), '<html><head></head><body><script src="assets/entry.js"></script></body></html>')
  16. await writeFile(join(root, 'assets/entry.js'), 'globalThis.entryLoaded = true')
  17. const fetch = vi.fn()
  18. vi.stubGlobal('fetch', fetch)
  19. const response = await serveWebDocument(new Request('dsh-app://app/'), root)
  20. const html = await response.text()
  21. expect(html.indexOf('Promise.withResolvers()')).toBeLessThan(html.indexOf('assets/entry.js'))
  22. expect(await (await serveWebDocument(new Request('dsh-app://app/assets/entry.js'), root)).text()).toContain('entryLoaded')
  23. expect(fetch).not.toHaveBeenCalled()
  24. expect((await serveWebDocument(new Request('dsh-app://app/%2e%2e%2fprivate'), root)).status).toBe(403)
  25. expect((await serveWebDocument(new Request('dsh-app://app/missing.js'), root)).status).toBe(404)
  26. })
  27. it('requires the Host authentication exchange and retains only its cookie value', async () => {
  28. const fetch = vi.fn().mockResolvedValueOnce(new Response(null, { status: 303, headers: { 'set-cookie': 'session=owned; HttpOnly; SameSite=Strict' } }))
  29. .mockResolvedValueOnce(new Response('unauthorized', { status: 401 }))
  30. vi.stubGlobal('fetch', fetch)
  31. expect(await authenticateWebHost('http://127.0.0.1:1234/?token=owned')).toBe('session=owned')
  32. await expect(authenticateWebHost('http://127.0.0.1:1234/')).rejects.toThrow('authentication failed')
  33. })
  34. it('forwards upload bytes and cancellation with Host credentials while keeping the response streaming', async () => {
  35. const body = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode('stream')); controller.close() } })
  36. const fetch = vi.fn().mockResolvedValue(new Response(body, { headers: { 'content-encoding': 'gzip', 'set-cookie': 'private' } }))
  37. vi.stubGlobal('fetch', fetch)
  38. const request = new Request('dsh-app://app/api/upload?name=file', {
  39. method: 'POST', body: 'upload bytes', headers: { origin: 'dsh-app://app', cookie: 'untrusted' },
  40. })
  41. const response = await forwardWebRequest(request, 'http://127.0.0.1:1234/?token=secret', 'session=owned')
  42. const [target, init] = fetch.mock.calls[0] as unknown as [URL, RequestInit]
  43. expect(target.href).toBe('http://127.0.0.1:1234/api/upload?name=file')
  44. expect(new Headers(init.headers).get('cookie')).toBe('session=owned')
  45. expect(new Headers(init.headers).get('origin')).toBeNull()
  46. expect(init.signal).toBe(request.signal)
  47. expect(init.body).toBe(request.body)
  48. expect(response.headers.get('set-cookie')).toBeNull()
  49. expect(response.headers.get('content-encoding')).toBeNull()
  50. expect(await response.text()).toBe('stream')
  51. })
  52. it('refuses another page origin without forwarding its request', async () => {
  53. const fetch = vi.fn()
  54. vi.stubGlobal('fetch', fetch)
  55. const response = await forwardWebRequest(new Request('dsh-app://app/api/read', { headers: { origin: 'https://other.example' } }), 'http://127.0.0.1:1234/', 'session=owned')
  56. expect(response.status).toBe(403)
  57. expect(fetch).not.toHaveBeenCalled()
  58. })