Skip to content

Commit 2017afc

Browse files
Naorayclaude
andcommitted
fix(tracing): strip empty flash data and token from session
Closes #35 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7b4f091 commit 2017afc

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

src/Tracing/SessionCollector.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ public function collect(): void
2727
return;
2828
}
2929

30-
Context::addHidden('session', [
31-
'data' => $this->redactPayload(Session::all()),
32-
'flash' => [
33-
'old' => Session::get('_flash.old', []),
34-
'new' => Session::get('_flash.new', []),
35-
],
36-
]);
30+
$data = collect(Session::all())
31+
->except(['_token', '_flash'])
32+
->toArray();
33+
34+
$session = [
35+
'data' => $this->redactPayload($data),
36+
];
37+
38+
$flash = [
39+
'old' => Session::get('_flash.old', []),
40+
'new' => Session::get('_flash.new', []),
41+
];
42+
43+
if (! empty($flash['old']) || ! empty($flash['new'])) {
44+
$session['flash'] = $flash;
45+
}
46+
47+
Context::addHidden('session', $session);
3748
}
3849
}

tests/Tracing/SessionCollectorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,41 @@
3232

3333
expect(Context::hasHidden('session'))->toBeFalse();
3434
});
35+
36+
it('strips empty flash data from session', function () {
37+
Session::start();
38+
Session::put('key', 'value');
39+
40+
$this->collector->collect();
41+
42+
$session = Context::getHidden('session');
43+
44+
expect($session)->not->toHaveKey('flash');
45+
expect($session['data'])->not->toHaveKey('_flash');
46+
});
47+
48+
it('preserves non-empty flash data', function () {
49+
Session::start();
50+
Session::put('key', 'value');
51+
Session::flash('message', 'Hello World');
52+
53+
$this->collector->collect();
54+
55+
$session = Context::getHidden('session');
56+
57+
expect($session)->toHaveKey('flash');
58+
expect($session['flash']['new'])->toContain('message');
59+
});
60+
61+
it('strips _token from session data', function () {
62+
Session::start();
63+
Session::put('key', 'value');
64+
Session::put('_token', 'csrf-token-value');
65+
66+
$this->collector->collect();
67+
68+
$session = Context::getHidden('session');
69+
70+
expect($session['data'])->not->toHaveKey('_token');
71+
expect($session['data']['key'])->toBe('value');
72+
});

0 commit comments

Comments
 (0)