Skip to content

Commit 5ea5115

Browse files
committed
Protect mass_action with a CSRF token
1 parent 27f0d94 commit 5ea5115

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Wallabag/CoreBundle/Controller/EntryController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ public function __construct(EntityManagerInterface $entityManager, EventDispatch
5353
}
5454

5555
/**
56-
* @Route("/mass", name="mass_action")
56+
* @Route("/mass", name="mass_action", methods={"POST"})
5757
*
5858
* @return Response
5959
*/
6060
public function massAction(Request $request, TagRepository $tagRepository)
6161
{
62+
if (!$this->isCsrfTokenValid('mass-action', $request->request->get('token'))) {
63+
throw new BadRequestHttpException('Bad CSRF token.');
64+
}
65+
6266
$values = $request->request->all();
6367

6468
$tagsToAdd = [];

src/Wallabag/CoreBundle/Resources/views/Entry/entries.html.twig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
{% if current_route == 'homepage' %}
2727
{% set current_route = 'unread' %}
2828
{% endif %}
29-
<form id="form_mass_action" name="form_mass_action" action="{{ path('mass_action', {redirect: current_path}) }}" method="post"></form>
29+
<form id="form_mass_action" name="form_mass_action" action="{{ path('mass_action', {redirect: current_path}) }}" method="post">
30+
<input type="hidden" name="token" value="{{ csrf_token('mass-action') }}"/>
31+
</form>
3032
<div class="results">
3133
<div class="nb-results">
3234
{{ 'entry.list.number_on_the_page'|trans({'%count%': entries.count}) }}

tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,8 +1764,12 @@ public function testMass()
17641764
$entries[] = $entry1Id = $entry1->getId();
17651765
$entries[] = $entry2Id = $entry2->getId();
17661766

1767+
$crawler = $client->request('GET', '/all/list');
1768+
$token = $crawler->filter('#form_mass_action input[name=token]')->attr('value');
1769+
17671770
// Mass actions : archive
17681771
$client->request('POST', '/mass', [
1772+
'token' => $token,
17691773
'toggle-archive' => '',
17701774
'entry-checkbox' => $entries,
17711775
]);
@@ -1786,8 +1790,12 @@ public function testMass()
17861790

17871791
$this->assertSame(1, $res->isArchived());
17881792

1793+
$crawler = $client->request('GET', '/all/list');
1794+
$token = $crawler->filter('#form_mass_action input[name=token]')->attr('value');
1795+
17891796
// Mass actions : star
17901797
$client->request('POST', '/mass', [
1798+
'token' => $token,
17911799
'toggle-star' => '',
17921800
'entry-checkbox' => $entries,
17931801
]);
@@ -1808,8 +1816,12 @@ public function testMass()
18081816

18091817
$this->assertSame(1, $res->isStarred());
18101818

1819+
$crawler = $client->request('GET', '/all/list');
1820+
$token = $crawler->filter('#form_mass_action input[name=token]')->attr('value');
1821+
18111822
// Mass actions : tag
18121823
$client->request('POST', '/mass', [
1824+
'token' => $token,
18131825
'tag' => '',
18141826
'tags' => 'foo',
18151827
'entry-checkbox' => $entries,
@@ -1838,8 +1850,12 @@ public function testMass()
18381850

18391851
$this->assertNotContains('foo', $res->getTagsLabel());
18401852

1853+
$crawler = $client->request('GET', '/all/list');
1854+
$token = $crawler->filter('#form_mass_action input[name=token]')->attr('value');
1855+
18411856
// Mass actions : delete
18421857
$client->request('POST', '/mass', [
1858+
'token' => $token,
18431859
'delete' => '',
18441860
'entry-checkbox' => $entries,
18451861
]);

0 commit comments

Comments
 (0)