summaryrefslogtreecommitdiff
path: root/lib/Log/Any/Proxy
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Log/Any/Proxy')
-rw-r--r--lib/Log/Any/Proxy/Null.pm4
-rw-r--r--lib/Log/Any/Proxy/Test.pm4
-rw-r--r--lib/Log/Any/Proxy/WithStackTrace.pm209
3 files changed, 178 insertions, 39 deletions
diff --git a/lib/Log/Any/Proxy/Null.pm b/lib/Log/Any/Proxy/Null.pm
index edc0c3c..b0e776d 100644
--- a/lib/Log/Any/Proxy/Null.pm
+++ b/lib/Log/Any/Proxy/Null.pm
@@ -5,7 +5,7 @@ use warnings;
package Log::Any::Proxy::Null;
# ABSTRACT: Log::Any generator proxy for no adapters
-our $VERSION = '1.715';
+our $VERSION = '1.717';
use Log::Any::Adapter::Util ();
use Log::Any::Proxy;
@@ -57,7 +57,7 @@ Log::Any::Proxy::Null - Log::Any generator proxy for no adapters
=head1 VERSION
-version 1.715
+version 1.717
=head1 AUTHORS
diff --git a/lib/Log/Any/Proxy/Test.pm b/lib/Log/Any/Proxy/Test.pm
index f8e2a99..c9ef9a7 100644
--- a/lib/Log/Any/Proxy/Test.pm
+++ b/lib/Log/Any/Proxy/Test.pm
@@ -4,7 +4,7 @@ use warnings;
package Log::Any::Proxy::Test;
-our $VERSION = '1.715';
+our $VERSION = '1.717';
use Log::Any::Proxy;
our @ISA = qw/Log::Any::Proxy/;
@@ -42,7 +42,7 @@ Log::Any::Proxy::Test
=head1 VERSION
-version 1.715
+version 1.717
=head1 AUTHORS
diff --git a/lib/Log/Any/Proxy/WithStackTrace.pm b/lib/Log/Any/Proxy/WithStackTrace.pm
index 3f4389d..f9dcd7c 100644
--- a/lib/Log/Any/Proxy/WithStackTrace.pm
+++ b/lib/Log/Any/Proxy/WithStackTrace.pm
@@ -5,26 +5,31 @@ use warnings;
package Log::Any::Proxy::WithStackTrace;
# ABSTRACT: Log::Any proxy to upgrade string errors to objects with stack traces
-our $VERSION = '1.715';
+our $VERSION = '1.717';
use Log::Any::Proxy;
our @ISA = qw/Log::Any::Proxy/;
use Devel::StackTrace 2.00;
use Log::Any::Adapter::Util ();
+use Scalar::Util qw/blessed reftype/;
use overload;
#pod =head1 SYNOPSIS
#pod
#pod use Log::Any qw( $log, proxy_class => 'WithStackTrace' );
#pod
-#pod # Some adapter that knows how to handle both structured data,
-#pod # and log messages which are actually objects with a
-#pod # "stack_trace" method:
-#pod #
+#pod # Allow stack trace call stack arguments to be logged:
+#pod use Log::Any qw( $log, proxy_class => 'WithStackTrace',
+#pod proxy_show_stack_trace_args => 1 );
+#pod
+#pod # Configure some adapter that knows how to:
+#pod # 1) handle structured data, and
+#pod # 2) handle message objects which have a "stack_trace" method:
#pod Log::Any::Adapter->set($adapter);
#pod
-#pod $log->error("Help!"); # stack trace gets automatically added
+#pod $log->error("Help!"); # stack trace gets automatically added,
+#pod # starting from this line of code
#pod
#pod =head1 DESCRIPTION
#pod
@@ -35,12 +40,20 @@ use overload;
#pod relative to where the log adapter was called, and not relative to where
#pod the logging method was originally called.
#pod
-#pod With this proxy in place, if any logging method is called with a message
-#pod that is a non-reference scalar, that message will be upgraded into a
-#pod C<Log::Any::MessageWithStackTrace> object with a C<stack_trace> method,
-#pod and that method will return a trace relative to where the logging method
-#pod was called. A string overload is provided on the object to return the
-#pod original message.
+#pod With this proxy in place, if any logging method is called with a log
+#pod message that is a non-reference scalar (i.e. a string), that log message
+#pod will be upgraded into a C<Log::Any::MessageWithStackTrace> object with a
+#pod C<stack_trace> method, and that method will return a trace relative to
+#pod where the logging method was called. A string overload is provided on
+#pod the object to return the original log message.
+#pod
+#pod Additionally, any call stack arguments in the stack trace will be
+#pod deleted before logging, to avoid accidentally logging sensitive data.
+#pod This happens both for message objects that were auto-generated from
+#pod string messages, as well as for message objects that were passed in
+#pod directly (if they appear to have a stack trace method). This default
+#pod argument scrubbing behavior can be turned off by specifying a true value
+#pod for the C<proxy_show_stack_trace_args> import flag.
#pod
#pod B<Important:> This proxy should be used with a L<Log::Any::Adapter> that
#pod is configured to handle structured data. Otherwise the object created
@@ -55,17 +68,16 @@ use overload;
use overload '""' => \&stringify;
- use Carp qw( croak );
-
sub new
{
- my ($class, $message) = @_;
- croak 'no "message"' unless defined $message;
+ my ($class, $message, %opts) = @_;
+
return bless {
message => $message,
stack_trace => Devel::StackTrace->new(
# Filter e.g "Log::Any::Proxy", "My::Log::Any::Proxy", etc.
ignore_package => [ qr/(?:^|::)Log::Any(?:::|$)/ ],
+ no_args => $opts{no_args},
),
}, $class;
}
@@ -79,29 +91,89 @@ use overload;
#pod
#pod =head2 maybe_upgrade_with_stack_trace
#pod
-#pod This is an internal use method that will convert a non-reference scalar
+#pod @args = $self->maybe_upgrade_with_stack_trace(@args);
+#pod
+#pod This is an internal-use method that will convert a non-reference scalar
#pod message into a C<Log::Any::MessageWithStackTrace> object with a
#pod C<stack_trace> method. A string overload is provided to return the
#pod original message.
#pod
+#pod Stack trace args are scrubbed out in case they contain sensitive data,
+#pod unless the C<proxy_show_stack_trace_args> option has been set.
+#pod
#pod =cut
sub maybe_upgrade_with_stack_trace
{
my ($self, @args) = @_;
- # Only want a non-ref arg, optionally followed by a structured data
- # context hashref:
- #
+ # We expect a message, optionally followed by a structured data
+ # context hashref. Bail if we get anything other than that rather
+ # than guess what the caller might be trying to do:
return @args unless @args == 1 ||
( @args == 2 && ref $args[1] eq 'HASH' );
- return @args if ref $args[0];
- $args[0] = Log::Any::MessageWithStackTrace->new($args[0]);
+ if (ref $args[0]) {
+ $self->maybe_delete_stack_trace_args($args[0])
+ unless $self->{proxy_show_stack_trace_args};
+ }
+ else {
+ $args[0] = Log::Any::MessageWithStackTrace->new(
+ $args[0],
+ no_args => !$self->{proxy_show_stack_trace_args},
+ );
+ }
return @args;
}
+#pod =head2 maybe_delete_stack_trace_args
+#pod
+#pod $self->maybe_delete_stack_trace_args($arg);
+#pod
+#pod This is an internal-use method that, given a single argument that is a
+#pod reference, tries to figure out whether the argument is an object with a
+#pod stack trace, and if so tries to delete any stack trace args.
+#pod
+#pod The logic is based on L<Devel::StackTrace::Extract>.
+#pod
+#pod It specifically looks for objects with a C<stack_trace> method (which
+#pod should catch anything that does L<StackTrace::Auto>, including anything
+#pod that does L<Throwable::Error>), or a C<trace> method (used by
+#pod L<Exception::Class> and L<Moose::Exception> and friends).
+#pod
+#pod It specifically ignores L<Mojo::Exception> objects, because their stack
+#pod traces don't contain any call stack args.
+#pod
+#pod =cut
+
+sub maybe_delete_stack_trace_args
+{
+ my ($self, $arg) = @_;
+
+ return unless blessed $arg;
+
+ if ($arg->can('stack_trace')) {
+ # This should catch anything that does StackTrace::Auto,
+ # including anything that does Throwable::Error.
+ my $trace = $arg->stack_trace;
+ $self->delete_args_from_stack_trace($trace);
+ }
+ elsif ($arg->isa('Mojo::Exception')) {
+ # Skip these, they don't have args in their stack traces.
+ }
+ elsif ($arg->can('trace')) {
+ # This should catch Exception::Class and Moose::Exception and
+ # friends. Make sure to check for the "trace" method *after*
+ # skipping the Mojo::Exception objects, because those also have
+ # a "trace" method.
+ my $trace = $arg->trace;
+ $self->delete_args_from_stack_trace($trace);
+ }
+
+ return;
+}
+
my %aliases = Log::Any::Adapter::Util::log_level_aliases();
# Set up methods/aliases and detection methods/aliases
@@ -118,6 +190,30 @@ foreach my $name ( Log::Any::Adapter::Util::logging_methods(), keys(%aliases) )
};
}
+#pod =head2 delete_args_from_stack_trace($trace)
+#pod
+#pod $self->delete_args_from_stack_trace($trace)
+#pod
+#pod To scrub potentially sensitive data from C<Devel::StackTrace> arguments,
+#pod this method deletes arguments from all of the C<Devel::StackTrace::Frame>
+#pod in the trace.
+#pod
+#pod =cut
+
+sub delete_args_from_stack_trace
+{
+ my ($self, $trace) = @_;
+
+ return unless $trace && $trace->can('frames');
+
+ foreach my $frame ($trace->frames) {
+ next unless $frame->{args};
+ $frame->{args} = [];
+ }
+
+ return;
+}
+
1;
__END__
@@ -132,19 +228,23 @@ Log::Any::Proxy::WithStackTrace - Log::Any proxy to upgrade string errors to obj
=head1 VERSION
-version 1.715
+version 1.717
=head1 SYNOPSIS
use Log::Any qw( $log, proxy_class => 'WithStackTrace' );
- # Some adapter that knows how to handle both structured data,
- # and log messages which are actually objects with a
- # "stack_trace" method:
- #
+ # Allow stack trace call stack arguments to be logged:
+ use Log::Any qw( $log, proxy_class => 'WithStackTrace',
+ proxy_show_stack_trace_args => 1 );
+
+ # Configure some adapter that knows how to:
+ # 1) handle structured data, and
+ # 2) handle message objects which have a "stack_trace" method:
Log::Any::Adapter->set($adapter);
- $log->error("Help!"); # stack trace gets automatically added
+ $log->error("Help!"); # stack trace gets automatically added,
+ # starting from this line of code
=head1 DESCRIPTION
@@ -155,12 +255,20 @@ used to generate one, the resulting trace can be confusing if it begins
relative to where the log adapter was called, and not relative to where
the logging method was originally called.
-With this proxy in place, if any logging method is called with a message
-that is a non-reference scalar, that message will be upgraded into a
-C<Log::Any::MessageWithStackTrace> object with a C<stack_trace> method,
-and that method will return a trace relative to where the logging method
-was called. A string overload is provided on the object to return the
-original message.
+With this proxy in place, if any logging method is called with a log
+message that is a non-reference scalar (i.e. a string), that log message
+will be upgraded into a C<Log::Any::MessageWithStackTrace> object with a
+C<stack_trace> method, and that method will return a trace relative to
+where the logging method was called. A string overload is provided on
+the object to return the original log message.
+
+Additionally, any call stack arguments in the stack trace will be
+deleted before logging, to avoid accidentally logging sensitive data.
+This happens both for message objects that were auto-generated from
+string messages, as well as for message objects that were passed in
+directly (if they appear to have a stack trace method). This default
+argument scrubbing behavior can be turned off by specifying a true value
+for the C<proxy_show_stack_trace_args> import flag.
B<Important:> This proxy should be used with a L<Log::Any::Adapter> that
is configured to handle structured data. Otherwise the object created
@@ -171,11 +279,42 @@ trace.
=head2 maybe_upgrade_with_stack_trace
-This is an internal use method that will convert a non-reference scalar
+ @args = $self->maybe_upgrade_with_stack_trace(@args);
+
+This is an internal-use method that will convert a non-reference scalar
message into a C<Log::Any::MessageWithStackTrace> object with a
C<stack_trace> method. A string overload is provided to return the
original message.
+Stack trace args are scrubbed out in case they contain sensitive data,
+unless the C<proxy_show_stack_trace_args> option has been set.
+
+=head2 maybe_delete_stack_trace_args
+
+ $self->maybe_delete_stack_trace_args($arg);
+
+This is an internal-use method that, given a single argument that is a
+reference, tries to figure out whether the argument is an object with a
+stack trace, and if so tries to delete any stack trace args.
+
+The logic is based on L<Devel::StackTrace::Extract>.
+
+It specifically looks for objects with a C<stack_trace> method (which
+should catch anything that does L<StackTrace::Auto>, including anything
+that does L<Throwable::Error>), or a C<trace> method (used by
+L<Exception::Class> and L<Moose::Exception> and friends).
+
+It specifically ignores L<Mojo::Exception> objects, because their stack
+traces don't contain any call stack args.
+
+=head2 delete_args_from_stack_trace($trace)
+
+ $self->delete_args_from_stack_trace($trace)
+
+To scrub potentially sensitive data from C<Devel::StackTrace> arguments,
+this method deletes arguments from all of the C<Devel::StackTrace::Frame>
+in the trace.
+
=head1 AUTHORS
=over 4