I am working on a CGI script where I need to take some user input, clean it, and redisplay it on a Web page. To eliminate a security concern, I am taking dangerous characters and converting them to their character code equivalent (e.g. & = & #38; -- All characters codes in this example have an extra space after the first ampersand to guarantee that they are not converted when displayed). Unfortunately, this winds up with hideous monstrosities like the following:
s/(&|;)/($1 eq "&")?"& #38;":"& #59;"/ge;
This substitutes the appropriate character code for either the ampersand or semicolon as it encounters it in the variable. I have to do both on the same line since the character codes themselves contain ampersands and semicolons and substituting for one would create false positives for the other is the other was substituted for later.
While I'm rather proud of that regex (for a newbie), I have to admit that it's not terribly clear. Is there an easier way to scrub incoming data that will be tossed out to a Web page later on?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.