| 1 | package MojoMojo::Formatter::Defang; | ||||||||||||||||||||||||||||||||||||
| 2 | 27 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 27 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use parent qw/MojoMojo::Formatter/; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use MojoMojo::Declaw; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | 27 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | MojoMojo::Formatter::Defang - Scrub user HTML and XSS | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | This formatter makes sure only a safe range of tags are | ||||||||||||||||||||||||||||||||||||
| 15 | allowed, using L<MojoMojo::Defang>; It also tries to remove XSS attempts. | ||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||
| 19 | =head2 format_content_order | ||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||
| 21 | Format order can be 1-99. The Defang formatter runs on 16, just after the main | ||||||||||||||||||||||||||||||||||||
| 22 | formatter, in order to catch direct user input. Defang trusts the main formatter | ||||||||||||||||||||||||||||||||||||
| 23 | and all subsequently ran plugins to not output unsafe HTML. | ||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | 1860 | ▶ | sub format_content_order { 16 } | ||||||||||||||||||||||||||||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||
| 29 | =head2 defang_tags_callback | ||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||
| 31 | Callback for custom handling specific HTML tags | ||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | =cut | ||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||
| 35 | 30 | ▶ | sub defang_tags_callback { | ||||||||||||||||||||||||||||||||||
|
Subroutine
defang_tags_callback: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 36 | my ( | ||||||||||||||||||||||||||||||||||||
| 37 | 30 | $c, $defang, $open_angle, | |||||||||||||||||||||||||||||||||||
| 38 | $lc_tag, $is_end_tag, $attribute_hash, | ||||||||||||||||||||||||||||||||||||
| 39 | $close_angle, $html_r, $out_r | ||||||||||||||||||||||||||||||||||||
| 40 | ) = @_; | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | # Explicitly whitelist this tag, although unsafe | ||||||||||||||||||||||||||||||||||||
| 43 | 30 | ▶ | return 0 if $lc_tag eq 'embed'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 44 | 28 | ▶ | return 0 if $lc_tag eq 'object'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 45 | 26 | ▶ | return 0 if $lc_tag eq 'param'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 46 | 20 | ▶ | return 0 if $lc_tag eq 'pre'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||
| 48 | # I am not sure what to do with this tag, so process as | ||||||||||||||||||||||||||||||||||||
| 49 | # HTML::Defang normally would | ||||||||||||||||||||||||||||||||||||
| 50 | #return 2 if $lc_tag eq 'img'; | ||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||
| 53 | =head2 defang_url_callback | ||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | Callback for custom handling URLs in HTML attributes as well as | ||||||||||||||||||||||||||||||||||||
| 56 | styletag/attribute declarations | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||
| 60 | 124 | ▶ | sub defang_url_callback { | ||||||||||||||||||||||||||||||||||
|
Subroutine
defang_url_callback: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 61 | 124 | my ( $c, $defang, $lc_tag, $lc_attr_key, $attr_val_r, $attribute_hash, | |||||||||||||||||||||||||||||||||||
| 62 | $html_r ) | ||||||||||||||||||||||||||||||||||||
| 63 | = @_; | ||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||
| 65 | # Do not allow javascript to start a URL in tag attributes or stylesheets | ||||||||||||||||||||||||||||||||||||
| 66 | 124 | ▶ | return 1 if $$attr_val_r =~ /^javascript/i; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | # Do not allow bypassing of protocol | ||||||||||||||||||||||||||||||||||||
| 68 | 120 | ▶ | return 1 if $$attr_val_r =~ m{^//}i; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | # Explicitly defang this URL in tag attributes or stylesheets | ||||||||||||||||||||||||||||||||||||
| 71 | 117 | ▶ | return 1 if $$attr_val_r =~ /youporn.com/i; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | # Allow URL's otherwise | ||||||||||||||||||||||||||||||||||||
| 74 | 117 | return 0; | |||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | =head2 defang_css_callback | ||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | Callback for custom handling style tags/attributes. | ||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 4 | ▶ | sub defang_css_callback { | ||||||||||||||||||||||||||||||||||
|
Subroutine
defang_css_callback: called CC=7 SCAR=20.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 84 | 4 | my ( $c, $defang, $selectors, $selector_rules, $tag, $is_attr ) = @_; | |||||||||||||||||||||||||||||||||||
| 85 | 4 | my $i = 0; | |||||||||||||||||||||||||||||||||||
| 86 | 4 | foreach (@$selectors) { | |||||||||||||||||||||||||||||||||||
| 87 | 4 | my $selector_rule = $$selector_rules[$i]; | |||||||||||||||||||||||||||||||||||
| 88 | 4 | foreach my $key_value_rules (@$selector_rule) { | |||||||||||||||||||||||||||||||||||
| 89 | 4 | foreach my $key_value_rule (@$key_value_rules) { | |||||||||||||||||||||||||||||||||||
| 90 | 6 | my ( $key, $value ) = @$key_value_rule; | |||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | # Comment out any ’!important’ directive | ||||||||||||||||||||||||||||||||||||
| 93 | 6 | ▶ | $$key_value_rule[2] = 1 if $value =~ '!important'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | # Comment out any ’position=fixed;’ declaration | ||||||||||||||||||||||||||||||||||||
| 96 | 6 | ▶ | $$key_value_rule[2] = 1 | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key =~ /position/ and $value =~ /fixed/
MC/DC0%0/2
$key =~ /position/ and $value =~ /fixed/
$key =~ /position/
$value =~ /fixed/
Truth table33%1/3
A and B
A= $key =~ /position/B= $value =~ /fixed/
| |||||||||||||||||||||||||||||||||||||
| 97 | if $key =~ 'position' && $value =~ 'fixed'; | ||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||
| 100 | 4 | $i++; | |||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||
| 104 | =head2 defang_attribs_callback | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | Callback for custom handling HTML tag attributes. | ||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||
| 110 | 61 | ▶ | sub defang_attribs_callback { | ||||||||||||||||||||||||||||||||||
|
Subroutine
defang_attribs_callback: called CC=13 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 111 | 61 | my ( $c, $defang, $lc_tag, $lc_attr_key, $attr_val_r, $html_r ) = @_; | |||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||
| 113 | # if $lc_attr_key eq 'value'; | ||||||||||||||||||||||||||||||||||||
| 114 | # Initial Defang effort on attributes applies specifically to 'src' | ||||||||||||||||||||||||||||||||||||
| 115 | 61 | ▶ | if ( $lc_attr_key eq 'src' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 116 | 16 | my $src_uri_object = URI->new($$attr_val_r); | |||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||
| 118 | # Allow src URI's from configuration. | ||||||||||||||||||||||||||||||||||||
| 119 | 16 | my @allowed_src_regex; | |||||||||||||||||||||||||||||||||||
| 120 | # Tests may not have a $c | ||||||||||||||||||||||||||||||||||||
| 121 | 16 | ▶ | if ( defined $c ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | if ( exists $c->stash->{allowed_src_regexes} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 124 | 0 | @allowed_src_regex = @{ $c->stash->{allowed_src_regexes} }; | |||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||||||||||
| 127 | 7 | my $allowed_src = $c->config->{allowed}{src}; | |||||||||||||||||||||||||||||||||||
| 128 | my @allowed_src = | ||||||||||||||||||||||||||||||||||||
| 129 | 7 | ▶ | ref $allowed_src ? @{$allowed_src} : ($allowed_src); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | @allowed_src_regex = map { qr/$_/ } @allowed_src if $allowed_src[0]; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||
| 132 | # TODO: Shouldn't this be using pref cache? | ||||||||||||||||||||||||||||||||||||
| 133 | 7 | $c->stash->{allowed_src_regexes} = \@allowed_src_regex; | |||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||
| 136 | 16 | for my $allowed_src_regex (@allowed_src_regex) { | |||||||||||||||||||||||||||||||||||
| 137 | 19 | ▶ | if ( $$attr_val_r =~ $allowed_src_regex ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 138 | 1 | return 0; | |||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||
| 143 | # When $c and src uri authority are defined we want to make sure | ||||||||||||||||||||||||||||||||||||
| 144 | # it matches the server of the img src. i.e. we allow images from the | ||||||||||||||||||||||||||||||||||||
| 145 | # local server whether the URI is relative or absolute.. | ||||||||||||||||||||||||||||||||||||
| 146 | 15 | ▶ | if ( defined $c && defined $src_uri_object->authority ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
defined $c and defined $src_uri_object->authority
MC/DC100%2/2
defined $c and defined $src_uri_object->authority
defined $c
defined $src_uri_object->authority
Truth table100%3/3
A and B
A= defined $cB= defined $src_uri_object->authority
| |||||||||||||||||||||||||||||||||||||
| 147 | 4 | ▶ | if ( $c->request->uri->authority eq $src_uri_object->authority ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 148 | 0 | return 0; | |||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||||||||||
| 151 | 4 | return 1; | |||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||
| 154 | # We have an authority but no context. | ||||||||||||||||||||||||||||||||||||
| 155 | # Probably means we're testing with just the Defang formatter | ||||||||||||||||||||||||||||||||||||
| 156 | # instead of the Full formatter chain. | ||||||||||||||||||||||||||||||||||||
| 157 | # We will defang any src's left with an authority (defang_src) | ||||||||||||||||||||||||||||||||||||
| 158 | # since the approved ones were already allowed in above. | ||||||||||||||||||||||||||||||||||||
| 159 | elsif ( defined $src_uri_object->authority ) { | ||||||||||||||||||||||||||||||||||||
| 160 | 5 | return 1; | |||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||
| 162 | # Explicitly defang javascript in img src. | ||||||||||||||||||||||||||||||||||||
| 163 | elsif ( $$attr_val_r =~ m{javascript}i ) { | ||||||||||||||||||||||||||||||||||||
| 164 | 3 | return 1; | |||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||
| 166 | else { | ||||||||||||||||||||||||||||||||||||
| 167 | 3 | return 0; | |||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||
| 170 | 45 | return 0; | |||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||
| 173 | =head2 format_content | ||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||
| 175 | Calls the formatter. Takes a ref to the content as well as the | ||||||||||||||||||||||||||||||||||||
| 176 | context object. | ||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||
| 180 | 141 | ▶ | sub format_content { | ||||||||||||||||||||||||||||||||||
|
Subroutine
format_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 181 | 141 | my ( $self, $content, $c ) = @_; | |||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | 141 | my $defang = MojoMojo::Declaw->new( | |||||||||||||||||||||||||||||||||||
| 184 | context => $c, | ||||||||||||||||||||||||||||||||||||
| 185 | fix_mismatched_tags => 1, | ||||||||||||||||||||||||||||||||||||
| 186 | tags_to_callback => [qw/br embed object param img/], | ||||||||||||||||||||||||||||||||||||
| 187 | tags_callback => \&defang_tags_callback, | ||||||||||||||||||||||||||||||||||||
| 188 | url_callback => \&defang_url_callback, | ||||||||||||||||||||||||||||||||||||
| 189 | css_callback => \&defang_css_callback, | ||||||||||||||||||||||||||||||||||||
| 190 | attribs_to_callback => [qw(src value title)], | ||||||||||||||||||||||||||||||||||||
| 191 | attribs_callback => \&defang_attribs_callback, | ||||||||||||||||||||||||||||||||||||
| 192 | ); | ||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||
| 194 | 141 | $$content = $defang->defang($$content); | |||||||||||||||||||||||||||||||||||
| 195 | 141 | return; | |||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||
| 198 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||
| 200 | L<MojoMojo>, L<Module::Pluggable::Ordered>, L<MojoMojo::Defang> | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||
| 204 | Marcus Ramberg <mramberg@cpan.org> | ||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||
| 206 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||
| 208 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||
| 209 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||
| 213 | 27 | 1; | |||||||||||||||||||||||||||||||||||