-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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(utils): Fail silently if the provided Dsn is invalid #8121
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { DsnComponents, DsnLike, DsnProtocol } from '@sentry/types'; | ||
|
||
import { SentryError } from './error'; | ||
import { logger } from './logger'; | ||
|
||
/** Regular expression used to parse a Dsn. */ | ||
const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/; | ||
|
@@ -30,13 +30,16 @@ export function dsnToString(dsn: DsnComponents, withPassword: boolean = false): | |
* Parses a Dsn from a given string. | ||
* | ||
* @param str A Dsn as string | ||
* @returns Dsn as DsnComponents | ||
* @returns Dsn as DsnComponents or undefined if @param str is not a valid DSN string | ||
*/ | ||
export function dsnFromString(str: string): DsnComponents { | ||
export function dsnFromString(str: string): DsnComponents | undefined { | ||
const match = DSN_REGEX.exec(str); | ||
|
||
if (!match) { | ||
throw new SentryError(`Invalid Sentry Dsn: ${str}`); | ||
// This should be logged to the console | ||
// eslint-disable-next-line no-console | ||
console.error(`Invalid Sentry Dsn: ${str}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to log this as a console error so that this error is easier to spot. More fine-grained validation is still only logged if |
||
return undefined; | ||
} | ||
|
||
const [protocol, publicKey, pass = '', host, port = '', lastPath] = match.slice(1); | ||
|
@@ -71,38 +74,52 @@ function dsnFromComponents(components: DsnComponents): DsnComponents { | |
}; | ||
} | ||
|
||
function validateDsn(dsn: DsnComponents): boolean | void { | ||
function validateDsn(dsn: DsnComponents): boolean { | ||
if (!__DEBUG_BUILD__) { | ||
return; | ||
return true; | ||
} | ||
|
||
const { port, projectId, protocol } = dsn; | ||
|
||
const requiredComponents: ReadonlyArray<keyof DsnComponents> = ['protocol', 'publicKey', 'host', 'projectId']; | ||
requiredComponents.forEach(component => { | ||
const hasMissingRequiredComponent = requiredComponents.find(component => { | ||
if (!dsn[component]) { | ||
throw new SentryError(`Invalid Sentry Dsn: ${component} missing`); | ||
logger.error(`Invalid Sentry Dsn: ${component} missing`); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
if (hasMissingRequiredComponent) { | ||
return false; | ||
} | ||
|
||
if (!projectId.match(/^\d+$/)) { | ||
throw new SentryError(`Invalid Sentry Dsn: Invalid projectId ${projectId}`); | ||
logger.error(`Invalid Sentry Dsn: Invalid projectId ${projectId}`); | ||
return false; | ||
} | ||
|
||
if (!isValidProtocol(protocol)) { | ||
throw new SentryError(`Invalid Sentry Dsn: Invalid protocol ${protocol}`); | ||
logger.error(`Invalid Sentry Dsn: Invalid protocol ${protocol}`); | ||
return false; | ||
} | ||
|
||
if (port && isNaN(parseInt(port, 10))) { | ||
throw new SentryError(`Invalid Sentry Dsn: Invalid port ${port}`); | ||
logger.error(`Invalid Sentry Dsn: Invalid port ${port}`); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** The Sentry Dsn, identifying a Sentry instance and project. */ | ||
export function makeDsn(from: DsnLike): DsnComponents { | ||
/** | ||
* Creates a valid Sentry Dsn object, identifying a Sentry instance and project. | ||
* @returns a valid DsnComponents object or `undefined` if @param from is an invalid DSN source | ||
*/ | ||
export function makeDsn(from: DsnLike): DsnComponents | undefined { | ||
const components = typeof from === 'string' ? dsnFromString(from) : dsnFromComponents(from); | ||
validateDsn(components); | ||
if (!components || !validateDsn(components)) { | ||
return undefined; | ||
} | ||
return components; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just return an empty script tag instead of throwing, causing the dialog not to be fetched (which is fine IMO)