HTML::Restrict(3)

πŸ’… HTML::Restrict

okay bestie, gather round, because this one is the manual page for HTML::Restrict (that's the manual name, yes, HTML::Restrict(3), we're being fancy) and it is genuinely one of the most useful little modules you will ever meet 🫢


✨ NAME ✨

HTML::Restrict β€” strip unwanted HTML tags and attributes. That's it, that's the whole vibe. It takes your HTML, yeets the stuff you don't want, and hands it back all clean and safe πŸ’–


πŸŽ€ VERSION πŸŽ€

version v3.0.2 β€” that's the version, no cap, write it down πŸ“


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the whole flow, literally just copy this energy:

use HTML::Restrict;

my $hr = HTML::Restrict->new();

# use default rules to start with (strip away all HTML)
my $processed = $hr->process('  <b>i am bold</b>  ');

# $processed now equals: 'i am bold'

so you make a HTML::Restrict object with HTML::Restrict->new(), no arguments, and the default rules are savage β€” they strip away all HTML. Look at that: you fed it <b>i am bold</b> and it gave you back just 'i am bold'. the <b> tags? gone. icon behavior, honestly πŸ‘‘

then, a less restrictive example, because sometimes you do want some tags to survive:

# Now, a less restrictive example:
$hr = HTML::Restrict->new(
    rules => {
        b   => [],
        img => [qw( src alt / )]
    }
);

my $html = q[<body><b>hello</b> <img src="pic.jpg" alt="me" id="test" /></body>];
$processed = $hr->process( $html );

# $processed now equals: <b>hello</b> <img src="pic.jpg" alt="me" />

okay so what's happening here β€” you pass rules to new(), and each key is a tag you're allowing, and the arrayref next to it is the attributes that tag is allowed to keep πŸŽ€ so b => [] means "b is allowed but with zero attributes" and img => [qw( src alt / )] means "img may keep src, alt and the self-closing / marker". Note the / in there, bestie, don't skip it πŸ™ˆ

you start with <body><b>hello</b> <img src="pic.jpg" alt="me" id="test" /></body> and end up with <b>hello</b> <img src="pic.jpg" alt="me" />:

  • <body> β€” not in the rules, so it's gone πŸ’€
  • <b>hello</b> β€” allowed, no attributes, slayed βœ…
  • <img src="pic.jpg" alt="me" id="test" /> β€” src and alt survive, id="test" gets dropped because id was never invited to the party 🚫

the way it just knows which attributes to keep. obsessed.


🌸 DESCRIPTION 🌸

so here's the tea β˜• β€” this module uses HTML::Parser to strip HTML from text in a restrictive manner. restrictive as in: it assumes nothing is allowed unless you say so. very "you are not on the list, you are not coming in" energy πŸšͺ

by default, all HTML is restricted. every single tag. gone. bye. ✨

but you're the main character here, so you may alter that default behaviour by supplying your own tag rules β€” that's the rules thing we just did in the synopsis. your rules, your rules, iykyk πŸ‘‘

fr fr though: default = strip everything. you want something to survive, you gotta explicitly allow it. no silent surprises, no vibes-based parsing. we love a predictable icon πŸ’…

more coming, bestie β€” don't go anywhere πŸ¦‹

🌸 CONSTRUCTOR AND STARTUP 🌸

✨ new() ✨

okay bestie, this is where the whole thing is born β€” it creates and returns a brand-new HTML::Restrict object πŸ’–

my $hr = HTML::Restrict->new()

HTML::Restrict doesn't need any params passed to new. Like, none. If your whole goal in life is to strip all the HTML out of some text, you literally need zero extra setup. Just yeet your text into the process() method and you're done, periodt:

my $plain_text = $hr->process( $html );

But if you do need to set up specific rules, take a lil look at the params HTML::Restrict recognizes πŸ‘‡


  • rules => \%rules β€” this sets the rules that'll be used to process your data. By default every single HTML tag is off limits 🚫. Use this argument to define the HTML elements and the corresponding attributes you actually want to use. Basically, think of the default behaviour as being:
rules => {}

Rules get passed as a HASHREF of allowed tags. Each hash value should represent the allowed attributes for the tag it's listed under. So, for example, if you wanna allow a fair amount of HTML, you can try something like this ✨:

my %rules = (
    a       => [qw( href target )],
    b       => [],
    caption => [],
    center  => [],
    em      => [],
    i       => [],
    img     => [qw( alt border height width src style )],
    li      => [],
    ol      => [],
    p       => [qw(style)],
    span    => [qw(style)],
    strong  => [],
    sub     => [],
    sup     => [],
    table   => [qw( style border cellspacing cellpadding align )],
    tbody   => [],
    td      => [],
    tr      => [],
    u       => [],
    ul      => [],
);

my $hr = HTML::Restrict->new( rules => \%rules )

Or, if you literally only want bolded text to survive:

my $hr = HTML::Restrict->new( rules => { b => [] } );

Allow bolded text, images, and some (but not all) image attributes:

