Skip to content

Commit dd72e4d

Browse files
authored
fix(core): Take user from current scope when starting a session (#10153)
When creating a session, we previously only read the user from the isolation scope. This was problematic because the user could be set in `initialScope` which will be applied to the _current_ scope on SDK init. This would happen before the session is started when using auto session tracking. This PR now takes the user from the current scope if it's set and falls back to the isolation scope should the user be set there. Added an integration test to cover this regression.
1 parent f17bc54 commit dd72e4d

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
release: '0.1',
8+
initialScope: {
9+
user: {
10+
id: '1337',
11+
12+
username: 'user1337',
13+
},
14+
},
15+
debug: true,
16+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<a id='navigate' href="foo">Navigate</button>
8+
</body>
9+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { Route } from '@playwright/test';
2+
import { expect } from '@playwright/test';
3+
import type { SessionContext } from '@sentry/types';
4+
5+
import { sentryTest } from '../../../utils/fixtures';
6+
import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers';
7+
8+
sentryTest('should start a new session on pageload.', async ({ getLocalTestPath, page }) => {
9+
const url = await getLocalTestPath({ testDir: __dirname });
10+
const session = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
11+
12+
expect(session).toBeDefined();
13+
expect(session.init).toBe(true);
14+
expect(session.errors).toBe(0);
15+
expect(session.status).toBe('ok');
16+
expect(session.did).toBe('1337');
17+
});
18+
19+
sentryTest('should start a new session with navigation.', async ({ getLocalTestPath, page, browserName }) => {
20+
// Navigations get CORS error on Firefox and WebKit as we're using `file://` protocol.
21+
if (browserName !== 'chromium') {
22+
sentryTest.skip();
23+
}
24+
25+
const url = await getLocalTestPath({ testDir: __dirname });
26+
await page.route('**/foo', (route: Route) => route.fulfill({ path: `${__dirname}/dist/index.html` }));
27+
28+
const initSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
29+
30+
await page.click('#navigate');
31+
32+
const newSession = await getFirstSentryEnvelopeRequest<SessionContext>(page, url);
33+
34+
expect(newSession).toBeDefined();
35+
expect(newSession.init).toBe(true);
36+
expect(newSession.errors).toBe(0);
37+
expect(newSession.status).toBe('ok');
38+
expect(newSession.sid).toBeDefined();
39+
expect(initSession.sid).not.toBe(newSession.sid);
40+
expect(newSession.did).toBe('1337');
41+
});

packages/core/src/exports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export function startSession(context?: SessionContext): Session {
404404
const session = makeSession({
405405
release,
406406
environment,
407-
user: isolationScope.getUser(),
407+
user: currentScope.getUser() || isolationScope.getUser(),
408408
...(userAgent && { userAgent }),
409409
...context,
410410
});
@@ -434,7 +434,7 @@ export function endSession(): void {
434434
const isolationScope = getIsolationScope();
435435
const currentScope = getCurrentScope();
436436

437-
const session = isolationScope.getSession();
437+
const session = currentScope.getSession() || isolationScope.getSession();
438438
if (session) {
439439
closeSession(session);
440440
}

0 commit comments

Comments
 (0)