Skip to content

Commit d99feca

Browse files
committed
update: encrypt private messages
1 parent dbc2484 commit d99feca

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

app/Http/Livewire/ConversationSearch.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
use App\Models\Conversation;
2020
use App\Traits\LivewireSort;
21+
use Illuminate\Support\Facades\DB;
2122
use Livewire\Attributes\Computed;
2223
use Livewire\Attributes\Rule;
2324
use Livewire\Attributes\Url;
@@ -80,7 +81,18 @@ final public function conversations(): \Illuminate\Pagination\LengthAwarePaginat
8081
)
8182
->when(
8283
$this->message !== null && $this->message !== '',
83-
fn ($query) => $query->whereRelation('messages', 'message', 'LIKE', '%'.str_replace(' ', '%', $this->message).'%')
84+
fn ($query) => $query->whereHas('messages', function ($query): void {
85+
DB::statement("SET block_encryption_mode = 'aes-256-cbc'");
86+
$query
87+
->selectRaw(<<<SQL
88+
AES_DECRYPT(
89+
FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(message) USING utf8), '$.value'))),
90+
?,
91+
FROM_BASE64(JSON_UNQUOTE(JSON_EXTRACT(CONVERT(FROM_BASE64(message) USING utf8), '$.iv')))
92+
) AS decrypted_message
93+
SQL, [base64_decode(substr(config('app.key'), 7))])
94+
->having('decrypted_message', 'LIKE', '%'.str_replace(' ', '%', $this->message).'%');
95+
})
8496
)
8597
->when(
8698
$this->tab === 'inbox' || $this->tab === 'unread',

app/Models/PrivateMessage.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ class PrivateMessage extends Model
4141
*/
4242
protected $guarded = ['id', 'created_at', 'updated_at'];
4343

44+
/**
45+
* Get the attributes that should be cast.
46+
*
47+
* @return array{
48+
* message: 'encrypted',
49+
* }
50+
*/
51+
protected function casts(): array
52+
{
53+
return [
54+
'message' => 'encrypted',
55+
];
56+
}
57+
4458
/**
4559
* Belongs To A User.
4660
*
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* NOTICE OF LICENSE.
4+
*
5+
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
6+
* The details is bundled with this project in the file LICENSE.txt.
7+
*
8+
* @project UNIT3D Community Edition
9+
*
10+
* @author Roardom <[email protected]>
11+
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
12+
*/
13+
14+
use Illuminate\Database\Migrations\Migration;
15+
use Illuminate\Support\Facades\Crypt;
16+
use Illuminate\Support\Facades\DB;
17+
use Illuminate\Support\Facades\Schema;
18+
19+
return new class () extends Migration {
20+
/**
21+
* Run the migrations.
22+
*/
23+
public function up(): void
24+
{
25+
DB::table('private_messages')
26+
->lazyById()
27+
->each(function (object $privateMessage): void {
28+
DB::table('private_messages')
29+
->where('id', '=', $privateMessage->id)
30+
->update([
31+
'message' => Crypt::encryptString($privateMessage->message),
32+
]);
33+
});
34+
}
35+
};

database/schema/mysql-schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,3 +3005,4 @@ INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (354,'2025_06_18_00
30053005
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (355,'2025_06_18_040627_alter_requests_drop_claimed',1);
30063006
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (356,'2025_06_21_234021_alter_requests_drop_votes',1);
30073007
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (357,'2025_07_15_061844_add_block_order_to_user_settings',1);
3008+
INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES (358,'2025_07_17_104138_encrypt_private_messages',1);

0 commit comments

Comments
 (0)