Skip to content

Commit dcab5ef

Browse files
HazATkamilogorek
andauthored
fix: Handle DSN qs and show better error messages (#2639)
Co-authored-by: Kamil Ogórek <[email protected]>
1 parent 5f49020 commit dcab5ef

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

packages/core/test/lib/base.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TestClient } from '../mocks/client';
77
import { TestIntegration } from '../mocks/integration';
88
import { FakeTransport } from '../mocks/transport';
99

10-
const PUBLIC_DSN = 'https://username@domain/path';
10+
const PUBLIC_DSN = 'https://username@domain/123';
1111
declare var global: any;
1212

1313
jest.mock('@sentry/utils', () => {

packages/core/test/lib/sdk.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { TestClient } from '../mocks/client';
66

77
declare var global: any;
88

9-
const PUBLIC_DSN = 'https://username@domain/path';
9+
const PUBLIC_DSN = 'https://username@domain/123';
1010

1111
jest.mock('@sentry/hub', () => ({
1212
getCurrentHub(): {

packages/utils/src/dsn.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ export class Dsn implements DsnComponents {
7272
projectId = split.pop() as string;
7373
}
7474

75+
if (projectId) {
76+
const projectMatch = projectId.match(/^\d+/);
77+
if (projectMatch) {
78+
projectId = projectMatch[0];
79+
}
80+
}
81+
7582
this._fromComponents({ host, pass, path, projectId, port, protocol: protocol as DsnProtocol, user });
7683
}
7784

@@ -90,16 +97,20 @@ export class Dsn implements DsnComponents {
9097
private _validate(): void {
9198
['protocol', 'user', 'host', 'projectId'].forEach(component => {
9299
if (!this[component as keyof DsnComponents]) {
93-
throw new SentryError(ERROR_MESSAGE);
100+
throw new SentryError(`${ERROR_MESSAGE}: ${component} missing`);
94101
}
95102
});
96103

104+
if (!this.projectId.match(/^\d+$/)) {
105+
throw new SentryError(`${ERROR_MESSAGE}: Invalid projectId ${this.projectId}`);
106+
}
107+
97108
if (this.protocol !== 'http' && this.protocol !== 'https') {
98-
throw new SentryError(ERROR_MESSAGE);
109+
throw new SentryError(`${ERROR_MESSAGE}: Invalid protocol ${this.protocol}`);
99110
}
100111

101112
if (this.port && isNaN(parseInt(this.port, 10))) {
102-
throw new SentryError(ERROR_MESSAGE);
113+
throw new SentryError(`${ERROR_MESSAGE}: Invalid port ${this.port}`);
103114
}
104115
}
105116
}

packages/utils/test/dsn.test.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ describe('Dsn', () => {
1717
expect(dsn.pass).toBe('xyz');
1818
expect(dsn.host).toBe('sentry.io');
1919
expect(dsn.port).toBe('1234');
20-
expect(dsn.projectId).toBe('123');
2120
expect(dsn.path).toBe('');
21+
expect(dsn.projectId).toBe('123');
2222
});
2323

2424
test('applies partial components', () => {
@@ -33,8 +33,8 @@ describe('Dsn', () => {
3333
expect(dsn.pass).toBe('');
3434
expect(dsn.host).toBe('sentry.io');
3535
expect(dsn.port).toBe('');
36-
expect(dsn.projectId).toBe('123');
3736
expect(dsn.path).toBe('');
37+
expect(dsn.projectId).toBe('123');
3838
});
3939

4040
test('throws for missing components', () => {
@@ -107,8 +107,8 @@ describe('Dsn', () => {
107107
expect(dsn.pass).toBe('xyz');
108108
expect(dsn.host).toBe('sentry.io');
109109
expect(dsn.port).toBe('1234');
110-
expect(dsn.projectId).toBe('123');
111110
expect(dsn.path).toBe('');
111+
expect(dsn.projectId).toBe('123');
112112
});
113113

114114
test('parses a valid partial Dsn', () => {
@@ -133,6 +133,17 @@ describe('Dsn', () => {
133133
expect(dsn.projectId).toBe('321');
134134
});
135135

136+
test('with a query string', () => {
137+
const dsn = new Dsn('https://[email protected]/321?sample.rate=0.1&other=value');
138+
expect(dsn.protocol).toBe('https');
139+
expect(dsn.user).toBe('abc');
140+
expect(dsn.pass).toBe('');
141+
expect(dsn.host).toBe('sentry.io');
142+
expect(dsn.port).toBe('');
143+
expect(dsn.path).toBe('');
144+
expect(dsn.projectId).toBe('321');
145+
});
146+
136147
test('throws when provided invalid Dsn', () => {
137148
expect(() => new Dsn('[email protected]')).toThrow(SentryError);
138149
});
@@ -147,6 +158,7 @@ describe('Dsn', () => {
147158
test('throws for invalid fields', () => {
148159
expect(() => new Dsn('httpx://[email protected]/123')).toThrow(SentryError);
149160
expect(() => new Dsn('httpx://[email protected]:xxx/123')).toThrow(SentryError);
161+
expect(() => new Dsn('http://[email protected]/abc')).toThrow(SentryError);
150162
});
151163
});
152164

0 commit comments

Comments
 (0)