Skip to content

Commit 5763ab3

Browse files
Automatically truncating the user agent string to 255 characters
1 parent a528c12 commit 5763ab3

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/History.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Illuminate\Support\Facades\App;
2121
use Illuminate\Support\Facades\Auth;
2222
use Illuminate\Support\Facades\Request;
23+
use Illuminate\Support\Str;
2324
use Konekt\History\Contracts\ModelHistoryEvent;
2425
use Konekt\History\Contracts\SceneResolver;
2526
use Konekt\History\Diff\Diff;
@@ -148,7 +149,7 @@ protected static function commonFields(Model $model): array
148149
'model_id' => $model->id,
149150
'user_id' => Auth::id(),
150151
'ip_address' => Request::ip(),
151-
'user_agent' => Request::userAgent(),
152+
'user_agent' => Str::limit(Request::userAgent(), 255, ''),
152153
];
153154
}
154155

tests/UserAgentTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class UserAgentTest extends TestCase
2323
private const STUPID_UA_STRING = 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/21G93 [FBAN/FBIOS;FBAV/492.0.0.101.111;FBBV/670308045;FBDV/iPhone14,5;FBMD/iPhone;FBSN/iOS;FBSV/17.6.1;FBSS/3;FBID/phone;FBLC/de_DE;FBOP/5;FBRV/673456666]';
2424

2525
/** @test */
26-
public function this_should_fail_with_postgres_at_least()
26+
public function it_truncates_user_agent_strings_longer_than_255_characters_without_errors()
2727
{
2828
$request = Request::create('/example-route', 'GET', [], [], [], [
2929
'HTTP_USER_AGENT' => self::STUPID_UA_STRING,
@@ -35,6 +35,6 @@ public function this_should_fail_with_postgres_at_least()
3535
$entry = History::begin($task);
3636
$entry = $entry->fresh();
3737

38-
$this->assertEquals(self::STUPID_UA_STRING, $entry->user_agent);
38+
$this->assertEquals(substr(self::STUPID_UA_STRING, 0, 255), $entry->user_agent);
3939
}
4040
}

0 commit comments

Comments
 (0)