Skip to content

Commit

Permalink
fix: Close session if right token is given
Browse files Browse the repository at this point in the history
This will reduce orphaned session from basex queries or monitoring
  • Loading branch information
kumy committed Sep 10, 2023
1 parent f03ac0a commit df7a7fa
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions website/app/GeoKrety/Controller/Pages/LegacyRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public function export2(\Base $f3) {
if ($f3->exists('GET.rate_limits_bypass')) {
$others['rate_limits_bypass'] = $f3->get('GET.rate_limits_bypass');
}
if ($f3->exists('GET.GET.short_lived_session_token')) {
$others['GET.short_lived_session_token'] = $f3->get('GET.GET.short_lived_session_token');
}
$url_params = $this->_export_query_params($f3, $others);
$f3->reroute(sprintf('@api_v1_export2?%s', $url_params), $permanent = false, $die = true);
}
Expand Down
1 change: 1 addition & 0 deletions website/app/GeoKrety/Service/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function __construct() {
define('GK_SITE_ADMINISTRATORS', explode(',', getenv('GK_SITE_ADMINISTRATORS') ?: '26422'));
define('GK_SITE_SESSION_REMEMBER', getenv('GK_SITE_SESSION_REMEMBER') ?: 60 * 60 * 24); // 24 hours
define('GK_SITE_SESSION_LIFETIME_REMEMBER', getenv('GK_SITE_SESSION_LIFETIME_REMEMBER') ?: 60 * 60 * 24 * 30); // 30 days
define('GK_SITE_SESSION_SHORT_LIVED_TOKEN', getenv('GK_SITE_SESSION_SHORT_LIVED_TOKEN') ?: substr(str_shuffle(md5(microtime())), 0, 10));
define('GK_SITE_ACCOUNT_ACTIVATION_CODE_LENGTH', getenv('GK_SITE_ACCOUNT_ACTIVATION_CODE_LENGTH') ?: 42);
define('GK_SITE_ACCOUNT_ACTIVATION_CODE_DAYS_VALIDITY', getenv('GK_SITE_ACCOUNT_ACTIVATION_CODE_DAYS_VALIDITY') ?: 15);
define('GK_SITE_EMAIL_ACTIVATION_CODE_LENGTH', getenv('GK_SITE_EMAIL_ACTIVATION_CODE_LENGTH') ?: 42);
Expand Down
9 changes: 9 additions & 0 deletions website/app/GeoKrety/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ public static function closeAllSessionsForUser(User $user) {
$f3 = \Base::instance();
$f3->get('DB')->exec('DELETE FROM sessions WHERE "user" = ?', [$user->id]);
}

public static function closeCurrentSession() {
$sessid = session_id();
if ($sessid === false) {
return;
}
$f3 = \Base::instance();
$f3->get('DB')->exec('DELETE FROM sessions WHERE "session_id" = ?', [$sessid]);
}
}
8 changes: 8 additions & 0 deletions website/app/shutdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
register_shutdown_function('shutdown_force_send_response_to_client', $f3);
register_shutdown_function('shutdown_prometheus_metrics', $f3);
register_shutdown_function('shutdown_audit_post', $f3);
register_shutdown_function('shutdown_short_lived_sessions', $f3);

// Piwik
if (GK_PIWIK_ENABLED) {
Expand All @@ -15,6 +16,13 @@ function shutdown_force_send_response_to_client(Base $f3) {
}
}

function shutdown_short_lived_sessions(Base $f3) {
$token = $f3->get('GET.short_lived_session_token');
if ($token === GK_SITE_SESSION_SHORT_LIVED_TOKEN) {
\GeoKrety\Session::closeCurrentSession();
}
}

function shutdown_piwik(Base $f3) {
if (!\GeoKrety\Service\UserSettings::getForCurrentUser('TRACKING_OPT_OUT')) {
try {
Expand Down

0 comments on commit df7a7fa

Please sign in to comment.