Skip to content

Commit

Permalink
Fix for unwanted modification of original data while dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
asergei committed Mar 24, 2014
1 parent 63b78bb commit a5ba2b6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
13 changes: 8 additions & 5 deletions lib/Dancer/Error.pm
Expand Up @@ -118,13 +118,16 @@ sub dumper {


# Take a copy of the data, so we can mask sensitive-looking stuff:
my %data = Dancer::ModuleLoader->load('Clone') ?
%{ Clone::clone($obj) } :
%$obj;
my $censored = _censor(\%data);
my $data = Dancer::ModuleLoader->load('Clone') ?
Clone::clone($obj)
: eval Data::Dumper->new([$obj])->Purity(1)->Terse(1)->Deepcopy(1)->Dump;

$data = {%$data} if blessed($data);

my $censored = _censor($data);

#use Data::Dumper;
my $dd = Data::Dumper->new([\%data]);
my $dd = Data::Dumper->new([$data]);
$dd->Terse(1)->Quotekeys(0)->Indent(1)->Sortkeys(1);
my $content = $dd->Dump();
$content =~ s{(\s*)(\S+)(\s*)=>}{$1<span class="key">$2</span>$3 =&gt;}g;
Expand Down
16 changes: 11 additions & 5 deletions t/12_response/10_error_dumper.t
Expand Up @@ -5,14 +5,14 @@ use Test::More;
use Dancer::Error;
use Dancer::ModuleLoader;

plan skip_all => 'Clone is required for this test'
unless Dancer::ModuleLoader->load('Clone');

plan tests => 4;
plan tests => 5;

my $error_obj = Dancer::Error->new(
code => '404',
pass => 'secret',
deep => {
pass => 'secret'
},
);

isa_ok( $error_obj, 'Dancer::Error' );
Expand All @@ -21,7 +21,7 @@ my $censored = $error_obj->dumper;

like(
$censored,
qr/\QNote: Values of 1 sensitive-looking key hidden\E/,
qr/\QNote: Values of 2 sensitive-looking keys hidden\E/,
'Data was censored in the output',
);

Expand All @@ -31,6 +31,12 @@ is(
'Original data was not overwritten',
);

is(
$error_obj->{'deep'}{'pass'},
'secret',
'Censoring of complex data structures works fine',
);

my %recursive;
$recursive{foo}{bar}{baz} = 1;
$recursive{foo}{bar}{oops} = $recursive{foo};
Expand Down

0 comments on commit a5ba2b6

Please sign in to comment.