Skip to content

Commit

Permalink
POST to login plugin in login form (matomo-org#14081)
Browse files Browse the repository at this point in the history
* Instead of using referrer URL, use redirect post param so we can post to Login module.

* Use actual login plugin name.

* Remove sanitization for form_redirect POST value.

* Couple more checks for a safer redirect.

* Do not include port in host check.

* Make sure hosts are not empty for more security.
  • Loading branch information
diosmosis authored Feb 11, 2019
1 parent 8e9942f commit 92fa86c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
19 changes: 12 additions & 7 deletions plugins/Login/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Piwik\QuickForm2;
use Piwik\Session;
use Piwik\Url;
use Piwik\UrlHelper;
use Piwik\View;

/**
Expand Down Expand Up @@ -126,7 +127,6 @@ function index()
function login($messageNoAccess = null, $infoMessage = false)
{
$form = new FormLogin();
$form->removeAttribute('action'); // remove action attribute, otherwise hash part will be lost
if ($form->validate()) {
$nonce = $form->getSubmitValue('form_nonce');
if (Nonce::verifyNonce('Login.login', $nonce)) {
Expand Down Expand Up @@ -303,14 +303,19 @@ protected function authenticateAndRedirect($login, $password, $urlToRedirect = f
$this->passwordResetter->removePasswordResetInfo($login);

if (empty($urlToRedirect)) {
$referrer = Url::getReferrer();
$module = Common::getRequestVar('module', '', 'string');
$redirect = Common::unsanitizeInputValue(Common::getRequestVar('form_redirect', false));
$redirectParams = UrlHelper::getArrayFromQueryString(UrlHelper::getQueryFromUrl($redirect));
$module = Common::getRequestVar('module', '', 'string', $redirectParams);
// when module is login, we redirect to home...
if ($module !== 'Login' && $module !== Piwik::getLoginPluginName() && $referrer) {
$host = Url::getHostFromUrl($referrer);
if (!empty($module) && $module !== 'Login' && $module !== Piwik::getLoginPluginName() && $redirect) {
$host = Url::getHostFromUrl($redirect);
$currentHost = Url::getHost();
$currentHost = explode(':', $currentHost, 2)[0];

// we only redirect to a trusted host
if ($host && Url::isValidHost($host)) {
$urlToRedirect = $referrer;
if (!empty($host) && !empty($currentHost) && $host == $currentHost && Url::isValidHost($host)
) {
$urlToRedirect = $redirect;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions plugins/Login/FormLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FormLogin extends QuickForm2
{
function __construct($id = 'login_form', $method = 'post', $attributes = null, $trackSubmit = false)
{
$attributes = array_merge($attributes ?: [], [ 'action' => '?module=' . Piwik::getLoginPluginName() ]);
parent::__construct($id, $method, $attributes, $trackSubmit);
}

Expand All @@ -30,6 +31,8 @@ function init()
$this->addElement('password', 'form_password')
->addRule('required', Piwik::translate('General_Required', Piwik::translate('General_Password')));

$this->addElement('hidden', 'form_redirect');

$this->addElement('hidden', 'form_nonce');

$this->addElement('checkbox', 'form_rememberme');
Expand Down
3 changes: 3 additions & 0 deletions plugins/Login/javascripts/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
});
};

// set login form redirect url
$('#login_form_redirect').val(window.location.href);

// 'lost your password?' on click
$('#login_form_nav').click(function (e) {
e.preventDefault();
Expand Down
1 change: 1 addition & 0 deletions plugins/Login/templates/login.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<div class="row">
<div class="col s12 input-field">
<input type="hidden" name="form_nonce" id="login_form_nonce" value="{{ nonce }}"/>
<input type="hidden" name="form_redirect" id="login_form_redirect" value=""/>
<input type="password" placeholder="" name="form_password" id="login_form_password" class="input" value="" size="20"
autocorrect="off" autocapitalize="none"
tabindex="20" />
Expand Down

0 comments on commit 92fa86c

Please sign in to comment.