Bug 38363 - get_template_and_user and checkauth don't use C4::Output for rendering auth pages
Summary: get_template_and_user and checkauth don't use C4::Output for rendering auth p...
Status: Pushed to main
Alias: None
Product: Koha
Classification: Unclassified
Component: Architecture, internals, and plumbing (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Lari Taskula
QA Contact: Jonathan Druart
URL:
Keywords:
Depends on: 41104
Blocks:
  Show dependency treegraph
 
Reported: 2024-11-05 23:24 UTC by David Cook
Modified: 2025-12-01 19:55 UTC (History)
3 users (show)

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


Attachments
Bug 38363: Use output_html_with_http_headers in C4::Auth (3.53 KB, patch)
2025-10-27 09:09 UTC, Lari Taskula
Details | Diff | Splinter Review
Bug 38363: Use output_html_with_http_headers in C4::Auth (3.78 KB, patch)
2025-10-27 12:47 UTC, Lari Taskula
Details | Diff | Splinter Review
Bug 38363: Use output_html_with_http_headers in C4::Auth (3.83 KB, patch)
2025-10-31 01:10 UTC, David Cook
Details | Diff | Splinter Review
Bug 38363: Use output_html_with_http_headers in C4::Auth (3.91 KB, patch)
2025-11-14 14:39 UTC, Jonathan Druart
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description David Cook 2024-11-05 23:24:21 UTC
I was testing the output of HTTP headers using C4::Output::output_html_with_http_headers, and I was going crazy wondering why my headers weren't coming through on http://localhost:8081

And the reason is that authentication pages don't use this function! 

C4::Auth::checkauth and C4::Auth::get_template_and_user do their our outputs which leads to duplication.
Comment 1 Lari Taskula 2025-10-27 09:09:39 UTC Comment hidden (obsolete)
Comment 2 Lari Taskula 2025-10-27 09:21:10 UTC
After this patch we now set some response headers that were previously unset in these two C4::Auth cases (see commit message).

- Status: 200 OK (since we are dealing with failed logins and permission issues it should really be either 401 or 403. But since we were not explicitly setting a 4xx response code before either, I see this being outside the scope of this Bug)
- Charset from lowercase utf-8 -> uppercase UTF-8 (probably does not matter)
- Cache-control and Pragma: no-cache
- Content-Style-Type: text/css
- Content-Script-Type: text/javascript
- Access-Control-Allow-Origin: value of preference 'AccessControlAllowOrigin'
Comment 3 Lari Taskula 2025-10-27 11:57:05 UTC
Looking at git history, I noticed that we have attempted to use output_html_with_http_headers here before (Bug 10074), but the change was reverted.

https://github.com/Koha-Community/Koha/commit/3536b10acf223b33819153532841494c816d6375
Comment 4 Lari Taskula 2025-10-27 12:47:48 UTC
Created attachment 188475 [details] [review]
Bug 38363: Use output_html_with_http_headers in C4::Auth

The following C4::Auth subroutines
  - checkauth()
  - get_template_and_user()

were in some cases outputting html outside of C4::Output's output mechanisms.

It is important to ensure the consistency of our HTTP responses. Having
multiple output mechanisms for what is essentialy the same type of response not
only leads to unneccessary code duplication and complexity but possibly even
bugs at some point in the future.

For these reasons it is better to output only through C4::Output, when possible.

This patch does that for the two aforementioned C4::Auth cases.

To test unit tests:
1. prove t/db_dependent/Auth.t

As for checkauth(), the change only applies to unauthenticated state and failed
logins.

To test checkauth:
1. Enable system preference opacuserlogin
2. Navigate to OPAC
3. Observe OPAC loading successfully
4. Log in to OPAC with invalid credentials
5. Observe "You entered an incorrect username or password"
6. Navigate to staff client without being logged in (logout if you are in)
7. Observe staff client login screen loading successfully
8. Log in to staff client with invalid credentials
9. Observe "Invalid username or password"

As for get_template_and_user(), the change only applies to SCO/SCI user
navigating outside of the self service modules.

To test get_template_and_user:
1. Enable system preference WebBasedSelfCheck
2. Create a new test patron and note down their username & password
3. Grant them a permission self_checkout_module (under Self check modules)
4. Navigate to http://OPAC/cgi-bin/koha/sco/sco-main.pl
5. Log in with the test patron
6. Observe successful login (should display "Self-checkout system" page)
7. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
8. Observe "Log in to your account" page, ie. you have been kicked out
9. Modify your test patron's permissions
10. Remove permission "self_checkout_module" and add "self_checkin_module"
11. Enable system preference SelfCheckInModule
12. Navigate to http://OPAC/cgi-bin/koha/sci/sci-main.pl
13. Log in using your test patron's credentials
14. Observe "Self check-in" page
15. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
16. Observe "Log in to your account" page, ie. you have been kicked out
Comment 5 Lari Taskula 2025-10-27 12:56:51 UTC
(In reply to Lari Taskula from comment #3)
> Looking at git history, I noticed that we have attempted to use
> output_html_with_http_headers here before (Bug 10074), but the change was
> reverted.
> 
> https://github.com/Koha-Community/Koha/commit/
> 3536b10acf223b33819153532841494c816d6375
There's plenty of depth into this topic (Bug 6554 and encoding issues). It's a bit too much for me at this moment.

It seems Bug 10074 was reverted as a part of larger set of reverts (Bug 6554 comment 103 , https://github.com/Koha-Community/Koha/commits/16753c750d97385ad243329aa6a1328352749bb1/).
Comment 6 David Cook 2025-10-31 01:00:54 UTC
Don't know that this needs a dependency on bug 41104?
Comment 7 David Cook 2025-10-31 01:01:55 UTC
(In reply to Lari Taskula from comment #5)
> (In reply to Lari Taskula from comment #3)
> > Looking at git history, I noticed that we have attempted to use
> > output_html_with_http_headers here before (Bug 10074), but the change was
> > reverted.
> > 
> > https://github.com/Koha-Community/Koha/commit/
> > 3536b10acf223b33819153532841494c816d6375
> There's plenty of depth into this topic (Bug 6554 and encoding issues). It's
> a bit too much for me at this moment.
> 
> It seems Bug 10074 was reverted as a part of larger set of reverts (Bug 6554
> comment 103 ,
> https://github.com/Koha-Community/Koha/commits/
> 16753c750d97385ad243329aa6a1328352749bb1/).

I've tried out the interface using es-ES and I don't see any encoding problems.
Comment 8 David Cook 2025-10-31 01:08:00 UTC
(In reply to Lari Taskula from comment #4)
> 12. Navigate to http://OPAC/cgi-bin/koha/sci/sci-main.pl
> 13. Log in using your test patron's credentials
> 14. Observe "Self check-in" page
> 15. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
> (you can get there by editing the address bar of your web browser)
> 16. Observe "Log in to your account" page, ie. you have been kicked out

I didn't get kicked out, but that's because I was using a superlibrarian (as I wasn't perfecting following the test plan). Looks like the SCI has different parameters than SCO... interesting. But that's a pre-existing thing.
Comment 9 David Cook 2025-10-31 01:10:25 UTC
Created attachment 188722 [details] [review]
Bug 38363: Use output_html_with_http_headers in C4::Auth

The following C4::Auth subroutines
  - checkauth()
  - get_template_and_user()

were in some cases outputting html outside of C4::Output's output mechanisms.

It is important to ensure the consistency of our HTTP responses. Having
multiple output mechanisms for what is essentialy the same type of response not
only leads to unneccessary code duplication and complexity but possibly even
bugs at some point in the future.

For these reasons it is better to output only through C4::Output, when possible.

This patch does that for the two aforementioned C4::Auth cases.

To test unit tests:
1. prove t/db_dependent/Auth.t

As for checkauth(), the change only applies to unauthenticated state and failed
logins.

To test checkauth:
1. Enable system preference opacuserlogin
2. Navigate to OPAC
3. Observe OPAC loading successfully
4. Log in to OPAC with invalid credentials
5. Observe "You entered an incorrect username or password"
6. Navigate to staff client without being logged in (logout if you are in)
7. Observe staff client login screen loading successfully
8. Log in to staff client with invalid credentials
9. Observe "Invalid username or password"

As for get_template_and_user(), the change only applies to SCO/SCI user
navigating outside of the self service modules.

To test get_template_and_user:
1. Enable system preference WebBasedSelfCheck
2. Create a new test patron and note down their username & password
3. Grant them a permission self_checkout_module (under Self check modules)
4. Navigate to http://OPAC/cgi-bin/koha/sco/sco-main.pl
5. Log in with the test patron
6. Observe successful login (should display "Self-checkout system" page)
7. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
8. Observe "Log in to your account" page, ie. you have been kicked out
9. Modify your test patron's permissions
10. Remove permission "self_checkout_module" and add "self_checkin_module"
11. Enable system preference SelfCheckInModule
12. Navigate to http://OPAC/cgi-bin/koha/sci/sci-main.pl
13. Log in using your test patron's credentials
14. Observe "Self check-in" page
15. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
16. Observe "Log in to your account" page, ie. you have been kicked out

Signed-off-by: David Cook <dcook@prosentient.com.au>
Comment 10 David Cook 2025-10-31 01:10:52 UTC
Looks good to me. Thanks, Lari!
Comment 11 Lari Taskula 2025-10-31 07:51:11 UTC
(In reply to David Cook from comment #6)
> Don't know that this needs a dependency on bug 41104?

This Bug 38363 modifies the same line as Bug 41104. In fact I initially discovered the bug in Bug 41104 while working on this Bug 38363, but I decided to fix it in another Bug 41104.
Comment 12 David Cook 2025-11-03 00:00:31 UTC
(In reply to Lari Taskula from comment #11)
> (In reply to David Cook from comment #6)
> > Don't know that this needs a dependency on bug 41104?
> 
> This Bug 38363 modifies the same line as Bug 41104. In fact I initially
> discovered the bug in Bug 41104 while working on this Bug 38363, but I
> decided to fix it in another Bug 41104.

Yeah I thought about that a bit more later. I guess it depends on how QA treats it. Hopefully both get done at the same time.
Comment 13 Jonathan Druart 2025-11-14 14:39:14 UTC
Created attachment 189617 [details] [review]
Bug 38363: Use output_html_with_http_headers in C4::Auth

The following C4::Auth subroutines
  - checkauth()
  - get_template_and_user()

were in some cases outputting html outside of C4::Output's output mechanisms.

It is important to ensure the consistency of our HTTP responses. Having
multiple output mechanisms for what is essentialy the same type of response not
only leads to unneccessary code duplication and complexity but possibly even
bugs at some point in the future.

For these reasons it is better to output only through C4::Output, when possible.

This patch does that for the two aforementioned C4::Auth cases.

To test unit tests:
1. prove t/db_dependent/Auth.t

As for checkauth(), the change only applies to unauthenticated state and failed
logins.

To test checkauth:
1. Enable system preference opacuserlogin
2. Navigate to OPAC
3. Observe OPAC loading successfully
4. Log in to OPAC with invalid credentials
5. Observe "You entered an incorrect username or password"
6. Navigate to staff client without being logged in (logout if you are in)
7. Observe staff client login screen loading successfully
8. Log in to staff client with invalid credentials
9. Observe "Invalid username or password"

As for get_template_and_user(), the change only applies to SCO/SCI user
navigating outside of the self service modules.

To test get_template_and_user:
1. Enable system preference WebBasedSelfCheck
2. Create a new test patron and note down their username & password
3. Grant them a permission self_checkout_module (under Self check modules)
4. Navigate to http://OPAC/cgi-bin/koha/sco/sco-main.pl
5. Log in with the test patron
6. Observe successful login (should display "Self-checkout system" page)
7. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
8. Observe "Log in to your account" page, ie. you have been kicked out
9. Modify your test patron's permissions
10. Remove permission "self_checkout_module" and add "self_checkin_module"
11. Enable system preference SelfCheckInModule
12. Navigate to http://OPAC/cgi-bin/koha/sci/sci-main.pl
13. Log in using your test patron's credentials
14. Observe "Self check-in" page
15. Navigate to http://OPAC/cgi-bin/koha/opac-main.pl
(you can get there by editing the address bar of your web browser)
16. Observe "Log in to your account" page, ie. you have been kicked out

Signed-off-by: David Cook <dcook@prosentient.com.au>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Comment 14 Lucas Gass (lukeg) 2025-12-01 19:55:08 UTC
Nice work everyone!

Pushed to main for 25.11