File Coverage

blib/lib/Archive/BagIt/Role/Portability.pm
Criterion Covered Total %
statement 34 34 100.0
branch n/a
condition 3 3 100.0
subroutine 10 10 100.0
pod 3 3 100.0
total 50 50 100.0


line stmt bran cond sub pod time code
1             package Archive::BagIt::Role::Portability;
2 9     9   66325 use strict;
  9         19  
  9         266  
3 9     9   50 use warnings;
  9         67  
  9         264  
4 9     9   3527 use namespace::autoclean;
  9         70170  
  9         28  
5 9     9   531 use Carp ();
  9         14  
  9         105  
6 9     9   51 use File::Spec ();
  9         14  
  9         149  
7 9     9   404 use Moo::Role;
  9         10535  
  9         50  
8             # ABSTRACT: A role that handles filepaths for improved portability
9             our $VERSION = '0.094'; # VERSION
10              
11              
12             sub chomp_portable {
13 1311     1311 1 2445 my ($line) = @_;
14 1311         5705 $line =~ s#\x{0d}?\x{0a}?\Z##s; # replace CR|CRNL with empty
15 1311         2802 return $line;
16             }
17              
18              
19             sub normalize_payload_filepath {
20 1298     1298 1 2157 my ($filename) = @_;
21 1298         1742 $filename =~ s#[\\](?![/])#/#g; # normalize Windows Backslashes, but only if they are no escape sequences
22 1298         1467 $filename =~ s#%#%25#g; # normalize percent
23 1298         1361 $filename =~ s#\x{0a}#%0A#g; #normalize NEWLINE
24 1298         1371 $filename =~ s#\x{0d}#%0D#g; #normalize CARRIAGE RETURN
25 1298         1308 $filename =~ s# #%20#g; # space
26 1298         1308 $filename =~ s#"##g; # quotes
27 1298         2313 return $filename;
28             }
29              
30              
31             sub check_if_payload_filepath_violates{
32 399     399 1 585 my ($local_name) = @_;
33             # HINT: there is no guarantuee *not* to escape!
34             return
35 399   100     6032 ($local_name =~ m/^~/) # Unix Home
36             || ($local_name =~ m#\./#) # Unix, parent dir escape
37             || ($local_name =~ m#^[A-Z]:[\\/]#) # Windows Drive
38             || ($local_name =~ m#^/#) # Unix absolute path
39             || ($local_name =~ m#^$#) # Unix Env
40             || ($local_name =~ m#^\\#) # Windows absolute path
41             || ($local_name =~ m#^%[^%]*%#) # Windows ENV
42             || ($local_name =~ m#^\*#) # Artifact of md5sum-Tool, where ' *' is allowed to separate checksum and file in fixity line
43             || ($local_name =~ m#[<>:"?|]#) # Windows reserved chars
44             || ($local_name =~ m#(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])#) # Windows reserved filenames
45             ;
46             }
47              
48 9     9   6287 no Moo::Role;
  9         20  
  9         44  
49             1;
50              
51             __END__