my %rules = (
    b   => [ ],
    img => [qw( src alt width height border / )
);
my $hr = HTML::Restrict->new( rules => \%rules );

okay so here's a thing you're gonna wanna know: HTML::Parser treats a closing slash as an attribute, so if you want your tags to keep their closing slashes you've gotta add "/" to your list of allowed attributes. Like this, bestie:

my $hr = HTML::Restrict->new( rules =>{ hr => [] } );
$hr->process( "<hr />"); # returns: <hr>

my $hr = HTML::Restrict->new( rules =>{ hr => [qw( / )] } );
$hr->process( "<hr />"); # returns: <hr />

HTML::Restrict strips away any tags and attributes that are not explicitly allowed. It also rebuilds your explicitly allowed tags and places their attributes in the order in which they appear in your rules πŸ”¨

So if you define these rules:

my %rules = (
    ...
    img => [qw( src alt title width height id / )]
    ...
);

then all your image tags are gonna get built like this:

<img src=".." alt="..." title="..." width="..." height="..." id=".." />

which gives you way greater consistency in your tag layout πŸ‘‘ If you don't care about element order, you can just ignore all of this β€” but you should be aware that your elements are being reconstructed rather than just stripped down. It's giving glow-up, not just haircut πŸ’…

As of 2.1.0, you can also specify a regex to be tested against the attribute value πŸ¦‹ This feature should be considered experimental for the time being:

my $hr = HTML::Restrict->new(
    rules => {
        iframe => [
            qw( width height allowfullscreen ),
            {   src         => qr{^http://www\.youtube\.com},
                frameborder => qr{^(0|1)$},
            }
        ],
        img => [ qw( alt ), { src => qr{^/my/images/} }, ],
    },
);

my $html = '<img src="http://www.example.com/image.jpg" alt="Alt Text">';
my $processed = $hr->process( $html );

# $processed now equals: <img alt="Alt Text">

As of 2.3.0, the value being tested against can also be a code reference πŸ”₯ The code ref gets passed the value of the attribute, and it should return either a string to use for the attribute value, or undef to remove the attribute entirely:

my $hr = HTML::Restrict->new(
    rules => {
        span => [
            { style     => sub {
                my $value = shift;
                # all colors are orange
                $value =~ s/\bcolor\s*:\s*[^;]+/color: orange/g;
                return $value;
            } }
        ],
    },
);

my $html = '<span style="color: #0000ff;">This is blue</span>';
my $processed = $hr->process( $html );

# $processed now equals: <span style="color: orange;">

  • trim => [0|1] β€” by default all leading and trailing spaces get removed when text is processed. Set this value to 0 if you wanna disable that behaviour πŸŽ€

  • uri_schemes => [undef, 'http', 'https', 'irc', ... ] β€” as of version 1.0.3, URI scheme checking is performed on all href and src tag attributes. These schemes are allowed right out of the box, and no action is required on your part:
[ undef, 'http', 'https' ]

(undef represents relative URIs). These restrictions have been put in place to prevent XSS in the form of:

<a href="javascript:alert(document.cookie)">click for cookie!</a>

see URI for more detailed info on scheme parsing. If, for example, you wanted to filter out every scheme barring SSL, you'd do it like this:

uri_schemes => ['https']

This feature is new in 1.0.3. Previous to this, there was no schema checking at all 😭 Going forward, you'll need to whitelist explicitly all URI schemas that aren't supported by default. This is in keeping with the whitelisting behaviour of this module and is also the safest possible approach πŸ’– Keep in mind that changes to uri_schemes are not additive, so you'll need to include the defaults in any changes you make, should you wish to keep them:

# defaults + irc + mailto
uri_schemes => [ 'undef', 'http', 'https', 'irc', 'mailto' ]

  • allow_declaration => [0|1] β€” set this value to true if you'd like to allow/preserve DOCTYPE declarations in your content πŸ“œ Super useful when you're cleaning up your own static files or templates. This feature is off by default.
my $html = q[<!doctype html><body>foo</body>];

my $hr = HTML::Restrict->new( allow_declaration => 1 );
$html = $hr->process( $html );
# $html is now: "<!doctype html>foo"

  • allow_comments => [0|1] β€” set this value to true if you'd like to allow/preserve HTML comments in your content πŸ’¬ Useful when cleaning up your own static files or templates. This feature is off by default.
my $html = q[<body><!-- comments! -->foo</body>];

my $hr = HTML::Restrict->new( allow_comments => 1 );
$html = $hr->process( $html );
# $html is now: "<!-- comments! -->foo"

  • create_newlines => [0|1] β€” set the value to true if you'd like every br tag replaced by a newline, and every p tag replaced by two newlines πŸ“ Note: if a tag is specified in the allowed HTML, it won't be replaced.

  • replace_img => [0|1|CodeRef] β€” set the value to true if you'd like img tags replaced with "[IMAGE: ...]" containing the alt attribute text πŸ–ΌοΈ If you set it to a code reference, you can provide your own replacement (which may even contain HTML):
sub replacer {
    my ($tagname, $attr, $text) = @_; # from HTML::Parser
    return qq{<a href="$attr->{src}">IMAGE: $attr->{alt}</a>};
}

my $hr = HTML::Restrict->new( replace_img => \&replacer );

This attribute will only take effect if the img tag is not included in the allowed HTML ⚠️


  • strip_enclosed_content => [0|1] β€” okay so the default behaviour up to 1.0.4 was to preserve the content between script and style tags, even when the tags themselves were being deleted πŸ’€ So you'd be left with a whole bunch of JavaScript or CSS, just with the enclosing tags missing. That is almost never what you want, so starting at 1.0.5 the default is to remove any script or style info enclosed in these tags β€” unless they've specifically been whitelisted in the rules. This is the sane default when you're cleaning up content submitted via a web form πŸ’… However, if you're using HTML::Restrict to purge your own HTML, you can be more restrictive:
# strip the head section, in addition to JS and CSS
my $html = '<html><head>...</head><body>...<script>JS here</script>foo';

my $hr = HTML::Restrict->new(
    strip_enclosed_content => [ 'script', 'style', 'head' ]
);

$html = $hr->process( $html );
# $html is now '<html><body>...foo';
⚠️ the caveat here is that HTML::Restrict will not try to fix broken HTML. In the above example, if you have any opening script, style or head tags that don't also include matching closing tags, all following content will be stripped away β€” regardless of any parent tags. no cap, that's just how it is 😭

Keep in mind that changes to strip_enclosed_content are not additive, so if you're adding additional tags you'll need to include the entire list of tags whose enclosed content you'd like to remove. This feature strips script and style tags by default.


  • "filter_text => [0|1|CodeRef]" β€” by default all text gets filtered to fix any encoding problems which may cause security issues πŸ›‘οΈ You may override the encoding behaviour by providing your own anonymous sub to filter_text. The first and only argument to the sub is the text which needs to be filtered. The sub should return a scalar containing the transformed text.
filter_text => sub {
    my $text = shift;
    ... # transform text
    return $text;
},

You may also set this value to 0 in order to disable this behaviour entirely. Please be advised this is a security risk. Use caution when disabling this parameter or providing your own filter function. 🚨

✨ SUBROUTINES/METHODS ✨

🌸 process( $html ) 🌸

This is the method that does the actual real work, bestie. It parses your data, removes any tags and attributes which are not specifically allowed, and returns the resulting text. It requires and returns a SCALAR. πŸ’…

πŸ¦‹ get_rules πŸ¦‹

Accessor time: this one returns a hash ref of the current rule set. Slay. ✨

πŸŽ€ get_uri_schemes πŸŽ€

Another accessor, and it returns an array ref of the current valid uri schemes. Cute, useful, iconic. πŸ’–


🚨 CAVEATS 🚨

Okay so please note: all tag and attribute names passed via the rules param must be supplied in lower case. No uppercase shenanigans, bestie. πŸ™ˆ

# correct
my $hr = HTML::Restrict->new( rules => { body => ['onload'] } );

# throws a fatal error
my $hr = HTML::Restrict->new( rules => { Body => ['onLoad'] } );
πŸ’€ The second one throws a fatal error. The lowercase rule is not a vibe suggestion, it's the rule. Periodt.

πŸ’– MOTIVATION πŸ’–

There are already several modules on the CPAN which accomplish much of the same thing, but after doing a lot of poking around, I was unable to find a solution with a simple setup which I was happy with. And honestly? That's the whole origin story. 🫢

The most common use case might be stripping HTML from user submitted data completely, or allowing just a few tags and attributes to be displayed. With the exception of URI scheme checking, this module doesn't do any validation on the actual content of the tags or attributes. If that's a requirement, you can either mess with the parser object, post-process the text yourself, or have a look at one of the more feature-rich modules in the SEE ALSO section below. πŸ‘€

My aim here is to keep things easy and, hopefully, cover a lot of the less complex use cases with just a few lines of code and some brief documentation. The idea is to be up and running quickly. Lowkey the dream. ✨


🌈 SEE ALSO 🌈

  • HTML::TagFilter
  • HTML::Defang
  • MojoMojo::Declaw
  • HTML::StripScripts
  • HTML::Detoxifier
  • HTML::Sanitizer
  • HTML::Scrubber

🫢 ACKNOWLEDGEMENTS 🫢

Thanks to Raybec Communications (<http://www.raybec.com>) for funding my work on this module and for releasing it to the world. Iconic behavior. πŸ’–

Thanks also to the many other contributors: <https://github.com/oalders/html-restrict/graphs/contributors> πŸ¦‹


πŸ‘‘ AUTHOR πŸ‘‘

Olaf Alders (<olaf@wundercounter.com>) πŸ’Œ


The legal tea β˜•: This software is copyright (c) 2009 by Olaf Alders.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. 🌸


πŸ’– That's the whole page, bestie! Source/version: HTML::Restrict(3) on Perl v5.42.0; date: 2023-04-24. Mwah! πŸ’…βœ¨

πŸ“¦ where this page comes from