From e162408139ac9bb12e69f4d49de45ade49369c21 Mon Sep 17 00:00:00 2001 From: Yassine Guedidi Date: Wed, 19 Mar 2025 00:28:34 +0100 Subject: [PATCH] Protect switch_view_mode with a CSRF token --- app/Resources/static/themes/material/index.js | 4 ++-- .../Controller/ConfigController.php | 6 ++++- .../views/Entry/Card/_mass_checkbox.html.twig | 2 +- .../Resources/views/Entry/entries.html.twig | 23 +++++++++++-------- .../Controller/ConfigControllerTest.php | 13 ++++------- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/Resources/static/themes/material/index.js b/app/Resources/static/themes/material/index.js index 704a9ea11..24adf8aab 100755 --- a/app/Resources/static/themes/material/index.js +++ b/app/Resources/static/themes/material/index.js @@ -228,10 +228,10 @@ $(document).ready(() => { }); }); } - $('form[name="form_mass_action"] input[name="tags"]').on('keydown', (e) => { + $('input[name="tags"][form="form_mass_action"]').on('keydown', (e) => { if (e.key === 'Enter') { e.preventDefault(); - $('form[name="form_mass_action"] button[name="tag"]').trigger('click'); + $('button[name="tag"][form="form_mass_action"]').trigger('click'); } }); }); diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 81bea532a..5aae8122b 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -646,12 +646,16 @@ class ConfigController extends AbstractController /** * Switch view mode for current user. * - * @Route("/config/view-mode", name="switch_view_mode") + * @Route("/config/view-mode", name="switch_view_mode", methods={"POST"}) * * @return RedirectResponse */ public function changeViewModeAction(Request $request) { + if (!$this->isCsrfTokenValid('switch-view-mode', $request->request->get('token'))) { + throw new BadRequestHttpException('Bad CSRF token.'); + } + $user = $this->getUser(); $user->getConfig()->setListMode(!$user->getConfig()->getListMode()); diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/Card/_mass_checkbox.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/Card/_mass_checkbox.html.twig index 5e4fe8f6d..b4bd1e94b 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Entry/Card/_mass_checkbox.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/Card/_mass_checkbox.html.twig @@ -1,3 +1,3 @@ diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/entries.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/entries.html.twig index 2c26b24af..93d5a82d1 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Entry/entries.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/entries.html.twig @@ -26,12 +26,18 @@ {% if current_route == 'homepage' %} {% set current_route = 'unread' %} {% endif %} -
+
{{ 'entry.list.number_on_the_page'|trans({'%count%': entries.count}) }} {% if entries.count > 0 %} - {% if list_mode == 0 %}view_list{% else %}view_module{% endif %} +
+ + + +
{% endif %} {% if entries.count > 0 %} @@ -50,15 +56,15 @@
- - - - + + + +
- - + +
@@ -77,7 +83,6 @@ {% endfor %} {% endif %} - {% if entries.getNbPages > 1 %}
diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php index 0a5a7da46..a9e51f3a8 100644 --- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php @@ -1116,18 +1116,17 @@ class ConfigControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getTestClient(); - $client->request('GET', '/unread/list'); + $crawler = $client->request('GET', '/unread/list'); $this->assertStringContainsString('row data', $client->getResponse()->getContent()); - $client->request('GET', '/config/view-mode'); - $crawler = $client->followRedirect(); + $form = $crawler->filter('.nb-results')->selectButton('view_list')->form(); - $client->request('GET', '/unread/list'); + $client->submit($form); + + $client->followRedirect(); $this->assertStringContainsString('collection', $client->getResponse()->getContent()); - - $client->request('GET', '/config/view-mode'); } public function testChangeLocaleWithoutReferer() @@ -1378,7 +1377,5 @@ class ConfigControllerTest extends WallabagCoreTestCase $client->request('GET', '/unread/list'); $this->assertStringNotContainsString('class="preview"', $client->getResponse()->getContent()); - - $client->request('GET', '/config/view-mode'); } }