Skip to content

Commit 6127716

Browse files
cedric-annetrasher
authored andcommitted
Fix session initialization
1 parent 80bf167 commit 6127716

File tree

9 files changed

+49
-95
lines changed

9 files changed

+49
-95
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ The present file will list all changes made to the project; according to the
544544
- `Toolbox::sodiumEncrypt()`
545545
- `Toolbox::unclean_cross_side_scripting_deep()`
546546
- `Transfer::manageConnectionComputer()`
547+
- `Update::initSession()`
547548
- `User::getDelegateGroupsForUser()`
548549
- `User::showDebug()`
549550
- `User::title()`

install/install.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,6 @@ function update1($dbname)
488488

489489
//------------Start of install script---------------------------
490490

491-
492-
// Use default session dir if not writable
493-
if (is_writable(GLPI_SESSION_DIR)) {
494-
Session::setPath();
495-
}
496-
497-
Session::start();
498491
error_reporting(0); // we want to check system before affraid the user.
499492

500493

install/update.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
Config::loadLegacyConfiguration();
6363

6464
$update = new Update($DB);
65-
$update->initSession();
6665

6766
if (isset($_POST['update_end'])) {
6867
if (isset($_POST['send_stats'])) {

src/Glpi/Config/LegacyConfigurators/SessionStart.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -34,56 +34,58 @@
3434

3535
namespace Glpi\Config\LegacyConfigurators;
3636

37-
use Glpi\Config\ConfigProviderHasRequestTrait;
3837
use Session;
39-
use Glpi\Config\ConfigProviderWithRequestInterface;
4038
use Glpi\Config\LegacyConfigProviderInterface;
39+
use Symfony\Component\HttpFoundation\Request;
4140

42-
final class SessionStart implements LegacyConfigProviderInterface, ConfigProviderWithRequestInterface
41+
final class SessionStart implements LegacyConfigProviderInterface
4342
{
44-
use ConfigProviderHasRequestTrait;
45-
46-
/**
47-
* An array of regular expressions of the paths that disable the Session.
48-
*/
49-
private const NO_COOKIE_PATHS = [
50-
'/api(rest)?\.php.*',
51-
'/caldav\.php.*',
52-
'/front/cron\.php.*',
53-
];
54-
55-
private const NO_SESSION_PATHS = [
56-
'/api(rest)?\.php.*',
57-
];
58-
5943
public function execute(): void
6044
{
61-
$path = $this->getRequest()->getRequestUri();
62-
$path = '/' . ltrim($path, '/');
45+
// The session must be started even in CLI context.
46+
// The GLPI code refers to the session in many places
47+
// and we cannot safely remove its initialization in the CLI context.
48+
$start_session = true;
6349

64-
$noCookiePaths = '~^' . implode('|', \array_map(static fn ($regex) => '(?:' . $regex . ')', self::NO_COOKIE_PATHS)) . '$~sUu';
50+
if (isset($_SERVER['REQUEST_URI'])) {
51+
// Specific configuration related to web context
6552

66-
Session::setPath();
53+
$request = Request::createFromGlobals();
54+
$path = $request->getPathInfo();
6755

68-
if (
69-
\preg_match($noCookiePaths, $path)
70-
|| (\preg_match('~^/front/planning\.php~Uu', $path) && $this->getRequest()->query->has('genical'))
71-
) {
72-
// Disable session cookie for these paths
73-
ini_set('session.use_cookies', 0);
74-
}
56+
$use_cookies = true;
57+
if (\str_starts_with($path, '/api.php') || \str_starts_with($path, '/apirest.php')) {
58+
// API clients must not use cookies, as the session token is expected to be passed in headers.
59+
$use_cookies = false;
60+
// The API endpoint is strating the session manually.
61+
$start_session = false;
62+
} elseif (\str_starts_with($path, '/caldav.php')) {
63+
// CalDAV clients must not use cookies, as the authentication is expected to be passed in headers.
64+
$use_cookies = false;
65+
} elseif (\str_starts_with($path, '/front/cron.php')) {
66+
// The cron endpoint is not expected to use the authenticated user session.
67+
$use_cookies = false;
68+
} elseif (\str_starts_with($path, '/front/planning.php') && $request->query->has('genical')) {
69+
// The `genical` endpoint must not use cookies, as the authentication is expected to be passed in the query parameters.
70+
$use_cookies = false;
71+
}
7572

76-
$noSessionPaths = '~^' . implode('|', \array_map(static fn ($regex) => '(?:' . $regex . ')', self::NO_SESSION_PATHS)) . '$~sUu';
77-
if (
78-
!\preg_match($noSessionPaths, $path)
79-
) {
80-
// Disable session cookie for these paths
81-
Session::start();
73+
if (!$use_cookies) {
74+
ini_set('session.use_cookies', 0);
75+
}
8276
}
8377

84-
// Default Use mode
85-
if (!isset($_SESSION['glpi_use_mode'])) {
86-
$_SESSION['glpi_use_mode'] = Session::NORMAL_MODE;
78+
if ($start_session) {
79+
if (Session::canWriteSessionFiles()) {
80+
Session::setPath();
81+
} else {
82+
\trigger_error(
83+
sprintf('Unable to write session files on `%s`.', GLPI_SESSION_DIR),
84+
E_USER_WARNING
85+
);
86+
}
87+
88+
Session::start();
8789
}
8890
}
8991
}

src/Glpi/Controller/IndexController.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ private function call(): void
9696
if (file_exists(GLPI_ROOT . '/install/install.php')) {
9797
Html::redirect("install/install.php");
9898
} else {
99-
// Init session (required by header display logic)
100-
Session::setPath();
101-
Session::start();
10299
Session::loadLanguage('', false);
103100
// Prevent inclusion of debug information in footer, as they are based on vars that are not initialized here.
104101
$_SESSION['glpi_use_mode'] = Session::NORMAL_MODE;

src/Glpi/Debug/Profile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function getDebugInfo(): array
217217

218218
public function save(): void
219219
{
220-
if ($_SESSION['glpi_use_mode'] !== \Session::DEBUG_MODE) {
220+
if (($_SESSION['glpi_use_mode'] ?? null) !== \Session::DEBUG_MODE) {
221221
// Don't save debug info for non-debug requests
222222
return;
223223
}

src/Session.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ public static function start()
257257

258258
// Define current time for sync of action timing
259259
$_SESSION["glpi_currenttime"] = date("Y-m-d H:i:s");
260+
261+
// Define session default mode
262+
if (!isset($_SESSION['glpi_use_mode'])) {
263+
$_SESSION['glpi_use_mode'] = Session::NORMAL_MODE;
264+
}
260265
}
261266

262267

src/Update.php

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
*/
3535

3636
use Glpi\Helpdesk\DefaultDataManager;
37-
use Glpi\Form\Form;
3837
use Glpi\Rules\RulesManager;
3938
use Glpi\System\Diagnostic\DatabaseSchemaIntegrityChecker;
4039
use Glpi\Toolbox\VersionParser;
@@ -44,7 +43,6 @@
4443
**/
4544
class Update
4645
{
47-
private $args = [];
4846
private $DB;
4947
/**
5048
* @var Migration
@@ -65,35 +63,14 @@ class Update
6563
* Constructor
6664
*
6765
* @param object $DB Database instance
68-
* @param array $args Command line arguments; default to empty array
6966
* @param string $migrations_directory
7067
*/
71-
public function __construct($DB, $args = [], string $migrations_directory = GLPI_ROOT . '/install/migrations/')
68+
public function __construct($DB, string $migrations_directory = GLPI_ROOT . '/install/migrations/')
7269
{
7370
$this->DB = $DB;
74-
$this->args = $args;
7571
$this->migrations_directory = $migrations_directory;
7672
}
7773

78-
/**
79-
* Initialize session for update
80-
*
81-
* @return void
82-
*/
83-
public function initSession()
84-
{
85-
if (Session::canWriteSessionFiles()) {
86-
Session::setPath();
87-
}
88-
Session::start();
89-
90-
if (isCommandLine()) {
91-
// Init debug variable
92-
$_SESSION = ['glpilanguage' => (isset($this->args['lang']) ? $this->args['lang'] : 'en_GB')];
93-
$_SESSION["glpi_currenttime"] = date("Y-m-d H:i:s");
94-
}
95-
}
96-
9774
/**
9875
* Get current values (versions, lang, ...)
9976
*

tests/functional/Update.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,6 @@ public function testCurrents()
5454
$this->array($update->getCurrents())->isEqualTo($expected);
5555
}
5656

57-
public function testInitSession()
58-
{
59-
global $DB;
60-
61-
$update = new \Update($DB);
62-
session_destroy();
63-
$this->variable(session_status())->isIdenticalTo(PHP_SESSION_NONE);
64-
65-
$update->initSession();
66-
$this->variable(session_status())->isIdenticalTo(PHP_SESSION_ACTIVE);
67-
68-
$this->array($_SESSION)->hasKeys([
69-
'glpilanguage',
70-
'glpi_currenttime',
71-
])->notHasKeys([
72-
'glpi_use_mode',
73-
'use_log_in_files'
74-
]);
75-
}
76-
7757
public function testSetMigration()
7858
{
7959
global $DB;
@@ -401,7 +381,7 @@ public function testGetMigrationsToDo(string $current_version, bool $force_lates
401381
$method->setAccessible(true);
402382

403383
global $DB;
404-
$update = new \Update($DB, [], vfsStream::url('install/migrations'));
384+
$update = new \Update($DB, vfsStream::url('install/migrations'));
405385
$this->array($method->invokeArgs($update, [$current_version, $force_latest]))->isIdenticalTo($expected_migrations);
406386
}
407387
}

0 commit comments

Comments
 (0)