diff --git a/application/bookmark/BookmarkFileService.php b/application/bookmark/BookmarkFileService.php index 3d15d4c9..6e04f3b7 100644 --- a/application/bookmark/BookmarkFileService.php +++ b/application/bookmark/BookmarkFileService.php @@ -46,6 +46,9 @@ class BookmarkFileService implements BookmarkServiceInterface /** @var bool true for logged in users. Default value to retrieve private bookmarks. */ protected $isLoggedIn; + /** @var bool Allow datastore alteration from not logged in users. */ + protected $anonymousPermission = false; + /** * @inheritDoc */ @@ -64,7 +67,7 @@ public function __construct(ConfigManager $conf, History $history, $isLoggedIn) $this->bookmarks = $this->bookmarksIO->read(); } catch (EmptyDataStoreException $e) { $this->bookmarks = new BookmarkArray(); - if ($isLoggedIn) { + if ($this->isLoggedIn) { $this->save(); } } @@ -154,7 +157,7 @@ public function get($id, $visibility = null) */ public function set($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -179,7 +182,7 @@ public function set($bookmark, $save = true) */ public function add($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -204,7 +207,7 @@ public function add($bookmark, $save = true) */ public function addOrSet($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -221,7 +224,7 @@ public function addOrSet($bookmark, $save = true) */ public function remove($bookmark, $save = true) { - if ($this->isLoggedIn !== true) { + if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { throw new Exception(t('You\'re not authorized to alter the datastore')); } if (! $bookmark instanceof Bookmark) { @@ -274,10 +277,11 @@ public function count($visibility = null) */ public function save() { - if (!$this->isLoggedIn) { + if (true !== $this->isLoggedIn && true !== $this->anonymousPermission) { // TODO: raise an Exception instead die('You are not authorized to change the database.'); } + $this->bookmarks->reorder(); $this->bookmarksIO->write($this->bookmarks); $this->pageCacheManager->invalidateCaches(); @@ -357,6 +361,16 @@ public function initialize() $initializer->initialize(); } + public function enableAnonymousPermission(): void + { + $this->anonymousPermission = true; + } + + public function disableAnonymousPermission(): void + { + $this->anonymousPermission = false; + } + /** * Handles migration to the new database format (BookmarksArray). */ diff --git a/application/bookmark/BookmarkInitializer.php b/application/bookmark/BookmarkInitializer.php index 9eee9a35..479ee9a9 100644 --- a/application/bookmark/BookmarkInitializer.php +++ b/application/bookmark/BookmarkInitializer.php @@ -34,13 +34,15 @@ public function __construct($bookmarkService) */ public function initialize() { + $this->bookmarkService->enableAnonymousPermission(); + $bookmark = new Bookmark(); $bookmark->setTitle(t('My secret stuff... - Pastebin.com')); - $bookmark->setUrl('http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8=', []); + $bookmark->setUrl('http://sebsauvage.net/paste/?8434b27936c09649#bR7XsXhoTiLcqCpQbmOpBi3rq2zzQUC5hBI7ZT1O3x8='); $bookmark->setDescription(t('Shhhh! I\'m a private link only YOU can see. You can delete me too.')); $bookmark->setTagsString('secretstuff'); $bookmark->setPrivate(true); - $this->bookmarkService->add($bookmark); + $this->bookmarkService->add($bookmark, false); $bookmark = new Bookmark(); $bookmark->setTitle(t('The personal, minimalist, super-fast, database free, bookmarking service')); @@ -54,6 +56,10 @@ public function initialize() You use the community supported version of the original Shaarli project, by Sebastien Sauvage.' )); $bookmark->setTagsString('opensource software'); - $this->bookmarkService->add($bookmark); + $this->bookmarkService->add($bookmark, false); + + $this->bookmarkService->save(); + + $this->bookmarkService->disableAnonymousPermission(); } } diff --git a/application/bookmark/BookmarkServiceInterface.php b/application/bookmark/BookmarkServiceInterface.php index 7b7a4f09..37fbda89 100644 --- a/application/bookmark/BookmarkServiceInterface.php +++ b/application/bookmark/BookmarkServiceInterface.php @@ -177,4 +177,17 @@ public function filterDay($request); * Creates the default database after a fresh install. */ public function initialize(); + + /** + * Allow to write the datastore from anonymous session (not logged in). + * + * This covers a few specific use cases, such as datastore initialization, + * but it should be used carefully as it can lead to security issues. + */ + public function enableAnonymousPermission(); + + /** + * Disable anonymous permission. + */ + public function disableAnonymousPermission(); } diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php index ccb87c3a..593aafb7 100644 --- a/application/container/ContainerBuilder.php +++ b/application/container/ContainerBuilder.php @@ -15,6 +15,7 @@ use Shaarli\Plugin\PluginManager; use Shaarli\Render\PageBuilder; use Shaarli\Render\PageCacheManager; +use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; use Shaarli\Thumbnailer; @@ -38,6 +39,9 @@ class ContainerBuilder /** @var SessionManager */ protected $session; + /** @var CookieManager */ + protected $cookieManager; + /** @var LoginManager */ protected $login; @@ -47,11 +51,13 @@ class ContainerBuilder public function __construct( ConfigManager $conf, SessionManager $session, + CookieManager $cookieManager, LoginManager $login ) { $this->conf = $conf; $this->session = $session; $this->login = $login; + $this->cookieManager = $cookieManager; } public function build(): ShaarliContainer @@ -60,6 +66,7 @@ public function build(): ShaarliContainer $container['conf'] = $this->conf; $container['sessionManager'] = $this->session; + $container['cookieManager'] = $this->cookieManager; $container['loginManager'] = $this->login; $container['basePath'] = $this->basePath; diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php index 09e7d5b1..c4fe753e 100644 --- a/application/container/ShaarliContainer.php +++ b/application/container/ShaarliContainer.php @@ -4,6 +4,7 @@ namespace Shaarli\Container; +use http\Cookie; use Shaarli\Bookmark\BookmarkServiceInterface; use Shaarli\Config\ConfigManager; use Shaarli\Feed\FeedBuilder; @@ -14,6 +15,7 @@ use Shaarli\Plugin\PluginManager; use Shaarli\Render\PageBuilder; use Shaarli\Render\PageCacheManager; +use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; use Shaarli\Thumbnailer; @@ -25,6 +27,7 @@ * * @property string $basePath Shaarli's instance base path (e.g. `/shaarli/`) * @property BookmarkServiceInterface $bookmarkService + * @property CookieManager $cookieManager * @property ConfigManager $conf * @property mixed[] $environment $_SERVER automatically injected by Slim * @property callable $errorHandler Overrides default Slim error display diff --git a/application/front/ShaarliMiddleware.php b/application/front/ShaarliMiddleware.php index baea6ef2..595182ac 100644 --- a/application/front/ShaarliMiddleware.php +++ b/application/front/ShaarliMiddleware.php @@ -43,6 +43,12 @@ public function __invoke(Request $request, Response $response, callable $next): $this->container->basePath = rtrim($request->getUri()->getBasePath(), '/'); try { + if (!is_file($this->container->conf->getConfigFileExt()) + && !in_array($next->getName(), ['displayInstall', 'saveInstall'], true) + ) { + return $response->withRedirect($this->container->basePath . '/install'); + } + $this->runUpdates(); $this->checkOpenShaarli($request, $response, $next); diff --git a/application/front/controller/admin/LogoutController.php b/application/front/controller/admin/LogoutController.php index c5984814..28165129 100644 --- a/application/front/controller/admin/LogoutController.php +++ b/application/front/controller/admin/LogoutController.php @@ -4,6 +4,7 @@ namespace Shaarli\Front\Controller\Admin; +use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Slim\Http\Request; use Slim\Http\Response; @@ -20,9 +21,12 @@ public function index(Request $request, Response $response): Response { $this->container->pageCacheManager->invalidateCaches(); $this->container->sessionManager->logout(); - - // TODO: switch to a simple Cookie manager allowing to check the session, and create mocks. - setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, 'false', 0, $this->container->basePath . '/'); + $this->container->cookieManager->setCookieParameter( + CookieManager::STAY_SIGNED_IN, + 'false', + 0, + $this->container->basePath . '/' + ); return $this->redirect($response, '/'); } diff --git a/application/front/controller/visitor/InstallController.php b/application/front/controller/visitor/InstallController.php new file mode 100644 index 00000000..aa032860 --- /dev/null +++ b/application/front/controller/visitor/InstallController.php @@ -0,0 +1,173 @@ +container->conf->getConfigFileExt())) { + throw new AlreadyInstalledException(); + } + } + + /** + * Display the install template page. + * Also test file permissions and sessions beforehand. + */ + public function index(Request $request, Response $response): Response + { + // Before installation, we'll make sure that permissions are set properly, and sessions are working. + $this->checkPermissions(); + + if (static::SESSION_TEST_VALUE + !== $this->container->sessionManager->getSessionParameter(static::SESSION_TEST_KEY) + ) { + $this->container->sessionManager->setSessionParameter(static::SESSION_TEST_KEY, static::SESSION_TEST_VALUE); + + return $this->redirect($response, '/install/session-test'); + } + + [$continents, $cities] = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); + + $this->assignView('continents', $continents); + $this->assignView('cities', $cities); + $this->assignView('languages', Languages::getAvailableLanguages()); + + return $response->write($this->render('install')); + } + + /** + * Route checking that the session parameter has been properly saved between two distinct requests. + * If the session parameter is preserved, redirect to install template page, otherwise displays error. + */ + public function sessionTest(Request $request, Response $response): Response + { + // This part makes sure sessions works correctly. + // (Because on some hosts, session.save_path may not be set correctly, + // or we may not have write access to it.) + if (static::SESSION_TEST_VALUE + !== $this->container->sessionManager->getSessionParameter(static::SESSION_TEST_KEY) + ) { + // Step 2: Check if data in session is correct. + $msg = t( + '
Sessions do not seem to work correctly on your server.
'. + 'Make sure the variable "session.save_path" is set correctly in your PHP config, '. + 'and that you have write access to it.
'. + 'It currently points to %s.
'. + 'On some browsers, accessing your server via a hostname like \'localhost\' '. + 'or any custom hostname without a dot causes cookie storage to fail. '. + 'We recommend accessing your server via it\'s IP address or Fully Qualified Domain Name.
' + ); + $msg = sprintf($msg, $this->container->sessionManager->getSavePath()); + + $this->assignView('message', $msg); + + return $response->write($this->render('error')); + } + + return $this->redirect($response, '/install'); + } + + /** + * Save installation form and initialize config file and datastore if necessary. + */ + public function save(Request $request, Response $response): Response + { + $timezone = 'UTC'; + if (!empty($request->getParam('continent')) + && !empty($request->getParam('city')) + && isTimeZoneValid($request->getParam('continent'), $request->getParam('city')) + ) { + $timezone = $request->getParam('continent') . '/' . $request->getParam('city'); + } + $this->container->conf->set('general.timezone', $timezone); + + $login = $request->getParam('setlogin'); + $this->container->conf->set('credentials.login', $login); + $salt = sha1(uniqid('', true) .'_'. mt_rand()); + $this->container->conf->set('credentials.salt', $salt); + $this->container->conf->set('credentials.hash', sha1($request->getParam('setpassword') . $login . $salt)); + + if (!empty($request->getParam('title'))) { + $this->container->conf->set('general.title', escape($request->getParam('title'))); + } else { + $this->container->conf->set( + 'general.title', + 'Shared bookmarks on '.escape(index_url($this->container->environment)) + ); + } + + $this->container->conf->set('translation.language', escape($request->getParam('language'))); + $this->container->conf->set('updates.check_updates', !empty($request->getParam('updateCheck'))); + $this->container->conf->set('api.enabled', !empty($request->getParam('enableApi'))); + $this->container->conf->set( + 'api.secret', + generate_api_secret( + $this->container->conf->get('credentials.login'), + $this->container->conf->get('credentials.salt') + ) + ); + + try { + // Everything is ok, let's create config file. + $this->container->conf->write($this->container->loginManager->isLoggedIn()); + } catch (\Exception $e) { + $this->assignView('message', $e->getMessage()); + $this->assignView('stacktrace', $e->getTraceAsString()); + + return $response->write($this->render('error')); + } + + if ($this->container->bookmarkService->count(BookmarkFilter::$ALL) === 0) { + $this->container->bookmarkService->initialize(); + } + + $this->container->sessionManager->setSessionParameter( + SessionManager::KEY_SUCCESS_MESSAGES, + [t('Shaarli is now configured. Please login and start shaaring your bookmarks!')] + ); + + return $this->redirect($response, '/'); + } + + protected function checkPermissions(): bool + { + // Ensure Shaarli has proper access to its resources + $errors = ApplicationUtils::checkResourcePermissions($this->container->conf); + + if (empty($errors)) { + return true; + } + + // FIXME! Do not insert HTML here. + $message = ''. t('Insufficient permissions:') .'
'. t('Insufficient permissions:') .'
Sessions do not seem to work correctly on your server.'; - die; - } - if (!isset($_SESSION['session_tested'])) { - // Step 1 : Try to store data in session and reload page. - $_SESSION['session_tested'] = 'Working'; // Try to set a variable in session. - header('Location: '.index_url($_SERVER).'?test_session'); // Redirect to check stored data. - } - if (isset($_GET['test_session'])) { - // Step 3: Sessions are OK. Remove test parameter from URL. - header('Location: '.index_url($_SERVER)); - } - - - if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) { - $tz = 'UTC'; - if (!empty($_POST['continent']) && !empty($_POST['city']) - && isTimeZoneValid($_POST['continent'], $_POST['city']) - ) { - $tz = $_POST['continent'].'/'.$_POST['city']; - } - $conf->set('general.timezone', $tz); - $login = $_POST['setlogin']; - $conf->set('credentials.login', $login); - $salt = sha1(uniqid('', true) .'_'. mt_rand()); - $conf->set('credentials.salt', $salt); - $conf->set('credentials.hash', sha1($_POST['setpassword'] . $login . $salt)); - if (!empty($_POST['title'])) { - $conf->set('general.title', escape($_POST['title'])); - } else { - $conf->set('general.title', 'Shared bookmarks on '.escape(index_url($_SERVER))); - } - $conf->set('translation.language', escape($_POST['language'])); - $conf->set('updates.check_updates', !empty($_POST['updateCheck'])); - $conf->set('api.enabled', !empty($_POST['enableApi'])); - $conf->set( - 'api.secret', - generate_api_secret( - $conf->get('credentials.login'), - $conf->get('credentials.salt') - ) - ); - try { - // Everything is ok, let's create config file. - $conf->write($loginManager->isLoggedIn()); - } catch (Exception $e) { - error_log( - 'ERROR while writing config file after installation.' . PHP_EOL . - $e->getMessage() - ); - - // TODO: do not handle exceptions/errors in JS. - echo ''; - exit; - } - - $history = new History($conf->get('resource.history')); - $bookmarkService = new BookmarkFileService($conf, $history, true); - if ($bookmarkService->count() === 0) { - $bookmarkService->initialize(); - } - - echo ''; - exit; - } - - $PAGE = new PageBuilder($conf, $_SESSION, null, $sessionManager->generateToken()); - list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); - $PAGE->assign('continents', $continents); - $PAGE->assign('cities', $cities); - $PAGE->assign('languages', Languages::getAvailableLanguages()); - $PAGE->renderPage('install'); - exit; -} - if (!isset($_SESSION['LINKS_PER_PAGE'])) { $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20); } -$containerBuilder = new ContainerBuilder($conf, $sessionManager, $loginManager); +$containerBuilder = new ContainerBuilder($conf, $sessionManager, $cookieManager, $loginManager); $container = $containerBuilder->build(); $app = new App($container); @@ -408,6 +275,10 @@ function install($conf, $sessionManager, $loginManager) })->add('\Shaarli\Api\ApiMiddleware'); $app->group('', function () { + $this->get('/install', '\Shaarli\Front\Controller\Visitor\InstallController:index')->setName('displayInstall'); + $this->get('/install/session-test', '\Shaarli\Front\Controller\Visitor\InstallController:sessionTest'); + $this->post('/install', '\Shaarli\Front\Controller\Visitor\InstallController:save')->setName('saveInstall'); + /* -- PUBLIC --*/ $this->get('/', '\Shaarli\Front\Controller\Visitor\BookmarkListController:index'); $this->get('/shaare/{hash}', '\Shaarli\Front\Controller\Visitor\BookmarkListController:permalink'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 511698ff..d4ddedd5 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -18,9 +18,14 @@ function is_iterable($var) require_once 'application/Utils.php'; require_once 'application/http/UrlUtils.php'; require_once 'application/http/HttpUtils.php'; -require_once 'tests/utils/ReferenceLinkDB.php'; -require_once 'tests/utils/ReferenceHistory.php'; -require_once 'tests/utils/FakeBookmarkService.php'; require_once 'tests/container/ShaarliTestContainer.php'; require_once 'tests/front/controller/visitor/FrontControllerMockHelper.php'; require_once 'tests/front/controller/admin/FrontAdminControllerMockHelper.php'; +require_once 'tests/updater/DummyUpdater.php'; +require_once 'tests/utils/FakeBookmarkService.php'; +require_once 'tests/utils/FakeConfigManager.php'; +require_once 'tests/utils/ReferenceHistory.php'; +require_once 'tests/utils/ReferenceLinkDB.php'; +require_once 'tests/utils/ReferenceSessionIdHashes.php'; + +\ReferenceSessionIdHashes::genAllHashes(); diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php index db533f37..fa77bf31 100644 --- a/tests/container/ContainerBuilderTest.php +++ b/tests/container/ContainerBuilderTest.php @@ -11,12 +11,15 @@ use Shaarli\Formatter\FormatterFactory; use Shaarli\History; use Shaarli\Http\HttpAccess; +use Shaarli\Netscape\NetscapeBookmarkUtils; use Shaarli\Plugin\PluginManager; use Shaarli\Render\PageBuilder; use Shaarli\Render\PageCacheManager; +use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; use Shaarli\Thumbnailer; +use Shaarli\Updater\Updater; class ContainerBuilderTest extends TestCase { @@ -32,10 +35,14 @@ class ContainerBuilderTest extends TestCase /** @var ContainerBuilder */ protected $containerBuilder; + /** @var CookieManager */ + protected $cookieManager; + public function setUp(): void { $this->conf = new ConfigManager('tests/utils/config/configJson'); $this->sessionManager = $this->createMock(SessionManager::class); + $this->cookieManager = $this->createMock(CookieManager::class); $this->loginManager = $this->createMock(LoginManager::class); $this->loginManager->method('isLoggedIn')->willReturn(true); @@ -43,6 +50,7 @@ public function setUp(): void $this->containerBuilder = new ContainerBuilder( $this->conf, $this->sessionManager, + $this->cookieManager, $this->loginManager ); } @@ -53,6 +61,7 @@ public function testBuildContainer(): void static::assertInstanceOf(ConfigManager::class, $container->conf); static::assertInstanceOf(SessionManager::class, $container->sessionManager); + static::assertInstanceOf(CookieManager::class, $container->cookieManager); static::assertInstanceOf(LoginManager::class, $container->loginManager); static::assertInstanceOf(History::class, $container->history); static::assertInstanceOf(BookmarkServiceInterface::class, $container->bookmarkService); @@ -63,6 +72,8 @@ public function testBuildContainer(): void static::assertInstanceOf(FeedBuilder::class, $container->feedBuilder); static::assertInstanceOf(Thumbnailer::class, $container->thumbnailer); static::assertInstanceOf(HttpAccess::class, $container->httpAccess); + static::assertInstanceOf(NetscapeBookmarkUtils::class, $container->netscapeBookmarkUtils); + static::assertInstanceOf(Updater::class, $container->updater); // Set by the middleware static::assertNull($container->basePath); diff --git a/tests/front/ShaarliMiddlewareTest.php b/tests/front/ShaarliMiddlewareTest.php index 81ea1344..20090d8b 100644 --- a/tests/front/ShaarliMiddlewareTest.php +++ b/tests/front/ShaarliMiddlewareTest.php @@ -19,6 +19,8 @@ class ShaarliMiddlewareTest extends TestCase { + protected const TMP_MOCK_FILE = '.tmp'; + /** @var ShaarliContainer */ protected $container; @@ -29,12 +31,21 @@ public function setUp(): void { $this->container = $this->createMock(ShaarliContainer::class); + touch(static::TMP_MOCK_FILE); + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); + $this->container->loginManager = $this->createMock(LoginManager::class); $this->middleware = new ShaarliMiddleware($this->container); } + public function tearDown() + { + unlink(static::TMP_MOCK_FILE); + } + /** * Test middleware execution with valid controller call */ @@ -179,6 +190,7 @@ public function testMiddlewareExecutionWithUpdates(): void $this->container->conf->method('get')->willReturnCallback(function (string $key): string { return $key; }); + $this->container->conf->method('getConfigFileExt')->willReturn(static::TMP_MOCK_FILE); $this->container->pageCacheManager = $this->createMock(PageCacheManager::class); $this->container->pageCacheManager->expects(static::once())->method('invalidateCaches'); diff --git a/tests/front/controller/admin/LogoutControllerTest.php b/tests/front/controller/admin/LogoutControllerTest.php index ca177085..45e84dc0 100644 --- a/tests/front/controller/admin/LogoutControllerTest.php +++ b/tests/front/controller/admin/LogoutControllerTest.php @@ -4,14 +4,8 @@ namespace Shaarli\Front\Controller\Admin; -/** Override PHP builtin setcookie function in the local namespace to mock it... more or less */ -if (!function_exists('Shaarli\Front\Controller\Admin\setcookie')) { - function setcookie(string $name, string $value): void { - $_COOKIE[$name] = $value; - } -} - use PHPUnit\Framework\TestCase; +use Shaarli\Security\CookieManager; use Shaarli\Security\LoginManager; use Shaarli\Security\SessionManager; use Slim\Http\Request; @@ -29,8 +23,6 @@ public function setUp(): void $this->createContainer(); $this->controller = new LogoutController($this->container); - - setcookie(LoginManager::$STAY_SIGNED_IN_COOKIE, $cookie = 'hi there'); } public function testValidControllerInvoke(): void @@ -43,13 +35,17 @@ public function testValidControllerInvoke(): void $this->container->sessionManager = $this->createMock(SessionManager::class); $this->container->sessionManager->expects(static::once())->method('logout'); - static::assertSame('hi there', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); + $this->container->cookieManager = $this->createMock(CookieManager::class); + $this->container->cookieManager + ->expects(static::once()) + ->method('setCookieParameter') + ->with(CookieManager::STAY_SIGNED_IN, 'false', 0, '/subfolder/') + ; $result = $this->controller->index($request, $response); static::assertInstanceOf(Response::class, $result); static::assertSame(302, $result->getStatusCode()); static::assertSame(['/subfolder/'], $result->getHeader('location')); - static::assertSame('false', $_COOKIE[LoginManager::$STAY_SIGNED_IN_COOKIE]); } } diff --git a/tests/front/controller/visitor/InstallControllerTest.php b/tests/front/controller/visitor/InstallControllerTest.php new file mode 100644 index 00000000..6871fdd9 --- /dev/null +++ b/tests/front/controller/visitor/InstallControllerTest.php @@ -0,0 +1,264 @@ +createContainer(); + + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf->method('getConfigFileExt')->willReturn(static::MOCK_FILE); + $this->container->conf->method('get')->willReturnCallback(function (string $key, $default) { + if ($key === 'resource.raintpl_tpl') { + return '.'; + } + + return $default ?? $key; + }); + + $this->controller = new InstallController($this->container); + } + + protected function tearDown(): void + { + if (file_exists(static::MOCK_FILE)) { + unlink(static::MOCK_FILE); + } + } + + /** + * Test displaying install page with valid session. + */ + public function testInstallIndexWithValidSession(): void + { + $assignedVariables = []; + $this->assignTemplateVars($assignedVariables); + + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager + ->method('getSessionParameter') + ->willReturnCallback(function (string $key, $default) { + return $key === 'session_tested' ? 'Working' : $default; + }) + ; + + $result = $this->controller->index($request, $response); + + static::assertSame(200, $result->getStatusCode()); + static::assertSame('install', (string) $result->getBody()); + + static::assertIsArray($assignedVariables['continents']); + static::assertSame('Africa', $assignedVariables['continents'][0]); + static::assertSame('UTC', $assignedVariables['continents']['selected']); + + static::assertIsArray($assignedVariables['cities']); + static::assertSame(['continent' => 'Africa', 'city' => 'Abidjan'], $assignedVariables['cities'][0]); + static::assertSame('UTC', $assignedVariables['continents']['selected']); + + static::assertIsArray($assignedVariables['languages']); + static::assertSame('Automatic', $assignedVariables['languages']['auto']); + static::assertSame('French', $assignedVariables['languages']['fr']); + } + + /** + * Instantiate the install controller with an existing config file: exception. + */ + public function testInstallWithExistingConfigFile(): void + { + $this->expectException(AlreadyInstalledException::class); + + touch(static::MOCK_FILE); + + $this->controller = new InstallController($this->container); + } + + /** + * Call controller without session yet defined, redirect to test session install page. + */ + public function testInstallRedirectToSessionTest(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager + ->expects(static::once()) + ->method('setSessionParameter') + ->with(InstallController::SESSION_TEST_KEY, InstallController::SESSION_TEST_VALUE) + ; + + $result = $this->controller->index($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/install/session-test', $result->getHeader('location')[0]); + } + + /** + * Call controller in session test mode: valid session then redirect to install page. + */ + public function testInstallSessionTestValid(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager + ->method('getSessionParameter') + ->with(InstallController::SESSION_TEST_KEY) + ->willReturn(InstallController::SESSION_TEST_VALUE) + ; + + $result = $this->controller->sessionTest($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/install', $result->getHeader('location')[0]); + } + + /** + * Call controller in session test mode: invalid session then redirect to error page. + */ + public function testInstallSessionTestError(): void + { + $assignedVars = []; + $this->assignTemplateVars($assignedVars); + + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager + ->method('getSessionParameter') + ->with(InstallController::SESSION_TEST_KEY) + ->willReturn('KO') + ; + + $result = $this->controller->sessionTest($request, $response); + + static::assertSame(200, $result->getStatusCode()); + static::assertSame('error', (string) $result->getBody()); + static::assertStringStartsWith( + '
'. - 'Make sure the variable "session.save_path" is set correctly in your PHP config, '. - 'and that you have write access to it.
'. - 'It currently points to %s.
'. - 'On some browsers, accessing your server via a hostname like \'localhost\' '. - 'or any custom hostname without a dot causes cookie storage to fail. '. - 'We recommend accessing your server via it\'s IP address or Fully Qualified Domain Name.
' - ); - $msg = sprintf($msg, session_save_path()); - echo $msg; - echo '
'. t('Click to try again.') .'
Sessions do not seem to work correctly on your server', + $assignedVars['message'] + ); + } + + /** + * Test saving valid data from install form. Also initialize datastore. + */ + public function testSaveInstallValid(): void + { + $providedParameters = [ + 'continent' => 'Europe', + 'city' => 'Berlin', + 'setlogin' => 'bob', + 'setpassword' => 'password', + 'title' => 'Shaarli', + 'language' => 'fr', + 'updateCheck' => true, + 'enableApi' => true, + ]; + + $expectedSettings = [ + 'general.timezone' => 'Europe/Berlin', + 'credentials.login' => 'bob', + 'credentials.salt' => '_NOT_EMPTY', + 'credentials.hash' => '_NOT_EMPTY', + 'general.title' => 'Shaarli', + 'translation.language' => 'en', + 'updates.check_updates' => true, + 'api.enabled' => true, + 'api.secret' => '_NOT_EMPTY', + ]; + + $request = $this->createMock(Request::class); + $request->method('getParam')->willReturnCallback(function (string $key) use ($providedParameters) { + return $providedParameters[$key] ?? null; + }); + $response = new Response(); + + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf + ->method('get') + ->willReturnCallback(function (string $key, $value) { + if ($key === 'credentials.login') { + return 'bob'; + } elseif ($key === 'credentials.salt') { + return 'salt'; + } + + return $value; + }) + ; + $this->container->conf + ->expects(static::exactly(count($expectedSettings))) + ->method('set') + ->willReturnCallback(function (string $key, $value) use ($expectedSettings) { + if ($expectedSettings[$key] ?? null === '_NOT_EMPTY') { + static::assertNotEmpty($value); + } else { + static::assertSame($expectedSettings[$key], $value); + } + }) + ; + $this->container->conf->expects(static::once())->method('write'); + + $this->container->bookmarkService->expects(static::once())->method('count')->willReturn(0); + $this->container->bookmarkService->expects(static::once())->method('initialize'); + + $this->container->sessionManager + ->expects(static::once()) + ->method('setSessionParameter') + ->with(SessionManager::KEY_SUCCESS_MESSAGES) + ; + + $result = $this->controller->save($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + } + + /** + * Test default settings (timezone and title). + * Also check that bookmarks are not initialized if + */ + public function testSaveInstallDefaultValues(): void + { + $confSettings = []; + + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->conf->method('set')->willReturnCallback(function (string $key, $value) use (&$confSettings) { + $confSettings[$key] = $value; + }); + + $result = $this->controller->save($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + + static::assertSame('UTC', $confSettings['general.timezone']); + static::assertSame('Shared bookmarks on http://shaarli', $confSettings['general.title']); + } +} diff --git a/tests/render/PageCacheManagerTest.php b/tests/render/PageCacheManagerTest.php index b870e6eb..c258f45f 100644 --- a/tests/render/PageCacheManagerTest.php +++ b/tests/render/PageCacheManagerTest.php @@ -1,15 +1,14 @@ cookie = []; $this->session = []; - $this->sessionManager = new SessionManager($this->session, $this->configManager); - $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); + $this->cookieManager = $this->createMock(CookieManager::class); + $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) { + return $this->cookie[$key] ?? null; + }); + $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path'); + $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager); $this->server['REMOTE_ADDR'] = $this->ipAddr; } @@ -193,8 +199,8 @@ public function testCheckLoginStateNotConfigured() $configManager = new \FakeConfigManager([ 'resource.ban_file' => $this->banFile, ]); - $loginManager = new LoginManager($configManager, null); - $loginManager->checkLoginState([], ''); + $loginManager = new LoginManager($configManager, null, $this->cookieManager); + $loginManager->checkLoginState(''); $this->assertFalse($loginManager->isLoggedIn()); } @@ -210,9 +216,9 @@ public function testCheckLoginStateStaySignedInWithInvalidToken() 'expires_on' => time() + 100, ]; $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; + $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope'; - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertTrue($this->loginManager->isLoggedIn()); $this->assertTrue(empty($this->session['username'])); @@ -224,9 +230,9 @@ public function testCheckLoginStateStaySignedInWithInvalidToken() public function testCheckLoginStateStaySignedInWithValidToken() { $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); + $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken(); - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertTrue($this->loginManager->isLoggedIn()); $this->assertEquals($this->login, $this->session['username']); @@ -241,7 +247,7 @@ public function testCheckLoginStateSessionExpired() $this->loginManager->generateStaySignedInToken($this->clientIpAddress); $this->session['expires_on'] = time() - 100; - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertFalse($this->loginManager->isLoggedIn()); } @@ -253,7 +259,7 @@ public function testCheckLoginStateClientIpChanged() { $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); + $this->loginManager->checkLoginState('10.7.157.98'); $this->assertFalse($this->loginManager->isLoggedIn()); } diff --git a/tests/security/SessionManagerTest.php b/tests/security/SessionManagerTest.php index d9db775e..60695dcf 100644 --- a/tests/security/SessionManagerTest.php +++ b/tests/security/SessionManagerTest.php @@ -1,12 +1,8 @@ conf = new FakeConfigManager([ + $this->conf = new \FakeConfigManager([ 'credentials.login' => 'johndoe', 'credentials.salt' => 'salt', 'security.session_protection_disabled' => false, ]); $this->session = []; - $this->sessionManager = new SessionManager($this->session, $this->conf); + $this->sessionManager = new SessionManager($this->session, $this->conf, 'session_path'); } /** @@ -69,7 +65,7 @@ public function testCheckToken() $token => 1, ], ]; - $sessionManager = new SessionManager($session, $this->conf); + $sessionManager = new SessionManager($session, $this->conf, 'session_path'); // check and destroy the token $this->assertTrue($sessionManager->checkToken($token)); diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php index afc35aec..c801d451 100644 --- a/tests/updater/UpdaterTest.php +++ b/tests/updater/UpdaterTest.php @@ -7,9 +7,6 @@ use Shaarli\Config\ConfigManager; use Shaarli\History; -require_once 'tests/updater/DummyUpdater.php'; -require_once 'tests/utils/ReferenceLinkDB.php'; -require_once 'inc/rain.tpl.class.php'; /** * Class UpdaterTest. @@ -35,6 +32,9 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase /** @var BookmarkServiceInterface */ protected $bookmarkService; + /** @var \ReferenceLinkDB */ + protected $refDB; + /** @var Updater */ protected $updater; @@ -43,6 +43,9 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase */ public function setUp() { + $this->refDB = new \ReferenceLinkDB(); + $this->refDB->write(self::$testDatastore); + copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); $this->conf = new ConfigManager(self::$configFile); $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true); @@ -181,9 +184,40 @@ public function testUpdateFailed() public function testUpdateMethodRelativeHomeLinkRename(): void { + $this->updater->setBasePath('/subfolder'); $this->conf->set('general.header_link', '?'); + $this->updater->updateMethodRelativeHomeLink(); - static::assertSame(); + static::assertSame('/subfolder/', $this->conf->get('general.header_link')); + } + + public function testUpdateMethodRelativeHomeLinkDoNotRename(): void + { + $this->updater->setBasePath('/subfolder'); + $this->conf->set('general.header_link', '~/my-blog'); + + $this->updater->updateMethodRelativeHomeLink(); + + static::assertSame('~/my-blog', $this->conf->get('general.header_link')); + } + + public function testUpdateMethodMigrateExistingNotesUrl(): void + { + $this->updater->setBasePath('/subfolder'); + + $this->updater->updateMethodMigrateExistingNotesUrl(); + + static::assertSame($this->refDB->getLinks()[0]->getUrl(), $this->bookmarkService->get(0)->getUrl()); + static::assertSame($this->refDB->getLinks()[1]->getUrl(), $this->bookmarkService->get(1)->getUrl()); + static::assertSame($this->refDB->getLinks()[4]->getUrl(), $this->bookmarkService->get(4)->getUrl()); + static::assertSame($this->refDB->getLinks()[6]->getUrl(), $this->bookmarkService->get(6)->getUrl()); + static::assertSame($this->refDB->getLinks()[7]->getUrl(), $this->bookmarkService->get(7)->getUrl()); + static::assertSame($this->refDB->getLinks()[8]->getUrl(), $this->bookmarkService->get(8)->getUrl()); + static::assertSame($this->refDB->getLinks()[9]->getUrl(), $this->bookmarkService->get(9)->getUrl()); + static::assertSame('/subfolder/shaare/WDWyig', $this->bookmarkService->get(42)->getUrl()); + static::assertSame('/subfolder/shaare/WDWyig', $this->bookmarkService->get(41)->getUrl()); + static::assertSame('/subfolder/shaare/0gCTjQ', $this->bookmarkService->get(10)->getUrl()); + static::assertSame('/subfolder/shaare/PCRizQ', $this->bookmarkService->get(11)->getUrl()); } } diff --git a/tpl/default/error.html b/tpl/default/error.html index 4abac9ca..c3e0c3c1 100644 --- a/tpl/default/error.html +++ b/tpl/default/error.html @@ -15,7 +15,7 @@{/if} - + {include="page.footer"}{$message}