| 1 | # | ||||||||||
| 2 | # Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::Untainted - functional untainting | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | use FP::Untainted; | ||||||||||
| 17 | exec untainted($ENV{CMD}); # doesn't change the taint flag on $ENV{CMD} | ||||||||||
| 18 | |||||||||||
| 19 | use FP::Untainted qw(untainted_with); | ||||||||||
| 20 | exec untainted_with($ENV{CMD}, qr/^\w+$/s); # dito | ||||||||||
| 21 | # NOTE that the ^ and $ anchors are essential if you want to make | ||||||||||
| 22 | # sure the whole string matches! | ||||||||||
| 23 | |||||||||||
| 24 | # or, (but this doesn't force the /s flag) | ||||||||||
| 25 | exec untainted_with($ENV{CMD}, '^\w+$'); | ||||||||||
| 26 | |||||||||||
| 27 | use FP::Untainted qw(is_untainted); | ||||||||||
| 28 | # complement of Scalar::Util's 'tainted' | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | L<Taint::Util> offers `untaint`, but it changes its argument. This | ||||||||||
| 33 | module provides a pure function to do the same (it (currently) uses a | ||||||||||
| 34 | regex match instead of XS to do so, though.) | ||||||||||
| 35 | |||||||||||
| 36 | Should this module stay? Vote your opinion if you like. | ||||||||||
| 37 | |||||||||||
| 38 | =head1 NOTE | ||||||||||
| 39 | |||||||||||
| 40 | This is alpha software! Read the status section in the package README | ||||||||||
| 41 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | package FP::Untainted; | ||||||||||
| 46 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Exporter "import"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | |||||||||||
| 51 | 1 | our @EXPORT = qw(untainted); | |||||||||
| 52 | 1 | our @EXPORT_OK = qw(untainted_with is_untainted); | |||||||||
| 53 | 1 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||
| 54 | |||||||||||
| 55 | 1 | ▶ | use Chj::TEST; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 56 | 1 | ▶ | use FP::Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub untainted { | ||||||||
|
Subroutine
untainted: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 59 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | 0 | ▶ | $_[0] =~ /(.*)/s or die "??"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | $1 | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 6 | ▶ | sub untainted_with { | ||||||||
|
Subroutine
untainted_with: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 65 | 6 | ▶ | @_ == 2 or fp_croak_arity 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 6 | my ($v, $re) = @_; | |||||||||
| 67 | 6 | ▶ | $v =~ /($re)/ or die "untainted_with: does not match regex $re: '$v'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 4 | $1 | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | TEST { untainted_with "Foo", qr/\w+/s } "Foo"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 1 | ▶ | TEST { untainted_with "Foo ", qr/\w+/s } "Foo"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 1 | ▶ | TEST_EXCEPTION { untainted_with "Foo ", qr/^\w+$/s } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 74 | 1 | 'untainted_with: does not match regex (?^s:^\\w+$): \'Foo \''; | |||||||||
| 75 | |||||||||||
| 76 | 1 | ▶ | TEST { untainted_with "Foo", '\w+' } "Foo"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 77 | 1 | ▶ | TEST { untainted_with "Foo ", '\w+' } "Foo"; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 78 | 1 | ▶ | TEST_EXCEPTION { untainted_with "Foo ", '^\w+$' } # /s missing. | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 79 | 1 | 'untainted_with: does not match regex ^\\w+$: \'Foo \''; | |||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | use Scalar::Util 'tainted'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 82 | |||||||||||
| 83 | 0 | ▶ | sub is_untainted { | ||||||||
|
Subroutine
is_untainted: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 84 | 0 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 0 | not tainted $_[0] | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | 1 | |||||||||