Bug 20568: CSRF protection
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 18 Apr 2018 17:38:02 +0000 (14:38 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 9 May 2018 15:55:59 +0000 (12:55 -0300)
Edit: fix warning introduced by this patch

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

koha-tmpl/intranet-tmpl/prog/en/modules/members/apikeys.tt
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-apikeys.tt
members/apikeys.pl
opac/opac-apikeys.pl

index 93ef624..73ea916 100644 (file)
@@ -25,6 +25,7 @@
                 <h1>API keys for [% INCLUDE 'patron-title.inc' %]</h1>
                 <form id="add-api-key" action="/cgi-bin/koha/members/apikeys.pl" method="post" style="display:none">
                     <input type="hidden" name="patron_id" value="[% patron.id %]" />
+                    <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                     <input type="hidden" name="op" value="generate" />
                     <fieldset class="brief">
                         <legend>Generate new client id/secret pair</legend>
                                             <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
                                                 <input type="hidden" name="patron_id" value="[% patron.id %]" />
                                                 <input type="hidden" name="key" value="[% key.id %]" />
+                                                <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                                                 <input type="hidden" name="op" value="delete" />
                                                 <button class="btn btn-default btn-xs delete" type="submit"><i class="fa fa-trash"></i> Delete</button>
                                             </form>
                                             <form action="/cgi-bin/koha/members/apikeys.pl" method="post">
                                                 <input type="hidden" name="patron_id" value="[% patron.id %]" />
                                                 <input type="hidden" name="key" value="[% key.id %]" />
+                                                <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                                                 [% IF key.active %]
                                                     <input type="hidden" name="op" value="revoke" />
                                                     <button class="btn btn-default btn-xs" type="submit"><i class="fa fa-remove"></i> Revoke</button>
index 7aaddf4..7d958b7 100644 (file)
@@ -35,6 +35,7 @@
                         <fieldset>
                             <legend>Generate new client id/secret pair</legend>
                             <input type="hidden" name="patron_id" value="[% patron.id %]" />
+                            <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                             <input type="hidden" name="op" value="generate" />
                             <label for="description">Description: </label>
                             <input type="text" name="description" />
                                         <td>
                                             <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
                                                 <input type="hidden" name="key" value="[% key.id %]" />
+                                                <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                                                 <input type="hidden" name="op" value="delete" />
                                                 <button class="btn btn-link btn-xs delete-key" type="submit"><i class="fa fa-trash"></i> Delete</button>
                                             </form>
                                             <form action="/cgi-bin/koha/opac-apikeys.pl" method="post" class="form-inline">
                                                 <input type="hidden" name="key" value="[% key.id %]" />
+                                                <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                                                 [% IF key.active %]
                                                     <input type="hidden" name="op" value="revoke" />
                                                     <button class="btn btn-link btn-xs" type="submit"><i class="fa fa-remove"></i> Revoke</button>
index fda98ed..7354fce 100755 (executable)
@@ -26,6 +26,7 @@ use C4::Output;
 
 use Koha::ApiKeys;
 use Koha::Patrons;
+use Koha::Token;
 
 my $cgi = new CGI;
 
@@ -51,7 +52,19 @@ if ( not defined $patron ) {
     exit;
 }
 
-my $op = $cgi->param('op');
+my $op = $cgi->param('op') // '';
+
+if ( $op eq 'generate' or
+     $op eq 'delete' or
+     $op eq 'revoke' or
+     $op eq 'activate' ) {
+
+    die "Wrong CSRF token"
+    unless Koha::Token->new->check_csrf({
+        session_id => scalar $cgi->cookie('CGISESSID'),
+        token      => scalar $cgi->param('csrf_token'),
+    });
+}
 
 if ($op) {
     if ( $op eq 'generate' ) {
@@ -102,8 +115,9 @@ if ($op) {
 my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id });
 
 $template->param(
-    api_keys => \@api_keys,
-    patron   => $patron
+    api_keys   => \@api_keys,
+    csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $cgi->cookie('CGISESSID') }),
+    patron     => $patron
 );
 
 output_html_with_http_headers $cgi, $cookie, $template->output;
index a571c16..021c21b 100755 (executable)
@@ -26,6 +26,7 @@ use C4::Output;
 
 use Koha::ApiKeys;
 use Koha::Patrons;
+use Koha::Token;
 
 my $cgi = new CGI;
 
@@ -47,7 +48,19 @@ if ( not defined $patron
     exit;
 }
 
-my $op = $cgi->param('op');
+my $op = $cgi->param('op') // '';
+
+if ( $op eq 'generate' or
+     $op eq 'delete' or
+     $op eq 'revoke' or
+     $op eq 'activate' ) {
+
+    die "Wrong CSRF token"
+    unless Koha::Token->new->check_csrf({
+        session_id => scalar $cgi->cookie('CGISESSID'),
+        token      => scalar $cgi->param('csrf_token'),
+    });
+}
 
 if ($op) {
     if ($op eq 'generate') {
@@ -99,6 +112,7 @@ my @api_keys = Koha::ApiKeys->search({ patron_id => $patron_id });
 $template->param(
     api_keys    => \@api_keys,
     apikeysview => 1,
+    csrf_token  => Koha::Token->new->generate_csrf({ session_id => scalar $cgi->cookie('CGISESSID') }),
     patron      => $patron
 );