Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/OC.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OC\Share20\UserRemovedListener;
use OC\User\DisabledUserException;
use OCP\App\Events\AppsLoadedEvent;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\BeforeFileSystemSetupEvent;
use OCP\Files\ISetupManager;
Expand Down Expand Up @@ -467,6 +468,7 @@ public static function initSession(): void {
$session = new \OC\Session\Internal(
$sessionName,
logger('core'),
Server::get(ITimeFactory::class),
);

$cryptoWrapper = Server::get(\OC\Session\CryptoWrapper::class);
Expand Down
6 changes: 4 additions & 2 deletions lib/private/Session/Internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OC\Authentication\Token\IProvider;
use OC\Diagnostics\TLogSlowOperation;
use OC\User\Session as UserSession;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Exceptions\InvalidTokenException;
use OCP\Server;
use OCP\Session\Exceptions\SessionNotAvailableException;
Expand Down Expand Up @@ -52,6 +53,7 @@ class Internal extends Session {
public function __construct(
string $name,
private readonly LoggerInterface $logger,
private readonly ITimeFactory $timeFactory,
) {
set_error_handler([$this, 'trapError']);
$this->invoke('session_name', [$name]);
Expand Down Expand Up @@ -271,7 +273,7 @@ private function switchToNewId(string $oldId, bool $deleteOldSession): bool {
$markOldId = $deleteOldSession && ($_COOKIE[session_name()] ?? null) === $oldId;
if ($markOldId) {
// The old id keeps the marker instead of the data, so session_regenerate_id() must not delete it
$_SESSION = [self::SUPERSEDED_AT => time()];
$_SESSION = [self::SUPERSEDED_AT => $this->timeFactory->getTime()];
}

$regenerated = $this->invoke('session_regenerate_id', [$deleteOldSession && !$markOldId], true) === true;
Expand Down Expand Up @@ -300,7 +302,7 @@ private function leaveSupersededSession(bool $atRequestStart): void {
]);
$this->invoke('session_abort', [], true);
$_SESSION = [];
$this->cookieAllowed = $atRequestStart && time() - $supersededAt >= self::SUPERSEDED_GRACE_PERIOD;
$this->cookieAllowed = $atRequestStart && $this->timeFactory->getTime() - $supersededAt >= self::SUPERSEDED_GRACE_PERIOD;
$freshId = $this->invoke('session_create_id', [], true);
if (is_string($freshId) && $this->invoke('session_id', [$freshId], true) !== false) {
$this->startSession(readAndClose: $atRequestStart, sendCookie: $this->cookieAllowed);
Expand Down
28 changes: 23 additions & 5 deletions tests/lib/Session/InternalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OC\Authentication\Token\IProvider;
use OC\Session\Internal;
use OC\User\Session as UserSession;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Authentication\Token\IToken;
use OCP\IConfig;
use OCP\ITempManager;
Expand All @@ -31,12 +32,18 @@
#[PreserveGlobalState(enabled: false)]
class InternalTest extends TestCase {
private LoggerInterface&MockObject $logger;
private ITimeFactory&MockObject $timeFactory;
private int $now;

#[\Override]
protected function setUp(): void {
parent::setUp();

$this->logger = $this->createMock(LoggerInterface::class);
$this->now = (new \DateTimeImmutable('2024-01-01 00:00:00 UTC'))->getTimestamp();
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->timeFactory->method('getTime')
->willReturnCallback(fn (): int => $this->now);
ini_set('session.save_handler', 'files');
ini_set('session.save_path', Server::get(ITempManager::class)->getTemporaryFolder());
ini_set('session.serialize_handler', 'php_serialize');
Expand All @@ -58,7 +65,7 @@ private function startRequest(?string $id = null): Internal {
session_id($id);
$_COOKIE['oc_session_test'] = $id;
}
return new Internal('oc_session_test', $this->logger);
return new Internal('oc_session_test', $this->logger, $this->timeFactory);
}

/**
Expand Down Expand Up @@ -99,7 +106,7 @@ private function createSupersededId(): string {

private function markSupersededLongAgo(string $id): void {
file_put_contents($this->getStoragePath($id), serialize([
Internal::SUPERSEDED_AT => time() - Internal::SUPERSEDED_GRACE_PERIOD,
Internal::SUPERSEDED_AT => $this->now - Internal::SUPERSEDED_GRACE_PERIOD,
]));
}

Expand Down Expand Up @@ -167,7 +174,7 @@ public function testRegenerateIdKeepsOnlyMarkerOnOldId(): void {
$session->regenerateId();
$session->close();

$this->assertSame([Internal::SUPERSEDED_AT], array_keys($this->readStoredSession($oldId)));
$this->assertSame([Internal::SUPERSEDED_AT => $this->now], $this->readStoredSession($oldId));
}

public function testRegenerateIdDeletesOldIdTheClientDidNotSend(): void {
Expand Down Expand Up @@ -200,9 +207,20 @@ public function testRequestOnSupersededIdGetsFreshSessionWithoutCookie(string $s
$this->assertFalse((bool)ini_get('session.use_cookies'));
}

public function testRequestStartingWithinGracePeriodGetsFreshSessionWithoutCookie(): void {
$oldId = $this->createSupersededId();
$this->now += Internal::SUPERSEDED_GRACE_PERIOD - 1;

$staleSession = $this->startRequest($oldId);

$this->assertNotSame($oldId, $staleSession->getId());
$this->assertFalse($staleSession->exists('foo'));
$this->assertFalse((bool)ini_get('session.use_cookies'));
}

public function testRequestStartingAfterGracePeriodGetsFreshSessionWithCookie(): void {
$oldId = $this->createSupersededId();
$this->markSupersededLongAgo($oldId);
$this->now += Internal::SUPERSEDED_GRACE_PERIOD;

$staleSession = $this->startRequest($oldId);

Expand Down Expand Up @@ -303,7 +321,7 @@ public function testReopenAfterConcurrentRegenerationMovesToFreshSession(): void
$concurrentRequest = $this->startRequest($oldId);
$concurrentRequest->regenerateId();
$concurrentRequest->close();
$this->markSupersededLongAgo($oldId);
$this->now += Internal::SUPERSEDED_GRACE_PERIOD;

// This request still runs on the old id and started before the regeneration
session_id($oldId);
Expand Down
Loading