Project

General

Profile

Feature #22685 » 14438.diff

Administrator Admin, 2010-08-23 20:17

View differences:

t3lib/class.t3lib_beuserauth.php (working copy)
* This class contains the configuration of the database fields used plus some functions for the authentication process of backend users.
*
* @author Kasper Skaarhoj <kasperYYYY@typo3.com>
* @author Oliver Klee <typo3-coding@oliverklee.de>
*
* @package TYPO3
* @subpackage t3lib
*/
......
'resizeTextareas_Flexible' => 1,
);
/**
* an instance of the form protection for BE forms
*
* @var unknown_type
*/
protected $formProtection = NULL;
/**
* The destructor. Frees as much memory used by this object as possible.
*/
public function __destruct() {
if ($this->formProtection !== null) {
$this->formProtection->__destruct();
$this->formProtection = NULL;
}
}
/**
* Sets the security level for the Backend login
......
return $isUserAllowedToLogin;
}
}
/**
* Returns the BE form protection instance.
*
* @return t3lib_formProtection_backend the BE form protection instance
*/
public function getFormProtection() {
if ($this->formProtection == NULL) {
$this->formProtection = t3lib_div::makeInstance(
't3lib_formProtection_backend'
);
}
return $this->formProtection;
}
/**
* Logs out the current user and clears the form protection tokens.
*/
public function logoff() {
$this->getFormProtection()->clean();
parent::logoff();
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_beuserauth.php']);
}
?>
t3lib/core_autoload.php (working copy)
't3lib_error_exceptionhandlerinterface' => PATH_t3lib . 'error/interface.t3lib_error_exceptionhandlerinterface.php',
't3lib_browselinkshook' => PATH_t3lib . 'interfaces/interface.t3lib_browselinkshook.php',
't3lib_extfilefunctions_processdatahook' => PATH_t3lib . 'interfaces/interface.t3lib_extfilefunctions_processdatahook.php',
't3lib_formprotection_abstract' => PATH_t3lib . 'formprotection/class.t3lib_formprotection_abstract.php',
't3lib_formprotection_backend' => PATH_t3lib . 'formprotection/class.t3lib_formprotection_backend.php',
't3lib_formprotection_installtool' => PATH_t3lib . 'formprotection/class.t3lib_formprotection_installtool.php',
't3lib_localrecordlistgettablehook' => PATH_t3lib . 'interfaces/interface.t3lib_localrecordlistgettablehook.php',
't3lib_pageselect_getpagehook' => PATH_t3lib . 'interfaces/interface.t3lib_pageselect_getpagehook.php',
't3lib_pageselect_getrecordoverlayhook' => PATH_t3lib . 'interfaces/interface.t3lib_pageselect_getrecordoverlayhook.php',
t3lib/formprotection/class.t3lib_formprotection_backend.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Class t3lib_formProtection_backend.
*
* This class provides protection against cross-site request forgery (XSRF/CSRF)
* for forms in the BE.
*
* How to use:
*
* For each form in the BE (or link that changes some data), create a token and
* insert is as a hidden form element. The name of the form element does not
* matter; you only need it to get the form token for verifying it.
*
* <pre>
* $formToken = $GLOBALS['BE_USER']->getFormProtection()->generateToken(
* 'BE user setup', 'edit'
* );
* $this->content .= '<input type="hidden" name="formToken" value="' .
* $formToken . '" />';
* </pre>
*
* The three parameters $formName, $action and $formInstanceName can be
* arbitrary strings, but they should make the form token as specific as
* possible. For different forms (e.g. BE user setup and editing a tt_content
* record) or different records (with different UIDs) from the same table,
* those values should be different.
*
* For editing a tt_content record, the call could look like this:
*
* <pre>
* $formToken = $GLOBALS['BE_USER']->getFormProtection()->generateToken(
* 'tt_content', 'edit', $uid
* );
* </pre>
*
* At the end of the form, you need to persist the tokens. This makes sure that
* generated tokens get saved, and also that removed tokens stay removed:
*
* <pre>
* $GLOBALS['BE_USER']->getFormProtection()->persistTokens();
* </pre>
*
* In BE lists, it might be necessary to generate hundreds of tokens. So the
* tokens do not get automatically persisted after creation for performance
* reasons.
*
*
* When processing the data that has been submitted by the form, you can check
* that the form token is valid like this:
*
* <pre>
* if ($dataHasBeenSubmitted && $BE_USER->getFormProtection()->validateToken(
* (string) t3lib_div::_POST('formToken'),
* 'BE user setup', 'edit
* )
* ) {
* // processes the data
* } else {
* // no need to do anything here as the BE form protection will create a
* // flash message for an invalid token
* }
* </pre>
*
* Note that validateToken invalidates the token with the token ID. So calling
* validate with the same parameters two times in a row will always return FALSE
* for the second call.
*
* It is important that the tokens get validated <em>before</em> the tokens are
* persisted. This makes sure that the tokens that get invalidated by
* validateToken cannot be used again.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_formProtection_backend extends t3lib_formProtection_abstract {
/**
* the maximum number of tokens that can exist at the same time
*
* @var integer
*/
protected $maximumNumberOfTokens = 20000;
/**
* Creates or displayes an error message telling the user that the submitted
* form token is invalid.
*/
protected function createValidationErrorMessage() {
$message = t3lib_div::makeInstance(
't3lib_FlashMessage',
$GLOBALS['LANG']->sL(
'LLL:EXT:lang/locallang_core.xml:error.formProtection.tokenInvalid'
),
'',
t3lib_FlashMessage::ERROR
);
t3lib_FlashMessageQueue::addMessage($message);
}
/**
* Retrieves all saved tokens.
*
* @return array<array>
* the saved tokens as, will be empty if no tokens have been saved
*/
protected function retrieveTokens() {
$tokens = $GLOBALS['BE_USER']->getSessionData('formTokens');
if (!is_array($tokens)) {
$tokens = array();
}
$this->tokens = $tokens;
}
/**
* Saves the tokens so that they can be used by a later incarnation of this
* class.
*/
public function persistTokens() {
$GLOBALS['BE_USER']->setAndSaveSessionData('formTokens', $this->tokens);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_backend.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_backend.php']);
}
?>
t3lib/formprotection/class.t3lib_formprotection_abstract.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Class t3lib_formProtection_abstract
*
* This class provides protection against cross-site request forgery (XSRF/CSRF)
* for forms.
*
* For documentation on how to use this class, please see the documentation of
* the corresponding subclasses, e.g. t3lib_formProtection_backend.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
abstract class t3lib_formProtection_abstract {
/**
* the maximum number of tokens that can exist at the same time
*
* @var integer
*/
protected $maximumNumberOfTokens = 0;
/**
* Valid tokens sorted from oldest to newest.
*
* [tokenId] => array(formName, formInstanceName)
*
* @var array
*/
protected $tokens = array();
/**
* Constructor. Makes sure existing tokens are read and available for
* checking.
*/
public function __construct() {
$this->retrieveTokens();
}
/**
* Frees as much memory as possible.
*/
public function __destruct() {
$this->tokens = array();
}
/**
* Deletes all existing tokens and persists the (empty) token table.
*
* This function is intended to be called when a user logs on or off.
*/
public function clean() {
$this->tokens = array();
$this->persistTokens();
}
/**
* Generates and stores a token for a form.
*
* Calling this function two times with the same parameters will create
* two valid, different tokens.
*
* Generating more tokens than $maximumNumberOfEntries will cause the oldest
* tokens to get dropped.
*
* Note: This function does not persist the tokens.
*
* @param string $formName
* the name of the form, for example a table name like "tt_content",
* or some other identifier like "install_tool_password", must not be
* empty
* @param string $action
* the name of the action of the form, for example "new", "delete" or
* "edit", may also be empty
* @param string $formInstanceName
* a string used to differentiate two instances of the same form,
* form example a record UID or a comma-separated list of UIDs,
* may also be empty
*
* @return string the 32-character hex ID of the generated token
*/
public function generateToken(
$formName, $action = '', $formInstanceName = ''
) {
if ($formName == '') {
throw new InvalidArgumentException('$formName must not be empty.');
}
do {
$tokenId = bin2hex(t3lib_div::generateRandomBytes(16));
} while (isset($this->tokens[$tokenId]));
$this->tokens[$tokenId] = array(
'formName' => $formName,
'action' => $action,
'formInstanceName' => $formInstanceName,
);
$this->preventOverflow();
return $tokenId;
}
/**
* Checks whether the token $tokenId is valid in the form $formName with
* $formInstanceName.
*
* A token is valid if $tokenId, $formName and $formInstanceName match and
* the token has not been used yet.
*
* Calling this function will mark the token $tokenId as invalud (if it
* exists).
*
* So calling this function with the same parameters two times will return
* FALSE the second time.
*
* @param string $tokenId
* a form token to check, may also be empty or utterly misformed
* @param string $formName
* the name of the form to check, for example "tt_content",
* may also be empty or utterly misformed
* @param string $action
* the action of the form to check, for example "edit",
* may also be empty or utterly misformed
* @param string $formInstanceName
* the instance name of the form to check, for example "42" or "foo"
* or "31,42", may also be empty or utterly misformed
*
* @return boolean
* TRUE if $tokenId, $formName, $action and $formInstanceName match
* and the token has not been used yet, FALSE otherwise
*/
public function validateToken(
$tokenId, $formName, $action = '', $formInstanceName = ''
) {
if (isset($this->tokens[$tokenId])) {
$token = $this->tokens[$tokenId];
$isValid = ($token['formName'] == $formName)
&& ($token['action'] == $action)
&& ($token['formInstanceName'] == $formInstanceName);
$this->dropToken($tokenId);
} else {
$isValid = FALSE;
}
if (!$isValid) {
$this->createValidationErrorMessage();
}
return $isValid;
}
/**
* Creates or displayes an error message telling the user that the submitted
* form token is invalid.
*
* This function may also be empty if the validation error should be handled
* silently.
*/
abstract protected function createValidationErrorMessage();
/**
* Retrieves all saved tokens.
*
* @return array<arrray>
* the saved tokens, will be empty if no tokens have been saved
*/
abstract protected function retrieveTokens();
/**
* Saves the tokens so that they can be used by a later incarnation of this
* class.
*/
abstract public function persistTokens();
/**
* Drops the token with the ID $tokenId.
*
* If there is no token with that ID, this function is a no-op.
*
* Note: This function does not persist the tokens.
*
* @param string $tokenId
* the 32-character ID of an existing token, must not be empty
*/
protected function dropToken($tokenId) {
if (isset($this->tokens[$tokenId])) {
unset($this->tokens[$tokenId]);
}
}
/**
* Checks whether the number of current tokens still is at most
* $this->maximumNumberOfTokens.
*
* If there are more tokens, the oldest tokens are removed until the number
* of tokens is low enough.
*
* Note: This function does not persist the tokens.
*/
protected function preventOverflow() {
if (empty($this->tokens)) {
return;
}
while (count($this->tokens) > $this->maximumNumberOfTokens) {
reset($this->tokens);
$this->dropToken(key($this->tokens));
}
}
}
?>
t3lib/formprotection/class.t3lib_formprotection_installtool.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Class t3lib_formProtection_installTool.
*
* This class provides protection against cross-site request forgery (XSRF/CSRF)
* in the install tool.
*
*
* How to use this in the install tool:
*
* For each form in the install tool (or link that changes some data), create a
* token and insert is as a hidden form element. The name of the form element
* does not matter; you only need it to get the form token for verifying it.
*
* <pre>
* $formToken = $this->formProtection->generateToken(
* 'installToolPassword', 'change'
* );
* // then puts the generated form token in a hidden field in the template
* </pre>
*
* The three parameters $formName, $action and $formInstanceName can be
* arbitrary strings, but they should make the form token as specific as
* possible. For different forms (e.g. the password change and editing a the
* configuration), those values should be different.
*
* At the end of the form, you need to persist the tokens. This makes sure that
* generated tokens get saved, and also that removed tokens stay removed:
*
* <pre>
* $this->formProtection()->persistTokens();
* </pre>
*
*
* When processing the data that has been submitted by the form, you can check
* that the form token is valid like this:
*
* <pre>
* if ($dataHasBeenSubmitted && $this->formProtection()->validateToken(
* (string) $_POST['formToken'],
* 'installToolPassword',
* 'change'
* ) {
* // processes the data
* } else {
* // no need to do anything here as the install tool form protection will
* // create an error message for an invalid token
* }
* </pre>
*
* Note that validateToken invalidates the token with the token ID. So calling
* validate with the same parameters two times in a row will always return FALSE
* for the second call.
*
* It is important that the tokens get validated <em>before</em> the tokens are
* persisted. This makes sure that the tokens that get invalidated by
* validateToken cannot be used again.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_formProtection_installTool extends t3lib_formProtection_abstract {
/**
* the maximum number of tokens that can exist at the same time
*
* @var integer
*/
protected $maximumNumberOfTokens = 100;
/**
* an instance of the install tool used for displaying messages
*
* @var tx_install
*/
protected $installTool = NULL;
/**
* Constructor. Makes sure existing tokens are read and available for
* checking.
*
* @param tx_install $installTool the current instance of the install tool
*/
public function __construct(tx_install $installTool) {
$this->injectInstallTool($installTool);
parent::__construct();
}
/**
* Frees as much memory as possible.
*/
public function __destruct() {
$this->installTool = NULL;
parent::__destruct();
}
/**
* Injects the current instance of the install tool.
*
* This instance will be used for displaying messages.
*
* @param tx_install $installTool the current instance of the install tool
*/
public function injectInstallTool(tx_install $installTool) {
$this->installTool = $installTool;
}
/**
* Creates or displayes an error message telling the user that the submitted
* form token is invalid.
*/
protected function createValidationErrorMessage() {
$this->installTool->addErrorMessage(
'Validating the security token of this form has failed. ' .
'Please reload the form and submit it again.'
);
}
/**
* Retrieves all saved tokens.
*
* @return array<array>
* the saved tokens, will be empty if no tokens have been saved
*/
protected function retrieveTokens() {
if (isset($_SESSION['installToolFormTokens'])
&& is_array($_SESSION['installToolFormTokens'])
) {
$this->tokens = $_SESSION['installToolFormTokens'];
} else {
$this->tokens = array();
}
}
/**
* Saves the tokens so that they can be used by a later incarnation of this
* class.
*/
public function persistTokens() {
$_SESSION['installToolFormTokens'] = $this->tokens;
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_installtool.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_installtool.php']);
}
?>
typo3/sysext/install/Resources/Private/Templates/AlterPasswordForm.html (working copy)
</li>
<li class="t3-install-hidden">
<input type="hidden" name="installToolPassword_md5" value="1" />
<input type="hidden" name="formToken" value="###FORMTOKEN###" />
</li>
</ol>
</fieldset>
typo3/sysext/install/mod/class.tx_install.php (working copy)
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Contains the class for the Install Tool
*
* $Id$
*
* @author Kasper Skaarhoj <kasperYYYY@typo3.com>
* @author Ingmar Schlecht <ingmar@typo3.org>
*/
/**
* [CLASS/FUNCTION INDEX of SCRIPT]
*
*
......
/**
* Install Tool module
*
* $Id$
*
* @author Kasper Skaarhoj <kasperYYYY@typo3.com>
* @author Ingmar Schlecht <ingmar@typo3.org>
* @package TYPO3
......
*/
protected $session = NULL;
/**
* the form protection instance used for creating and verifying form tokens
*
* @var t3lib_formProtection_installTool
*/
protected $formProtection = NULL;
var $menuitems = array(
'config' => 'Basic Configuration',
'database' => 'Database Analyser',
......
if($this->redirect_url) {
t3lib_utility_Http::redirect($this->redirect_url);
}
$this->formProtection = t3lib_div::makeInstance(
't3lib_formProtection_installTool', $this
);
} else {
$this->loginForm();
}
......
TRUE,
TRUE
);
// Send content to the page wrapper function
$this->output($this->outputWrapper($content));
}
......
if (is_file($enableInstallToolFile) && trim(file_get_contents($enableInstallToolFile)) !== 'KEEP_FILE') {
unlink(PATH_typo3conf . 'ENABLE_INSTALL_TOOL');
}
$this->formProtection->clean();
$this->session->destroySession();
t3lib_utility_Http::redirect($this->scriptSelf);
break;
......
break;
}
}
$this->formProtection->persistTokens();
}
/**
......
if (isset($GLOBALS['TYPO3_CONF_VARS'][$k][$vk])) {
$doit=1;
if ($k=='BE' && $vk=='installToolPassword') {
if (!$this->formProtection->validateToken(
(string) $_POST['formToken'],
'installToolPassword',
'change'
)) {
$doit = FALSE;
break;
}
if ($value) {
if (isset($_POST['installToolPassword_check']) && (!t3lib_div::_GP('installToolPassword_check') || strcmp(t3lib_div::_GP('installToolPassword_check'),$value))) {
$doit=0;
......
'action' => $this->scriptSelf.'?TYPO3_INSTALL[type]=extConfig',
'enterPassword' => 'Enter new password:',
'enterAgain' => 'Enter again:',
'submit' => 'Set new password'
'submit' => 'Set new password',
'formToken' => $this->formProtection->generateToken(
'installToolPassword', 'change'
),
);
// Fill the markers
$content = t3lib_parsehtml::substituteMarkerArray(
......
$bytes = t3lib_div::generateRandomBytes($keyLength);
return substr(bin2hex($bytes), -96);
}
/**
* Adds an error message that should be displayed.
*
* @param string $messageText
* the text of the message to display, must not be empty
*/
public function addErrorMessage($messageText) {
if ($messageText == '') {
throw new InvalidArgumentException('$messageText must not be empty.');
}
$this->errorMessages[] = $messageText;
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']) {
typo3/sysext/setup/mod/index.php (working copy)
/**
* If settings are submitted to _POST[DATA], store them
* NOTICE: This method is called before the template.php is included. See buttom of document
*
* @return void
* NOTICE: This method is called before the template.php is included. See
* bottom of document.
*/
function storeIncomingData() {
public function storeIncomingData() {
/* @var $BE_USER t3lib_beUserAuth */
global $BE_USER;
......
$storeRec = array();
$fieldList = $this->getFieldsFromShowItem();
if (is_array($d)) {
if (is_array($d) && $BE_USER->getFormProtection()->validateToken(
(string) t3lib_div::_POST('formToken'),
'BE user setup', 'edit'
)
) {
// UC hashed before applying changes
$save_before = md5(serialize($BE_USER->uc));
......
$this->content .= $this->doc->spacer(20) . $this->doc->getDynTabMenu($menuItems, 'user-setup', false, false, 100, 1, false, 1, $this->dividers2tabs);
$formToken = $BE_USER->getFormProtection()->generateToken(
'BE user setup', 'edit'
);
// Submit and reset buttons
$this->content .= $this->doc->spacer(20);
$this->content .= $this->doc->section('',
t3lib_BEfunc::cshItem('_MOD_user_setup', 'reset', $BACK_PATH) . '
<input type="hidden" name="simUser" value="'.$this->simUser.'" />
<input type="hidden" name="formToken" value="' . $formToken . '" />
<input type="submit" name="data[save]" value="'.$LANG->getLL('save').'" />
<input type="submit" name="data[setValuesToDefault]" value="'.$LANG->getLL('resetConfiguration').'" onclick="return confirm(\''.$LANG->getLL('setToStandardQuestion').'\');" />
<input type="submit" name="data[clearSessionVars]" value="' . $LANG->getLL('clearSessionVars') . '" onclick="return confirm(\'' . $LANG->getLL('clearSessionVarsQuestion') . '\');" />'
);
// Notice
$this->content .= $this->doc->spacer(30);
$flashMessage = t3lib_div::makeInstance(
......
$SOBE->init();
$SOBE->main();
$SOBE->printContent();
$GLOBALS['BE_USER']->getFormProtection()->persistTokens();
?>
typo3/sysext/lang/locallang_core.xml (working copy)
<label index="warning.memcache_not_usable">Memcache is configured, but connection to memcached failed with these configuration(s):</label>
<label index="warning.file_missing">File is missing!</label>
<label index="warning.file_missing_text">This content element references a file which seems to not exist:</label>
<label index="error.formProtection.tokenInvalid">Validating the security token of this form has failed. Please reload the form and submit it again.</label>
<label index="toolbarItems.shortcuts">Shortcuts</label>
<label index="toolbarItems.shortcut">Shortcut</label>
<label index="toolbarItems.shortcutsGroup">Shortcut Group</label>
tests/t3lib/t3lib_beuserauthTest.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee (typo3-coding@oliverklee.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Testcase for the t3lib_beUserAuth class in the TYPO3 Core extension.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_beUserAuthTest extends tx_phpunit_testcase {
/**
* @var t3lib_beUserAuth
*/
private $fixture;
public function setUp() {
$this->fixture = new t3lib_beUserAuth();
}
public function tearDown() {
$this->fixture->__destruct();
unset($this->fixture);
}
/////////////////////////////////////////
// Tests concerning the form protection
/////////////////////////////////////////
/**
* @test
*/
public function getFormProtectectionReturnsBackEndFormProtectionInstance() {
$this->assertTrue(
$this->fixture->getFormProtection()
instanceof t3lib_formProtection_backend
);
}
/**
* @test
*/
public function getFormProtectectionCalledTwoTimesReturnsSameInstance() {
$this->assertSame(
$this->fixture->getFormProtection(),
$this->fixture->getFormProtection()
);
}
/**
* @test
*/
public function logoffCleansFormProtection() {
$className = uniqid('t3lib_beUserAuth');
eval(
'class ' . $className . ' extends t3lib_beUserAuth {' .
'public function setFormProtection(t3lib_formProtection_backend $formProtection) {' .
'$this->formProtection = $formProtection;' .
'}' .
'}'
);
$formProtection = $this->getMock(
't3lib_formProtection_backend', array('clean')
);
$formProtection->expects($this->atLeastOnce())->method('clean');
$fixture = new $className();
$fixture->setFormProtection($formProtection);
$fixture->logoff();
$fixture->__destruct();
}
}
?>
tests/t3lib/formprotection/t3lib_formprotection_installToolTest.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee (typo3-coding@oliverklee.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
require_once(t3lib_extMgm::extPath('install') . 'mod/class.tx_install.php');
/**
* Testcase for the t3lib_formProtection_installTool class.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_formProtection_installToolTest extends tx_phpunit_testcase {
/**
* @var t3lib_formProtection_installTool
*/
private $fixture;
/**
* backup of $_SESSION
*
* @var array
*/
private $sessionBackup;
public function setUp() {
$this->sessionBackup = $_SESSION;
$className = $this->createAccessibleProxyClass();
$installTool = $this->getMock('tx_install', array(), array(), '', FALSE);
$this->fixture = new $className($installTool);
}
public function tearDown() {
$this->fixture->__destruct();
unset($this->fixture);
t3lib_FlashMessageQueue::getAllMessagesAndFlush();
$_SESSION = $this->sessionBackup;
}
//////////////////////
// Utility functions
//////////////////////
/**
* Creates a subclass t3lib_formProtection_installTool with retrieveTokens made
* public.
*
* @return string the name of the created class, will not be empty
*/
private function createAccessibleProxyClass() {
$className = 't3lib_formProtection_installToolAccessibleProxy';
if (!class_exists($className)) {
eval(
'class ' . $className . ' extends t3lib_formProtection_installTool {' .
' public function createValidationErrorMessage() {' .
' parent::createValidationErrorMessage();' .
' }' .
' public function retrieveTokens() {' .
' return parent::retrieveTokens();' .
' }' .
'}'
);
}
return $className;
}
////////////////////////////////////
// Tests for the utility functions
////////////////////////////////////
/**
* @test
*/
public function createAccessibleProxyCreatesInstallToolFormProtectionSubclass() {
$className = $this->createAccessibleProxyClass();
$installTool = $this->getMock('tx_install', array(), array(), '', FALSE);
$this->assertTrue(
(new $className($installTool)) instanceof t3lib_formProtection_installTool
);
}
//////////////////////////////////////////////////////////
// Tests concerning the reading and saving of the tokens
//////////////////////////////////////////////////////////
/**
* @test
*/
public function tokensFromSessionDataAreAvailableForValidateToken() {
$tokenId = '51a655b55c54d54e5454c5f521f6552a';
$formName = 'foo';
$action = 'edit';
$formInstanceName = '42';
$_SESSION['installToolFormTokens'] = array(
$tokenId => array(
'formName' => $formName,
'action' => $action,
'formInstanceName' => $formInstanceName,
),
);
$this->fixture->retrieveTokens();
$this->assertTrue(
$this->fixture->validateToken(
$tokenId, $formName, $action, $formInstanceName
)
);
}
/**
* @test
*/
public function persistTokensWritesTokensToSession() {
$formName = 'foo';
$action = 'edit';
$formInstanceName = '42';
$tokenId = $this->fixture->generateToken(
$formName, $action, $formInstanceName
);
$this->fixture->persistTokens();
$this->assertEquals(
array(
$tokenId => array(
'formName' => $formName,
'action' => $action,
'formInstanceName' => $formInstanceName,
),
),
$_SESSION['installToolFormTokens']
);
}
//////////////////////////////////////////////////
// Tests concerning createValidationErrorMessage
//////////////////////////////////////////////////
/**
* @test
*/
public function createValidationErrorMessageAddsErrorMessage() {
$installTool = $this->getMock(
'tx_install', array('addErrorMessage'), array(), '', FALSE
);
$installTool->expects($this->once())->method('addErrorMessage')
->with(
'Validating the security token of this form has failed. ' .
'Please reload the form and submit it again.'
);
$this->fixture->injectInstallTool($installTool);
$this->fixture->createValidationErrorMessage();
}
}
?>
tests/t3lib/formprotection/fixtures/class.t3lib_formprotection_testing.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee <typo3-coding@oliverklee.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Class t3lib_formProtection_testing.
*
* This is a testing subclass of the abstract t3lib_formProtection_abstract
* class.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_formProtection_testing extends t3lib_formProtection_abstract {
/**
* the maximum number of tokens that can exist at the same time
*
* @var integer
*/
protected $maximumNumberOfTokens = 100;
/**
* Sets the maximum number of tokens that can exist at the same time.
*
* @param integer $number maximum number of tokens, must be > 0
*/
public function setMaximumNumberOfTokens($number) {
$this->maximumNumberOfTokens = $number;
}
/**
* Creates or displayes an error message telling the user that the submitted
* form token is invalid.
*/
protected function createValidationErrorMessage() {}
/**
* Retrieves all saved tokens.
*
* @return array the saved tokens as a two-dimensional array, will be empty
* if no tokens have been saved
*/
protected function retrieveTokens() {}
/**
* Saves the tokens so that they can be used by a later incarnation of this
* class.
*/
public function persistTokens() {}
/**
* Drops the token with the ID $tokenId and persists all tokens.
*
* If there is no token with that ID, this function is a no-op.
*
* @param string $tokenId
* the 32-character ID of an existing token, must not be empty
*/
public function dropToken($tokenId) {
parent::dropToken($tokenId);
}
}
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_testing.php']) {
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/formprotection/class.t3lib_formprotection_testing.php']);
}
?>
tests/t3lib/formprotection/t3lib_formprotectionTest.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2010 Oliver Klee (typo3-coding@oliverklee.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
require_once('fixtures/class.t3lib_formprotection_testing.php');
/**
* Testcase for the t3lib_formProtection_abstract class.
*
* @package TYPO3
* @subpackage t3lib
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
class t3lib_formProtectionTest extends tx_phpunit_testcase {
/**
* @var t3lib_formProtection_testing
*/
private $fixture;
public function setUp() {
$this->fixture = new t3lib_formProtection_testing();
}
public function tearDown() {
$this->fixture->__destruct();
unset($this->fixture);
}
/////////////////////////////////////////
// Tests concerning the basic functions
/////////////////////////////////////////
/**
* @test
*/
public function constructionRetrievesTokens() {
$className = uniqid('t3lib_formProtection');
eval(
'class ' . $className . ' extends t3lib_formProtection_testing {' .
'public $tokensHaveBeenRetrieved = FALSE; ' .
'protected function retrieveTokens() {' .
'$this->tokensHaveBeenRetrieved = TRUE;' .
'}' .
'}'
);
$fixture = new $className();
$this->assertTrue(
$fixture->tokensHaveBeenRetrieved
);
}
/**
* @test
*/
public function cleanMakesTokenInvalid() {
$formName = 'foo';
$tokenId = $this->fixture->generateToken($formName);
$this->fixture->clean();
$this->assertFalse(
$this->fixture->validateToken($tokenId, $formName)
);
}
/**
* @test
*/
public function cleanPersistsTokens() {
$fixture = $this->getMock(
't3lib_formProtection_testing', array('persistTokens')
);
$fixture->expects($this->once())->method('persistTokens');
$fixture->clean();
}
///////////////////////////////////
// Tests concerning generateToken
///////////////////////////////////
/**
* @test
*/
public function generateTokenFormForEmptyFormNameThrowsException() {
$this->setExpectedException(
'InvalidArgumentException', '$formName must not be empty.'
);
$this->fixture->generateToken('', 'edit', 'bar');
}
/**
* @test
*/
public function generateTokenFormForEmptyActionNotThrowsException() {
$this->fixture->generateToken('foo', '', '42');
}
/**
* @test
*/
public function generateTokenFormForEmptyFormInstanceNameNotThrowsException() {
$this->fixture->generateToken('foo', 'edit', '');
}
/**
* @test
*/
public function generateTokenFormForOmittedActionAndFormInstanceNameNotThrowsException() {
$this->fixture->generateToken('foo');
}
/**
* @test
*/
public function generateTokenReturns32CharacterHexToken() {
$this->assertRegexp(
'/^[0-9a-f]{32}$/',
$this->fixture->generateToken('foo')
);
}
/**
* @test
*/
public function generateTokenCalledTwoTimesWithSameParametersReturnsDifferentTokens() {
$this->assertNotEquals(
$this->fixture->generateToken('foo', 'edit', 'bar'),
$this->fixture->generateToken('foo', 'edit', 'bar')
);
}
/**
* @test
*/
public function generatingTooManyTokensInvalidatesOldestToken() {
$this->fixture->setMaximumNumberOfTokens(2);
$formName = 'foo';
$token1 = $this->fixture->generateToken($formName);
$token2 = $this->fixture->generateToken($formName);
$token3 = $this->fixture->generateToken($formName);
$this->assertFalse(
$this->fixture->validateToken($token1, $formName)
);
}
/**
* @test
*/
public function generatingTooManyTokensNotInvalidatesNewestToken() {
$this->fixture->setMaximumNumberOfTokens(2);
$formName = 'foo';
$formInstanceName = 'bar';
... This diff was truncated because it exceeds the maximum size that can be displayed.
(4-4/6)