Skip to content

Commit 0f9a3df

Browse files
Naddiseolforst
andauthored
feat(node): Check for invalid url in node transport (#6623)
Co-authored-by: Luca Forstner <[email protected]>
1 parent 55d708f commit 0f9a3df

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/node/src/transports/http.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ function streamFromBody(body: Uint8Array | string): Readable {
4747
* Creates a Transport that uses native the native 'http' and 'https' modules to send events to Sentry.
4848
*/
4949
export function makeNodeTransport(options: NodeTransportOptions): Transport {
50-
const urlSegments = new URL(options.url);
50+
let urlSegments: URL;
51+
52+
try {
53+
urlSegments = new URL(options.url);
54+
} catch (e) {
55+
// eslint-disable-next-line no-console
56+
console.warn(
57+
'[@sentry/node]: Invalid dsn or tunnel option, will not send any events. The tunnel option must be a full URL when used.',
58+
);
59+
return createTransport(options, () => Promise.resolve({}));
60+
}
61+
5162
const isHttps = urlSegments.protocol === 'https:';
5263

5364
// Proxy prioritization: http => `options.proxy` | `process.env.http_proxy`

packages/node/test/transports/http.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,20 @@ describe('makeNewHttpTransport()', () => {
399399
);
400400
});
401401
});
402+
403+
it('should create a noop transport if an invalid url is passed', async () => {
404+
const requestSpy = jest.spyOn(http, 'request');
405+
const transport = makeNodeTransport({ ...defaultOptions, url: 'foo' });
406+
await transport.send(EVENT_ENVELOPE);
407+
expect(requestSpy).not.toHaveBeenCalled();
408+
});
409+
410+
it('should warn if an invalid url is passed', async () => {
411+
const consoleWarnSpy = jest.spyOn(console, 'warn');
412+
const transport = makeNodeTransport({ ...defaultOptions, url: 'invalid url' });
413+
await transport.send(EVENT_ENVELOPE);
414+
expect(consoleWarnSpy).toHaveBeenCalledWith(
415+
'[@sentry/node]: Invalid dsn or tunnel option, will not send any events. The tunnel option must be a full URL when used.',
416+
);
417+
});
402418
});

0 commit comments

Comments
 (0)