|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getEnvelopeEndpointWithUrlEncodedAuth } from '../../../src/api'; |
| 3 | +import { makeDsn } from '../../../src/utils/dsn'; |
| 4 | +import { createEnvelope, serializeEnvelope } from '../../../src/utils/envelope'; |
| 5 | +import { handleTunnelRequest } from '../../../src/utils/tunnel'; |
| 6 | + |
| 7 | +const TEST_DSN = 'https://public@dsn.ingest.sentry.io/1337'; |
| 8 | + |
| 9 | +function makeEnvelopeRequest(envelopeHeader: Record<string, unknown>): Request { |
| 10 | + const envelope = createEnvelope(envelopeHeader, []); |
| 11 | + const body = serializeEnvelope(envelope); |
| 12 | + return new Request('http://localhost/tunnel', { method: 'POST', body }); |
| 13 | +} |
| 14 | + |
| 15 | +describe('handleTunnelRequest', () => { |
| 16 | + let fetchMock: ReturnType<typeof vi.fn>; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + fetchMock = vi.fn(); |
| 20 | + vi.stubGlobal('fetch', fetchMock); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + vi.restoreAllMocks(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('forwards the envelope to Sentry and returns the upstream response', async () => { |
| 28 | + const upstreamResponse = new Response('ok', { status: 200 }); |
| 29 | + fetchMock.mockResolvedValueOnce(upstreamResponse); |
| 30 | + |
| 31 | + const result = await handleTunnelRequest({ |
| 32 | + request: makeEnvelopeRequest({ dsn: TEST_DSN }), |
| 33 | + allowedDsns: [TEST_DSN], |
| 34 | + }); |
| 35 | + |
| 36 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 37 | + const [url, init] = fetchMock.mock.calls[0]!; |
| 38 | + expect(url).toBe(getEnvelopeEndpointWithUrlEncodedAuth(makeDsn(TEST_DSN)!)); |
| 39 | + expect(init.method).toBe('POST'); |
| 40 | + expect(init.headers).toEqual({ 'Content-Type': 'application/x-sentry-envelope' }); |
| 41 | + expect(init.body).toBeInstanceOf(Uint8Array); |
| 42 | + |
| 43 | + expect(result).toBe(upstreamResponse); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns 500 when allowedDsns is empty', async () => { |
| 47 | + const result = await handleTunnelRequest({ |
| 48 | + request: makeEnvelopeRequest({ dsn: TEST_DSN }), |
| 49 | + allowedDsns: [], |
| 50 | + }); |
| 51 | + |
| 52 | + expect(result).toBeInstanceOf(Response); |
| 53 | + expect(result.status).toBe(500); |
| 54 | + expect(await result.text()).toBe('Tunnel not configured'); |
| 55 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('returns 400 when the envelope has no DSN in the header', async () => { |
| 59 | + const result = await handleTunnelRequest({ |
| 60 | + request: makeEnvelopeRequest({}), |
| 61 | + allowedDsns: [TEST_DSN], |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result).toBeInstanceOf(Response); |
| 65 | + expect(result.status).toBe(400); |
| 66 | + expect(await result.text()).toBe('Invalid envelope: missing DSN'); |
| 67 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns 400 when the envelope body contains malformed JSON', async () => { |
| 71 | + const result = await handleTunnelRequest({ |
| 72 | + request: new Request('http://localhost/tunnel', { method: 'POST', body: 'not valid envelope data{{{' }), |
| 73 | + allowedDsns: [TEST_DSN], |
| 74 | + }); |
| 75 | + |
| 76 | + expect(result).toBeInstanceOf(Response); |
| 77 | + expect(result.status).toBe(400); |
| 78 | + expect(await result.text()).toBe('Invalid envelope'); |
| 79 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('returns 403 when the envelope DSN is not in allowedDsns', async () => { |
| 83 | + const result = await handleTunnelRequest({ |
| 84 | + request: makeEnvelopeRequest({ dsn: 'https://other@example.com/9999' }), |
| 85 | + allowedDsns: [TEST_DSN], |
| 86 | + }); |
| 87 | + |
| 88 | + expect(result).toBeInstanceOf(Response); |
| 89 | + expect(result.status).toBe(403); |
| 90 | + expect(await result.text()).toBe('DSN not allowed'); |
| 91 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('returns 403 when the DSN string cannot be parsed into components', async () => { |
| 95 | + const malformedDsn = 'not-a-valid-dsn'; |
| 96 | + |
| 97 | + const result = await handleTunnelRequest({ |
| 98 | + request: makeEnvelopeRequest({ dsn: malformedDsn }), |
| 99 | + allowedDsns: [malformedDsn], |
| 100 | + }); |
| 101 | + |
| 102 | + expect(result).toBeInstanceOf(Response); |
| 103 | + expect(result.status).toBe(403); |
| 104 | + expect(await result.text()).toBe('Invalid DSN'); |
| 105 | + expect(fetchMock).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('forwards the envelope when multiple DSNs are configured', async () => { |
| 109 | + const otherDsn = 'https://other@example.com/9999'; |
| 110 | + const upstreamResponse = new Response('ok', { status: 200 }); |
| 111 | + fetchMock.mockResolvedValueOnce(upstreamResponse); |
| 112 | + |
| 113 | + const result = await handleTunnelRequest({ |
| 114 | + request: makeEnvelopeRequest({ dsn: TEST_DSN }), |
| 115 | + allowedDsns: [otherDsn, TEST_DSN], |
| 116 | + }); |
| 117 | + |
| 118 | + expect(fetchMock).toHaveBeenCalledOnce(); |
| 119 | + const [url] = fetchMock.mock.calls[0]!; |
| 120 | + expect(url).toBe(getEnvelopeEndpointWithUrlEncodedAuth(makeDsn(TEST_DSN)!)); |
| 121 | + expect(result).toBe(upstreamResponse); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns 500 when fetch throws a network error', async () => { |
| 125 | + fetchMock.mockRejectedValueOnce(new Error('Network failure')); |
| 126 | + |
| 127 | + const result = await handleTunnelRequest({ |
| 128 | + request: makeEnvelopeRequest({ dsn: TEST_DSN }), |
| 129 | + allowedDsns: [TEST_DSN], |
| 130 | + }); |
| 131 | + |
| 132 | + expect(result).toBeInstanceOf(Response); |
| 133 | + expect(result.status).toBe(500); |
| 134 | + expect(await result.text()).toBe('Failed to forward envelope to Sentry'); |
| 135 | + }); |
| 136 | +}); |
0 commit comments