Bug 37060 - KOCT cannot send circulation data due to missing CSRF token
Summary: KOCT cannot send circulation data due to missing CSRF token
Status: NEW
Alias: None
Product: Koha
Classification: Unclassified
Component: Circulation (show other bugs)
Version: Main
Hardware: All All
: P5 - low normal
Assignee: Matthias Meusburger
QA Contact: Testopia
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-06-10 12:07 UTC by Jan Kissig
Modified: 2024-10-23 06:26 UTC (History)
10 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Kissig 2024-06-10 12:07:47 UTC
Just tried using the firefox offline circulation tool (koct) and ran into these console errors:

 ==> /var/log/koha/kohadev/plack-intranet-error.log <==
[2024/06/10 11:55:12] [WARN] Programming error - No CSRF token passed for POST http://localhost:8081/intranet/offline_circ/service.pl (referer: No referer) at /kohadevbox/koha/Koha/Middleware/CSRF.pm line 82.


To reproduce:
a) install firefox addon linked in http://localhost:8081/cgi-bin/koha/circ/circulation-home.pl 
b) in the plugin settings:
  server: http://localhost:8081
  Login: koha
  Password: koha
c) test login
d) check logs for 

[WARN] Programming error - No CSRF token passed for POST http://localhost:8081/intranet/offline_circ/service.pl (referer: No referer) at /kohadevbox/koha/Koha/Middleware/CSRF.pm line 82.
Comment 1 David Cook 2024-06-11 05:01:01 UTC
Looks like BibLibre might maintain that tool? 

Looks like Bywater will need to update their desktop app too.
Comment 2 David Cook 2024-06-11 05:02:10 UTC
Note that a short-term solution might be for them to use the SVC API.

(I don't include links anymore as Bugzilla always autobans me when I do, but you can find out more on the wiki for page "Koha /svc/ HTTP API")
Comment 3 Katrin Fischer 2024-06-25 09:31:22 UTC
Adding Claire, Matts and Frido to the bug, as they are listed here:
https://addons.mozilla.org/de/firefox/addon/koct/

And Kyle for the desktop application.

I believe the KOCT plugin is still used in a lot of libraries, I know ours do. 

I am not sure the svc is a great option, do we have others?
Comment 4 Jan Kissig 2024-07-15 11:49:19 UTC
With Bug 24401 (checkin endpoint) and Bug 37253 (checkout via barcode) it would be possible to use the API for KOCT.
Comment 5 David Cook 2024-10-14 22:44:04 UTC
So I'm not 100% sure what I meant by my earlier comments.

But what I should've meant was that the author of the Firefox add-on could use the SVC API to obtain a CSRF token and then do its existing POST.

Happy to collaborate with BibLibre on this one, if they want any clarification on what I mean here.
Comment 6 Matthias Meusburger 2024-10-16 08:31:05 UTC
Thanks David for the feedback!

You're thinking about https://wiki.koha-community.org/wiki/Koha_/svc/_HTTP_API#Changes_coming_in_Koha_24.05 ?

"This initial request generates a CGISESSID cookie linked to an anonymous session and provides a Csrf-Token in a response header."

Correct?
Comment 7 David Cook 2024-10-17 00:30:52 UTC
(In reply to Matthias Meusburger from comment #6)
> Thanks David for the feedback!
> 
> You're thinking about
> https://wiki.koha-community.org/wiki/Koha_/svc/
> _HTTP_API#Changes_coming_in_Koha_24.05 ?
> 
> "This initial request generates a CGISESSID cookie linked to an anonymous
> session and provides a Csrf-Token in a response header."
> 
> Correct?

More or less!

Basically, you do that first anonymous GET request to get the anonymous CGISESSID cookie and a Csrf-Token (via the HTTP response headers):

e.g. curl -v 'http://localhost:8081/cgi-bin/koha/svc/authentication' --cookie-jar /tmp/test.cookies

Then, using that same cookie and that Csrf-Token, you'll probably want to login using that endpoint as well (I'll explain more in a moment):

e.g. curl -v -H "Content-Type: application/x-www-form-urlencoded" -H "Csrf-Token: 4dda36756ec4f7ac178fb5500e1873b6f50cf9bc,699bc3e9fbb01ca7282bc8d847d8a018e696d327,1716342462" -XPOST -v 'http://localhost:8081/cgi-bin/koha/svc/authentication' -d "login_userid=koha&login_password=koha" --cookie /tmp/test.cookies --cookie-jar /tmp/test.cookies

Now the reason you'll probably want to do this is because you'll get a *NEW* authenticated CGISESSID cookie, and a *NEW* CSRF token.

You can use these *NEW* credentials for as long as the cookie and token are valid (the token is valid for up to 8 hours if I recall correctly). 

If you skip this step, you'll probably get stuck, since "offline/service.pl" doesn't appear to return a cookie or Csrf-Token. In other words, if you skip this login step, you'd have to repeat the 1st step for every "offline_circ/service.pl" request, and you don't want to do that.

Anyway, then, you should be able to do as many API requests as you want to that "offline_circ/service.pl" script. 

--

Let me know if that doesn't make sense. 

You can look at "./misc/migration_tools/koha-svc.pl" for some examples of what this looks like in practice. 

If you have a public git for it, I could also take a little look to make more in-context comments.
Comment 8 David Cook 2024-10-21 23:52:20 UTC
Matts, can we marked this one as assigned to you?
Comment 9 Matthias Meusburger 2024-10-23 06:26:07 UTC
I've assigned this ticket to myself, yes.