Skip to content

Commit 36a4548

Browse files
committed
Validate anti-CSRF token in password reset form
1 parent 7259639 commit 36a4548

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/controllers/User/ResetPassword.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use \CarlBennett\MVC\Libraries\Template;
99
use \CarlBennett\MVC\Libraries\View;
1010

11+
use \BNETDocs\Libraries\CSRF;
1112
use \BNETDocs\Libraries\EventTypes;
1213
use \BNETDocs\Libraries\Exceptions\UserNotFoundException;
1314
use \BNETDocs\Libraries\Logger;
@@ -37,18 +38,16 @@ public function &run( Router &$router, View &$view, array &$args ) {
3738
$model = new UserResetPasswordModel();
3839

3940
$model->error = null;
40-
$model->csrf_id = isset( $data[ 'csrf_id' ]) ? $data[ 'csrf_id' ] : null;
41-
$model->csrf_token = (
42-
isset( $data[ 'csrf_token' ]) ? $data[ 'csrf_token' ] : null
43-
);
41+
$model->csrf_id = mt_rand();
42+
$model->csrf_token = CSRF::generate( $model->csrf_id );
4443
$model->pw1 = isset( $data[ 'pw1' ]) ? $data[ 'pw1' ] : null;
4544
$model->pw2 = isset( $data[ 'pw2' ]) ? $data[ 'pw2' ] : null;
4645
$model->token = isset( $data[ 't' ]) ? $data[ 't' ] : null;
4746
$model->user = null;
4847
$model->username = isset( $data[ 'username' ]) ? $data[ 'username' ] : null;
4948

5049
if ( $router->getRequestMethod() == 'POST' ) {
51-
$ret = $this->doPasswordReset( $model );
50+
$ret = $this->doPasswordReset( $model, $data );
5251
if ( $ret !== self::RET_EMAIL ) {
5352
Logger::logEvent(
5453
EventTypes::USER_PASSWORD_RESET,
@@ -73,9 +72,21 @@ public function &run( Router &$router, View &$view, array &$args ) {
7372

7473
}
7574

76-
protected function doPasswordReset( UserResetPasswordModel &$model ) {
75+
protected function doPasswordReset( UserResetPasswordModel &$model, &$data ) {
7776
$model->error = 'INTERNAL_ERROR';
7877

78+
$csrf_id = isset( $data[ 'csrf_id' ]) ? $data[ 'csrf_id' ] : null;
79+
$csrf_token = (
80+
isset( $data[ 'csrf_token' ]) ? $data[ 'csrf_token' ] : null
81+
);
82+
$csrf_valid = CSRF::validate( $csrf_id, $csrf_token );
83+
84+
if ( !$csrf_valid ) {
85+
$model->error = 'INVALID_CSRF';
86+
return self::RET_FAILURE;
87+
}
88+
CSRF::invalidate( $csrf_id );
89+
7990
if ( empty( $model->username )) {
8091
$model->error = 'EMPTY_USERNAME';
8192
return self::RET_FAILURE;

0 commit comments

Comments
 (0)