Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHRAS-3857 : Check CSRF token on account #4556

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Alchemy/Phrasea/Controller/Root/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ public function displayAccount()

$initiatedValidations = $this->getBasketRepository()->findby(['vote_initiator' => $user, ]);

$this->setSessionFormToken('userAccount');

return $this->render('account/account.html.twig', [
'user' => $user,
'evt_mngr' => $manager,
Expand Down Expand Up @@ -417,6 +419,10 @@ public function confirmDeleteAccount(Request $request)
*/
public function updateAccount(Request $request)
{
if (!$this->isCrsfValid($request, 'userAccount')) {
return new Response('invalid crsf token form', 403);
}

$registrations = $request->request->get('registrations', []);

if (false === is_array($registrations)) {
Expand Down
6 changes: 6 additions & 0 deletions lib/Alchemy/Phrasea/Controller/Root/DeveloperController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ public function authorizeGrantPassword(Request $request, ApiApplication $applica
*/
public function newApp(Request $request)
{
if (!$this->isCrsfValid($request, 'newApplication')) {
return new Response('invalid crsf token form', 403);
}

if ($request->request->get('type') === ApiApplication::DESKTOP_TYPE) {
$form = new \API_OAuth2_Form_DevAppDesktop($request);
} else {
Expand Down Expand Up @@ -223,6 +227,8 @@ public function listApps()
*/
public function displayFormApp(Request $request)
{
$this->setSessionFormToken('newApplication');

return $this->render('developers/application_form.html.twig', [
"violations" => null,
'form' => null,
Expand Down
1 change: 1 addition & 0 deletions templates/web/account/account.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@

</div>
</div>
<input type="hidden" name="userAccount_token" value="{{ app['session'].get('userAccount_token') }}">
</form>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions templates/web/developers/application_form.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,6 @@
</div>

</div>
<input type="hidden" name="newApplication_token" value="{{ app['session'].get('newApplication_token') }}">
</form>
{% endblock %}
4 changes: 3 additions & 1 deletion tests/Alchemy/Tests/Phrasea/Controller/Root/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ public function testUpdateAccount()
$app = $this->getApplication();
$client = $this->getClient();
$bases = $notifs = [];
$randomValue = $this->setSessionFormToken('userAccount');

foreach ($app->getDataboxes() as $databox) {
foreach ($databox->get_collections() as $collection) {
Expand Down Expand Up @@ -424,7 +425,8 @@ public function testUpdateAccount()
'form_retryFTP' => '',
'notifications' => $notifs,
'form_defaultdataFTP' => ['document', 'preview', 'caption'],
'mail_notifications' => '1'
'mail_notifications' => '1',
'userAccount_token' => $randomValue
]);

$response = $client->getResponse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,17 @@ public function testDisplayformApp()
*/
public function testPostNewAppInvalidArguments()
{
$randomValue = $this->setSessionFormToken('newApplication');

$crawler = self::$DI['client']->request('POST', '/developers/application/', [
'type' => ApiApplication::WEB_TYPE,
'name' => '',
'description' => 'okok',
'website' => 'my.website.com',
'callback' => 'my.callback.com',
'scheme-website' => 'http://',
'scheme-callback' => 'http://'
'scheme-callback' => 'http://',
'newApplication_token' => $randomValue
]);

$this->assertTrue(self::$DI['client']->getResponse()->isOk());
Expand All @@ -63,6 +66,7 @@ public function testPostNewApp()
{
$apps = self::$DI['app']['repo.api-applications']->findByCreator(self::$DI['user']);
$nbApp = count($apps);
$randomValue = $this->setSessionFormToken('newApplication');

self::$DI['client']->request('POST', '/developers/application/', [
'type' => ApiApplication::WEB_TYPE,
Expand All @@ -71,7 +75,8 @@ public function testPostNewApp()
'website' => 'my.website.com',
'callback' => 'my.callback.com',
'scheme-website' => 'http://',
'scheme-callback' => 'http://'
'scheme-callback' => 'http://',
'newApplication_token' => $randomValue
]);

$apps = self::$DI['app']['repo.api-applications']->findByCreator(self::$DI['user']);
Expand Down
Loading