Skip to content

Commit b9aa5d9

Browse files
committed
use Data::Censor
1 parent 9ff8f34 commit b9aa5d9

File tree

3 files changed

+50
-70
lines changed

3 files changed

+50
-70
lines changed

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ requires 'Attribute::Handlers';
33
requires 'Carp';
44
requires 'Clone';
55
requires 'Config::Any';
6+
requires 'Data::Censor' => '0.04';
67
requires 'Digest::SHA';
78
requires 'Encode';
89
requires 'Exporter', '5.57';

lib/Dancer2/Core/Error.pm

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,14 @@ has censor => (
8282
return eval '\&'.$custom;
8383
}
8484

85-
# once Data::Censor has been updated with https://github.com/bigpresh/Data-Censor/pull/2
86-
# my $data_censor = use_module('Data::Censor')->new(
87-
# sensitive_fields => qr/pass|card?num|pan|secret/i,
88-
# replacement => "Hidden (looks potentially sensitive)",
89-
# );
90-
91-
# return sub {
92-
# $data_censor->censor(@_);
93-
# };
94-
95-
return \&_censor;
85+
my $data_censor = use_module('Data::Censor')->new(
86+
sensitive_fields => qr/pass|card?num|pan|secret/i,
87+
replacement => "Hidden (looks potentially sensitive)",
88+
);
89+
90+
return sub {
91+
$data_censor->censor(@_);
92+
};
9693
}
9794
);
9895

@@ -471,37 +468,6 @@ sub get_caller {
471468

472469
# private
473470

474-
# Given a hashref, censor anything that looks sensitive. Returns number of
475-
# items which were "censored".
476-
477-
sub _censor {
478-
my $hash = shift;
479-
my $visited = shift || {};
480-
481-
unless ( $hash && is_hashref($hash) ) {
482-
carp "_censor given incorrect input: $hash";
483-
return;
484-
}
485-
486-
my $censored = 0;
487-
for my $key ( keys %$hash ) {
488-
if ( is_hashref( $hash->{$key} ) ) {
489-
if (!$visited->{ $hash->{$key} }) {
490-
# mark the new ref as visited
491-
$visited->{ $hash->{$key} } = 1;
492-
493-
$censored += _censor( $hash->{$key}, $visited );
494-
}
495-
}
496-
elsif ( $key =~ /(pass|card?num|pan|secret)/i ) {
497-
$hash->{$key} = "Hidden (looks potentially sensitive)";
498-
$censored++;
499-
}
500-
}
501-
502-
return $censored;
503-
}
504-
505471
# Replaces the entities that are illegal in (X)HTML.
506472
sub _html_encode {
507473
my $value = shift;
@@ -573,12 +539,22 @@ The message of the error page.
573539
574540
=attr censor
575541
576-
The function to use to censor error messages. By default it uses the
577-
C<_censor> function of this package, but it can be configured via the
578-
app setting 'error_censor'. If provided, C<error_censor> has to be
579-
the fully qualified name of the censor function to use. That function is
580-
expected to take in the data as a hashref, modify it in place and return
581-
the number of items 'censored'.
542+
The function to use to censor error messages. By default it uses the C<censor> method of L<Data::Censor> C<_censor>"
543+
544+
# default censor function used by `error_censor`
545+
# is equivalent to
546+
sub MyApp::censor {
547+
Data::Censor->new(
548+
sensitive_fields => qr/pass|card?num|pan|secret/i,
549+
replacement => "Hidden (looks potentially sensitive)",
550+
)->censor(@_);
551+
}
552+
setting error_censor => 'MyApp::censor';
553+
554+
It can be configured via the app setting C<error_censor>. If provided,
555+
C<error_censor> has to be the fully qualified name of the censor
556+
function. That function is expected to take in the data as a hashref,
557+
modify it in place and return the number of items 'censored'.
582558
583559
For example, using L<Data::Censor>.
584560
@@ -613,6 +589,8 @@ L<Data::Censor> above could also have been done via the config
613589
- hush
614590
replacement: '(Sensitive data hidden)'
615591
592+
593+
616594
=method throw($response)
617595
618596
Populates the content of the response with the error's information.

t/error.t

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -259,37 +259,38 @@ subtest censor => sub {
259259
like $error->environment => qr/^.*password.*Hidden.*$/m, 'we say it is hidden';
260260
};
261261

262-
subtest 'custom censor()' => sub {
263-
my $app = Dancer2::Core::App->new( name => 'main' );
264-
my $error = Dancer2::Core::Error->new( app => $app );
262+
subtest 'custom censor' => sub {
265263

266-
$app->setting( hush => 'potato' );
264+
subtest 'via function string' => sub {
265+
my $app = Dancer2::Core::App->new( name => 'main' );
266+
my $error = Dancer2::Core::Error->new( app => $app );
267267

268-
$app->setting( error_censor => 'MyApp::Censor::censor' );
268+
$app->setting( hush => 'potato' );
269269

270-
unlike $error->environment => qr/potato/, 'the password is censored';
271-
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden';
272-
};
270+
$app->setting( error_censor => 'MyApp::Censor::censor' );
273271

274-
subtest 'custom censor via config' => sub {
275-
plan skip_all => "requires Data::Censor" unless require_module('Data::Censor');
272+
unlike $error->environment => qr/potato/, 'the password is censored';
273+
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden';
274+
};
276275

277-
my $app = Dancer2::Core::App->new( name => 'main' );
278-
$app->setting( 'error_censor' => {
279-
'Data::Censor' => {
280-
sensitive_fields => ['hush'],
281-
replacement => 'NOT TELLING',
282-
}
283-
});
276+
subtest 'via class hashref' => sub {
277+
my $app = Dancer2::Core::App->new( name => 'main' );
278+
$app->setting( 'error_censor' => {
279+
'Data::Censor' => {
280+
sensitive_fields => ['hush'],
281+
replacement => 'NOT TELLING',
282+
}
283+
});
284284

285-
my $error = Dancer2::Core::Error->new( app => $app );
285+
my $error = Dancer2::Core::Error->new( app => $app );
286286

287-
$app->setting( hush => 'potato' );
287+
$app->setting( hush => 'potato' );
288288

289-
unlike $error->environment => qr/potato/, 'the password is censored';
290-
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden';
291-
};
289+
unlike $error->environment => qr/potato/, 'the password is censored';
290+
like $error->environment => qr/^ .* hush .* NOT \s TELLING .* $/xm, 'we say it is hidden';
291+
};
292292

293+
}
293294
};
294295

295296

0 commit comments

Comments
 (0)