[Bug 44272] New: Provide friendlier links to public files uploaded via the file uploads tool
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 Bug ID: 44272 Summary: Provide friendlier links to public files uploaded via the file uploads tool Initiative type: --- Sponsorship --- status: Product: Koha Version: Main Hardware: All OS: All Status: NEW Severity: enhancement Priority: P5 - low Component: Tools Assignee: koha-bugs@lists.koha-community.org Reporter: nick@bywatersolutions.com QA Contact: testopia@bugs.koha-community.org Target Milestone: --- Currently when uploading a file we generate a hash - this is somewhere between a convenience to avoid duplicate filenames and a way to prevent files from being discovered. This patch set will try to make the URL a little more human friendly, including either the filename at the end, or allowing a slug that can be specified rather than the file name This is another stab at bug 19318 and related work, it can be a duplicate of those, but placing here as a fresh start -- You are receiving this mail because: You are the assignee for the bug. You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Depends on| |44271 Assignee|koha-bugs@lists.koha-commun |nick@bywatersolutions.com |ity.org | -- You are receiving this mail because: You are watching all bug changes. You are the assignee for the bug.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |Needs Signoff -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #1 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212574 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212574&action=edit Bug 44272: Add readable links for uploaded files Uploaded files are currently linked as OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=<hashvalue> This patch also accepts, and generates, links in the form OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<hashvalue>/<filename> The file is still served by opac-retrieve-file.pl, so the public flag and the Content-Type/Content-Disposition headers still apply. The filename must match the upload exactly: a different or missing filename, or any other path, returns 404 Not Found. Existing ?id= links keep working. A missing or non-public file now returns a 404 status instead of 200. - opac-retrieve-file.pl reads <hashvalue>/<filename> from PATH_INFO - Koha::UploadedFile->looks_like_hashvalue recognises a hashvalue: MD5, or SHA-1 for uploads made before Koha 3.22 - Koha::UploadedFile->opac_url builds the readable link. It is already URI-escaped (including OPACBaseURL), so the template outputs it with $raw: the url filter would escape it a second time (%20 -> %2520) - Tools > Upload and the cataloguing plugin (upload.pl) use opac_url - C4::HTML5Media recognises both link forms Test plan: 1. Apply patch, restart_all 2. prove t/db_dependent/Upload.t 3. Tools > Upload: upload a file with 'Allow public downloads' checked 4. Click the 'Yes' link in the Public column: the URL now ends in /opac-retrieve-file.pl/<hash>/<filename> and the file downloads 5. Replace the URL with the old ?id=<hash> form: still works 6. Change the filename at the end of the URL, or remove it: 404 7. Upload a non-public file and try its readable URL in the OPAC: 404 8. Link the upload.pl plugin to 856$u, edit a record, open the plugin, search for your file and click Choose: 856$u gets the readable URL 9. Enable HTML5MediaEnable, upload an mp3/mp4 publicly, put the readable URL in 856$u and confirm the player appears on the OPAC detail page Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #2 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212575 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212575&action=edit Bug 44272: Add optional slugs for readable links to uploaded files This adds an optional, unique 'slug' (link name) to uploaded files. A public upload with a slug is linked as OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<slug> instead of OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<hashvalue>/<filename> The file is still served by opac-retrieve-file.pl, so the public flag and the Content-Type/Content-Disposition headers still apply. A slug on a non-public upload does not make it reachable in the OPAC. Slug rules (Koha::UploadedFile->is_valid_slug): - 1 to 100 characters from a-z, 0-9, '_' and '-' - starts and ends with a letter or digit - no dots, so a slug cannot claim a file extension (e.g. 'invoice.pdf' pointing to an executable) - cannot look like a hashvalue (Koha::UploadedFile->looks_like_hashvalue), so it cannot be confused with a hashvalue link - unique; input is trimmed and lowercased, empty removes the slug - the OPAC matches slugs exactly: /Annual-Report is a 404 when the slug is annual-report Retired slugs: when an upload's slug is changed or removed, or the upload is deleted, the slug is recorded in the new table uploaded_files_retired_slugs. Another upload can only take a retired slug if the user has tools/upload_manage, so a link that was published (e.g. in 856$u) cannot silently be pointed to other content. The upload that released a slug can take it back. This is enforced in Koha::UploadedFile->store; Koha::UploadedFile->set_slug is the way to allow the reuse. Only the upload's owner or a user with upload_manage can set or change a slug (Koha::UploadedFile->can_be_managed_by, from bug YYYYY). Search in Tools > Upload now also matches slugs. Test plan: 1. Apply patches, updatedatabase, restart_all 2. prove t/db_dependent/Upload.t 3. Tools > Upload: upload a public file, then click 'Link name' on it and enter 'annual-report' 4. The row shows 'Link name: annual-report' and the Public link is .../opac-retrieve-file.pl/annual-report 5. Open that link: the file is served under its real filename 6. Try .../opac-retrieve-file.pl/Annual-Report and .../opac-retrieve-file.pl/annual-report/x: 404 7. Upload a second file and try to give it the link name 'annual-report': 'This link name is already used by another upload.' 8. Try 'bad name!', 'invoice.pdf' or a 32-character hex string: 'Link name is not valid...' 9. Give a non-public upload a link name and open it in the OPAC: 404 10. Search uploads for 'annual-rep': the file is found by its slug 11. Clear the link name (empty value): the link falls back to .../opac-retrieve-file.pl/<hash>/<filename> 12. Set it back to 'annual-report' on the same file: allowed 13. Create a staff user with only catalogue + tools > upload_general_files. As that user: a. try to set a link name on someone else's upload: the button is not shown, and a forged POST is refused b. upload a file of your own 14. As the superlibrarian, delete the 'annual-report' upload 15. As the limited user, give your file the link name 'annual-report': 'This link name was used by another upload before...' 16. As the superlibrarian, give another upload the link name 'annual-report': allowed 17. With the upload.pl plugin on 856$u, Choose a file with a slug: the slug link is used 18. Enable HTML5MediaEnable, put a slug link to a public mp3/mp4 in 856$u and confirm the player appears on the OPAC detail page Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #3 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212576 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212576&action=edit Bug 44272: [DO NOT PUSH] DBIC schema changes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #4 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212577 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212577&action=edit Bug 44272: Add Cypress tests for readable links to uploads Files are uploaded through Tools > Upload, in an UPLOAD category created for the tests, and the links are requested from the OPAC. Links by hashvalue and filename: - Tools > Upload shows the readable link - a public upload is served by its readable link and by ?id= - a wrong, missing or extra part in the link is a 404 - an upload that is not public is never served Link names (slugs): - a public upload is served by its link name, exactly (case, trailing slash and extra parts are a 404) - the cataloguing plugin's Choose button uses the link name, and the plugin does not offer to change it - a non-public upload is not served by its link name - invalid link names are refused (dot, space, leading dash, hashvalue) - a link name used by another upload is refused - emptying the link name removes it Released link names: - only staff with upload_manage can reuse a link name released by a deleted upload - the upload that released a link name can take it back Everything is removed afterwards. Koha leaves the empty CYPRESS directory it created in upload_path, as it does for every category. The staff member is created with insertSampleStaff and cy.loginAs, from bug ZZZZZ. Test plan: npx cypress run --spec t/cypress/integration/Tools/UploadReadableLinks_spec.ts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://bugs.koha-community | |.org/bugzilla3/show_bug.cgi | |?id=19318 -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 Nick Clemens (kidclamp) <nick@bywatersolutions.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Attachment #212574|0 |1 is obsolete| | Attachment #212575|0 |1 is obsolete| | Attachment #212576|0 |1 is obsolete| | Attachment #212577|0 |1 is obsolete| | -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #5 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212580 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212580&action=edit Bug 44272: Add readable links for uploaded files Uploaded files are currently linked as OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl?id=<hashvalue> This patch also accepts, and generates, links in the form OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<hashvalue>/<filename> The file is still served by opac-retrieve-file.pl, so the public flag and the Content-Type/Content-Disposition headers still apply. The filename must match the upload exactly: a different or missing filename, or any other path, returns 404 Not Found. Existing ?id= links keep working. A missing or non-public file now returns a 404 status instead of 200. - opac-retrieve-file.pl reads <hashvalue>/<filename> from PATH_INFO - Koha::UploadedFile->looks_like_hashvalue recognises a hashvalue: MD5, or SHA-1 for uploads made before Koha 3.22 - Koha::UploadedFile->opac_url builds the readable link. It is already URI-escaped (including OPACBaseURL), so the template outputs it with $raw: the url filter would escape it a second time (%20 -> %2520) - Tools > Upload and the cataloguing plugin (upload.pl) use opac_url - C4::HTML5Media recognises both link forms Test plan: 1. Apply patch, restart_all 2. prove t/db_dependent/Upload.t 3. Tools > Upload: upload a file with 'Allow public downloads' checked 4. Click the 'Yes' link in the Public column: the URL now ends in /opac-retrieve-file.pl/<hash>/<filename> and the file downloads 5. Replace the URL with the old ?id=<hash> form: still works 6. Change the filename at the end of the URL, or remove it: 404 7. Upload a non-public file and try its readable URL in the OPAC: 404 8. Link the upload.pl plugin to 856$u, edit a record, open the plugin, search for your file and click Choose: 856$u gets the readable URL 9. Enable HTML5MediaEnable, upload an mp3/mp4 publicly, put the readable URL in 856$u and confirm the player appears on the OPAC detail page Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #6 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212581 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212581&action=edit Bug 44272: Add optional slugs for readable links to uploaded files This adds an optional, unique 'slug' (link name) to uploaded files. A public upload with a slug is linked as OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<slug> instead of OPACBaseURL/cgi-bin/koha/opac-retrieve-file.pl/<hashvalue>/<filename> The file is still served by opac-retrieve-file.pl, so the public flag and the Content-Type/Content-Disposition headers still apply. A slug on a non-public upload does not make it reachable in the OPAC. Slug rules (Koha::UploadedFile->is_valid_slug): - 1 to 100 characters from a-z, 0-9, '_' and '-' - starts and ends with a letter or digit - no dots, so a slug cannot claim a file extension (e.g. 'invoice.pdf' pointing to an executable) - cannot look like a hashvalue (Koha::UploadedFile->looks_like_hashvalue), so it cannot be confused with a hashvalue link - unique; input is trimmed and lowercased, empty removes the slug - the OPAC matches slugs exactly: /Annual-Report is a 404 when the slug is annual-report Retired slugs: when an upload's slug is changed or removed, or the upload is deleted, the slug is recorded in the new table uploaded_files_retired_slugs. Another upload can only take a retired slug if the user has tools/upload_manage, so a link that was published (e.g. in 856$u) cannot silently be pointed to other content. The upload that released a slug can take it back. This is enforced in Koha::UploadedFile->store; Koha::UploadedFile->set_slug is the way to allow the reuse. Only the upload's owner or a user with upload_manage can set or change a slug (Koha::UploadedFile->can_be_managed_by, from bug 44271). Search in Tools > Upload now also matches slugs. Test plan: 1. Apply patches, updatedatabase, restart_all 2. prove t/db_dependent/Upload.t 3. Tools > Upload: upload a public file, then click 'Link name' on it and enter 'annual-report' 4. The row shows 'Link name: annual-report' and the Public link is .../opac-retrieve-file.pl/annual-report 5. Open that link: the file is served under its real filename 6. Try .../opac-retrieve-file.pl/Annual-Report and .../opac-retrieve-file.pl/annual-report/x: 404 7. Upload a second file and try to give it the link name 'annual-report': 'This link name is already used by another upload.' 8. Try 'bad name!', 'invoice.pdf' or a 32-character hex string: 'Link name is not valid...' 9. Give a non-public upload a link name and open it in the OPAC: 404 10. Search uploads for 'annual-rep': the file is found by its slug 11. Clear the link name (empty value): the link falls back to .../opac-retrieve-file.pl/<hash>/<filename> 12. Set it back to 'annual-report' on the same file: allowed 13. Create a staff user with only catalogue + tools > upload_general_files. As that user: a. try to set a link name on someone else's upload: the button is not shown, and a forged POST is refused b. upload a file of your own 14. As the superlibrarian, delete the 'annual-report' upload 15. As the limited user, give your file the link name 'annual-report': 'This link name was used by another upload before...' 16. As the superlibrarian, give another upload the link name 'annual-report': allowed 17. With the upload.pl plugin on 856$u, Choose a file with a slug: the slug link is used 18. Enable HTML5MediaEnable, put a slug link to a public mp3/mp4 in 856$u and confirm the player appears on the OPAC detail page Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #7 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212582 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212582&action=edit Bug 44272: [DO NOT PUSH] DBIC schema changes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=44272 --- Comment #8 from Nick Clemens (kidclamp) <nick@bywatersolutions.com> --- Created attachment 212583 --> https://bugs.koha-community.org/bugzilla3/attachment.cgi?id=212583&action=edit Bug 44272: Add Cypress tests for readable links to uploads Files are uploaded through Tools > Upload, in an UPLOAD category created for the tests, and the links are requested from the OPAC. Links by hashvalue and filename: - Tools > Upload shows the readable link - a public upload is served by its readable link and by ?id= - a wrong, missing or extra part in the link is a 404 - an upload that is not public is never served Link names (slugs): - a public upload is served by its link name, exactly (case, trailing slash and extra parts are a 404) - the cataloguing plugin's Choose button uses the link name, and the plugin does not offer to change it - a non-public upload is not served by its link name - invalid link names are refused (dot, space, leading dash, hashvalue) - a link name used by another upload is refused - emptying the link name removes it Released link names: - only staff with upload_manage can reuse a link name released by a deleted upload - the upload that released a link name can take it back Everything is removed afterwards. Koha leaves the empty CYPRESS directory it created in upload_path, as it does for every category. The staff member is created with insertSampleStaff and cy.loginAs, from bug 44270. Test plan: npx cypress run --spec t/cypress/integration/Tools/UploadReadableLinks_spec.ts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> -- You are receiving this mail because: You are watching all bug changes.
participants (1)
-
bugzilla-daemon@bugs.koha-community.org