Skip to content

fix(nextjs): Proxy public key with tunnelRoute option #6562

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/nextjs/src/config/withSentryConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ function setUpTunnelRewriteRules(userNextConfig: NextConfigObject, tunnelPath: s
key: 'p', // short for projectId - we keep it short so matching is harder for ad-blockers
value: '(?<projectid>.*)',
},
{
type: 'query',
key: 'k', // short for sentryKey - we keep it short so matching is harder for ad-blockers
value: '(?<sentrykey>.*)',
},
],
destination: 'https://o:orgid.ingest.sentry.io/api/:projectid/envelope/',
destination: 'https://o:orgid.ingest.sentry.io/api/:projectid/envelope/?sentry_key=:sentrykey',
};

if (typeof originalRewrites !== 'function') {
Expand Down
10 changes: 6 additions & 4 deletions packages/nextjs/src/utils/tunnelRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export function applyTunnelRouteOption(options: NextjsOptions): void {
if (tunnelRouteOption && options.dsn) {
const dsnComponents = dsnFromString(options.dsn);
const sentrySaasDsnMatch = dsnComponents.host.match(/^o(\d+)\.ingest\.sentry\.io$/);
if (sentrySaasDsnMatch) {
if (!sentrySaasDsnMatch) {
__DEBUG_BUILD__ && logger.warn('Provided DSN is not a Sentry SaaS DSN. Will not tunnel events.');
} else if (!dsnComponents.publicKey) {
__DEBUG_BUILD__ && logger.warn('DSN is missing public key. Will not tunnel events.');
} else {
const orgId = sentrySaasDsnMatch[1];
const tunnelPath = `${tunnelRouteOption}?o=${orgId}&p=${dsnComponents.projectId}`;
const tunnelPath = `${tunnelRouteOption}?o=${orgId}&p=${dsnComponents.projectId}&k=${dsnComponents.publicKey}`;
options.tunnel = tunnelPath;
__DEBUG_BUILD__ && logger.info(`Tunneling events to "${tunnelPath}"`);
} else {
__DEBUG_BUILD__ && logger.warn('Provided DSN is not a Sentry SaaS DSN. Will not tunnel events.');
}
}
}
2 changes: 1 addition & 1 deletion packages/nextjs/test/utils/tunnelRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('applyTunnelRouteOption()', () => {

applyTunnelRouteOption(options);

expect(options.tunnel).toBe('/my-error-monitoring-route?o=2222222&p=3333333');
expect(options.tunnel).toBe('/my-error-monitoring-route?o=2222222&p=3333333&k=11111111111111111111111111111111');
});

it('should not apply `tunnelRoute` when DSN is missing', () => {
Expand Down