Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle DSN qs and show better error messages #2639

Merged
merged 1 commit into from Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/test/lib/base.test.ts
Expand Up @@ -7,7 +7,7 @@ import { TestClient } from '../mocks/client';
import { TestIntegration } from '../mocks/integration';
import { FakeTransport } from '../mocks/transport';

const PUBLIC_DSN = 'https://username@domain/path';
const PUBLIC_DSN = 'https://username@domain/123';
declare var global: any;

jest.mock('@sentry/utils', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/lib/sdk.test.ts
Expand Up @@ -6,7 +6,7 @@ import { TestClient } from '../mocks/client';

declare var global: any;

const PUBLIC_DSN = 'https://username@domain/path';
const PUBLIC_DSN = 'https://username@domain/123';

jest.mock('@sentry/hub', () => ({
getCurrentHub(): {
Expand Down
17 changes: 14 additions & 3 deletions packages/utils/src/dsn.ts
Expand Up @@ -72,6 +72,13 @@ export class Dsn implements DsnComponents {
projectId = split.pop() as string;
}

if (projectId) {
const projectMatch = projectId.match(/^\d+/);
if (projectMatch) {
projectId = projectMatch[0];
}
}

this._fromComponents({ host, pass, path, projectId, port, protocol: protocol as DsnProtocol, user });
}

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

if (!this.projectId.match(/^\d+$/)) {
throw new SentryError(`${ERROR_MESSAGE}: Invalid projectId ${this.projectId}`);
}

if (this.protocol !== 'http' && this.protocol !== 'https') {
throw new SentryError(ERROR_MESSAGE);
throw new SentryError(`${ERROR_MESSAGE}: Invalid protocol ${this.protocol}`);
}

if (this.port && isNaN(parseInt(this.port, 10))) {
throw new SentryError(ERROR_MESSAGE);
throw new SentryError(`${ERROR_MESSAGE}: Invalid port ${this.port}`);
}
}
}
18 changes: 15 additions & 3 deletions packages/utils/test/dsn.test.ts
Expand Up @@ -17,8 +17,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

test('applies partial components', () => {
Expand All @@ -33,8 +33,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

test('throws for missing components', () => {
Expand Down Expand Up @@ -107,8 +107,8 @@ describe('Dsn', () => {
expect(dsn.pass).toBe('xyz');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('1234');
expect(dsn.projectId).toBe('123');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('123');
});

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

test('with a query string', () => {
const dsn = new Dsn('https://abc@sentry.io/321?sample.rate=0.1&other=value');
expect(dsn.protocol).toBe('https');
expect(dsn.user).toBe('abc');
expect(dsn.pass).toBe('');
expect(dsn.host).toBe('sentry.io');
expect(dsn.port).toBe('');
expect(dsn.path).toBe('');
expect(dsn.projectId).toBe('321');
});

test('throws when provided invalid Dsn', () => {
expect(() => new Dsn('some@random.dsn')).toThrow(SentryError);
});
Expand All @@ -147,6 +158,7 @@ describe('Dsn', () => {
test('throws for invalid fields', () => {
expect(() => new Dsn('httpx://abc@sentry.io/123')).toThrow(SentryError);
expect(() => new Dsn('httpx://abc@sentry.io:xxx/123')).toThrow(SentryError);
expect(() => new Dsn('http://abc@sentry.io/abc')).toThrow(SentryError);
});
});

Expand Down