File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed
Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,16 @@ Version 0.01
1111
1212our $VERSION = ' 0.01' ;
1313
14+ use v5.20;
1415use strict;
1516use warnings;
17+ use feature qw( signatures) ;
18+ no warnings qw( experimental::signatures) ;
1619
1720use Config::Abstraction;
1821use CGI::Info;
1922use Data::Dumper;
23+ use Digest::SHA qw( sha256_hex) ;
2024use File::Spec;
2125use Template::Filters;
2226use Template::Plugin::EnvHash;
@@ -438,6 +442,12 @@ sub http
438442 }
439443 }
440444
445+ # Generate CSRF token for forms
446+ if ($self -> {config }-> {security }-> {enable_csrf } // 1) {
447+ my $csrf_token = $self -> _generate_csrf_token();
448+ print " Set-Cookie: csrf_token=$csrf_token ; path=/; HttpOnly; SameSite=Strict\n " ;
449+ }
450+
441451 # Determine language, defaulting to English
442452 # TODO: Change the headers, e.g. character set, based on the language
443453 # my $language = $self->{_lingua} ? $self->{_lingua}->language() : 'English';
@@ -591,4 +601,15 @@ sub _types
591601 return @rc ;
592602}
593603
604+ sub _generate_csrf_token ($self ) {
605+ my $timestamp = time ();
606+ my $random = sprintf (" %08x" , int (rand (0xFFFFFFFF)));
607+ my $secret = $self -> {config }-> {security }-> {csrf_secret } // ' default_secret' ;
608+
609+ my $token_data = " $timestamp :$random " ;
610+ my $signature = sha256_hex(" $token_data :$secret " );
611+
612+ return " $token_data :$signature " ;
613+ }
614+
5946151;
You can’t perform that action at this time.
0 commit comments