Bug 27286 - Patron picture-upload.pl allows arbitrary file extension during upload
Summary: Patron picture-upload.pl allows arbitrary file extension during upload
Status: Signed Off
Alias: None
Product: Koha
Classification: Unclassified
Component: Patrons (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Saiful Amin
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-21 07:20 UTC by Saiful Amin
Modified: 2025-11-29 17:40 UTC (History)
4 users (show)

See Also:
GIT URL:
Initiative type: ---
Sponsorship status: ---
Comma delimited list of Sponsors:
Crowdfunding goal: 0
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Double extension or wrong extension in picture file upload (143.37 KB, image/png)
2020-12-21 07:20 UTC, Saiful Amin
Details
Bug 27286: Patron picture-upload.pl allows arbitrary file extension during upload (3.59 KB, patch)
2025-11-26 18:45 UTC, Saiful Amin
Details | Diff | Splinter Review
Bug 27286: Patron picture-upload.pl allows arbitrary file extension during upload (3.65 KB, patch)
2025-11-28 20:33 UTC, David Nind
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Saiful Amin 2020-12-21 07:20:38 UTC
Created attachment 114546 [details]
Double extension or wrong extension in picture file upload

The IT team has flagged two issues with the picture upload tool (Patron Images feature):
1. The tool allows upload of files with double extension.
2. The tool allows upload of files with arbitrary file extension.

In essence, the tool does not validate the image file restriction that is mentioned in the top of upload input tag which is:
"Only PNG, GIF, JPEG, XPM formats are supported."
Comment 1 Saiful Amin 2020-12-21 07:25:14 UTC
I have tested the following changes to fix both the issues. Need someone to test and approve (I don't have the full environment to submit the patch).

/usr/share/koha/intranet/cgi-bin/tools/picture-upload.pl
Line: 96++
 
    output_and_exit( $input, $cookie, $template, 'wrong_image_file_ext' )
        unless $uploadfilename =~ m/^[^\.]+\.(png|gif|jpg|jpeg|xpm)$/i;


/usr/share/koha/intranet/htdocs/intranet-tmpl/prog/en/includes/blocking_errors.inc

Line: 9++
    [% CASE 'wrong_image_file_ext' %]
        <div class="dialog message">Wrong image file extension or double extension in file name</div>
Comment 2 Katrin Fischer 2021-02-06 18:37:50 UTC
Resetting to NEW to make this a bit more visible!
Comment 3 Katrin Fischer 2023-07-01 15:03:54 UTC
When I use the tool, there is a note:
NOTE: Only PNG, GIF, JPEG, XPM formats are supported. Maximum image size is 2MB.
And if I try to upload a .txt it fails giving a nice error.

When I try the same on the patron record, the modal closes without any visible error.
Comment 4 Saiful Amin 2025-11-26 18:45:03 UTC
Created attachment 189978 [details] [review]
Bug 27286: Patron picture-upload.pl allows arbitrary file extension during upload

This patch introduces strict validation for filenames in the patron
image upload tool. It addresses VAPT security concerns regarding
"double extension" attacks (e.g., image.php.jpg or archive.php.zip).

The patch ensures that:
1. Filenames containing more than one period are rejected.
2. Only strictly allowed extensions (zip, png, gif, jpg, jpeg, xpm) are accepted.

Test Plan:

1. Pre-patch check:
   a. Go to Tools -> Upload patron images.
   b. Rename an image file to 'test.php.jpg'.
   c. Upload this file.
   d. Observe that the upload is accepted, but the filename itself is not
      flagged as invalid.
   e. Create a valid file named 'test.php.zip' (with valid content).
   f. Upload this file.
   g. Result: The upload proceeds.

2. Apply the patch.

3. Verify rejection (images):
   a. Go to Tools -> Upload patron images.
   b. Attempt to upload 'test.php.jpg'.
   c. Result: The tool stops immediately with a "BADFILENAME" error message.
   d. Attempt to upload 'test.v1.jpg' (valid image, extra dot).
   e. Result: The tool stops immediately with a "BADFILENAME" error message.

4. Verify rejection (zip):
   a. Attempt to upload 'archive.php.zip'.
   b. Result: The tool stops immediately with a "BADFILENAME" error message.

5. Verify valid upload:
   a. Attempt to upload 'patron.jpg' (standard valid file).
   b. Result: Upload succeeds.
   c. Attempt to upload 'patrons.zip' (standard valid zip).
   d. Result: Upload succeeds and files are unpacked.

6. Sign-off.
Comment 5 David Nind 2025-11-28 20:33:15 UTC
Created attachment 190027 [details] [review]
Bug 27286: Patron picture-upload.pl allows arbitrary file extension during upload

This patch introduces strict validation for filenames in the patron
image upload tool. It addresses VAPT security concerns regarding
"double extension" attacks (e.g., image.php.jpg or archive.php.zip).

The patch ensures that:
1. Filenames containing more than one period are rejected.
2. Only strictly allowed extensions (zip, png, gif, jpg, jpeg, xpm) are accepted.

Test Plan:

1. Pre-patch check:
   a. Go to Tools -> Upload patron images.
   b. Rename an image file to 'test.php.jpg'.
   c. Upload this file.
   d. Observe that the upload is accepted, but the filename itself is not
      flagged as invalid.
   e. Create a valid file named 'test.php.zip' (with valid content).
   f. Upload this file.
   g. Result: The upload proceeds.

2. Apply the patch.

3. Verify rejection (images):
   a. Go to Tools -> Upload patron images.
   b. Attempt to upload 'test.php.jpg'.
   c. Result: The tool stops immediately with a "BADFILENAME" error message.
   d. Attempt to upload 'test.v1.jpg' (valid image, extra dot).
   e. Result: The tool stops immediately with a "BADFILENAME" error message.

4. Verify rejection (zip):
   a. Attempt to upload 'archive.php.zip'.
   b. Result: The tool stops immediately with a "BADFILENAME" error message.

5. Verify valid upload:
   a. Attempt to upload 'patron.jpg' (standard valid file).
   b. Result: Upload succeeds.
   c. Attempt to upload 'patrons.zip' (standard valid zip).
   d. Result: Upload succeeds and files are unpacked.

6. Sign-off.

Signed-off-by: David Nind <david@davidnind.com>
Comment 6 David Nind 2025-11-28 20:37:35 UTC
Testing notes (using koha-testing-dcoker):

1. Enable the patronimages system preference (set to 'Allow').

I'm not on the QA team, but:

1. The QA errors need fixing (from running qa in koha-testing-docker):

qa
Tag v25.05.00-1 found in /kohadevbox/koha — checking out branch main in /kohadevbox/qa-test-tools
Already on 'main'
Your branch is up to date with 'origin/main'.
testing 1 commit(s) (applied to 7adeda9 'f1 Bug 38426: yarn.lock changes')

Processing files
[RUNNING] koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt
[RUNNING] tools/picture-upload.pl
[PASS] tools/picture-upload.pl
[FAIL] koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt

Result:
[FAIL] koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt
   FAIL	  tidiness
		File is not tidy, please run `perl misc/devel/tidy.pl koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt`
[PASS] tools/picture-upload.pl

Processing additional checks PASS!


	* Was this bug sponsored by anyone?

2. I don't think it is necessary to have the comment "# Fix bug 27286" in the patch.
Comment 7 Saiful Amin 2025-11-29 12:03:56 UTC
> Result:
> [FAIL] koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt
>    FAIL	  tidiness
> 		File is not tidy, please run `perl misc/devel/tidy.pl
> koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt`
> [PASS] tools/picture-upload.pl
> 
> 
> 2. I don't think it is necessary to have the comment "# Fix bug 27286" in
> the patch.

Do I need to resubmit the patch with the changes recommended or is the QA team allowed to do these changes themselves?
Comment 8 David Nind 2025-11-29 17:40:11 UTC
(In reply to Saiful Amin from comment #7)
> > Result:
> > [FAIL] koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt
> >    FAIL	  tidiness
> > 		File is not tidy, please run `perl misc/devel/tidy.pl
> > koha-tmpl/intranet-tmpl/prog/en/modules/tools/picture-upload.tt`
> > [PASS] tools/picture-upload.pl
> > 
> > 
> > 2. I don't think it is necessary to have the comment "# Fix bug 27286" in
> > the patch.
> 
> Do I need to resubmit the patch with the changes recommended or is the QA
> team allowed to do these changes themselves?

The QA Team would normally ask you to make these changes.

Add these in a follow-up patch - see https://wiki.koha-community.org/wiki/Commit_messages#Follow-ups

For example: Bug 27286: (follow-up) Tidy code and remove comment