Skip to content

Commit d48ae6b

Browse files
committed
Fix not replacing session key before session is started
1 parent c081654 commit d48ae6b

File tree

2 files changed

+6
-33
lines changed

2 files changed

+6
-33
lines changed

src/Sentry/Laravel/Features/CacheIntegration.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Cache\Events;
66
use Illuminate\Contracts\Events\Dispatcher;
7-
use Illuminate\Http\Request;
7+
use Illuminate\Contracts\Session\Session;
88
use Illuminate\Redis\Events as RedisEvents;
99
use Illuminate\Redis\RedisManager;
1010
use Illuminate\Support\Str;
@@ -262,23 +262,13 @@ private function maybeHandleCacheEventAsEndOfSpan(Events\CacheEvent $event): boo
262262
private function getSessionKey(): ?string
263263
{
264264
try {
265-
/** @var Request $request */
266-
$request = $this->container()->make('request');
265+
/** @var Session $request */
266+
$request = $this->container()->make('session.store');
267267

268-
if (!$request->hasSession()) {
269-
return false;
270-
}
271-
272-
$session = $request->session();
273-
274-
if (!$session->isStarted()) {
275-
return false;
276-
}
277-
278-
return $session->getId();
268+
return $request->getId();
279269
} catch (\Exception $e) {
280-
// If anything goes wrong, we assume it's not a session key
281-
return false;
270+
// We can assume the session store is not available here so there is no session key to retrieve
271+
return null;
282272
}
283273
}
284274

test/Sentry/Features/CacheIntegrationTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ public function testCacheBreadcrumbIsNotRecordedWhenDisabled(): void
5858
public function testCacheBreadcrumbReplacesSessionKeyWithPlaceholder(): void
5959
{
6060
// Start a session properly in the test environment
61-
$this->ensureRequestIsBoundWithSession();
6261
$this->startSession();
6362
$sessionId = $this->app['session']->getId();
6463

@@ -182,8 +181,6 @@ public function testCacheSpanReplacesSessionKeyWithPlaceholder(): void
182181
{
183182
$this->markSkippedIfTracingEventsNotAvailable();
184183

185-
// Start a session properly in the test environment
186-
$this->ensureRequestIsBoundWithSession();
187184
$this->startSession();
188185
$sessionId = $this->app['session']->getId();
189186

@@ -201,7 +198,6 @@ public function testCacheSpanReplacesMultipleSessionKeysWithPlaceholder(): void
201198
$this->markSkippedIfTracingEventsNotAvailable();
202199

203200
// Start a session properly in the test environment
204-
$this->ensureRequestIsBoundWithSession();
205201
$this->startSession();
206202
$sessionId = $this->app['session']->getId();
207203

@@ -252,17 +248,4 @@ private function executeAndReturnMostRecentSpan(callable $callable): Span
252248

253249
return array_pop($spans);
254250
}
255-
256-
private function ensureRequestIsBoundWithSession(): void
257-
{
258-
if ($this->app->bound('request')) {
259-
$request = $this->app['request'];
260-
} else {
261-
$request = $this->app->make(\Illuminate\Http\Request::class);
262-
263-
$this->app->instance('request', $request);
264-
}
265-
266-
$request->setLaravelSession($this->app['session']->driver());
267-
}
268251
}

0 commit comments

Comments
 (0)