1HTML::Restrict(3)     User Contributed Perl Documentation    HTML::Restrict(3)
2
3
4

NAME

6       HTML::Restrict - Strip unwanted HTML tags and attributes
7

VERSION

9       version v2.5.0
10

SYNOPSIS

12           use HTML::Restrict;
13
14           my $hr = HTML::Restrict->new();
15
16           # use default rules to start with (strip away all HTML)
17           my $processed = $hr->process('  <b>i am bold</b>  ');
18
19           # $processed now equals: 'i am bold'
20
21           # Now, a less restrictive example:
22           $hr = HTML::Restrict->new(
23               rules => {
24                   b   => [],
25                   img => [qw( src alt / )]
26               }
27           );
28
29           my $html = q[<body><b>hello</b> <img src="pic.jpg" alt="me" id="test" /></body>];
30           $processed = $hr->process( $html );
31
32           # $processed now equals: <b>hello</b> <img src="pic.jpg" alt="me" />
33

DESCRIPTION

35       This module uses HTML::Parser to strip HTML from text in a restrictive
36       manner.  By default all HTML is restricted.  You may alter the default
37       behaviour by supplying your own tag rules.
38

CONSTRUCTOR AND STARTUP

40   new()
41       Creates and returns a new HTML::Restrict object.
42
43           my $hr = HTML::Restrict->new()
44
45       HTML::Restrict doesn't require any params to be passed to new.  If your
46       goal is to remove all HTML from text, then no further setup is
47       required.  Just pass your text to the process() method and you're done:
48
49           my $plain_text = $hr->process( $html );
50
51       If you need to set up specific rules, have a look at the params which
52       HTML::Restrict recognizes:
53
54       ·   "rules => \%rules"
55
56           Sets the rules which will be used to process your data.  By default
57           all HTML tags are off limits.  Use this argument to define the HTML
58           elements and corresponding attributes you'd like to use.
59           Essentially, consider the default behaviour to be:
60
61               rules => {}
62
63           Rules should be passed as a HASHREF of allowed tags.  Each hash
64           value should represent the allowed attributes for the listed tag.
65           For example, if you want to allow a fair amount of HTML, you can
66           try something like this:
67
68               my %rules = (
69                   a       => [qw( href target )],
70                   b       => [],
71                   caption => [],
72                   center  => [],
73                   em      => [],
74                   i       => [],
75                   img     => [qw( alt border height width src style )],
76                   li      => [],
77                   ol      => [],
78                   p       => [qw(style)],
79                   span    => [qw(style)],
80                   strong  => [],
81                   sub     => [],
82                   sup     => [],
83                   table   => [qw( style border cellspacing cellpadding align )],
84                   tbody   => [],
85                   td      => [],
86                   tr      => [],
87                   u       => [],
88                   ul      => [],
89               );
90
91               my $hr = HTML::Restrict->new( rules => \%rules )
92
93           Or, to allow only bolded text:
94
95               my $hr = HTML::Restrict->new( rules => { b => [] } );
96
97           Allow bolded text, images and some (but not all) image attributes:
98
99               my %rules = (
100                   b   => [ ],
101                   img => [qw( src alt width height border / )
102               );
103               my $hr = HTML::Restrict->new( rules => \%rules );
104
105           Since HTML::Parser treats a closing slash as an attribute, you'll
106           need to add "/" to your list of allowed attributes if you'd like
107           your tags to retain closing slashes.  For example:
108
109               my $hr = HTML::Restrict->new( rules =>{ hr => [] } );
110               $hr->process( "<hr />"); # returns: <hr>
111
112               my $hr = HTML::Restrict->new( rules =>{ hr => [qw( / )] } );
113               $hr->process( "<hr />"); # returns: <hr />
114
115           HTML::Restrict strips away any tags and attributes which are not
116           explicitly allowed. It also rebuilds your explicitly allowed tags
117           and places their attributes in the order in which they appear in
118           your rules.
119
120           So, if you define the following rules:
121
122               my %rules = (
123                   ...
124                   img => [qw( src alt title width height id / )]
125                   ...
126               );
127
128           then your image tags will all be built like this:
129
130               <img src=".." alt="..." title="..." width="..." height="..." id=".." />
131
132           This gives you greater consistency in your tag layout.  If you
133           don't care about element order you don't need to pay any attention
134           to this, but you should be aware that your elements are being
135           reconstructed rather than just stripped down.
136
137           As of 2.1.0, you can also specify a regex to be tested against the
138           attribute value. This feature should be considered experimental for
139           the time being:
140
141               my $hr = HTML::Restrict->new(
142                   rules => {
143                       iframe => [
144                           qw( width height allowfullscreen ),
145                           {   src         => qr{^http://www\.youtube\.com},
146                               frameborder => qr{^(0|1)$},
147                           }
148                       ],
149                       img => [ qw( alt ), { src => qr{^/my/images/} }, ],
150                   },
151               );
152
153               my $html = '<img src="http://www.example.com/image.jpg" alt="Alt Text">';
154               my $processed = $hr->process( $html );
155
156               # $processed now equals: <img alt="Alt Text">
157
158           As of 2.3.0, the value to be tested against can also be a code
159           reference.  The code reference will be passed the value of the
160           attribute, and should return either a string to use for the
161           attribute value, or undef to remove the attribute.
162
163               my $hr = HTML::Restrict->new(
164                   rules => {
165                       span => [
166                           { style     => sub {
167                               my $value = shift;
168                               # all colors are orange
169                               $value =~ s/\bcolor\s*:\s*[^;]+/color: orange/g;
170                               return $value;
171                           } }
172                       ],
173                   },
174               );
175
176               my $html = '<span style="color: #0000ff;">This is blue</span>';
177               my $processed = $hr->process( $html );
178
179               # $processed now equals: <span style="color: orange;">
180
181       ·   "trim => [0|1]"
182
183           By default all leading and trailing spaces will be removed when
184           text is processed.  Set this value to 0 in order to disable this
185           behaviour.
186
187       ·   "uri_schemes => [undef, 'http', 'https', 'irc', ... ]"
188
189           As of version 1.0.3, URI scheme checking is performed on all href
190           and src tag attributes. The following schemes are allowed out of
191           the box.  No action is required on your part:
192
193               [ undef, 'http', 'https' ]
194
195           (undef represents relative URIs). These restrictions have been put
196           in place to prevent XSS in the form of:
197
198               <a href="javascript:alert(document.cookie)">click for cookie!</a>
199
200           See URI for more detailed info on scheme parsing.  If, for example,
201           you wanted to filter out every scheme barring SSL, you would do it
202           like this:
203
204               uri_schemes => ['https']
205
206           This feature is new in 1.0.3.  Previous to this, there was no
207           schema checking at all.  Moving forward, you'll need to whitelist
208           explicitly all URI schemas which are not supported by default.
209           This is in keeping with the whitelisting behaviour of this module
210           and is also the safest possible approach.  Keep in mind that
211           changes to uri_schemes are not additive, so you'll need to include
212           the defaults in any changes you make, should you wish to keep them:
213
214               # defaults + irc + mailto
215               uri_schemes => [ 'undef', 'http', 'https', 'irc', 'mailto' ]
216
217       ·   allow_declaration => [0|1]
218
219           Set this value to true if you'd like to allow/preserve DOCTYPE
220           declarations in your content.  Useful when cleaning up your own
221           static files or templates. This feature is off by default.
222
223               my $html = q[<!doctype html><body>foo</body>];
224
225               my $hr = HTML::Restrict->new( allow_declaration => 1 );
226               $html = $hr->process( $html );
227               # $html is now: "<!doctype html>foo"
228
229       ·   allow_comments => [0|1]
230
231           Set this value to true if you'd like to allow/preserve HTML
232           comments in your content.  Useful when cleaning up your own static
233           files or templates. This feature is off by default.
234
235               my $html = q[<body><!-- comments! -->foo</body>];
236
237               my $hr = HTML::Restrict->new( allow_comments => 1 );
238               $html = $hr->process( $html );
239               # $html is now: "<!-- comments! -->foo"
240
241       ·   max_parser_loops => [Integer]
242
243           Defaults to 25.  Should never be less than 2.
244
245           As of v2.4.0, calling "process()" will force the parser to clean
246           the text multiple times, stopping only once the text is no longer
247           changed or once "max_parser_loops" has been reached.
248
249           The reason for this is that HTML::Parser could take malformed HTML
250           and turn it into well formed HTML.  This can defeat our processing
251           logic and allow malicious input to be returned.  In order to
252           mitigate this, we will clean all input at least two times.  If the
253           second attempt at cleaning does not match the previous attempt, we
254           will make a third attempt and so on.  This helps to ensure that we
255           get the expected output.
256
257           If we are unable to get unchanged values after reaching
258           "max_parser_loops", an exception will be thrown.  Returning
259           partially cleaned text would be wrong, as would be returning
260           "undef" or an empty string.  Throwing an exception forces the user
261           to choose the appropriate way of dealing with this.
262
263           If you choose to set this value, please note that it can be no less
264           than 2, or the parser will never be able to make a comparison with
265           a previous value.  An exception will be thrown if you attempt to
266           set this to a value less than 2.
267
268       ·   replace_img => [0|1|CodeRef]
269
270           Set the value to true if you'd like to have img tags replaced with
271           "[IMAGE: ...]" containing the alt attribute text.  If you set it to
272           a code reference, you can provide your own replacement (which may
273           even contain HTML).
274
275               sub replacer {
276                   my ($tagname, $attr, $text) = @_; # from HTML::Parser
277                   return qq{<a href="$attr->{src}">IMAGE: $attr->{alt}</a>};
278               }
279
280               my $hr = HTML::Restrict->new( replace_img => \&replacer );
281
282           This attribute will only take effect if the img tag is not included
283           in the allowed HTML.
284
285       ·   strip_enclosed_content => [0|1]
286
287           The default behaviour up to 1.0.4 was to preserve the content
288           between script and style tags, even when the tags themselves were
289           being deleted.  So, you'd be left with a bunch of JavaScript or
290           CSS, just with the enclosing tags missing.  This is almost never
291           what you want, so starting at 1.0.5 the default will be to remove
292           any script or style info which is enclosed in these tags, unless
293           they have specifically been whitelisted in the rules.  This will be
294           a sane default when cleaning up content submitted via a web form.
295           However, if you're using HTML::Restrict to purge your own HTML you
296           can be more restrictive.
297
298               # strip the head section, in addition to JS and CSS
299               my $html = '<html><head>...</head><body>...<script>JS here</script>foo';
300
301               my $hr = HTML::Restrict->new(
302                   strip_enclosed_content => [ 'script', 'style', 'head' ]
303               );
304
305               $html = $hr->process( $html );
306               # $html is now '<html><body>...foo';
307
308           The caveat here is that HTML::Restrict will not try to fix broken
309           HTML. In the above example, if you have any opening script, style
310           or head tags which don't also include matching closing tags, all
311           following content will be stripped away, regardless of any parent
312           tags.
313
314           Keep in mind that changes to strip_enclosed_content are not
315           additive, so if you are adding additional tags you'll need to
316           include the entire list of tags whose enclosed content you'd like
317           to remove.  This feature strips script and style tags by default.
318

SUBROUTINES/METHODS

320   process( $html )
321       This is the method which does the real work.  It parses your data,
322       removes any tags and attributes which are not specifically allowed and
323       returns the resulting text.  Requires and returns a SCALAR.
324
325   get_rules
326       Accessor which returns a hash ref of the current rule set.
327
328   get_uri_schemes
329       Accessor which returns an array ref of the current valid uri schemes.
330

CAVEATS

332       Please note that all tag and attribute names passed via the rules param
333       must be supplied in lower case.
334
335           # correct
336           my $hr = HTML::Restrict->new( rules => { body => ['onload'] } );
337
338           # throws a fatal error
339           my $hr = HTML::Restrict->new( rules => { Body => ['onLoad'] } );
340

MOTIVATION

342       There are already several modules on the CPAN which accomplish much of
343       the same thing, but after doing a lot of poking around, I was unable to
344       find a solution with a simple setup which I was happy with.
345
346       The most common use case might be stripping HTML from user submitted
347       data completely or allowing just a few tags and attributes to be
348       displayed.  With the exception of URI scheme checking, this module
349       doesn't do any validation on the actual content of the tags or
350       attributes.  If this is a requirement, you can either mess with the
351       parser object, post-process the text yourself or have a look at one of
352       the more feature-rich modules in the SEE ALSO section below.
353
354       My aim here is to keep things easy and, hopefully, cover a lot of the
355       less complex use cases with just a few lines of code and some brief
356       documentation.  The idea is to be up and running quickly.
357

SEE ALSO

359       HTML::TagFilter, HTML::Defang, MojoMojo::Declaw, HTML::StripScripts,
360       HTML::Detoxifier, HTML::Sanitizer, HTML::Scrubber
361

ACKNOWLEDGEMENTS

363       Thanks to Raybec Communications <http://www.raybec.com> for funding my
364       work on this module and for releasing it to the world.
365
366       Thanks also to the following for patches, bug reports and assistance:
367
368       Mark Jubenville (ioncache)
369
370       Duncan Forsyth
371
372       Rick Moore
373
374       Arthur Axel 'fREW' Schmidt
375
376       perlpong
377
378       David Golden
379
380       Graham TerMarsch
381
382       Dagfinn Ilmari Mannsåker
383
384       Graham Knop
385
386       Carwyn Ellis
387

AUTHOR

389       Olaf Alders <olaf@wundercounter.com>
390
392       This software is copyright (c) 2013-2017 by Olaf Alders.
393
394       This is free software; you can redistribute it and/or modify it under
395       the same terms as the Perl 5 programming language system itself.
396
397
398
399perl v5.28.1                      2019-02-08                 HTML::Restrict(3)
Impressum