Commit Graph

21 Commits

Author SHA1 Message Date
David Cook 2691d566ed
Bug 39055: Fix login condition when using op and login_op
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
2026-03-19 10:51:45 +00:00
David Cook 137b41c510
Bug 39055: Add change to Koha::App::Plugin::CSRF and fix unit test
Signed-off-by: David Cook <dcook@prosentient.com.au>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
2026-03-19 10:51:41 +00:00
Tomas Cohen Arazi 1873d97c07
Bug 40163: Fix several http addresses in the code docs
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Brendan Lawlor <blawlor@clamsnet.org>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
2025-09-23 16:21:29 -06:00
Julian Maurice b92fda21ce
Bug 39980: Fix vendors pages when using Koha as a Mojo app
Bug 38010 broke the vendors pages when Koha is running as a Mojolicious
application

This adds new routes to Koha::App::Intranet to mimic the Apache rewrite
rules added in bug 38010.

To be able to do that, the CGIBinKoha plugin's code had to be moved to a
controller. The logic remains the same.

Test plan:
1. Run `bin/intranet daemon -l http://*:3000/`
2. Point your browser to http://localhost:3000 (replace localhost by the
   hostname/IP of koha's container, if needed)
3. Go to Acquisitions and search for a vendor. You should see the list
   of vendors.
4. Click on "New vendor" button. Notice the URL changed to
   .../acquisition/vendors/add.
5. Reload the page. You should still see the form to create a new vendor
6. Verify that you can create a vendor and that you are then redirected
   to the list of vendors

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
2025-09-18 13:52:06 -06:00
Julian Maurice 751391cc5c
Bug 37286: Fix REST API authentication when using Mojo apps
Koha::REST::V1::Auth checks the request URL path and do various things
depending on how it looks.
For instance, it allows everyone to access paths starting with
"/api/v1/oauth/"

But because of how Koha::REST::V1 was written, when using mojolicious
applications Koha::App::Intranet and Koha::App::Opac, paths had to add a
path prefix ("/api"), which means the final path as seen by
Koha::REST::V1::Auth looked like this: "/api/api/v1/oauth".

I said "had to", but actually there is another way that does not require
this path manipulation and that's what this patch does:

Koha::REST::V1 now accepts a configuration parameter that allows to
change the base path for API routes, which allows
Koha::App::Plugin::RESTV1 (used by Koha::App::Intranet and
Koha::App::Opac) to generate the right routes.

This configuration parameter defaults to "/api/v1", so when outside of
Koha::App::Intranet and Koha::App::Opac (when using
debian/templates/plack.psgi for instance), the behavior is unchanged.

Test plan:
1. Do not apply the patch yet
2. Run `bin/intranet daemon -l http://*:8080`
3. Run `curl -i -d{} http://127.0.0.1:8080/api/v1/oauth/token`
   It should return a 403 error, with an error message "Authentication
   failure".
4. Stop `bin/intranet daemon -l http://*:8080` by hitting Ctrl-C on the
   terminal you started it
5. Apply the patch
6. Run `bin/intranet daemon -l http://*:8080` again
7. Run `curl -i -d{} http://127.0.0.1:8080/api/v1/oauth/token`
   This time it should return a 400 error with an error message saying
   the "grant_type" property is missing.
   This error is normal as we did not send any data in the POST request
   body, and seeing this means Koha allowed us to use that route because
   it recognized '/api/v1/oauth/' at the start of the URL path
8. You can do the same test with `bin/opac`

Signed-off-by: Magnus Enger <magnus@libriotech.no>
Works as advertised. See Bugzilla for notes from testing.
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2025-03-17 08:21:23 +01:00
Koha Development Team d659526b5a
Bug 38664: Tidy the whole codebase
This commit is generated using:
  % perl misc/devel/tidy.pl
