X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FAction%2FDeserialize%2FData%2FSerializer.pm;h=88823608e90b65125ce5bf09e77a84ecad5a001f;hb=3ed677b16323396b3479e6f32db337381665d653;hp=7044655db37dcdb19f76843d368556f8ff4f8ac0;hpb=e601addaf89882fccbc824c1a53328f0d049b32b;p=catagits%2FCatalyst-Action-REST.git diff --git a/lib/Catalyst/Action/Deserialize/Data/Serializer.pm b/lib/Catalyst/Action/Deserialize/Data/Serializer.pm index 7044655..8882360 100644 --- a/lib/Catalyst/Action/Deserialize/Data/Serializer.pm +++ b/lib/Catalyst/Action/Deserialize/Data/Serializer.pm @@ -1,17 +1,17 @@ -# -# Catalyst::Action::Deserialize::Data::Serializer.pm -# Created by: Adam Jacob, Marchex, -# Created on: 10/12/2006 03:00:32 PM PDT -# -# $Id$ - package Catalyst::Action::Deserialize::Data::Serializer; -use strict; -use warnings; +use Moose; +use namespace::autoclean; -use base 'Catalyst::Action'; +extends 'Catalyst::Action'; use Data::Serializer; +use Safe; +use Scalar::Util qw(openhandle); +my $compartment = Safe->new; +$compartment->permit_only( qw(padany null lineseq const pushmark list anonhash anonlist refgen leaveeval undef) ); + +our $VERSION = '0.99'; +$VERSION = eval $VERSION; sub execute { my $self = shift; @@ -24,33 +24,47 @@ sub execute { require $sp }; if ($@) { - $c->log->debug("Could not load $serializer, refusing to serialize: $@"); + $c->log->debug("Could not load $serializer, refusing to serialize: $@") + if $c->debug; return 0; } my $body = $c->request->body; if ($body) { - my $rbody; - if ( -f $c->request->body ) { - open( BODY, "<", $c->request->body ); - while ( my $line = ) { + my $rbody = ''; + + if(openhandle $body) { + seek($body, 0, 0); # in case something has already read from it + while ( defined( my $line = <$body> ) ) { $rbody .= $line; } - close(BODY); + } else { + $rbody = $body; } - my $dso = Data::Serializer->new( serializer => $serializer ); + my $rdata; - eval { - $rdata = $dso->raw_deserialize($rbody); - }; + if ( $serializer eq "Data::Dumper" ) { + # Taken from Data::Serialize::Data::Dumper::deserialize, but run within a Safe compartment + my $code = $rbody =~ /^\{/ ? "+".$rbody : $rbody; + $rdata = $compartment->reval( $code ); + } + else { + my $dso = Data::Serializer->new( serializer => $serializer ); + eval { + $rdata = $dso->raw_deserialize($rbody); + }; + } if ($@) { return $@; } $c->request->data($rdata); } else { $c->log->debug( - 'I would have deserialized, but there was nothing in the body!'); + 'I would have deserialized, but there was nothing in the body!') + if $c->debug; } return 1; } +__PACKAGE__->meta->make_immutable; + 1;