| 1 | package Plack::Middleware::Security::Common; | ||
| 2 | |||
| 3 | # ABSTRACT: A simple security filter for Plack with common rules. | ||
| 4 | |||
| 5 | 1 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 1 | ▶ | use parent qw( Plack::Middleware::Security::Simple Exporter::Tiny ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | ▶ | use Regexp::Common qw/ net /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 1 | our @EXPORT = qw( | |
| 14 | archive_extensions | ||
| 15 | backup_files | ||
| 16 | cgi_bin | ||
| 17 | cms_prefixes | ||
| 18 | config_scripts | ||
| 19 | document_extensions | ||
| 20 | dot_files | ||
| 21 | exchange_prefixes | ||
| 22 | fake_extensions | ||
| 23 | header_injection | ||
| 24 | ip_address_referer | ||
| 25 | misc_extensions | ||
| 26 | misc_vulns | ||
| 27 | non_printable_chars | ||
| 28 | null_or_escape | ||
| 29 | protocol_in_path_or_referer | ||
| 30 | require_content | ||
| 31 | script_extensions | ||
| 32 | script_injection | ||
| 33 | system_dirs | ||
| 34 | unexpected_content | ||
| 35 | webdav_methods | ||
| 36 | wordpress | ||
| 37 | ); | ||
| 38 | |||
| 39 | 1 | our $VERSION = 'v0.13.2'; | |
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | 1 | ▶ | sub archive_extensions { |
|
Subroutine
archive_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 44 | 1 | my $re = qr{\.(?:bz2|iso|rar|tar|u?zip|[7glx]?z|tgz)\b}; | |
| 45 | return ( | ||
| 46 | 1 | PATH_INFO => $re, | |
| 47 | QUERY_STRING => $re, | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | 1 | ▶ | sub backup_files { |
|
Subroutine
backup_files: called CC=1 SCAR=0.0
Pod
documented
| |||
| 53 | return ( | ||
| 54 | 1 | misc_extensions(), | |
| 55 | PATH_INFO => qr{(?:backup|database|db|dump|localhost)\.}, | ||
| 56 | ); | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | 1 | ▶ | sub cgi_bin { |
|
Subroutine
cgi_bin: called CC=1 SCAR=0.0
Pod
documented
| |||
| 61 | 1 | my $re = qr{/cgi[_\-](?:bin|wrapper)}; | |
| 62 | return ( | ||
| 63 | 1 | PATH_INFO => $re, | |
| 64 | QUERY_STRING => $re, | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | 1 | ▶ | sub cms_prefixes { |
|
Subroutine
cms_prefixes: called CC=1 SCAR=0.0
Pod
documented
| |||
| 70 | 1 | my $re = qr{/(?:docroot|drupal|ftproot|include|inetpub|joomla|laravel|lib|magento|plugin|plus|swagger|vendor|webroot|wp|wordpress|yii|zend)}; | |
| 71 | return ( | ||
| 72 | 1 | PATH_INFO => $re, | |
| 73 | ); | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | 1 | ▶ | sub config_scripts { |
|
Subroutine
config_scripts: called CC=1 SCAR=0.0
Pod
documented
| |||
| 78 | 1 | my $re = | |
| 79 | qr{/(?:Dockerfile|alienfile|composer\.json|config(?:uration)?|cpanfile|deployment-config|live_env|local\.(?:json|xml)|package\.(?:json|xml)|robomongo|sendgrid|settings|storage|twilio)}; | ||
| 80 | 1 | return ( PATH_INFO => $re, ); | |
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | 1 | ▶ | sub document_extensions { |
|
Subroutine
document_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 85 | 1 | my $re = qr{\.(?:a[bz]w|csv|docx?|e?pub|od[pst]|pdf|pptx?|one|rtf|vsd|xlsx?)\b}; | |
| 86 | return ( | ||
| 87 | 1 | PATH_INFO => $re, | |
| 88 | QUERY_STRING => $re, | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | 1 | ▶ | sub dot_files { |
|
Subroutine
dot_files: called CC=1 SCAR=0.0
Pod
documented
| |||
| 95 | return ( | ||
| 96 | 1 | PATH_INFO => qr{(?:\.\./|/\.(?!well-known/))}, | |
| 97 | QUERY_STRING => qr{\.\./}, | ||
| 98 | ); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | 1 | ▶ | sub exchange_prefixes { |
|
Subroutine
exchange_prefixes: called CC=1 SCAR=0.0
Pod
documented
| |||
| 103 | return ( | ||
| 104 | 1 | PATH_INFO => qr{^/+(?:ecp|owa|autodiscover)/}i, | |
| 105 | ) | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | 1 | ▶ | sub fake_extensions { |
|
Subroutine
fake_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 110 | 1 | my $re = qr{;[.](?:\w+)\b}; | |
| 111 | return ( | ||
| 112 | 1 | PATH_INFO => $re, | |
| 113 | ) | ||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | 1 | ▶ | sub header_injection { |
|
Subroutine
header_injection: called CC=1 SCAR=0.0
Pod
documented
| |||
| 118 | 1 | my $re = qr{(?:\%20HTTP/[0-9]|%0[ad]|[\r\n])}i; | |
| 119 | return ( | ||
| 120 | 1 | REQUEST_URI => $re, | |
| 121 | PATH_INFO => $re, | ||
| 122 | ); | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | 1 | ▶ | sub ip_address_referer { |
|
Subroutine
ip_address_referer: called CC=1 SCAR=0.0
Pod
documented
| |||
| 128 | return ( | ||
| 129 | 1 | HTTP_REFERER => qr{^https?://$RE{net}{IPv4}/}, | |
| 130 | HTTP_REFERER => qr{^https?://$RE{net}{IPv6}/}, | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | 1 | ▶ | sub misc_extensions { |
|
Subroutine
misc_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 136 | 1 | my $re = qr{[.](?:backup|bak|bck|bkp|cfg|conf(?:ig)?|dat|dist|env|ibz|in[ci]|lo?ck|npb|old|ps[bc]|rdg|save?|swa?p|te?mp|to?ml|ya?ml)\b}; | |
| 137 | return ( | ||
| 138 | 1 | PATH_INFO => $re, | |
| 139 | QUERY_STRING => $re, | ||
| 140 | ) | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | |||
| 145 | 1 | ▶ | sub misc_vulns { |
|
Subroutine
misc_vulns: called CC=1 SCAR=0.0
Pod
documented
| |||
| 146 | 1 | my $re = qr{^/(?:shell)}; | |
| 147 | 1 | return ( PATH_INFO => $re, ); | |
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | 1 | ▶ | sub non_printable_chars { |
|
Subroutine
non_printable_chars: called CC=1 SCAR=0.0
Pod
documented
| |||
| 152 | 1 | return ( PATH_INFO => qr/[^[:print:]]/ ) | |
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | 1 | ▶ | sub null_or_escape { |
|
Subroutine
null_or_escape: called CC=1 SCAR=0.0
Pod
documented
| |||
| 157 | 1 | my $re = qr{\%(?:00|1b|1B)}; | |
| 158 | return ( | ||
| 159 | 1 | REQUEST_URI => $re, | |
| 160 | ) | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | 1 | ▶ | sub protocol_in_path_or_referer { |
|
Subroutine
protocol_in_path_or_referer: called CC=1 SCAR=0.0
Pod
documented
| |||
| 165 | 1 | my $re = qr{\b(?:file|dns|jndi|unix|ldap|php):}; | |
| 166 | return ( | ||
| 167 | 1 | PATH_INFO => $re, | |
| 168 | QUERY_STRING => $re, | ||
| 169 | HTTP_REFERER => $re, | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | 1 | ▶ | sub require_content { |
|
Subroutine
require_content: called CC=1 SCAR=0.0
Pod
documented
| |||
| 175 | return ( | ||
| 176 | -and => [ | ||
| 177 | REQUEST_METHOD => qr{^(?:POST|PUT)$}, | ||
| 178 | 3 | ▶ | CONTENT_LENGTH => sub { !$_[0] }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 179 | 1 | ], | |
| 180 | ); | ||
| 181 | } | ||
| 182 | |||
| 183 | |||
| 184 | 1 | ▶ | sub script_extensions { |
|
Subroutine
script_extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 185 | 1 | my $re = qr{[.](?:as[hp]x?|axd|bat|cc?|cfm|cgi|com|csc|dll|do|exe|jspa?|lua|mvc?|php5?|p[lm]|ps[dm]?[1h]|sht?|shtml|sql)\b}; | |
| 186 | return ( | ||
| 187 | 1 | PATH_INFO => $re, | |
| 188 | QUERY_STRING => $re, | ||
| 189 | ) | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 1 | ▶ | sub script_injection { |
|
Subroutine
script_injection: called CC=1 SCAR=0.0
Pod
documented
| |||
| 194 | 1 | my $re = qr{data:(?:application|text)/(?:x-)?(?:javascript|wasm);}; | |
| 195 | return ( | ||
| 196 | 1 | PATH_INFO => $re, | |
| 197 | QUERY_STRING => $re, | ||
| 198 | ); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | 1 | ▶ | sub system_dirs { |
|
Subroutine
system_dirs: called CC=1 SCAR=0.0
Pod
documented
| |||
| 203 | 1 | my $re = qr{/(?:s?adm|bin|etc|usr|var|srv|opt|__MACOSX|META-INF)/}; | |
| 204 | return ( | ||
| 205 | 1 | PATH_INFO => $re, | |
| 206 | QUERY_STRING => $re, | ||
| 207 | ); | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | 1 | ▶ | sub unexpected_content { |
|
Subroutine
unexpected_content: called CC=1 SCAR=0.0
Pod
documented
| |||
| 212 | return ( | ||
| 213 | -and => [ | ||
| 214 | REQUEST_METHOD => qr{^(?:GET|HEAD|CONNECT|OPTIONS|TRACE)$}, | ||
| 215 | 6 | ▶ | CONTENT_LENGTH => sub { !!$_[0] }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 216 | 1 | ], | |
| 217 | ); | ||
| 218 | } | ||
| 219 | |||
| 220 | |||
| 221 | 1 | ▶ | sub webdav_methods { |
|
Subroutine
webdav_methods: called CC=1 SCAR=0.0
Pod
documented
| |||
| 222 | 1 | return ( REQUEST_METHOD => | |
| 223 | qr{^(COPY|LOCK|MKCOL|MOVE|PROPFIND|PROPPATCH|UNLOCK)$} ); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | 1 | ▶ | sub wordpress { |
|
Subroutine
wordpress: called CC=1 SCAR=0.0
Pod
documented
| |||
| 228 | 1 | return ( PATH_INFO => qr{\b(?:wp(-\w+)?|wordpress)\b} ); | |
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | 1 | 1; | |
| 233 | |||
| 234 | __END__ | ||