Bugzilla – Attachment 193302 Details for
Bug 38365
Add Content-Security-Policy HTTP header to HTML responses
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 38365: Add default log4perl configuration
Bug-38365-Add-default-log4perl-configuration.patch (text/plain), 8.14 KB, created by
David Cook
on 2026-02-17 05:53:20 UTC
(
hide
)
Description:
Bug 38365: Add default log4perl configuration
Filename:
MIME Type:
Creator:
David Cook
Created:
2026-02-17 05:53:20 UTC
Size:
8.14 KB
patch
obsolete
>From 93c7ff2117e3794ab9fd3e1e8aac4c8d4319bbf1 Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@hypernova.fi> >Date: Thu, 5 Feb 2026 10:44:33 +0200 >Subject: [PATCH] Bug 38365: Add default log4perl configuration > >This patch adds the default configuration to log4perl.conf. > >Note we are passing a non-standard interface 'csp' to Koha::Logger. This is to >avoid log entries from propagating up to api-error.log. > >To test: >1. prove t/db_dependent/api/v1/public/csp_reports.t >--- > Koha/REST/V1/CSPReports.pm | 2 +- > debian/templates/log4perl-site.conf.in | 16 ++++ > etc/log4perl.conf | 16 ++++ > t/db_dependent/api/v1/public/csp_reports.t | 92 +++++++++++++++++++++- > 4 files changed, 124 insertions(+), 2 deletions(-) > >diff --git a/Koha/REST/V1/CSPReports.pm b/Koha/REST/V1/CSPReports.pm >index 874ea9144e8..0040208852b 100644 >--- a/Koha/REST/V1/CSPReports.pm >+++ b/Koha/REST/V1/CSPReports.pm >@@ -51,7 +51,7 @@ sub add { > # CSP reports come wrapped in a 'csp-report' key > my $csp_report = $report->{'csp-report'} // $report; > >- my $logger = Koha::Logger->get( { interface => 'api', category => 'csp' } ); >+ my $logger = Koha::Logger->get( { interface => 'csp' } ); > > # Extract key fields for logging > my $document_uri = $csp_report->{'document-uri'} // 'unknown'; >diff --git a/debian/templates/log4perl-site.conf.in b/debian/templates/log4perl-site.conf.in >index f6587541e9a..c45b4d48ef6 100644 >--- a/debian/templates/log4perl-site.conf.in >+++ b/debian/templates/log4perl-site.conf.in >@@ -76,3 +76,19 @@ log4perl.appender.EDI.mode=append > log4perl.appender.EDI.layout=PatternLayout > log4perl.appender.EDI.layout.ConversionPattern=[%d] [%P] [%p] %X{accountid}@%X{peeraddr}: %m %l%n > log4perl.appender.EDI.utf8=1 >+ >+log4perl.logger.csp = WARN, CSP >+log4perl.appender.CSP=Log::Log4perl::Appender::File >+log4perl.appender.CSP.filename=__LOG_DIR__/csp-violations.log >+log4perl.appender.CSP.mode=append >+log4perl.appender.CSP.layout=PatternLayout >+log4perl.appender.CSP.layout.ConversionPattern=[%d] %m%n >+log4perl.appender.CSP.utf8=1 >+ >+log4perl.logger.plack-csp = WARN, PLACKCSP >+log4perl.appender.PLACKCSP=Log::Log4perl::Appender::File >+log4perl.appender.PLACKCSP.filename=__LOG_DIR__/plack-csp-violations.log >+log4perl.appender.PLACKCSP.mode=append >+log4perl.appender.PLACKCSP.layout=PatternLayout >+log4perl.appender.PLACKCSP.layout.ConversionPattern=[%d] %m%n >+log4perl.appender.PLACKCSP.utf8=1 >diff --git a/etc/log4perl.conf b/etc/log4perl.conf >index 9c16c523487..4214c1e17c5 100644 >--- a/etc/log4perl.conf >+++ b/etc/log4perl.conf >@@ -76,3 +76,19 @@ log4perl.appender.EDI.mode=append > log4perl.appender.EDI.layout=PatternLayout > log4perl.appender.EDI.layout.ConversionPattern=[%d] [%P] [%p] %X{accountid}@%X{peeraddr}: %m %l%n > log4perl.appender.EDI.utf8=1 >+ >+log4perl.logger.csp = WARN, CSP >+log4perl.appender.CSP=Log::Log4perl::Appender::File >+log4perl.appender.CSP.filename=__LOG_DIR__/csp-violations.log >+log4perl.appender.CSP.mode=append >+log4perl.appender.CSP.layout=PatternLayout >+log4perl.appender.CSP.layout.ConversionPattern=[%d] %m%n >+log4perl.appender.CSP.utf8=1 >+ >+log4perl.logger.plack-csp = WARN, PLACKCSP >+log4perl.appender.PLACKCSP=Log::Log4perl::Appender::File >+log4perl.appender.PLACKCSP.filename=__LOG_DIR__/plack-csp-violations.log >+log4perl.appender.PLACKCSP.mode=append >+log4perl.appender.PLACKCSP.layout=PatternLayout >+log4perl.appender.PLACKCSP.layout.ConversionPattern=[%d] %m%n >+log4perl.appender.PLACKCSP.utf8=1 >diff --git a/t/db_dependent/api/v1/public/csp_reports.t b/t/db_dependent/api/v1/public/csp_reports.t >index 3be9bd845af..40da813de6a 100755 >--- a/t/db_dependent/api/v1/public/csp_reports.t >+++ b/t/db_dependent/api/v1/public/csp_reports.t >@@ -22,6 +22,9 @@ use Test::More tests => 2; > use Test::Mojo; > use Test::Warn; > >+use Carp qw( carp ); >+use Log::Log4perl; >+ > use t::lib::Mocks; > > use Koha::Database; >@@ -33,7 +36,7 @@ t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); > > subtest 'add() tests' => sub { > >- plan tests => 6; >+ plan tests => 7; > > $schema->storage->txn_begin; > >@@ -74,6 +77,93 @@ subtest 'add() tests' => sub { > $t->post_ok( '/api/v1/public/csp-reports' => json => $minimal_report ) > ->status_is( 204, 'Minimal CSP report accepted' ); > >+ subtest 'make sure log entries are being written' => sub { >+ plan tests => 17; >+ >+ my $log4perl_conf_file = C4::Context->config('intranetdir') . '/etc/log4perl.conf'; >+ open my $fh, '<:encoding(UTF-8)', $log4perl_conf_file or do { >+ carp "Cannot read $log4perl_conf_file: $!"; >+ return; >+ }; >+ my $log4perl_conf_content = ""; >+ while (<$fh>) { >+ my $line = $_; >+ next unless $line =~ /^(log4perl.logger.(plack-)?csp)|(log4perl.appender.(PLACK)?CSP)/; >+ $log4perl_conf_content .= "$line"; >+ } >+ close $fh; >+ like( $log4perl_conf_content, qr/__LOG_DIR__\/csp-violations\.log/, 'csp-violations.log is defined' ); >+ like( >+ $log4perl_conf_content, qr/__LOG_DIR__\/plack-csp-violations\.log/, >+ 'plack-csp-violations.log is defined' >+ ); >+ like( >+ $log4perl_conf_content, qr/log4perl.appender.CSP=Log::Log4perl::Appender::File/, >+ 'CSP: File appender exists' >+ ); >+ like( >+ $log4perl_conf_content, qr/log4perl.appender.PLACKCSP=Log::Log4perl::Appender::File/, >+ 'PLACKCSP: File appender exists' >+ ); >+ $log4perl_conf_content =~ s/Log::Log4perl::Appender::File/Log::Log4perl::Appender::TestBuffer/g; >+ Log::Log4perl::init( \$log4perl_conf_content ); >+ my $appenders = Log::Log4perl->appenders; >+ my $appender = Log::Log4perl->appenders->{CSP}; >+ my $appenderplack = Log::Log4perl->appenders->{PLACKCSP}; >+ >+ is( $appender->buffer, '', 'Nothing in log buffer yet' ); >+ is( $appenderplack->buffer, '', 'Nothing in plack log buffer yet' ); >+ >+ $t->post_ok( >+ '/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) >+ ->status_is( 204, 'CSP report accepted' ); >+ >+ my $expected_log_entry = sprintf( >+ "CSP violation: '%s' blocked '%s' on page '%s'%s", >+ $csp_report->{'csp-report'}->{'violated-directive'}, >+ $csp_report->{'csp-report'}->{'blocked-uri'}, >+ $csp_report->{'csp-report'}->{'document-uri'}, >+ ' at ' >+ . $csp_report->{'csp-report'}->{'source-file'} . ':' >+ . $csp_report->{'csp-report'}->{'line-number'} . ':' >+ . $csp_report->{'csp-report'}->{'column-number'} >+ ); >+ >+ like( >+ $appender->buffer, qr/$expected_log_entry/ >+ , 'Log contains expected log entry' >+ ); >+ is( $appenderplack->buffer, '', 'Nothing in plack log buffer yet' ); >+ $appender->clear(); >+ >+ $ENV{'plack.is.enabled.for.this.test'} = 1; # tricking C4::Context->psgi_env >+ $t->post_ok( >+ '/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) >+ ->status_is( 204, 'CSP report accepted' ); >+ like( >+ $appenderplack->buffer, qr/$expected_log_entry/ >+ , 'Plack log contains expected log entry' >+ ); >+ is( $appender->buffer, '', 'Nothing in log buffer yet' ); >+ >+ Log::Log4perl::init( >+ \<<"HERE" >+log4perl.logger.api = WARN, API >+log4perl.appender.API=Log::Log4perl::Appender::TestBuffer >+log4perl.appender.API.mode=append >+log4perl.appender.API.layout=PatternLayout >+log4perl.appender.API.layout.ConversionPattern=[%d] [%p] %m %l%n >+log4perl.appender.API.utf8=1 >+HERE >+ ); >+ $appender = Log::Log4perl->appenders()->{API}; >+ $t->post_ok( >+ '/api/v1/public/csp-reports' => { 'Content-Type' => 'application/csp-report' } => json => $csp_report ) >+ ->status_is( 204, 'CSP report returns 204 even when no CSP loggers are defined' ); >+ is( $appender->buffer, '', 'Nothing in the only defined log buffer, because it is unrelated to CSP' ); >+ >+ }; >+ > $schema->storage->txn_rollback; > }; > >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 38365
:
174054
|
188487
|
188488
|
188489
|
188490
|
188491
|
188733
|
188734
|
188735
|
188736
|
188737
|
188881
|
188882
|
189465
|
189466
|
189467
|
189468
|
189469
|
189470
|
189471
|
189472
|
191618
|
191619
|
191620
|
191621
|
191622
|
191623
|
191660
|
191661
|
191662
|
191663
|
191664
|
191665
|
191666
|
191667
|
191668
|
192294
|
192295
|
192296
|
192297
|
192298
|
192299
|
192300
|
192301
|
192302
|
192303
|
192304
|
192305
|
192306
|
192412
|
192413
|
192414
|
192415
|
192416
|
192417
|
192418
|
192419
|
192420
|
192421
|
192422
|
192423
|
192424
|
192425
|
192426
|
192427
|
192428
|
192429
|
192524
|
192535
|
192536
|
192537
|
192553
|
192554
|
192555
|
192556
|
192557
|
192558
|
192559
|
192560
|
192561
|
192562
|
192563
|
192564
|
192565
|
192566
|
192567
|
192568
|
192569
|
192570
|
192571
|
192572
|
192573
|
192574
|
192684
|
192685
|
192686
|
192691
|
192692
|
192693
|
192694
|
192695
|
192696
|
192697
|
192698
|
192699
|
192700
|
192701
|
192702
|
192703
|
192704
|
192705
|
192706
|
192707
|
192708
|
192709
|
192738
|
192843
|
192844
|
192934
|
192935
|
192936
|
192937
|
193288
|
193289
|
193290
|
193291
|
193292
|
193293
|
193294
|
193295
|
193296
|
193297
|
193298
|
193299
|
193300
|
193301
|
193302
|
193303
|
193304
|
193305
|
193306
|
193307
|
193308
|
193309
|
193310
|
193311
|
193312
|
193335
|
193705
|
193706
|
193707
|
193708
|
193709
|
193710
|
193711
|
193712
|
193713
|
193714
|
193941
|
193942
|
193943
|
193944
|
193945
|
193946
|
193947
|
193948
|
193949
|
193950
|
193951
|
193952
|
193953
|
193954
|
193955
|
193956
|
193957
|
193958
|
193959
|
193960
|
193961
|
193962
|
193963
|
193964
|
193965
|
193966
|
193967
|
193968