-
-
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(core): Ensure tunnel
is considered for isSentryUrl
checks
#9130
Merged
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { DsnComponents, Hub } from '@sentry/types'; | ||
|
||
/** | ||
* Checks whether given url points to Sentry server | ||
* @param url url to verify | ||
*/ | ||
export function isSentryRequestUrl(url: string, hub: Hub): boolean { | ||
const client = hub.getClient(); | ||
const dsn = client && client.getDsn(); | ||
const tunnel = client && client.getOptions().tunnel; | ||
|
||
return checkDsn(url, dsn) || checkTunnel(url, tunnel); | ||
} | ||
|
||
function checkTunnel(url: string, tunnel: string | undefined): boolean { | ||
if (!tunnel) { | ||
return false; | ||
} | ||
|
||
return removeTrailingSlash(url) === removeTrailingSlash(tunnel); | ||
} | ||
|
||
function checkDsn(url: string, dsn: DsnComponents | undefined): boolean { | ||
return dsn ? url.includes(dsn.host) : false; | ||
} | ||
|
||
function removeTrailingSlash(str: string): string { | ||
return str[str.length - 1] === '/' ? str.slice(0, -1) : str; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Hub } from '@sentry/types'; | ||
|
||
import { isSentryRequestUrl } from '../../../src'; | ||
|
||
describe('isSentryRequestUrl', () => { | ||
it.each([ | ||
['', 'sentry-dsn.com', '', false], | ||
['http://sentry-dsn.com/my-url', 'sentry-dsn.com', '', true], | ||
['http://sentry-dsn.com', 'sentry-dsn.com', '', true], | ||
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200', true], | ||
['http://tunnel:4200', 'sentry-dsn.com', 'http://tunnel:4200/', true], | ||
['http://tunnel:4200/', 'sentry-dsn.com', 'http://tunnel:4200', true], | ||
['http://tunnel:4200/a', 'sentry-dsn.com', 'http://tunnel:4200', false], | ||
])('works with url=%s, dsn=%s, tunnel=%s', (url: string, dsn: string, tunnel: string, expected: boolean) => { | ||
const hub = { | ||
getClient: () => { | ||
return { | ||
getOptions: () => ({ tunnel }), | ||
getDsn: () => ({ host: dsn }), | ||
}; | ||
}, | ||
} as unknown as Hub; | ||
|
||
expect(isSentryRequestUrl(url, hub)).toBe(expected); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { RequestOptions } from 'http'; | ||
|
||
/** Build a full URL from request options. */ | ||
export function getRequestUrl(requestOptions: RequestOptions): string { | ||
const protocol = requestOptions.protocol || ''; | ||
const hostname = requestOptions.hostname || requestOptions.host || ''; | ||
// Don't log standard :80 (http) and :443 (https) ports to reduce the noise | ||
// Also don't add port if the hostname already includes a port | ||
const port = | ||
!requestOptions.port || requestOptions.port === 80 || requestOptions.port === 443 || /^(.*):(\d+)$/.test(hostname) | ||
? '' | ||
: `:${requestOptions.port}`; | ||
const path = requestOptions.path ? requestOptions.path : '/'; | ||
return `${protocol}//${hostname}${port}${path}`; | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/node-experimental/test/utils/getRequestUrl.test.ts
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { RequestOptions } from 'http'; | ||
|
||
import { getRequestUrl } from '../../src/utils/getRequestUrl'; | ||
|
||
describe('getRequestUrl', () => { | ||
it.each([ | ||
[{ protocol: 'http:', hostname: 'localhost', port: 80 }, 'http://localhost/'], | ||
[{ protocol: 'http:', hostname: 'localhost', host: 'localhost:80', port: 80 }, 'http://localhost/'], | ||
[{ protocol: 'http:', hostname: 'localhost', port: 3000 }, 'http://localhost:3000/'], | ||
[{ protocol: 'http:', host: 'localhost:3000', port: 3000 }, 'http://localhost:3000/'], | ||
[{ protocol: 'https:', hostname: 'localhost', port: 443 }, 'https://localhost/'], | ||
[{ protocol: 'https:', hostname: 'localhost', port: 443, path: '/my-path' }, 'https://localhost/my-path'], | ||
[ | ||
{ protocol: 'https:', hostname: 'www.example.com', port: 443, path: '/my-path' }, | ||
'https://www.example.com/my-path', | ||
], | ||
])('works with %s', (input: RequestOptions, expected: string | undefined) => { | ||
expect(getRequestUrl(input)).toBe(expected); | ||
}); | ||
}); |
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
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.
Also noticed that
host
may include the port (e.g.localhost:3000
), so we better check that this is not the case before re-adding the port.