Skip to content

Commit

Permalink
Convert <style> blocks to inline before scrubbing the HTML
Browse files Browse the repository at this point in the history
Scrubber drops <style> blocks by default, and it's risky to keep them
considering they may affect other parts of RT pages. By converting them to
inline styles in advance, they can apply to related HTML only, no more, no
less.
  • Loading branch information
sunnavy committed Dec 24, 2024
1 parent d18adc5 commit fdff41f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions etc/cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ requires 'Convert::Color';
requires 'Crypt::Eksblowfish';
requires 'CSS::Minifier::XS';
requires 'CSS::Squish', '>= 0.06';
requires 'CSS::Inliner', '>= 4018';
requires 'Data::GUID';
requires 'Data::ICal';
requires 'Data::Page';
Expand Down
7 changes: 7 additions & 0 deletions lib/RT/Interface/Web/Scrubber.pm
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ sub scrub {
warn "HTML::Gumbo pre-parse failed: $@" if $@;
}

if ( $Content =~ /<style.*>/ ) {
require CSS::Inliner;
my $css_inliner = CSS::Inliner->new;
$css_inliner->read( { html => $Content } );
$Content = $css_inliner->inlinify();
}

return $self->SUPER::scrub($Content);
}

Expand Down
7 changes: 7 additions & 0 deletions lib/RT/Transaction.pm
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ sub Content {
if ($args{Type} ne 'text/html') {
$content = RT::Interface::Email::ConvertHTMLToText($content);
} else {
if ( $content =~ /<style.*>/ ) {
require CSS::Inliner;
my $css_inliner = CSS::Inliner->new;
$css_inliner->read( { html => $content } );
$content = $css_inliner->inlinify();
}

# Scrub out <html>, <head>, <meta>, and <body>, and
# leave all else untouched.
my $scrubber = HTML::Scrubber->new();
Expand Down

0 comments on commit fdff41f

Please sign in to comment.