Bug 17900

Summary: Possible SQL injection in patroncards template editing
Product: Koha Reporter: Jonathan Druart <jonathan.druart>
Component: Label/patron card printingAssignee: Jonathan Druart <jonathan.druart>
Status: CLOSED FIXED QA Contact: Marcel de Rooy <m.de.rooy>
Severity: major    
Priority: P5 - low CC: brendan, chris, julian.maurice, katrin.fischer, kyle, m.de.rooy, mtj, nick
Version: unspecified   
Hardware: All   
OS: All   
Change sponsored?: --- Patch complexity: ---
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
Bug Depends on:    
Bug Blocks: 18044, 18535    
Attachments: Bug 17900: Fix possible SQL injection in patron cards template editing
Bug 17900: Fix possible SQL injection in patron cards template editing
Bug 17900: Update the tests to the new API
Bug 17900: Fix possible SQL injection in patron cards template editing
Bug 17900: Update the tests to the new API

Description Jonathan Druart 2017-01-13 15:33:59 UTC

    
Comment 1 Jonathan Druart 2017-01-13 16:53:01 UTC
Created attachment 58965 [details] [review]
Bug 17900: Fix possible SQL injection in patron cards template editing

To recreate:
/cgi-bin/koha/patroncards/edit-template.pl?op=edit&element_id=23%20and%201%3d2+union+all+select+1,user(),@@version+--%20

Look at the Profile dropdown list.

To fix this problem and to make sure it does not appears anywhere else
in the label and patroncards modules, I have refactored the way the
queries are built in C4::Creators::Lib
Now all of the subroutine takes a hashref in parameters with a 'fields'
and 'filters' parameters.
From these 2 parameters the new internal subroutine _build_query will
build the query and use placeholders.

Test plan:
1/ Make sure you do not recreate the vulnerability with this patch
applied.
2/ With decent data in the labels and patroncards modules, compare all
the different view (undef the New and Manage button groups) with and
without this patch applied.
=> You should not see any differences.

This vulnerability has been reported by MDSec.
Comment 2 Chris Cormack 2017-01-17 07:35:47 UTC
Created attachment 59064 [details] [review]
Bug 17900: Fix possible SQL injection in patron cards template editing

To recreate:
/cgi-bin/koha/patroncards/edit-template.pl?op=edit&element_id=23%20and%201%3d2+union+all+select+1,user(),@@version+--%20

Look at the Profile dropdown list.

To fix this problem and to make sure it does not appears anywhere else
in the label and patroncards modules, I have refactored the way the
queries are built in C4::Creators::Lib
Now all of the subroutine takes a hashref in parameters with a 'fields'
and 'filters' parameters.
From these 2 parameters the new internal subroutine _build_query will
build the query and use placeholders.

Test plan:
1/ Make sure you do not recreate the vulnerability with this patch
applied.
2/ With decent data in the labels and patroncards modules, compare all
the different view (undef the New and Manage button groups) with and
without this patch applied.
=> You should not see any differences.

This vulnerability has been reported by MDSec.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Comment 3 Marcel de Rooy 2017-01-27 07:46:03 UTC
Running t/db_dependent/Creators/Lib.t

ok 86 - creator          is good
Can't use string ("filter") as a HASH ref while "strict refs" in use at /usr/share/koha/masterclone/C4/Creators/Lib.pm line 147.
# Looks like your test exited with 255 just after 86.
Comment 4 Jonathan Druart 2017-01-27 09:02:15 UTC
Created attachment 59603 [details] [review]
Bug 17900: Update the tests to the new API
Comment 5 Marcel de Rooy 2017-01-27 09:53:33 UTC
Created attachment 59612 [details] [review]
Bug 17900: Fix possible SQL injection in patron cards template editing

To recreate:
/cgi-bin/koha/patroncards/edit-template.pl?op=edit&element_id=23%20and%201%3d2+union+all+select+1,user(),@@version+--%20

Look at the Profile dropdown list.

To fix this problem and to make sure it does not appears anywhere else
in the label and patroncards modules, I have refactored the way the
queries are built in C4::Creators::Lib
Now all of the subroutine takes a hashref in parameters with a 'fields'
and 'filters' parameters.
From these 2 parameters the new internal subroutine _build_query will
build the query and use placeholders.

Test plan:
1/ Make sure you do not recreate the vulnerability with this patch
applied.
2/ With decent data in the labels and patroncards modules, compare all
the different view (undef the New and Manage button groups) with and
without this patch applied.
=> You should not see any differences.

This vulnerability has been reported by MDSec.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 6 Marcel de Rooy 2017-01-27 09:53:37 UTC
Created attachment 59613 [details] [review]
Bug 17900: Update the tests to the new API

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 7 Marcel de Rooy 2017-01-27 09:54:19 UTC
Good job, Jonathan.
Code looks good to me. Mostly dry QA (code review). Test passes.
Passed QA
Comment 8 Kyle M Hall 2017-01-30 14:18:34 UTC
Pushed to master for 17.05, thanks Jonathan!
Comment 9 Katrin Fischer 2017-01-30 14:39:35 UTC
This patch has been pushed to 16.11.x and will be in 16.11.03.
Comment 10 Julian Maurice 2017-01-30 15:28:18 UTC
Pushed to 3.22.x for 3.22.16
Comment 11 Mason James 2017-01-31 09:23:26 UTC
Pushed to 16.05.x, for 16.05.09 release