Bug 17109: Add CSRF token to [opac-]sendbasket
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Thu, 11 Aug 2016 12:17:14 +0000 (14:17 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 9 Sep 2016 13:37:47 +0000 (13:37 +0000)
If you have no (valid) token, you will not be able to send the message.

Test plan:
[1] Verify if you can still send the cart from opac and intranet.
[2] While still being logged in, try to send the cart from opac by
    using the following URL:
    /cgi-bin/koha/opac-sendbasket.pl?email_add=you@somedomain.com&comment=csrf_test&bib_list=doesnotmatter&csrf_token=justsomeguess12345
    This should now result in a csrf error.

Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
basket/sendbasket.pl
koha-tmpl/intranet-tmpl/prog/en/modules/basket/sendbasketform.tt
koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-sendbasketform.tt
opac/opac-sendbasket.pl

index 16155d7..802ca33 100755 (executable)
@@ -20,16 +20,18 @@ use Modern::Perl;
 use CGI qw ( -utf8 );
 use Encode qw(encode);
 use Carp;
-
+use Digest::MD5 qw(md5_base64);
 use Mail::Sendmail;
 use MIME::QuotedPrint;
 use MIME::Base64;
+
 use C4::Biblio;
 use C4::Items;
 use C4::Auth;
 use C4::Output;
 use C4::Templates ();
 use Koha::Email;
+use Koha::Token;
 
 my $query = new CGI;
 
@@ -43,12 +45,24 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
     }
 );
 
-my $bib_list     = $query->param('bib_list');
+my $bib_list     = $query->param('bib_list') || '';
 my $email_add    = $query->param('email_add');
 
 my $dbh          = C4::Context->dbh;
 
+my $csrf_err;
 if ( $email_add ) {
+    $csrf_err = 1 unless Koha::Token->new->check_csrf({
+        id     => C4::Context->userenv->{id},
+        secret => md5_base64( C4::Context->config('pass') ),
+        token  => scalar $query->param('csrf_token'),
+    });
+}
+
+if( $csrf_err ) {
+    $template->param( csrf_error => 1, email_add => 1 );
+    output_html_with_http_headers $query, $cookie, $template->output;
+} elsif ( $email_add ) {
     my $email = Koha::Email->new();
     my %mail = $email->create_message_headers({ to => $email_add });
     my $comment    = $query->param('comment');
@@ -165,11 +179,16 @@ END_OF_BODY
     output_html_with_http_headers $query, $cookie, $template->output;
 }
 else {
-    $template->param( bib_list => $bib_list );
     $template->param(
+        bib_list       => $bib_list,
         url            => "/cgi-bin/koha/basket/sendbasket.pl",
         suggestion     => C4::Context->preference("suggestion"),
         virtualshelves => C4::Context->preference("virtualshelves"),
+        csrf_token     => Koha::Token->new->generate_csrf(
+            {   id     => C4::Context->userenv->{id},
+                secret => md5_base64( C4::Context->config('pass') ),
+            }
+        ),
     );
     output_html_with_http_headers $query, $cookie, $template->output;
 }
index ef116fc..07d004d 100644 (file)
         <p>The cart was sent to: [% email_add |html %]</p>
                <p><a class="focus close" href="#">Close window</a></p>
        [% END %]
+    [% IF csrf_error %]
+        <p>No valid CSRF token!</p>
+        <p><a class="focus close" href="#">Close window</a></p>
+    [% END %]
        [% IF ( error ) %]
        <p>Problem sending the cart...</p>
        [% END %]
             <label for="comment">Comment:</label>
             <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
     </li>
-    <li>
-        <input type="hidden" name="bib_list" value="[% bib_list %]" />
-    </li></ol></fieldset>
-       <fieldset class="action"> <input type="submit" value="Send" /> <a class="cancel close" href="#">Cancel</a> </fieldset>
+    </ol>
+    </fieldset>
+    <fieldset class="action"> <input type="submit" value="Send" /> <a class="cancel close" href="#">Cancel</a> </fieldset>
+    <input type="hidden" name="bib_list" value="[% bib_list %]" />
+    <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
 </form>
 
 [% END %]</div>
index 25b248b..e1f8f60 100644 (file)
                                 <p><a class="focus close" href="#">Close window</a></p>
                             [% END %]
 
+                            [% IF csrf_error %]
+                                <p>No valid CSRF token!</p>
+                                <p><a class="focus close" href="#">Close window</a></p>
+                            [% END %]
                             [% IF ( error ) %]
                                 <div class="alert">
                                     <p>There was an error sending the cart.</p>
@@ -34,6 +38,7 @@
                                     <label for="comment">Comment:</label>
                                     <textarea id="comment" name="comment" rows="4" cols="40"></textarea>
                                     <input type="hidden" name="bib_list" value="[% bib_list %]" />
+                                    <input type="hidden" name="csrf_token" value="[% csrf_token %]" />
                                 </fieldset>
                                 <fieldset class="action">
                                     <input type="submit" class="btn" value="Send" />
index baa88b5..b1f6f3e 100755 (executable)
@@ -22,10 +22,11 @@ use Modern::Perl;
 use CGI qw ( -utf8 );
 use Encode qw(encode);
 use Carp;
-
+use Digest::MD5 qw(md5_base64);
 use Mail::Sendmail;
 use MIME::QuotedPrint;
 use MIME::Base64;
+
 use C4::Biblio;
 use C4::Items;
 use C4::Auth;
@@ -33,6 +34,7 @@ use C4::Output;
 use C4::Members;
 use C4::Templates ();
 use Koha::Email;
+use Koha::Token;
 
 my $query = new CGI;
 
@@ -45,12 +47,24 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user (
     }
 );
 
-my $bib_list     = $query->param('bib_list');
+my $bib_list     = $query->param('bib_list') || '';
 my $email_add    = $query->param('email_add');
 
 my $dbh          = C4::Context->dbh;
 
+my $csrf_err;
 if ( $email_add ) {
+    $csrf_err = 1 unless Koha::Token->new->check_csrf({
+        id     => C4::Context->userenv->{id},
+        secret => md5_base64( C4::Context->config('pass') ),
+        token  => scalar $query->param('csrf_token'),
+    });
+}
+
+if( $csrf_err ) {
+    $template->param( csrf_error => 1, email_add => 1 );
+    output_html_with_http_headers $query, $cookie, $template->output;
+} elsif ( $email_add ) {
     my $email = Koha::Email->new();
     my $user = GetMember(borrowernumber => $borrowernumber);
     my $user_email = GetFirstValidEmailAddress($borrowernumber)
@@ -185,11 +199,16 @@ END_OF_BODY
     output_html_with_http_headers $query, $cookie, $template->output;
 }
 else {
-    $template->param( bib_list => $bib_list );
     $template->param(
+        bib_list       => $bib_list,
         url            => "/cgi-bin/koha/opac-sendbasket.pl",
         suggestion     => C4::Context->preference("suggestion"),
         virtualshelves => C4::Context->preference("virtualshelves"),
+        csrf_token     => Koha::Token->new->generate_csrf(
+            {   id     => C4::Context->userenv->{id},
+                secret => md5_base64( C4::Context->config('pass') ),
+            }
+        ),
     );
     output_html_with_http_headers $query, $cookie, $template->output;
 }