*within* ktd, to get the same version of perltidy than what will be used
by our CI (currently v20230309).

Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2025-02-11 14:58:24 +01:00
Marcel de Rooy bbf0392359
Bug 38630: (QA follow-up) Add copyright, license and/or POD
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2025-01-30 18:05:24 +01:00
Julian Maurice ca5db9ffb7
Bug 38630: Make the REST API respect KohaOpacLanguage cookie
The REST API can return translated strings to the opac or staff
interface, so it should respect the KohaOpacLanguage cookie and return
translations in the same language than the interface.

Test plan:
1. Do not apply patch yet
2. Log in to koha staff interface
3. Use the browser's devtools to get the value of CGISESSID cookie
4. Make sure you have at least 2 languages installed ('en' + another
   one - I'll use 'fr-FR' as an example in this test plan)
5. Find an item and note its itemnumber and itemtype
6. Go translate this itemtype's description (admin » item types » edit
   item type » translate). Add a translation for 'en' and another one
   for your language.
7. Close your browser tab (do not logout) and restart Koha to flush the
   memory cache.
8. Execute the following commands (replace variables by the correct
   value):

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=en; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   If you do not have jq, it can be found in most distributions
   packages. Or you could inspect the JSON manually at the path
   /_strings/item_type_id/str

   Restart Koha again, and execute the same command but with your
   language set in KohaOpacLanguage cookie:

    curl -sSL "$KOHA_URL/api/v1/items/$ITEMNUMBER" \
        -H 'x-koha-embed: +strings' \
        -H "Cookie: KohaOpacLanguage=fr-FR; CGISESSID=$CGISESSID" \
        | jq -r '._strings.item_type_id.str'

   Depending on how Koha sort the languages (not sure how it's done),
   you should get either the 'en' one or the 'fr-FR' one, but the
   important thing is you should get the same translation, no matter
   what the value of KohaOpacLanguage is, which is the issue this patch
   is trying to fix.
9. Apply the patch and restart Koha
10. Repeat step 8. This time you should get the translation
    corresponding to the language set in KohaOpacLanguage cookie

Side note:
* The need to restart Koha before each request is due to a bug (see bug
  38454). If already pushed when you're testing this one, you don't have
  to restart.

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2025-01-30 18:05:24 +01:00
Victor Grousset/tuxayo 3972f5460a
Bug 36598: (QA follow-up): tidy up code
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:50 +02:00
Julian Maurice ab84ff6407
Bug 36598: Add comments asking to keep both CSRF checks in sync
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:50 +02:00
Julian Maurice 36858c8c57
Bug 36598: Prevent use of unsafe HTTP method with non-cud op parameter
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:49 +02:00
Julian Maurice 4325e607cf
Bug 36598: Improve documentation and error message in CSRF plugin
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:48 +02:00
Julian Maurice f36c071f5b
Bug 36598: Fix CSRF header name (underscore -> hyphen)
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:48 +02:00
Julian Maurice 30670cba57
Bug 36598: Prohibit CUD operations with safe HTTP methods (GET/HEAD/...)
Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:47 +02:00
Julian Maurice e61b92c8dd
Bug 36598: Enable CSRF protection for Mojolicious apps
Test plan:
1. Run bin/opac daemon -l http://*:3001/
2. Go to http://localhost:3001/cgi-bin/koha/opac-user.pl
3. With browser devtools, locate csrf_token hidden input within the
   login form and remove it or modify it
4. Try to submit the form with correct credentials, it should fail
   ("Wrong CSRF token")
5. Reload the page, try to log in normally without modifying the DOM, it
   should succeed
6. Run bin/intranet daemon -l http://*:3002/
7. Go to http://localhost:3002/cgi-bin/koha/mainpage.pl
8. With browser devtools, locate csrf_token hidden input within the
   login form and remove it or modify it
9. Try to submit the form with correct credentials, it should fail
   ("Wrong CSRF token")
10. Reload the page, try to log in normally without modifying the DOM,
    it should succeed
11. Run prove t/db_dependent/mojo/csrf.t

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
2024-10-18 12:07:47 +02:00
Julian Maurice 40da37caa6 Bug 29498: Remove usage of deprecated Mojolicious::Routes::Route::detour
Mojolicious::Routes::Route::detour was deprecated in Mojolicious 8.67
and removed in Mojolicious 9

It was used in Koha::App::Plugin::RESTV1 to connect the "REST API"
Mojolicious app to the other Mojolicious apps (Koha::App::Intranet and
Koha::App::Opac), so it's only a problem when running Koha as a
Mojolicious app.

This patch makes use of the "partial" attribute instead, which does the
same thing.

Test plan:
1. Run `morbo bin/intranet`
2. Point your browser to what morbo says (should be localhost:3000)
3. Log in, and go to Administration » Libraries. This table uses the
   REST API, so if you see data in it, that means the API (and the
   patch) works

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-01-18 21:15:04 -10:00
Julian Maurice 7eb1969828 Bug 29516: Remove dependency on IO::Scalar
IO::Scalar is used in Koha::App::Plugin::CGIBinKoha to create a
filehandle tied to a scalar.
Perl has this feature built in since 5.8.0 so IO::Scalar is not needed

I'm not sure how to test this, since it should behave exactly the same
with or without the patch, but we can at least verify that it doesn't
introduce encoding issues.

Test plan:
1. Run `morbo bin/intranet`
2. Go to http://localhost:3000
3. Create a bibliographic record with some non-latin characters (try
   emoji characters for instance). Verify that there is no visible
   encoding issues.
4. Save this record as MARCXML and re-import it as a new record using
   Tools » Stage MARC records for import. Verify that the new record has
   no visible encoding issues
5. `git grep IO::Scalar` should not return any result

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-01-13 16:37:32 -10:00
Julian Maurice 5f492e3049 Bug 20582: Reset CGI::PARAM_UTF8 to 1 before each CGI request
CGI::Compile calls CGI::initialize_globals before each request, which
resets PARAM_UTF8 to 0
We need to set it back to the correct value

This is the same trick used in debian/templates/plack.psgi

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-10-06 14:34:17 +02:00
Martin Renvoize 0f26c7c3d7 Bug 20582: (QA follow-up) Add POD to satisfy coding guidelines
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-10-06 12:00:05 +02:00
Julian Maurice 92340d03af Bug 20582: Suppress warning in CGIBinKoha.pm
Signed-off-by: Jerome Charaoui <jcharaoui@cmaisonneuve.qc.ca>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-10-06 12:00:04 +02:00
Julian Maurice 57ff9af3bb Bug 20582: Turn Koha into a Mojolicious application
This patch is a proof-of-concept of Koha as a Mojolicious application

This has several benefits:
- Development setup is easier. No need for apache or nginx. Just run
  `morbo bin/intranet` or `morbo bin/opac` and go to
  http://localhost:3000 (URL rewrites and static files are handled by
  the app)
- apache2/nginx configuration is simpler too (an example of nginx
  configuration is included in the patch)
- starman and plack middlewares can still be used for debug or gzip
  compression for instance (see app.psgi)
- Using Test::Mojo we can test the whole application, as we do with the
  REST API (which is a Mojolicious application too)
- It opens a way for converting CGI scripts into Mojolicious
  controllers and actions (even if that's not possible at the moment
  because of the authentication code)

It uses the same mechanism as Plack::App::CGIBin to deal with CGI
scripts, so it should be equivalent in terms of performance

How to test ?
- Run `morbo bin/intranet`, then go to http://localhost:3000/ and try to
  find bugs. Check the REST API at http://localhost:3000/api/v1
- Run `morbo bin/opac`, then go to http://localhost:3000/ and try to
  find bugs. Check the REST API at http://localhost:3000/api/v1
- Run `starman -l :5000 -l :5001` and verify that intranet
  (http://localhost:5000) and opac (http://localhost:5001) work normally
- Read the code (and the comments), it's not very long

Signed-off-by: Jerome Charaoui <jcharaoui@cmaisonneuve.qc.ca>
Signed-off-by: Victor Grousset/tuxayo <victor@tuxayo.net>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-10-06 12:00:04 +02:00