diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2025-04-08 10:54:24 +0000 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2025-04-08 10:54:24 +0000 |
| commit | 008c518d5d8e60c0168cd107dbfd1f23f9c4a701 (patch) | |
| tree | 8f3d8afbbf73ae7d59791a3347a557c194f691d0 /classes/Sessions.php | |
| parent | bb2c4b380165731c3f8abf0596fffb2a0953265b (diff) | |
| parent | 17b4e98249462a1feb71586d10cd5293d9487ab8 (diff) | |
Merge branch 'session-encryption' into 'master'
add optional encryption for stored session data using Sodium library
See merge request tt-rss/tt-rss!117
Diffstat (limited to 'classes/Sessions.php')
| -rw-r--r-- | classes/Sessions.php | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/classes/Sessions.php b/classes/Sessions.php index 5c586154b..f3c0cea42 100644 --- a/classes/Sessions.php +++ b/classes/Sessions.php @@ -58,7 +58,17 @@ class Sessions implements \SessionHandlerInterface { $sth->execute([$id]); if ($row = $sth->fetch()) { - return base64_decode($row['data']); + $data = base64_decode($row['data']); + + if (Config::get(Config::ENCRYPTION_KEY)) { + $unserialized_data = @unserialize($data); // avoid leaking plaintext session via error message + + if ($unserialized_data !== false) + return Crypt::decrypt_string($unserialized_data); + } + + // if encryption key is missing or session data is not in serialized format, assume plaintext data and return as-is + return $data; } $expire = time() + $this->session_expire; @@ -69,7 +79,12 @@ class Sessions implements \SessionHandlerInterface { } public function write(string $id, string $data): bool { + + if (Config::get(Config::ENCRYPTION_KEY)) + $data = serialize(Crypt::encrypt_string($data)); + $data = base64_encode($data); + $expire = time() + $this->session_expire; $sth = Db::pdo()->prepare('SELECT id FROM ttrss_sessions WHERE id=?'); |