Skip to content

Commit

Permalink
Automatically truncating the user agent string to 255 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Dec 13, 2024
1 parent a528c12 commit 5763ab3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Str;
use Konekt\History\Contracts\ModelHistoryEvent;
use Konekt\History\Contracts\SceneResolver;
use Konekt\History\Diff\Diff;
Expand Down Expand Up @@ -148,7 +149,7 @@ protected static function commonFields(Model $model): array
'model_id' => $model->id,
'user_id' => Auth::id(),
'ip_address' => Request::ip(),
'user_agent' => Request::userAgent(),
'user_agent' => Str::limit(Request::userAgent(), 255, ''),
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/UserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UserAgentTest extends TestCase
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]';

/** @test */
public function this_should_fail_with_postgres_at_least()
public function it_truncates_user_agent_strings_longer_than_255_characters_without_errors()
{
$request = Request::create('/example-route', 'GET', [], [], [], [
'HTTP_USER_AGENT' => self::STUPID_UA_STRING,
Expand All @@ -35,6 +35,6 @@ public function this_should_fail_with_postgres_at_least()
$entry = History::begin($task);
$entry = $entry->fresh();

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

0 comments on commit 5763ab3

Please sign in to comment.