Upload/File.pm

filter: Statementstmt 100% 68 / 68 Branchbran 90.0% 18 / 20 Conditioncond 88.2% 30 / 34 MC/DCmcdc 78.9% 15 / 19 Subroutinesub 100% 14 / 14 Podpod 100% 8 / 8 totaltotal 93.8% 153 / 163 CC 24 SCAR 15.8
CC
24
Cov
95%
save_to
8.0
sanitize_filename
6.0
allowed_types
6.0
CRAP 4.8
↑ Summary Upload.pm »
1 package Uniform::Upload::File;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use File::Copy qw(copy);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Uniform::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Uniform::Utils qw(parse_size_limit);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '0.01';
11  
12 # Constructor wraps the raw hash attributes
13 sub new {
14 17 ▶ my ($class, $meta) = @_;
Subroutine new: called CC=5 SCAR=16.1
Pod documented
15  
16 my $self = {
17 tempname => $meta->{tempname} || undef,
18 filename => $meta->{filename} || undef,
19 size => $meta->{size} || 0,
20 type => $meta->{type} || undef,
21 17 ▶ };
Condition100%8/8
$meta->{'tempname'} || undef
l!l
89
$meta->{'filename'} || undef
l!l
107
$meta->{'size'} || 0
l!l
116
$meta->{'type'} || undef
l!l
98
MC/DC100%1/1
$meta->{'tempname'} || undef
$meta->{'tempname'}
MC/DC100%1/1
$meta->{'filename'} || undef
$meta->{'filename'}
MC/DC100%1/1
$meta->{'size'} || 0
$meta->{'size'}
MC/DC100%1/1
$meta->{'type'} || undef
$meta->{'type'}
Truth table100%2/2
A
A= $meta->{'tempname'}
Aresult
11
00
Truth table100%2/2
A
A= $meta->{'filename'}
Aresult
11
00
Truth table100%2/2
A
A= $meta->{'size'}
Aresult
11
00
Truth table100%2/2
A
A= $meta->{'type'}
Aresult
11
00
22  
23 17 return bless $self, $class;
24 }
25  
26 2 ▶sub size { my $self = shift; return $self->{size} }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
27 2 ▶sub type { my $self = shift; return $self->{type} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
28 9 ▶sub filename { my $self = shift; return $self->{filename} }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
29  
30 # Validates the file's size against a human-readable max, e.g. '2M', '500K', '1G', or a plain byte count.
31 # Delegates the string parsing to Uniform::Utils::parse_size_limit, shared across the
32 # Uniform ecosystem. Throws a ValidationError if the file exceeds the limit (parse_size_limit
33 # throws its own ValidationError for a missing/unparsable limit string). Returns $self on success.
34 sub max_size {
35 8 ▶ my ($self, $limit) = @_;
Subroutine max_size: called CC=3 SCAR=11.0
Pod documented
36  
37 8 my $bytes = parse_size_limit($limit);
38  
39 5 ▶ if (($self->{size} || 0) > $bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->{"size"} || 0) > $bytes) 2 3 5
Condition50%1/2
$self->{'size'} || 0
l!l
50
MC/DC0%0/1
$self->{'size'} || 0
$self->{'size'}
Truth table50%1/2
A
A= $self->{'size'}
Aresult
11
00
40 2 Uniform::Exceptions->throw(
41 type => 'ValidationError',
42 message => "File size $self->{size} exceeds maximum allowed size of $limit",
43 attribute => 'size',
44 );
45 }
46  
47 3 return $self;
48 }
49  
50 # Validates the file's MIME type against a whitelist of allowed types.
51 # Throws a ValidationError if the type is not permitted. Returns $self on success for chaining.
52 sub allowed_types {
53 5 ▶ my ($self, $types) = @_;
Subroutine allowed_types: called CC=6 SCAR=17.9
Pod documented
54  
55 5 ▶ unless (defined $types && ref($types) eq 'ARRAY' && @$types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $types and ref $types eq "ARRAY" and @$types) 2 2 4
Condition100%6/6
defined $types and ref $types eq "ARRAY" and @$types
!ll&&!rl&&r
212
defined $types and ref $types eq "ARRAY"
!ll&&!rl&&r
113
MC/DC100%3/3
defined $types and ref $types eq "ARRAY" and @$types
defined $types ref $types eq "ARRAY" @$types
Truth table100%4/4
A and B and C
A= defined $typesB= ref $types eq "ARRAY"C= @$types
ABCresult
0XX0
10X0
1100
1111
56 3 Uniform::Exceptions->throw(
57 type => 'ValidationError',
58 message => 'allowed_types requires a non-empty arrayref of MIME type strings',
59 );
60 }
61  
62 2 ▶ my $type = defined $self->{type} ? $self->{type} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'type'} ? : 2 0 2
63 2 ▶ unless (grep { $_ eq $type } @$types) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $type;} @$types) 1 1 2
64 1 Uniform::Exceptions->throw(
65 type => 'ValidationError',
66 message => "File type '$type' is not among the allowed types: " . join(', ', @$types),
67 attribute => 'type',
68 );
69 }
70  
71 1 return $self;
72 }
73  
74 # Defensive security guard scrubbing malicious directory path mutations or null-byte hacks
75 sub sanitize_filename {
76 9 ▶ my ($self) = @_;
Subroutine sanitize_filename: called CC=6 SCAR=17.9
Pod documented
77 9 ▶ return $self unless defined $self->{filename} && length $self->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"filename"} and length $self->{"filename"} 2 7 9
Condition66%2/3
defined $self->{"filename"} and length $self->{"filename"}
!ll&&!rl&&r
207
MC/DC50%1/2
defined $self->{"filename"} and length $self->{"filename"}
defined $self->{"filename"} length $self->{"filename"}
Truth table66%2/3
A and B
A= defined $self->{"filename"}B= length $self->{"filename"}
ABresult
0X0
100
111
78  
79 7 my $raw_name = $self->{filename};
80  
81 # 1. Standardize cross-platform separators
82 7 $raw_name =~ s{\\}{/}g;
83  
84 # 2. Strip directory segments completely to isolate the true file tail node
85 7 my $clean = basename($raw_name);
86  
87 # 3. Purge dangerous language boundary markers (null bytes)
88 7 $clean =~ s/\x00//g;
89  
90 # 4. Whitelist safe alphanumeric boundaries, converting illegal parameters to hyphens
91 7 $clean =~ s/[^a-zA-Z0-9._-]/-/g;
92  
93 # 5. Collapse duplicate or trailing hyphen strings
94 7 $clean =~ s/-+/-/g;
95 7 $clean =~ s/^-+|-+$//g;
96  
97 # 6. Safety fallback: If string filtering reduces the name to nothing, provide a safe default string
98 7 ▶ if (!defined $clean || length($clean) == 0 || $clean eq '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $clean or length $clean == 0 or $clean eq ".") 2 5 7
Condition83%5/6
not defined $clean or length $clean == 0 or $clean eq "."
l!l&&r!l&&!r
115
not defined $clean or length $clean == 0
l!l&&r!l&&!r
016
MC/DC66%2/3
not defined $clean or length $clean == 0 or $clean eq "."
not defined $clean length $clean == 0 $clean eq "."
Truth table75%3/4
not(A) or B or C
A= not defined $cleanB= length $clean == 0C= $clean eq "."
ABCresult
1XX1
01X1
0011
0000
99 2 $clean = 'uploaded_file';
100 }
101  
102 7 $self->{filename} = $clean;
103 7 return $self;
104 }
105  
106 # Output execution boundary: Copies the file from temporary cache storage to live system storage destinations.
107 # Always sanitizes the filename first so callers can't accidentally skip that step and reopen a
108 # path-traversal hole by passing an unsanitized $self->{filename} straight into the target path.
109 sub save_to {
110 7 ▶ my ($self, $destination) = @_;
Subroutine save_to: called CC=8 SCAR=20.8
Pod documented
111  
112 7 ▶ unless (defined $destination && length $destination) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $destination and length $destination) 2 5 7
Condition100%3/3
defined $destination and length $destination
!ll&&!rl&&r
115
MC/DC100%2/2
defined $destination and length $destination
defined $destination length $destination
Truth table100%3/3
A and B
A= defined $destinationB= length $destination
ABresult
0X0
100
111
113 2 Uniform::Exceptions->throw(type => 'ValidationError', message => 'save_to requires an absolute folder or string path target destination');
114 }
115  
116 5 my $source = $self->{tempname};
117 5 ▶ unless (defined $source && -e $source) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $source and -e $source) 1 4 5
Condition66%2/3
defined $source and -e $source
!ll&&!rl&&r
014
MC/DC50%1/2
defined $source and -e $source
defined $source -e $source
Truth table66%2/3
A and B
A= defined $sourceB= -e $source
ABresult
0X0
100
111
118 1 Uniform::Exceptions->throw(type => 'IOError', message => 'Temporary source cache file does not exist or has expired from system paths');
119 }
120  
121 4 $self->sanitize_filename;
122  
123 4 my $target = $destination;
124 4 ▶ if ($destination =~ m{[\\/]$} || -d $destination) {
Branch: 1 branch
BranchTrueFalseTotal
if ($destination =~ m[[\\/]$] or -d $destination) 3 1 4
Condition100%3/3
$destination =~ m[[\\/]$] or -d $destination
l!l&&r!l&&!r
211
MC/DC100%2/2
$destination =~ m[[\\/]$] or -d $destination
$destination =~ m[[\\/]$] -d $destination
Truth table100%3/3
A or B
A= $destination =~ m[[\\/]$]B= -d $destination
ABresult
1X1
011
000
125 3 $destination =~ s{[\\/]$}{};
126 3 $target = "$destination/" . $self->{filename};
127 }
128  
129 4 ▶ copy($source, $target) or Uniform::Exceptions->throw(
Branch: 1 branch
BranchTrueFalseTotal
copy($source, $target) or "Uniform::Exceptions"->throw("type", "IOError", "message", "Failed to copy file from '${source}' to '${target}': $!") 0 4 4
130 type => 'IOError',
131 message => "Failed to copy file from '$source' to '$target': $!",
132 );
133  
134 4 return $self;
135 }
136  
137 3 1;
138  
139 __END__
↑ Summary Upload.pm »