Sounds like you're trying to protect against XSS attacks, which is a Good Thing. But I think you may be overcomplicating your solution. Try calling the following with the URL test.cgi?link=http://www.google.com%22%3E%3Cscript%3Ealert%28%27Bang!%27%29%3C/script%3E (i.e. "test.cgi?link=http://www.google.com"><script>alert('Bang!')</script>") and look at the resulting HTML.

use CGI; my $cgi=CGI->new; print $cgi->header; print $cgi->start_html; my $link=$cgi->param("link"); print "<a href=\"$link\">Link</a><br/>\n"; # BAD! print "<a href=\"".$cgi->escapeHTML($link) # Good ."\">Link</a><br/>\n"; print $cgi->a({href=>$link},"Link"), $cgi->br; # Better print $cgi->end_html;

CGI's escapeHTML is just a shortcut to HTML::Entities's encode_entities. Also, I don't understand why you need to escape param names (at least I think that's what you're trying to do?), unless at some point in your script you are dumping all of your params somewhere? If so, the advice is simple: don't do that!


In reply to Re: HTML::ENTITIES for URL encoding by Anonymous Monk
in thread HTML::ENTITIES for URL encoding by aakashmgupta

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.