diff options
-rw-r--r-- | application/PageBuilder.php | 6 | ||||
-rw-r--r-- | application/SessionManager.php | 53 | ||||
-rw-r--r-- | index.php | 71 | ||||
-rw-r--r-- | tests/SessionManagerTest.php | 72 |
4 files changed, 153 insertions, 49 deletions
diff --git a/application/PageBuilder.php b/application/PageBuilder.php index af290671..468f144b 100644 --- a/application/PageBuilder.php +++ b/application/PageBuilder.php | |||
@@ -32,12 +32,14 @@ class PageBuilder | |||
32 | * | 32 | * |
33 | * @param ConfigManager $conf Configuration Manager instance (reference). | 33 | * @param ConfigManager $conf Configuration Manager instance (reference). |
34 | * @param LinkDB $linkDB instance. | 34 | * @param LinkDB $linkDB instance. |
35 | * @param string $token Session token | ||
35 | */ | 36 | */ |
36 | public function __construct(&$conf, $linkDB = null) | 37 | public function __construct(&$conf, $linkDB = null, $token = null) |
37 | { | 38 | { |
38 | $this->tpl = false; | 39 | $this->tpl = false; |
39 | $this->conf = $conf; | 40 | $this->conf = $conf; |
40 | $this->linkDB = $linkDB; | 41 | $this->linkDB = $linkDB; |
42 | $this->token = $token; | ||
41 | } | 43 | } |
42 | 44 | ||
43 | /** | 45 | /** |
@@ -92,7 +94,7 @@ class PageBuilder | |||
92 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); | 94 | $this->tpl->assign('showatom', $this->conf->get('feed.show_atom', true)); |
93 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); | 95 | $this->tpl->assign('feed_type', $this->conf->get('feed.show_atom', true) !== false ? 'atom' : 'rss'); |
94 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); | 96 | $this->tpl->assign('hide_timestamps', $this->conf->get('privacy.hide_timestamps', false)); |
95 | $this->tpl->assign('token', getToken($this->conf)); | 97 | $this->tpl->assign('token', $this->token); |
96 | 98 | ||
97 | if ($this->linkDB !== null) { | 99 | if ($this->linkDB !== null) { |
98 | $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); | 100 | $this->tpl->assign('tags', $this->linkDB->linksCountPerTag()); |
diff --git a/application/SessionManager.php b/application/SessionManager.php new file mode 100644 index 00000000..2083df42 --- /dev/null +++ b/application/SessionManager.php | |||
@@ -0,0 +1,53 @@ | |||
1 | <?php | ||
2 | namespace Shaarli; | ||
3 | |||
4 | /** | ||
5 | * Manages the server-side session | ||
6 | */ | ||
7 | class SessionManager | ||
8 | { | ||
9 | protected $session = []; | ||
10 | |||
11 | /** | ||
12 | * Constructor | ||
13 | * | ||
14 | * @param array $session The $_SESSION array (reference) | ||
15 | * @param ConfigManager $conf ConfigManager instance (reference) | ||
16 | */ | ||
17 | public function __construct(& $session, & $conf) | ||
18 | { | ||
19 | $this->session = &$session; | ||
20 | $this->conf = &$conf; | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * Generates a session token | ||
25 | * | ||
26 | * @return string token | ||
27 | */ | ||
28 | public function generateToken() | ||
29 | { | ||
30 | $token = sha1(uniqid('', true) .'_'. mt_rand() . $this->conf->get('credentials.salt')); | ||
31 | $this->session['tokens'][$token] = 1; | ||
32 | return $token; | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Checks the validity of a session token, and destroys it afterwards | ||
37 | * | ||
38 | * @param string $token The token to check | ||
39 | * | ||
40 | * @return bool true if the token is valid, else false | ||
41 | */ | ||
42 | public function checkToken($token) | ||
43 | { | ||
44 | if (! isset($this->session['tokens'][$token])) { | ||
45 | // the token is wrong, or has already been used | ||
46 | return false; | ||
47 | } | ||
48 | |||
49 | // destroy the token to prevent future use | ||
50 | unset($this->session['tokens'][$token]); | ||
51 | return true; | ||
52 | } | ||
53 | } | ||
@@ -78,6 +78,7 @@ require_once 'application/Updater.php'; | |||
78 | use \Shaarli\Languages; | 78 | use \Shaarli\Languages; |
79 | use \Shaarli\ThemeUtils; | 79 | use \Shaarli\ThemeUtils; |
80 | use \Shaarli\Config\ConfigManager; | 80 | use \Shaarli\Config\ConfigManager; |
81 | use \Shaarli\SessionManager; | ||
81 | 82 | ||
82 | // Ensure the PHP version is supported | 83 | // Ensure the PHP version is supported |
83 | try { | 84 | try { |
@@ -121,6 +122,7 @@ if (isset($_COOKIE['shaarli']) && !is_session_id_valid($_COOKIE['shaarli'])) { | |||
121 | } | 122 | } |
122 | 123 | ||
123 | $conf = new ConfigManager(); | 124 | $conf = new ConfigManager(); |
125 | $sessionManager = new SessionManager($_SESSION, $conf); | ||
124 | 126 | ||
125 | // Sniff browser language and set date format accordingly. | 127 | // Sniff browser language and set date format accordingly. |
126 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { | 128 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
@@ -165,7 +167,7 @@ if (! is_file($conf->getConfigFileExt())) { | |||
165 | } | 167 | } |
166 | 168 | ||
167 | // Display the installation form if no existing config is found | 169 | // Display the installation form if no existing config is found |
168 | install($conf); | 170 | install($conf, $sessionManager); |
169 | } | 171 | } |
170 | 172 | ||
171 | // a token depending of deployment salt, user password, and the current ip | 173 | // a token depending of deployment salt, user password, and the current ip |
@@ -381,7 +383,7 @@ if (isset($_POST['login'])) | |||
381 | { | 383 | { |
382 | if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.')); | 384 | if (!ban_canLogin($conf)) die(t('I said: NO. You are banned for the moment. Go away.')); |
383 | if (isset($_POST['password']) | 385 | if (isset($_POST['password']) |
384 | && tokenOk($_POST['token']) | 386 | && $sessionManager->checkToken($_POST['token']) |
385 | && (check_auth($_POST['login'], $_POST['password'], $conf)) | 387 | && (check_auth($_POST['login'], $_POST['password'], $conf)) |
386 | ) { // Login/password is OK. | 388 | ) { // Login/password is OK. |
387 | ban_loginOk($conf); | 389 | ban_loginOk($conf); |
@@ -455,32 +457,6 @@ if (isset($_POST['login'])) | |||
455 | if (!isset($_SESSION['tokens'])) $_SESSION['tokens']=array(); // Token are attached to the session. | 457 | if (!isset($_SESSION['tokens'])) $_SESSION['tokens']=array(); // Token are attached to the session. |
456 | 458 | ||
457 | /** | 459 | /** |
458 | * Returns a token. | ||
459 | * | ||
460 | * @param ConfigManager $conf Configuration Manager instance. | ||
461 | * | ||
462 | * @return string token. | ||
463 | */ | ||
464 | function getToken($conf) | ||
465 | { | ||
466 | $rnd = sha1(uniqid('', true) .'_'. mt_rand() . $conf->get('credentials.salt')); // We generate a random string. | ||
467 | $_SESSION['tokens'][$rnd]=1; // Store it on the server side. | ||
468 | return $rnd; | ||
469 | } | ||
470 | |||
471 | // Tells if a token is OK. Using this function will destroy the token. | ||
472 | // true=token is OK. | ||
473 | function tokenOk($token) | ||
474 | { | ||
475 | if (isset($_SESSION['tokens'][$token])) | ||
476 | { | ||
477 | unset($_SESSION['tokens'][$token]); // Token is used: destroy it. | ||
478 | return true; // Token is OK. | ||
479 | } | ||
480 | return false; // Wrong token, or already used. | ||
481 | } | ||
482 | |||
483 | /** | ||
484 | * Daily RSS feed: 1 RSS entry per day giving all the links on that day. | 460 | * Daily RSS feed: 1 RSS entry per day giving all the links on that day. |
485 | * Gives the last 7 days (which have links). | 461 | * Gives the last 7 days (which have links). |
486 | * This RSS feed cannot be filtered. | 462 | * This RSS feed cannot be filtered. |
@@ -687,12 +663,13 @@ function showLinkList($PAGE, $LINKSDB, $conf, $pluginManager) { | |||
687 | /** | 663 | /** |
688 | * Render HTML page (according to URL parameters and user rights) | 664 | * Render HTML page (according to URL parameters and user rights) |
689 | * | 665 | * |
690 | * @param ConfigManager $conf Configuration Manager instance. | 666 | * @param ConfigManager $conf Configuration Manager instance. |
691 | * @param PluginManager $pluginManager Plugin Manager instance, | 667 | * @param PluginManager $pluginManager Plugin Manager instance, |
692 | * @param LinkDB $LINKSDB | 668 | * @param LinkDB $LINKSDB |
693 | * @param History $history instance | 669 | * @param History $history instance |
670 | * @param SessionManager $sessionManager SessionManager instance | ||
694 | */ | 671 | */ |
695 | function renderPage($conf, $pluginManager, $LINKSDB, $history) | 672 | function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager) |
696 | { | 673 | { |
697 | $updater = new Updater( | 674 | $updater = new Updater( |
698 | read_updates_file($conf->get('resource.updates')), | 675 | read_updates_file($conf->get('resource.updates')), |
@@ -713,7 +690,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
713 | die($e->getMessage()); | 690 | die($e->getMessage()); |
714 | } | 691 | } |
715 | 692 | ||
716 | $PAGE = new PageBuilder($conf, $LINKSDB); | 693 | $PAGE = new PageBuilder($conf, $LINKSDB, $sessionManager->generateToken()); |
717 | $PAGE->assign('linkcount', count($LINKSDB)); | 694 | $PAGE->assign('linkcount', count($LINKSDB)); |
718 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); | 695 | $PAGE->assign('privateLinkcount', count_private($LINKSDB)); |
719 | $PAGE->assign('plugin_errors', $pluginManager->getErrors()); | 696 | $PAGE->assign('plugin_errors', $pluginManager->getErrors()); |
@@ -1109,13 +1086,13 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1109 | 1086 | ||
1110 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) | 1087 | if (!empty($_POST['setpassword']) && !empty($_POST['oldpassword'])) |
1111 | { | 1088 | { |
1112 | if (!tokenOk($_POST['token'])) die(t('Wrong token.')); // Go away! | 1089 | if (!$sessionManager->checkToken($_POST['token'])) die(t('Wrong token.')); // Go away! |
1113 | 1090 | ||
1114 | // Make sure old password is correct. | 1091 | // Make sure old password is correct. |
1115 | $oldhash = sha1($_POST['oldpassword'].$conf->get('credentials.login').$conf->get('credentials.salt')); | 1092 | $oldhash = sha1($_POST['oldpassword'].$conf->get('credentials.login').$conf->get('credentials.salt')); |
1116 | if ($oldhash!= $conf->get('credentials.hash')) { | 1093 | if ($oldhash!= $conf->get('credentials.hash')) { |
1117 | echo '<script>alert("'. t('The old password is not correct.') .'");document.location=\'?do=changepasswd\';</script>'; | 1094 | echo '<script>alert("'. t('The old password is not correct.') .'");document.location=\'?do=changepasswd\';</script>'; |
1118 | exit; | 1095 | exit; |
1119 | } | 1096 | } |
1120 | // Save new password | 1097 | // Save new password |
1121 | // Salt renders rainbow-tables attacks useless. | 1098 | // Salt renders rainbow-tables attacks useless. |
@@ -1149,7 +1126,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1149 | { | 1126 | { |
1150 | if (!empty($_POST['title']) ) | 1127 | if (!empty($_POST['title']) ) |
1151 | { | 1128 | { |
1152 | if (!tokenOk($_POST['token'])) { | 1129 | if (!$sessionManager->checkToken($_POST['token'])) { |
1153 | die(t('Wrong token.')); // Go away! | 1130 | die(t('Wrong token.')); // Go away! |
1154 | } | 1131 | } |
1155 | $tz = 'UTC'; | 1132 | $tz = 'UTC'; |
@@ -1225,7 +1202,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1225 | exit; | 1202 | exit; |
1226 | } | 1203 | } |
1227 | 1204 | ||
1228 | if (!tokenOk($_POST['token'])) { | 1205 | if (!$sessionManager->checkToken($_POST['token'])) { |
1229 | die(t('Wrong token.')); | 1206 | die(t('Wrong token.')); |
1230 | } | 1207 | } |
1231 | 1208 | ||
@@ -1255,7 +1232,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1255 | if (isset($_POST['save_edit'])) | 1232 | if (isset($_POST['save_edit'])) |
1256 | { | 1233 | { |
1257 | // Go away! | 1234 | // Go away! |
1258 | if (! tokenOk($_POST['token'])) { | 1235 | if (! $sessionManager->checkToken($_POST['token'])) { |
1259 | die(t('Wrong token.')); | 1236 | die(t('Wrong token.')); |
1260 | } | 1237 | } |
1261 | 1238 | ||
@@ -1355,7 +1332,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1355 | // -------- User clicked the "Delete" button when editing a link: Delete link from database. | 1332 | // -------- User clicked the "Delete" button when editing a link: Delete link from database. |
1356 | if ($targetPage == Router::$PAGE_DELETELINK) | 1333 | if ($targetPage == Router::$PAGE_DELETELINK) |
1357 | { | 1334 | { |
1358 | if (! tokenOk($_GET['token'])) { | 1335 | if (! $sessionManager->checkToken($_GET['token'])) { |
1359 | die(t('Wrong token.')); | 1336 | die(t('Wrong token.')); |
1360 | } | 1337 | } |
1361 | 1338 | ||
@@ -1572,7 +1549,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1572 | echo '<script>alert("'. $msg .'");document.location=\'?do='.Router::$PAGE_IMPORT .'\';</script>'; | 1549 | echo '<script>alert("'. $msg .'");document.location=\'?do='.Router::$PAGE_IMPORT .'\';</script>'; |
1573 | exit; | 1550 | exit; |
1574 | } | 1551 | } |
1575 | if (! tokenOk($_POST['token'])) { | 1552 | if (! $sessionManager->checkToken($_POST['token'])) { |
1576 | die('Wrong token.'); | 1553 | die('Wrong token.'); |
1577 | } | 1554 | } |
1578 | $status = NetscapeBookmarkUtils::import( | 1555 | $status = NetscapeBookmarkUtils::import( |
@@ -1639,7 +1616,7 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history) | |||
1639 | // Get a fresh token | 1616 | // Get a fresh token |
1640 | if ($targetPage == Router::$GET_TOKEN) { | 1617 | if ($targetPage == Router::$GET_TOKEN) { |
1641 | header('Content-Type:text/plain'); | 1618 | header('Content-Type:text/plain'); |
1642 | echo getToken($conf); | 1619 | echo $sessionManager->generateToken($conf); |
1643 | exit; | 1620 | exit; |
1644 | } | 1621 | } |
1645 | 1622 | ||
@@ -1965,10 +1942,10 @@ function lazyThumbnail($conf, $url,$href=false) | |||
1965 | * Installation | 1942 | * Installation |
1966 | * This function should NEVER be called if the file data/config.php exists. | 1943 | * This function should NEVER be called if the file data/config.php exists. |
1967 | * | 1944 | * |
1968 | * @param ConfigManager $conf Configuration Manager instance. | 1945 | * @param ConfigManager $conf Configuration Manager instance. |
1946 | * @param SessionManager $sessionManager SessionManager instance | ||
1969 | */ | 1947 | */ |
1970 | function install($conf) | 1948 | function install($conf, $sessionManager) { |
1971 | { | ||
1972 | // On free.fr host, make sure the /sessions directory exists, otherwise login will not work. | 1949 | // On free.fr host, make sure the /sessions directory exists, otherwise login will not work. |
1973 | if (endsWith($_SERVER['HTTP_HOST'],'.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'].'/sessions')) mkdir($_SERVER['DOCUMENT_ROOT'].'/sessions',0705); | 1950 | if (endsWith($_SERVER['HTTP_HOST'],'.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'].'/sessions')) mkdir($_SERVER['DOCUMENT_ROOT'].'/sessions',0705); |
1974 | 1951 | ||
@@ -2051,7 +2028,7 @@ function install($conf) | |||
2051 | exit; | 2028 | exit; |
2052 | } | 2029 | } |
2053 | 2030 | ||
2054 | $PAGE = new PageBuilder($conf); | 2031 | $PAGE = new PageBuilder($conf, null, $sessionManager->generateToken()); |
2055 | list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); | 2032 | list($continents, $cities) = generateTimeZoneData(timezone_identifiers_list(), date_default_timezone_get()); |
2056 | $PAGE->assign('continents', $continents); | 2033 | $PAGE->assign('continents', $continents); |
2057 | $PAGE->assign('cities', $cities); | 2034 | $PAGE->assign('cities', $cities); |
@@ -2328,7 +2305,7 @@ $response = $app->run(true); | |||
2328 | if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) { | 2305 | if ($response->getStatusCode() == 404 && strpos($_SERVER['REQUEST_URI'], '/api/v1') === false) { |
2329 | // We use UTF-8 for proper international characters handling. | 2306 | // We use UTF-8 for proper international characters handling. |
2330 | header('Content-Type: text/html; charset=utf-8'); | 2307 | header('Content-Type: text/html; charset=utf-8'); |
2331 | renderPage($conf, $pluginManager, $linkDb, $history); | 2308 | renderPage($conf, $pluginManager, $linkDb, $history, $sessionManager); |
2332 | } else { | 2309 | } else { |
2333 | $app->respond($response); | 2310 | $app->respond($response); |
2334 | } | 2311 | } |
diff --git a/tests/SessionManagerTest.php b/tests/SessionManagerTest.php new file mode 100644 index 00000000..3a270303 --- /dev/null +++ b/tests/SessionManagerTest.php | |||
@@ -0,0 +1,72 @@ | |||
1 | <?php | ||
2 | namespace Shaarli; | ||
3 | |||
4 | use \PHPUnit\Framework\TestCase; | ||
5 | |||
6 | /** | ||
7 | * Fake ConfigManager | ||
8 | */ | ||
9 | class FakeConfigManager | ||
10 | { | ||
11 | public static function get($key) | ||
12 | { | ||
13 | return $key; | ||
14 | } | ||
15 | } | ||
16 | |||
17 | |||
18 | /** | ||
19 | * Test coverage for SessionManager | ||
20 | */ | ||
21 | class SessionManagerTest extends TestCase | ||
22 | { | ||
23 | /** | ||
24 | * Generate a session token | ||
25 | */ | ||
26 | public function testGenerateToken() | ||
27 | { | ||
28 | $session = []; | ||
29 | $conf = new FakeConfigManager(); | ||
30 | $sessionManager = new SessionManager($session, $conf); | ||
31 | |||
32 | $token = $sessionManager->generateToken(); | ||
33 | |||
34 | $this->assertEquals(1, $session['tokens'][$token]); | ||
35 | $this->assertEquals(40, strlen($token)); | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Generate and check a session token | ||
40 | */ | ||
41 | public function testGenerateAndCheckToken() | ||
42 | { | ||
43 | $session = []; | ||
44 | $conf = new FakeConfigManager(); | ||
45 | $sessionManager = new SessionManager($session, $conf); | ||
46 | |||
47 | $token = $sessionManager->generateToken(); | ||
48 | |||
49 | // ensure a token has been generated | ||
50 | $this->assertEquals(1, $session['tokens'][$token]); | ||
51 | $this->assertEquals(40, strlen($token)); | ||
52 | |||
53 | // check and destroy the token | ||
54 | $this->assertTrue($sessionManager->checkToken($token)); | ||
55 | $this->assertFalse(isset($session['tokens'][$token])); | ||
56 | |||
57 | // ensure the token has been destroyed | ||
58 | $this->assertFalse($sessionManager->checkToken($token)); | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Check an invalid session token | ||
63 | */ | ||
64 | public function testCheckInvalidToken() | ||
65 | { | ||
66 | $session = []; | ||
67 | $conf = new FakeConfigManager(); | ||
68 | $sessionManager = new SessionManager($session, $conf); | ||
69 | |||
70 | $this->assertFalse($sessionManager->checkToken('4dccc3a45ad9d03e5542b90c37d8db6d10f2b38b')); | ||
71 | } | ||
72 | } | ||