|
| 1 | +import Fastify, { type FastifyInstance } from 'fastify'; |
| 2 | +import fastifyPlugin from 'fastify-plugin'; |
| 3 | +import open from 'open'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import type { Server } from '../../../types.js'; |
| 6 | +import devtoolsPlugin from '../devtoolsPlugin.js'; |
| 7 | + |
| 8 | +vi.mock('open', () => ({ |
| 9 | + default: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +const openMock = vi.mocked(open); |
| 13 | + |
| 14 | +async function createTestServer() { |
| 15 | + const instance = Fastify(); |
| 16 | + |
| 17 | + await instance.register( |
| 18 | + fastifyPlugin( |
| 19 | + async (fastify) => { |
| 20 | + fastify.decorate('wss', { |
| 21 | + messageServer: { |
| 22 | + broadcast: vi.fn(), |
| 23 | + }, |
| 24 | + } as unknown as FastifyInstance['wss']); |
| 25 | + }, |
| 26 | + { name: 'wss-plugin' } |
| 27 | + ) |
| 28 | + ); |
| 29 | + await instance.register(devtoolsPlugin, { |
| 30 | + delegate: {} as Server.Delegate, |
| 31 | + }); |
| 32 | + |
| 33 | + return instance; |
| 34 | +} |
| 35 | + |
| 36 | +describe('devtoolsPlugin /open-url', () => { |
| 37 | + let instance: FastifyInstance; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + openMock.mockResolvedValue({} as never); |
| 41 | + instance = await createTestServer(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + await instance.close(); |
| 46 | + vi.clearAllMocks(); |
| 47 | + }); |
| 48 | + |
| 49 | + it.each([ |
| 50 | + 'https://reactnative.dev/docs/tutorial', |
| 51 | + 'http://localhost:8081/docs?platform=ios#debugging', |
| 52 | + 'https://[::1]:8081/path', |
| 53 | + 'https://example.com/a%20path?value=%24safe#fragment', |
| 54 | + ])('should open the validated browser URL %s', async (url) => { |
| 55 | + const response = await instance.inject({ |
| 56 | + method: 'POST', |
| 57 | + url: '/open-url', |
| 58 | + payload: { url }, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(response.statusCode).toBe(200); |
| 62 | + expect(response.body).toBe('OK'); |
| 63 | + expect(openMock).toHaveBeenCalledOnce(); |
| 64 | + expect(openMock).toHaveBeenCalledWith(new URL(url).href); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should accept a text/plain JSON request body', async () => { |
| 68 | + const url = 'https://reactnative.dev/docs/debugging'; |
| 69 | + const response = await instance.inject({ |
| 70 | + method: 'POST', |
| 71 | + url: '/open-url', |
| 72 | + headers: { |
| 73 | + 'content-type': 'text/plain', |
| 74 | + }, |
| 75 | + payload: JSON.stringify({ url }), |
| 76 | + }); |
| 77 | + |
| 78 | + expect(response.statusCode).toBe(200); |
| 79 | + expect(openMock).toHaveBeenCalledWith(url); |
| 80 | + }); |
| 81 | + |
| 82 | + it.each([ |
| 83 | + ['bare executable', 'calc.exe'], |
| 84 | + ['Windows executable path', 'C:\\Windows\\System32\\calc.exe'], |
| 85 | + ['POSIX application path', '/Applications/Calculator.app'], |
| 86 | + ['UNC path', '\\\\server\\share\\payload.exe'], |
| 87 | + ['file URL', 'file:///etc/passwd'], |
| 88 | + ['JavaScript URL', 'javascript:alert(1)'], |
| 89 | + ['data URL', 'data:text/html,<script>alert(1)</script>'], |
| 90 | + ['custom URL scheme', 'ms-msdt:/id'], |
| 91 | + ['PowerShell subexpression', 'https://example.invalid/$(calc.exe)'], |
| 92 | + ['PowerShell environment expansion', 'https://example.invalid/$env:PATH'], |
| 93 | + ['PowerShell escape character', 'https://example.invalid/?value=`whoami'], |
| 94 | + ])('should reject a %s', async (_name, url) => { |
| 95 | + const response = await instance.inject({ |
| 96 | + method: 'POST', |
| 97 | + url: '/open-url', |
| 98 | + payload: { url }, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(response.statusCode).toBe(400); |
| 102 | + expect(response.body).toBe('Invalid URL'); |
| 103 | + expect(openMock).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it.each([ |
| 107 | + ['missing URL', {}], |
| 108 | + ['non-string URL', { url: 123 }], |
| 109 | + ['array body', [{ url: 'https://example.com' }]], |
| 110 | + ])('should reject a request with a %s', async (_name, payload) => { |
| 111 | + const response = await instance.inject({ |
| 112 | + method: 'POST', |
| 113 | + url: '/open-url', |
| 114 | + payload, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(response.statusCode).toBe(400); |
| 118 | + expect(response.body).toBe('Invalid URL'); |
| 119 | + expect(openMock).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should reject a request with no body', async () => { |
| 123 | + const response = await instance.inject({ |
| 124 | + method: 'POST', |
| 125 | + url: '/open-url', |
| 126 | + }); |
| 127 | + |
| 128 | + expect(response.statusCode).toBe(400); |
| 129 | + expect(response.body).toBe('Invalid URL'); |
| 130 | + expect(openMock).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should reject malformed text/plain JSON', async () => { |
| 134 | + const response = await instance.inject({ |
| 135 | + method: 'POST', |
| 136 | + url: '/open-url', |
| 137 | + headers: { |
| 138 | + 'content-type': 'text/plain', |
| 139 | + }, |
| 140 | + payload: 'not JSON', |
| 141 | + }); |
| 142 | + |
| 143 | + expect(response.statusCode).toBe(400); |
| 144 | + expect(response.body).toBe('Invalid URL'); |
| 145 | + expect(openMock).not.toHaveBeenCalled(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments