Bug 40980

Summary: Clicking a search facet without logging in may trigger a cud-login error
Product: Koha Reporter: Pedro Amorim <pedro.amorim>
Component: Searching - ElasticsearchAssignee: Pedro Amorim <pedro.amorim>
Status: Pushed to stable --- QA Contact: Marcel de Rooy <m.de.rooy>
Severity: normal    
Priority: P5 - low CC: david, dcook, jonathan.druart, lucas, m.de.rooy, martin.renvoize, pedro.amorim
Version: Main   
Hardware: All   
OS: All   
GIT URL: Initiative type: ---
Sponsorship status: --- Comma delimited list of Sponsors:
Crowdfunding goal: 0 Patch complexity: Trivial patch
Documentation contact: Documentation submission:
Text to go in the release notes:
This fixes using facets in the OPAC for searching when not logged in, where Elasticsearch or OpenSearch is used as the search engine. In some circumstances, a 403 Forbidden Error was incorrectly generated (this is related to changes made in previous versions of Koha to improve form security).
Version(s) released in:
26.05.00,25.11.01
Circulation function:
Attachments: Bug 40980: Only consider AND|OR|NOT/i operators
Bug 40980: Only consider AND|OR|NOT/i operators
Bug 40980: Only consider AND|OR|NOT/i operators
Bug 40980: [ALTERNATIVE] Sanitize operators param
Bug 40980: [ALTERNATIVE] Sanitize operators param

Description Pedro Amorim 2025-10-09 08:29:31 UTC

    
Comment 1 Pedro Amorim 2025-10-09 08:33:08 UTC
Created attachment 187634 [details] [review]
Bug 40980: Only consider AND|OR|NOT/i operators

Test plan, k-t-d with elastic search:
1) Make sure you're logged out
2) Access a search results page directly e.g.:
http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=music
3) You're prompted to login, login.
4) You're shown the search results directly, click a facet result (any facet)
5) You get a Error 403:
Programming error - op 'cud-login' must not start with 'cud-' for GET
6) Apply patch, restart plack, repeat
Comment 2 David Nind 2025-11-08 03:52:08 UTC
Created attachment 189384 [details] [review]
Bug 40980: Only consider AND|OR|NOT/i operators

Test plan, k-t-d with elastic search:
1) Make sure you're logged out
2) Access a search results page directly e.g.:
http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=music
3) You're prompted to login, login.
4) You're shown the search results directly, click a facet result (any facet)
5) You get a Error 403:
Programming error - op 'cud-login' must not start with 'cud-' for GET
6) Apply patch, restart plack, repeat

Signed-off-by: David Nind <david@davidnind.com>
Comment 3 Pedro Amorim 2025-11-20 12:35:38 UTC
Created attachment 189766 [details] [review]
Bug 40980: Only consider AND|OR|NOT/i operators

Test plan, k-t-d with elastic search:
1) Make sure you're logged out
2) Access a search results page directly e.g.:
http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=music
3) You're prompted to login, login.
4) You're shown the search results directly, click a facet result (any facet)
5) You get a Error 403:
Programming error - op 'cud-login' must not start with 'cud-' for GET
6) Apply patch, restart plack, repeat

Signed-off-by: David Nind <david@davidnind.com>
Comment 4 Marcel de Rooy 2025-11-28 10:47:56 UTC
Please explain the relation of this otor with op=cud-login ?
Comment 5 Pedro Amorim 2025-11-28 12:43:10 UTC
(In reply to Marcel de Rooy from comment #4)
> Please explain the relation of this otor with op=cud-login ?

Hi Marcel yes, of course, this is what I shared with my colleagues internally when diagnosing/fixing this issue almost 2 months ago:

op=cud-login is always POSTed when logging in, but because for ElasticSearch an op may be AND OR or NOT it adds it to the facet link, thus causing it to be added to the URL as GET and causing the error. This is my fix for now, but there may be a better approach.

------

Upon looking at it again, my analysis remains the same, but I'll explain again phrasing it differently.
'op' is a param that ElasticSearch considers for building the search query (AND, OR or NOT), but 'op' is also what we use for POST cud-login (and other form submissions).
With the above in mind, in Koha, when we're logged out and attempt to access a URL with a GET parameter, e.g. http://localhost:8081/cgi-bin/koha/circ/circulation.pl?borrowernumber=19. We're prompted to login, and after login we're brought to the borrowernumber 19 page, but if we look at the URL, we see that it looks like this:
http://localhost:8081/cgi-bin/koha/circ/circulation.pl. This means borrowernumber=19 was passed onto circulation.pl as a POST parameter, not as GET.

This is what I think is happening to the ElasticSearch issue above, op='cud-login' is passed to search.pl as a POST param (can be confirmed by the network browser tab) which then causes the following code in facets.inc to add the op=cud-login to the facet link as a GET param:
[% SET url = "/cgi-bin/koha/catalogue/search.pl?" _ query_cgi _ limit_cgi %]

Because query_cgi contains the problematic op=cud-login, seemingly not distinguishing between GET and POST params, if it matters here at all.

I've explained the issue the best I can (and to the best of my knowledge), and I'm happy to discuss my patch further and/or other alternative patches.
Comment 6 Marcel de Rooy 2025-12-05 11:08:40 UTC
(In reply to Pedro Amorim from comment #5)
> (In reply to Marcel de Rooy from comment #4)
> > Please explain the relation of this otor with op=cud-login ?
> 
> Hi Marcel yes, of course, this is what I shared with my colleagues
> internally when diagnosing/fixing this issue almost 2 months ago:

Thx for explaining. I am seeing it in a debug print also when using Zebra. But there is no error.

I wonder if we should not just remove cud-login from the @operators in catalogue/search? It seems to be read twice btw.

Are you sure we are not passing other operators than AND, OR, NOT ?
Comment 7 Pedro Amorim 2025-12-09 13:23:56 UTC
Created attachment 190350 [details] [review]
Bug 40980: [ALTERNATIVE] Sanitize operators param

Ensure it does not contain 'cud-login'

Test plan, k-t-d with elastic search:
1) Make sure you're logged out
2) Access a search results page directly e.g.:
http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=music
3) You're prompted to login, login.
4) You're shown the search results directly, click a facet result (any facet)
5) You get a Error 403:
Programming error - op 'cud-login' must not start with 'cud-' for GET
6) Apply patch, restart plack, repeat
Comment 8 Pedro Amorim 2025-12-09 13:26:53 UTC
(In reply to Marcel de Rooy from comment #6)
> I wonder if we should not just remove cud-login from the @operators in
> catalogue/search? It seems to be read twice btw.
> 
> Are you sure we are not passing other operators than AND, OR, NOT ?

I am fairly certain though your question does highlight the fact that it would be yet another place that would need maintaining should the need of other possible come up.

I've provided an alternative patch, instead tackling the issue at catalogue/search as you've pointed out. Following the same test plan proves this alternative patch also fixes it. This alternative approach may indeed be more sensible, please let me know if you agree. Thanks for looking Marcel.
Comment 9 Marcel de Rooy 2025-12-09 15:12:06 UTC
Just for the record:

catalogue/search.pl:# operators include boolean and proximity operators and are used
opac/opac-search.pl:# operators include boolean and proximity operators and are used

Well, Zebra does not use those proximity operators definitely. Not sure about Elastic.
etc/zebradb/explain-biblios.xml:    <!-- <supports type="proximity"></supports> --> <!-- DEAD,  title = (house prox/distance=1/unit=word  the) FAILS -->
Comment 10 Marcel de Rooy 2025-12-09 15:13:50 UTC
Created attachment 190351 [details] [review]
Bug 40980: [ALTERNATIVE] Sanitize operators param

Ensure it does not contain 'cud-login'

Test plan, k-t-d with elastic search:
1) Make sure you're logged out
2) Access a search results page directly e.g.:
http://localhost:8081/cgi-bin/koha/catalogue/search.pl?q=music
3) You're prompted to login, login.
4) You're shown the search results directly, click a facet result (any facet)
5) You get a Error 403:
Programming error - op 'cud-login' must not start with 'cud-' for GET
6) Apply patch, restart plack, repeat

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Comment 11 Lucas Gass (lukeg) 2025-12-17 20:26:45 UTC
Nice work everyone!

Pushed to main for 26.05
Comment 12 Jacob O'Mara 2026-01-16 16:18:00 UTC
Thanks all, pushed to 25.11.x