Skip to content

Commit afaab04

Browse files
authored
Merge commit from fork
implement csrf-token check for all registration functions
2 parents 7f9f2ec + a248b89 commit afaab04

File tree

7 files changed

+32
-7
lines changed

7 files changed

+32
-7
lines changed

modules/profile/profile_new.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
foreach ($getUserUuids as $userUuid) {
5353
// read user data
5454
if (!$gValidLogin || $getAcceptRegistration) {
55+
if ($getAcceptRegistration) {
56+
// check the CSRF token of the form against the session token
57+
SecurityUtils::validateCsrfToken($_POST['adm_csrf_token']);
58+
}
59+
5560
// create a user registration object and set requested organization
5661
$user = new UserRegistration($gDb, $gProfileFields);
5762
$user->readDataByUuid($userUuid);

modules/registration.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@
8888
$page->createContentAssignUser($registrationUser, true);
8989
$page->show();
9090
} elseif (in_array($getMode, array('assign_member', 'assign_user'))) {
91+
// check the CSRF token of the form against the session token
92+
SecurityUtils::validateCsrfToken($_POST['adm_csrf_token']);
93+
9194
$registrationService = new RegistrationService($gDb, $getUserUUID);
9295
$message = $registrationService->assignRegistration($getUserUUIDAssigned, $getMode === 'assign_member');
9396

@@ -104,6 +107,10 @@
104107
exit();
105108
} elseif ($getMode === 'create_user') {
106109
// accept a registration, assign necessary roles and send a notification email
110+
111+
// check the CSRF token of the form against the session token
112+
SecurityUtils::validateCsrfToken($_POST['adm_csrf_token']);
113+
107114
$registrationUser->acceptRegistration();
108115

109116
// if current user has the right to assign roles then show roles dialog

src/Session/Entity/Session.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ protected function clearUserData()
150150

151151
/**
152152
* Returns a CSRF token from the session. If no CSRF token exists a new one will be
153-
* generated and stored within the session. The next call of the method will than
153+
* generated and stored within the session. The next call of the method will then
154154
* return the existing token. The CSRF token has 30 characters. A new token could
155155
* be forced by the parameter **$newToken**
156156
* @param bool $newToken If set to true, always a new token will be generated.
157157
* @return string Returns the CSRF token
158-
* @throws Exception
158+
* @throws \Exception
159159
*/
160160
public function getCsrfToken(bool $newToken = false): string
161161
{

src/UI/Presenter/RegistrationPresenter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@ public function createRegistrationList(): void
8989
'name' => $gL10n->get('SYS_ASSIGN_REGISTRATION')
9090
);
9191
} else {
92+
if ($gCurrentUser->isAdministratorUsers()) {
93+
$url = SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES.'/profile/profile_new.php', array('accept_registration' => true, 'user_uuid' => $row['usr_uuid']));
94+
} else {
95+
$url = SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES.'/registration.php', array('mode' => 'create_user', 'user_uuid' => $row['usr_uuid']));
96+
}
9297
$templateRow['buttons'][] = array(
93-
'url' => ($gCurrentUser->isAdministratorUsers() ? SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES.'/profile/profile_new.php', array('accept_registration' => true, 'user_uuid' => $row['usr_uuid'])) : SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES.'/registration.php', array('mode' => 'create_user', 'user_uuid' => $row['usr_uuid']))),
98+
'csrfToken' => $gCurrentSession->getCsrfToken(),
99+
'url' => $url,
94100
'name' => $gL10n->get('SYS_CONFIRM_REGISTRATION')
95101
);
96102
}

system/classes/ModuleContacts.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(string $id, string $headline = '')
4545
*/
4646
public function createContentAssignUser(User $user, bool $assignRegistration = false)
4747
{
48-
global $gL10n, $gSettingsManager, $gCurrentUser, $gDb, $gProfileFields, $gCurrentOrganization;
48+
global $gL10n, $gSettingsManager, $gCurrentUser, $gDb, $gProfileFields, $gCurrentOrganization, $gCurrentSession;
4949

5050
$templateData = array();
5151
$userUuid = $user->getValue('usr_uuid');
@@ -127,11 +127,13 @@ public function createContentAssignUser(User $user, bool $assignRegistration = f
127127
$button['icon'] = 'bi-person-check-fill';
128128
$button['url'] = SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES . '/registration.php', array('user_uuid' => $userUuid, 'user_uuid_assigned' => $similarUser->getValue('usr_uuid'), 'mode' => 'assign_member'));
129129
}
130+
$button['csrfToken'] = $gCurrentSession->getCsrfToken();
130131
}
131132
} else {
132133
// found user is NOT a member of this organization yet
133134
$button['label'] = $gL10n->get('SYS_ASSIGN_MEMBERSHIP');
134135
$button['icon'] = 'bi-person-check-fill';
136+
$button['csrfToken'] = $gCurrentSession->getCsrfToken();
135137

136138
if($assignRegistration) {
137139
$button['url'] = SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES . '/registration.php', array('user_uuid' => $userUuid, 'user_uuid_assigned' => $similarUser->getValue('usr_uuid'), 'mode' => 'assign_user'));
@@ -156,6 +158,7 @@ public function createContentAssignUser(User $user, bool $assignRegistration = f
156158
$templateData[] = $templateRow;
157159
}
158160

161+
$this->smarty->assign('csrfToken', $gCurrentSession->getCsrfToken());
159162
$this->smarty->assign('similarUsers', $templateData);
160163
$this->smarty->assign('l10n', $gL10n);
161164
$this->pageContent .= $this->smarty->fetch('modules/contacts.assign.tpl');

themes/simple/templates/modules/contacts.assign.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{if {array_key_exists array=$similarUser key='button'}}
2020
<br />
2121
<p>{$similarUser.button.description}</p>
22-
<button class="btn btn-primary" onclick="window.location.href='{$similarUser.button.url}'">
22+
<button class="btn btn-primary" onclick="redirectPost('{$similarUser.button.url}',{ adm_csrf_token: '{$similarUser.button.csrfToken}' });">
2323
<i class="bi {$similarUser.button.icon}"></i>{$similarUser.button.label}</button>
2424
{/if}
2525
</li>
@@ -32,7 +32,7 @@
3232
<div class="card-body">
3333
<p>{$l10n->get('SYS_CONTACT_NOT_FOUND_CREATE_NEW')}</p>
3434

35-
<button class="btn btn-primary" onclick="window.location.href='{$createNewUserUrl}'">
35+
<button class="btn btn-primary" onclick="redirectPost('{$createNewUserUrl}', { adm_csrf_token: '{$csrfToken}' });">
3636
<i class="bi bi-plus-circle-fill"></i>{$l10n->get('SYS_CREATE_CONTACT')}</button>
3737
</div>
3838
</div>

themes/simple/templates/sys-template-parts/card.information.button.tpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
</ul>
2323
{if {array_key_exists array=$card key="buttons"} && count($card.buttons) > 0}
2424
{foreach $card.buttons as $buttonItem}
25-
<a class="btn btn-primary mt-auto" href="{$buttonItem.url}">{$buttonItem.name}</a>
25+
{if isset($buttonItem.csrfToken)}
26+
<a class="btn btn-primary mt-auto" onclick="redirectPost('{$buttonItem.url}', { adm_csrf_token: '{$buttonItem.csrfToken}' });">{$buttonItem.name}</a>
27+
{else}
28+
<a class="btn btn-primary mt-auto" href="{$buttonItem.url}">{$buttonItem.name}</a>
29+
{/if}
2630
{/foreach}
2731
{/if}
2832
</div>

0 commit comments

Comments
 (0)