|
| 1 | +import type { IncomingMessage, ServerResponse } from 'http'; |
| 2 | + |
| 3 | +import { getRawBody } from './getRawBody'; |
| 4 | + |
| 5 | +/* |
| 6 | + * Prefix for Sentry Metro logs to make them stand out to the user. |
| 7 | + */ |
| 8 | +const S = '\u001b[45;1m SENTRY \u001b[0m'; |
| 9 | + |
| 10 | +let open: ((url: string) => Promise<void>) | undefined = undefined; |
| 11 | + |
| 12 | +/** |
| 13 | + * Open a URL in the system browser. |
| 14 | + * |
| 15 | + * Inspired by https://github.com/react-native-community/cli/blob/a856ce027a6b25f9363a8689311cdd4416c0fc89/packages/cli-server-api/src/openURLMiddleware.ts#L17 |
| 16 | + */ |
| 17 | +export async function openURLMiddleware(req: IncomingMessage, res: ServerResponse): Promise<void> { |
| 18 | + if (req.method !== 'POST') { |
| 19 | + res.writeHead(405); |
| 20 | + res.end('Method not allowed. Use POST.'); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (!open) { |
| 25 | + try { |
| 26 | + // oxlint-disable-next-line import/no-extraneous-dependencies |
| 27 | + const imported = require('open'); |
| 28 | + // Handle both CJS (`module.exports = fn`) and ESM default export (`{ default: fn }`) |
| 29 | + // oxlint-disable-next-line typescript-eslint(no-unsafe-member-access) |
| 30 | + open = typeof imported === 'function' ? imported : imported?.default; |
| 31 | + } catch (e) { |
| 32 | + // noop |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const body = await getRawBody(req); |
| 37 | + let url: string | undefined = undefined; |
| 38 | + |
| 39 | + try { |
| 40 | + const parsedBody = JSON.parse(body) as { url?: string }; |
| 41 | + url = parsedBody.url; |
| 42 | + } catch (e) { |
| 43 | + res.writeHead(400); |
| 44 | + res.end('Invalid request body. Expected a JSON object with a url key.'); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (!url) { |
| 49 | + res.writeHead(400); |
| 50 | + res.end('Invalid request body. Expected a JSON object with a url key.'); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + if (!url.startsWith('https://') && !url.startsWith('http://')) { |
| 55 | + res.writeHead(400); |
| 56 | + res.end('Invalid URL scheme. Only http:// and https:// URLs are allowed.'); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + if (!isTrustedSentryHost(url)) { |
| 61 | + // oxlint-disable-next-line no-console |
| 62 | + console.log( |
| 63 | + `${S} Untrusted host, not opening automatically. Open manually if you trust this URL: ${sanitizeForLog(url)}`, |
| 64 | + ); |
| 65 | + res.writeHead(200); |
| 66 | + res.end(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (!open) { |
| 71 | + // oxlint-disable-next-line no-console |
| 72 | + console.log(`${S} Could not open URL automatically. Open manually: ${sanitizeForLog(url)}`); |
| 73 | + res.writeHead(500); |
| 74 | + res.end('Failed to open URL. The "open" package is not available. Install it or open the URL manually.'); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + await open(url); |
| 80 | + } catch (e) { |
| 81 | + // oxlint-disable-next-line no-console |
| 82 | + console.log(`${S} Failed to open URL automatically. Open manually: ${sanitizeForLog(url)}`); |
| 83 | + res.writeHead(500); |
| 84 | + res.end('Failed to open URL.'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // oxlint-disable-next-line no-console |
| 89 | + console.log(`${S} Opened URL: ${sanitizeForLog(url)}`); |
| 90 | + res.writeHead(200); |
| 91 | + res.end(); |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Strip control characters to prevent terminal escape sequence injection when logging URLs. |
| 96 | + */ |
| 97 | +function sanitizeForLog(value: string): string { |
| 98 | + // oxlint-disable-next-line no-control-regex |
| 99 | + return value.replace(/[\x00-\x1f\x7f]/g, ''); |
| 100 | +} |
| 101 | + |
| 102 | +function isTrustedSentryHost(url: string): boolean { |
| 103 | + try { |
| 104 | + const { hostname } = new URL(url); |
| 105 | + return hostname === 'sentry.io' || hostname.endsWith('.sentry.io'); |
| 106 | + } catch (e) { |
| 107 | + return false; |
| 108 | + } |
| 109 | +} |
0 commit comments