| 1 | package Mail::Exim::ACL::Attachments; | ||||||||||||
| 2 | |||||||||||||
| 3 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use 5.016; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use utf8; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 1 | our $VERSION = 1.007; | |||||||||||
| 10 | |||||||||||||
| 11 | 1 | ▶ | use Exporter qw(import); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 1 | ▶ | use IO::Uncompress::Unzip; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 1 | our @EXPORT_OK = qw(check_filename check_zip); | |||||||||||
| 15 | |||||||||||||
| 16 | # https://docs.microsoft.com/en-us/deployoffice/compat/office-file-format-reference | ||||||||||||
| 17 | # https://en.wikipedia.org/wiki/List_of_Microsoft_Office_filename_extensions | ||||||||||||
| 18 | 1 | our %MACRO_ENABLED = ( | |||||||||||
| 19 | doc => 'Word Document', | ||||||||||||
| 20 | docb => 'Word Document', | ||||||||||||
| 21 | docm => 'Word Document', | ||||||||||||
| 22 | dot => 'Word Template', | ||||||||||||
| 23 | dotm => 'Word Template', | ||||||||||||
| 24 | one => 'OneNote Export File', | ||||||||||||
| 25 | pot => 'PowerPoint Template', | ||||||||||||
| 26 | potm => 'PowerPoint Template', | ||||||||||||
| 27 | ppa => 'PowerPoint Add-in', | ||||||||||||
| 28 | ppam => 'PowerPoint Add-in', | ||||||||||||
| 29 | pps => 'PowerPoint Slide Show', | ||||||||||||
| 30 | ppsm => 'PowerPoint Slide Show', | ||||||||||||
| 31 | ppt => 'PowerPoint Presentation', | ||||||||||||
| 32 | pptm => 'PowerPoint Presentation', | ||||||||||||
| 33 | sldm => 'PowerPoint Slide', | ||||||||||||
| 34 | vsd => 'Visio Drawing File', | ||||||||||||
| 35 | vsdm => 'Visio Drawing File', | ||||||||||||
| 36 | vss => 'Visio Stencil File', | ||||||||||||
| 37 | vssm => 'Visio Stencil File', | ||||||||||||
| 38 | vst => 'Visio Drawing Template', | ||||||||||||
| 39 | vstm => 'Visio Drawing Template', | ||||||||||||
| 40 | xla => 'Excel Add-in', | ||||||||||||
| 41 | xlam => 'Excel Add-in', | ||||||||||||
| 42 | xlm => 'Excel Macro', | ||||||||||||
| 43 | xls => 'Excel Spreadsheet', | ||||||||||||
| 44 | xlsb => 'Excel Spreadsheet', | ||||||||||||
| 45 | xlsm => 'Excel Spreadsheet', | ||||||||||||
| 46 | xlt => 'Excel Spreadsheet Template', | ||||||||||||
| 47 | xltm => 'Excel Spreadsheet Template', | ||||||||||||
| 48 | xlw => 'Excel Workspace', | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | # https://support.office.com/en-us/article/blocked-attachments-in-outlook-434752e1-02d3-4e90-9124-8b81e49a8519 | ||||||||||||
| 52 | 1 | our %BLOCKED_BY_OUTLOOK = ( | |||||||||||
| 53 | ade => 'Access Project Extension', | ||||||||||||
| 54 | adp => 'Access Project', | ||||||||||||
| 55 | apk => 'Android Application Package', | ||||||||||||
| 56 | app => 'Executable Application', | ||||||||||||
| 57 | 'appcontent-ms' => 'Microsoft Windows Application Content', | ||||||||||||
| 58 | application => 'ClickOnce Deployment Manifest File', | ||||||||||||
| 59 | 'appref-ms' => 'ClickOnce Application Reference File', | ||||||||||||
| 60 | appx => 'Windows application package file', | ||||||||||||
| 61 | asp => 'Active Server Page', | ||||||||||||
| 62 | aspx => 'Active Server Page Extended', | ||||||||||||
| 63 | asx => 'ASF Redirector file', | ||||||||||||
| 64 | bas => 'BASIC Source Code', | ||||||||||||
| 65 | bat => 'Batch Processing', | ||||||||||||
| 66 | bgi => 'Borland Graphics Interface', | ||||||||||||
| 67 | cab => 'Windows Cabinet File', | ||||||||||||
| 68 | cdxml => 'ChemDraw XM', | ||||||||||||
| 69 | cer => 'Internet Security Certificate File', | ||||||||||||
| 70 | chm => 'Compiled HTML Help', | ||||||||||||
| 71 | cmd => 'Command File', | ||||||||||||
| 72 | cnt => 'Microsoft Help Workshop Application', | ||||||||||||
| 73 | com => 'Command', | ||||||||||||
| 74 | cpl => 'Windows Control Panel Extension', | ||||||||||||
| 75 | crt => 'Certificate File', | ||||||||||||
| 76 | csh => 'csh Script', | ||||||||||||
| 77 | der => 'DER Encoded X509 Certificate File', | ||||||||||||
| 78 | diagcab => 'Microsoft Support diagnostic tools', | ||||||||||||
| 79 | exe => 'Executable File', | ||||||||||||
| 80 | fxp => 'FoxPro Compiled Source', | ||||||||||||
| 81 | gadget => 'Windows Vista gadget', | ||||||||||||
| 82 | grp => 'Microsoft program group', | ||||||||||||
| 83 | hlp => 'Windows Help File', | ||||||||||||
| 84 | hpj => 'AppWizard Help project', | ||||||||||||
| 85 | hta => 'Hypertext Application', | ||||||||||||
| 86 | htc => 'HTML component file', | ||||||||||||
| 87 | img => 'Image', | ||||||||||||
| 88 | inf => 'Information or Setup File', | ||||||||||||
| 89 | ins => 'IIS Internet Communications Settings', | ||||||||||||
| 90 | iso => 'Optical Disk Media File System', | ||||||||||||
| 91 | isp => 'IIS Internet Service Provider Settings', | ||||||||||||
| 92 | its => 'Internet Document Set', | ||||||||||||
| 93 | jar => 'Java Archive', | ||||||||||||
| 94 | jnlp => 'Java Network Launch Protocol', | ||||||||||||
| 95 | js => 'JavaScript Source Code', | ||||||||||||
| 96 | jse => 'JScript Encoded Script File', | ||||||||||||
| 97 | ksh => 'UNIX Shell Script', | ||||||||||||
| 98 | 'library-ms' => 'Windows Library Description File format', | ||||||||||||
| 99 | lnk => 'Windows Shortcut File', | ||||||||||||
| 100 | mad => 'Access Module Shortcut', | ||||||||||||
| 101 | maf => 'Access', | ||||||||||||
| 102 | mag => 'Access Diagram Shortcut', | ||||||||||||
| 103 | mam => 'Access Macro Shortcut', | ||||||||||||
| 104 | maq => 'Access Query Shortcut', | ||||||||||||
| 105 | mar => 'Access Report Shortcut', | ||||||||||||
| 106 | mas => 'Access Stored Procedures', | ||||||||||||
| 107 | mat => 'Access Table Shortcut', | ||||||||||||
| 108 | mau => 'Media Attachment Unit', | ||||||||||||
| 109 | mav => 'Access View Shortcut', | ||||||||||||
| 110 | maw => 'Access Data Access Page', | ||||||||||||
| 111 | mcf => 'Media Container Format', | ||||||||||||
| 112 | mda => 'Access Add-in', | ||||||||||||
| 113 | mdb => 'Access Application', | ||||||||||||
| 114 | mde => 'Access MDE Database File', | ||||||||||||
| 115 | mdt => 'Access Add-in Data', | ||||||||||||
| 116 | mdw => 'Access Workgroup Information', | ||||||||||||
| 117 | mdz => 'Access Wizard Template', | ||||||||||||
| 118 | msc => 'Microsoft Management Console Snap-in Control File', | ||||||||||||
| 119 | mht => 'Web HTM', | ||||||||||||
| 120 | mhtml => 'Web HTM', | ||||||||||||
| 121 | msh => 'Microsoft Shell', | ||||||||||||
| 122 | msh1 => 'Microsoft Shell', | ||||||||||||
| 123 | msh2 => 'Microsoft Shell', | ||||||||||||
| 124 | mshxml => 'Microsoft Shell', | ||||||||||||
| 125 | msh1xml => 'Microsoft Shell', | ||||||||||||
| 126 | msh2xml => 'Microsoft Shell', | ||||||||||||
| 127 | msi => 'Windows Installer File', | ||||||||||||
| 128 | msp => 'Windows Installer Update', | ||||||||||||
| 129 | mst => 'Windows SDK Setup Transform Script', | ||||||||||||
| 130 | msu => 'Windows Update file', | ||||||||||||
| 131 | ops => 'Office Profile Settings File', | ||||||||||||
| 132 | osd => 'Open Software Description ', | ||||||||||||
| 133 | pcd => 'Visual Test', | ||||||||||||
| 134 | pif => 'Windows Program Information File', | ||||||||||||
| 135 | pl => 'Perl script', | ||||||||||||
| 136 | plg => 'Developer Studio Build Log', | ||||||||||||
| 137 | prf => 'Windows System File', | ||||||||||||
| 138 | prg => 'Program File', | ||||||||||||
| 139 | printerexport => 'Printer backup file', | ||||||||||||
| 140 | ps1 => 'Windows PowerShell', | ||||||||||||
| 141 | ps1xml => 'Windows PowerShell', | ||||||||||||
| 142 | ps2 => 'Windows PowerShell', | ||||||||||||
| 143 | ps2xml => 'Windows PowerShell', | ||||||||||||
| 144 | psc1 => 'Windows PowerShell', | ||||||||||||
| 145 | psc2 => 'Windows PowerShell', | ||||||||||||
| 146 | psd1 => 'Windows PowerShell', | ||||||||||||
| 147 | psdm1 => 'Windows PowerShell', | ||||||||||||
| 148 | pssc => 'Windows PowerShell Session Configuration', | ||||||||||||
| 149 | pst => 'MS Exchange Address Book File, Outlook Personal Folder File (Microsoft)', | ||||||||||||
| 150 | py => 'Python script', | ||||||||||||
| 151 | pyc => 'Python script', | ||||||||||||
| 152 | pyo => 'Python script', | ||||||||||||
| 153 | pyw => 'Python script', | ||||||||||||
| 154 | pyz => 'Python script', | ||||||||||||
| 155 | pyzw => 'Python script', | ||||||||||||
| 156 | reg => 'Registry Data File', | ||||||||||||
| 157 | scf => 'Windows Explorer Command', | ||||||||||||
| 158 | scr => 'Windows Screen Saver', | ||||||||||||
| 159 | sct => 'Windows Script Component', | ||||||||||||
| 160 | 'search-ms' => 'Microsoft Vista Saved Search file', | ||||||||||||
| 161 | 'settingcontent-ms' => 'Windows Shortcut file', | ||||||||||||
| 162 | shb => 'Windows Shortcut into a Document', | ||||||||||||
| 163 | shs => 'Shell Scrap Object File', | ||||||||||||
| 164 | theme => 'Desktop theme file settings', | ||||||||||||
| 165 | tmp => 'Temporary File/Folder', | ||||||||||||
| 166 | udl => 'Universal Data Link', | ||||||||||||
| 167 | url => 'Internet Location', | ||||||||||||
| 168 | vb => 'VBScript File or Any Visual Basic Source', | ||||||||||||
| 169 | vbe => 'VBScript Encoded Script File', | ||||||||||||
| 170 | vbp => 'Visual Basic project file', | ||||||||||||
| 171 | vbs => 'VBScript File', | ||||||||||||
| 172 | vhd => 'Virtual Hard Disk', | ||||||||||||
| 173 | vhdx => 'Virtual Hard Disk Extended', | ||||||||||||
| 174 | vsmacros => 'Visual Studio .NET Binary-based Macro Project', | ||||||||||||
| 175 | vsw => 'Visio Workspace File', | ||||||||||||
| 176 | webpnp => 'Internet printing file', | ||||||||||||
| 177 | website => 'Pinned site shortcut from Internet Explorer', | ||||||||||||
| 178 | ws => 'Windows Script File', | ||||||||||||
| 179 | wsb => 'Windows Sandbox File', | ||||||||||||
| 180 | wsc => 'Windows Script Component', | ||||||||||||
| 181 | wsf => 'Windows Script File', | ||||||||||||
| 182 | wsh => 'Windows Script Host Settings File', | ||||||||||||
| 183 | xbap => 'Browser applications', | ||||||||||||
| 184 | xll => 'Excel Add-in', | ||||||||||||
| 185 | xnk => 'Exchange Public Folder Shortcut', | ||||||||||||
| 186 | ); | ||||||||||||
| 187 | |||||||||||||
| 188 | # File associations from 7-Zip and others | ||||||||||||
| 189 | 1 | our %ARCHIVES = ( | |||||||||||
| 190 | '7z' => '7-Zip Archive', | ||||||||||||
| 191 | ace => 'ACE File', | ||||||||||||
| 192 | arj => 'ARJ File', | ||||||||||||
| 193 | bz2 => 'bzip2 File', | ||||||||||||
| 194 | bzip2 => 'bzip2 File', | ||||||||||||
| 195 | cpio => 'CPIO Archive', | ||||||||||||
| 196 | deb => 'Debian Package', | ||||||||||||
| 197 | dll => 'Dynamic Link Library', | ||||||||||||
| 198 | dmg => 'Disk Image', | ||||||||||||
| 199 | esd => 'Disk Image', | ||||||||||||
| 200 | fat => 'Zip Archive', | ||||||||||||
| 201 | gz => 'gzip File', | ||||||||||||
| 202 | gzip => 'gzip File', | ||||||||||||
| 203 | hfs => 'Disk Image', | ||||||||||||
| 204 | lha => 'LHA File', | ||||||||||||
| 205 | lz => 'Lzip Compressed File', | ||||||||||||
| 206 | lzh => 'LZH File', | ||||||||||||
| 207 | lzma => 'LZMA File', | ||||||||||||
| 208 | ntfs => 'Disk Image', | ||||||||||||
| 209 | rar => 'RAR Archive', | ||||||||||||
| 210 | rpm => 'RPM File', | ||||||||||||
| 211 | sfx => 'Self-extracting Archive', | ||||||||||||
| 212 | squashfs => 'Disk Image', | ||||||||||||
| 213 | swm => 'Disk Image', | ||||||||||||
| 214 | tar => 'tar Archive', | ||||||||||||
| 215 | taz => 'tar Archive', | ||||||||||||
| 216 | tbz => 'tar Archive', | ||||||||||||
| 217 | tbz2 => 'tar Archive', | ||||||||||||
| 218 | tgz => 'tar Archive', | ||||||||||||
| 219 | tpz => 'tar Archive', | ||||||||||||
| 220 | txz => 'tar Archive', | ||||||||||||
| 221 | uue => 'uuencoded File', | ||||||||||||
| 222 | wim => 'Disk Image', | ||||||||||||
| 223 | xar => 'XAR File', | ||||||||||||
| 224 | xz => 'XZ File', | ||||||||||||
| 225 | z => 'gzip File', | ||||||||||||
| 226 | zip => 'Zip Archive', | ||||||||||||
| 227 | ); | ||||||||||||
| 228 | |||||||||||||
| 229 | 1 | our %BLOCKLIST = (%MACRO_ENABLED, %BLOCKED_BY_OUTLOOK, %ARCHIVES); | |||||||||||
| 230 | |||||||||||||
| 231 | 9 | ▶ | sub check_filename { | ||||||||||
|
Subroutine
check_filename: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 232 | 9 | my $filename = shift; | |||||||||||
| 233 | |||||||||||||
| 234 | 9 | my $extension = q{}; | |||||||||||
| 235 | 9 | ▶ | if ($filename =~ m{[.]\h*([^.]+)\z}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 236 | 9 | $extension = lc $1; | |||||||||||
| 237 | } | ||||||||||||
| 238 | |||||||||||||
| 239 | 9 | ▶ | if (exists $BLOCKLIST{$extension}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 240 | 5 | return 'blocked'; | |||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | # Reject split archives like "001" and "r01". | ||||||||||||
| 244 | 4 | ▶ | if ($extension =~ m{\A[r\d]\d{2,}\z}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 245 | 2 | return 'blocked'; | |||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | 2 | return 'ok'; | |||||||||||
| 249 | } | ||||||||||||
| 250 | |||||||||||||
| 251 | 4 | ▶ | sub _get_filename { | ||||||||||
|
Subroutine
_get_filename: called CC=2 SCAR=6.9
| |||||||||||||
| 252 | 4 | my $zip = shift; | |||||||||||
| 253 | |||||||||||||
| 254 | 4 | my $filename; | |||||||||||
| 255 | |||||||||||||
| 256 | 4 | my $header = eval { $zip->getHeaderInfo }; | |||||||||||
| 257 | 4 | ▶ | if (defined $header) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 258 | 3 | $filename = $header->{Name}; | |||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | 4 | return $filename; | |||||||||||
| 262 | } | ||||||||||||
| 263 | |||||||||||||
| 264 | 4 | ▶ | sub check_zip { | ||||||||||
|
Subroutine
check_zip: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 265 | 4 | my $input = shift; | |||||||||||
| 266 | |||||||||||||
| 267 | 4 | my $result = 'blocked'; | |||||||||||
| 268 | |||||||||||||
| 269 | 4 | my $zip = eval { IO::Uncompress::Unzip->new($input) }; | |||||||||||
| 270 | 4 | ▶ | if (defined $zip) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 271 | 4 | my $status = 1; | |||||||||||
| 272 | STREAM: | ||||||||||||
| 273 | 4 | ▶ | while ($status > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 274 | 4 | my $filename = _get_filename($zip); | |||||||||||
| 275 | 4 | ▶ | if (defined $filename) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 276 | 3 | $result = check_filename($filename); | |||||||||||
| 277 | } | ||||||||||||
| 278 | else { | ||||||||||||
| 279 | 1 | $result = 'blocked'; | |||||||||||
| 280 | } | ||||||||||||
| 281 | 4 | ▶ | if ($result ne 'ok') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 282 | 3 | last STREAM; | |||||||||||
| 283 | } | ||||||||||||
| 284 | 1 | ▶ | $status = eval { $zip->nextStream } // -1; | ||||||||||
|
Condition50%1/2
eval {
do {
$zip->nextStream
}
} // -1
MC/DC0%0/1
eval {
do {
$zip->nextStream
}
} // -1
eval {
do {
$zip->nextStream
}
}
Truth table50%1/2
A
A= eval {
do {
$zip->nextStream
}
}
| |||||||||||||
| 285 | } | ||||||||||||
| 286 | 4 | $zip->close; | |||||||||||
| 287 | } | ||||||||||||
| 288 | |||||||||||||
| 289 | 4 | return $result; | |||||||||||
| 290 | } | ||||||||||||
| 291 | |||||||||||||
| 292 | 1 | 1; | |||||||||||
| 293 | __END__ | ||||||||||||