<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.webmaster-forums.net/crss/node/1013026" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title></title>
    <link>https://www.webmaster-forums.net/crss/node/1013026</link>
    <description></description>
    <language>en</language>
          <item>
    <title>interpolation</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075650</link>
    <description> &lt;p&gt;Perl does interpolation only on things in your code.  If you run the following Perl program:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;#!/usr/bin/perl&lt;br /&gt;$X = 100;&lt;br /&gt;print $ARGV[0];&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;and run it as &lt;strong&gt;perl my_program &#039;this is $X&#039;&lt;/strong&gt;, you&#039;ll get the actual string &lt;strong&gt;this is $X&lt;/strong&gt;, you won&#039;t get &lt;strong&gt;this is 100&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;If that worked, templates would be simple.  But Perl would also be terribly insecure.&lt;/p&gt;
 </description>
     <pubDate>Thu, 14 Dec 2000 03:19:04 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1075650 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075648</link>
    <description> &lt;p&gt;um...&lt;br /&gt;
Aren&#039;t you also suppose to escape &lt;strong&gt;$&lt;/strong&gt; and &lt;strong&gt;@&lt;/strong&gt; and &lt;strong&gt;%&lt;/strong&gt;?&lt;/p&gt;
&lt;p&gt;if you get user input containing:&lt;br /&gt;
&quot;blah $ENV{PATH} blah&quot;&lt;/p&gt;
&lt;p&gt;won&#039;t it print &quot;blah &quot;, then whatever $ENV{PATH} is, then &quot; blah&quot;?&lt;br /&gt;
and same for @arrays and %hases?&lt;/p&gt;
 </description>
     <pubDate>Thu, 14 Dec 2000 03:01:31 +0000</pubDate>
 <dc:creator>Mark Hensler</dc:creator>
 <guid isPermaLink="false">comment 1075648 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Data location</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075619</link>
    <description> &lt;p&gt;It is far safer to store your data in a directory NOT accessible from the web.  That will make it impossible to be reached from the web UNLESS you provide a person a gateway to get the content, like:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;open FILE, $some_path_the_user_enters;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;That line is unsafe in and of itself.  I could enter &quot;/etc/passwd&quot;, or &quot;rm -rf / |&quot;, or something else bad.  The point is that you should not trust the end user, and should make sure that you are ok with what they give you.  Paranoia helps in this case.&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 15:44:07 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1075619 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title></title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075618</link>
    <description> &lt;p&gt;ou well, i see there&#039;s a long way to go... thank you for your help! it&#039;ll take some time to learn all the stuff... but another short question for my understanding: it doesn&#039;t matter, where (cgi-bin-dir or htdocs-dir) the data is stored? it remains always a security-risk?&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 15:27:33 +0000</pubDate>
 <dc:creator>merlin</dc:creator>
 <guid isPermaLink="false">comment 1075618 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Changing Characters to Entities</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075617</link>
    <description> &lt;p&gt;The simplest mechanism is to set up a translation table:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;my %HTML = (&lt;br /&gt;&amp;nbsp; &amp;#039;&amp;lt;&amp;#039; =&amp;gt; &amp;#039;lt&amp;#039;,&lt;br /&gt;&amp;nbsp; &amp;#039;&amp;gt;&amp;#039; =&amp;gt; &amp;#039;gt&amp;#039;,&lt;br /&gt;&amp;nbsp; &amp;#039;&amp;amp;&amp;#039; =&amp;gt; &amp;#039;amp&amp;#039;,&lt;br /&gt;);&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Then create a regex based on the keys:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;$REx = &amp;quot;[&amp;quot; . join(&amp;quot;&amp;quot;, keys %HTML) . &amp;quot;]&amp;quot;;&amp;nbsp; # [&amp;lt;&amp;gt;&amp;amp;]&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;And then use it:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;$user_content =~ s/($REx)/&amp;amp;$HTML{$1};/g;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;(Notice how I saved the &lt;strong&gt;&amp;amp;&lt;/strong&gt; and &lt;strong&gt;;&lt;/strong&gt; for the very end, there, instead of putting them in EVERY SINGLE value in the hash.)&lt;/p&gt;
&lt;p&gt;There&#039;s a module for this already, HTML::Entities, which does even more -- it fixes accented characters and such.  It&#039;s quite useful and comprehensive.&lt;/p&gt;
&lt;p&gt;As far as HTML parsers go, you&#039;re not likely to find much about them in your books.  I&#039;ve not used HTML::Parser, but I can tell you how to use my YAPE::HTML module.  Once you get the module from &lt;a href=&quot;http://www.pobox.com/~japhy/YAPE/HTML.pm&quot; class=&quot;bb-url&quot;&gt;http://www.pobox.com/~japhy/YAPE/HTML.pm&lt;/a&gt; then you can try this program.  This program will spit out the HTML content, and remove ALL TAGS except for &lt;A&gt;, , and .&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This can be run as a CGI program OR as a command-line program.  This reads a sample HTML file from beneath the __DATA__ marker in the file.&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;#!/usr/bin/perl -w&lt;br /&gt;&lt;br /&gt;use YAPE::HTML;&lt;br /&gt;use strict;&lt;br /&gt;&lt;br /&gt;print &amp;quot;Content-type: text/html\n\n&amp;quot; if $ENV{REMOTE_HOST};&lt;br /&gt;&lt;br /&gt;my $content;&lt;br /&gt;{ local $/;&amp;nbsp; $content = &amp;lt;DATA&amp;gt;; }&lt;br /&gt;&lt;br /&gt;my $parser = YAPE::HTML-&amp;gt;new($content);&lt;br /&gt;my %ok = map +($_, 1), qw( a b i );&lt;br /&gt;&lt;br /&gt;while (my $chunk = $parser-&amp;gt;next) {&lt;br /&gt;&amp;nbsp; next if&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $chunk-&amp;gt;type eq &amp;#039;comment&amp;#039; or&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $chunk-&amp;gt;type eq &amp;#039;tag&amp;#039; and not $ok{$chunk-&amp;gt;tag} or&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; $chunk-&amp;gt;type eq &amp;#039;closetag&amp;#039; and not $ok{$chunk-&amp;gt;tag};&lt;br /&gt;&amp;nbsp; print $chunk-&amp;gt;string;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;__DATA__&lt;br /&gt;This is such a &amp;lt;b&amp;gt;cool&amp;lt;/b&amp;gt; site.&lt;br /&gt;&amp;lt;hr&amp;gt;&lt;br /&gt;I hope all this markup gets &amp;lt;i&amp;gt;through&amp;lt;/i&amp;gt; ok...&lt;br /&gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;&amp;lt;h2 align=&amp;quot;center&amp;quot;&amp;gt;Hooray for &amp;lt;a href=&amp;quot;http://www.perl.com/&amp;quot;&amp;gt;Perl&amp;lt;/a&amp;gt;!&amp;lt;/h2&amp;gt;&lt;br /&gt;&amp;lt;a href=&amp;quot;http://www.pobox.com/~japhy/&amp;quot;&amp;gt;Jeff&amp;#039;s&amp;lt;/a&amp;gt; web site&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;This code, when run, will produce:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;This is such a &amp;lt;b&amp;gt;cool&amp;lt;/b&amp;gt; site.&lt;br /&gt;&lt;br /&gt;I hope all this markup gets &amp;lt;i&amp;gt;through&amp;lt;/i&amp;gt; ok...&lt;br /&gt;&lt;br /&gt;Hooray for &amp;lt;a href=&amp;quot;http://www.perl.com/&amp;quot;&amp;gt;Perl&amp;lt;/a&amp;gt;!&lt;br /&gt;&amp;lt;a href=&amp;quot;http://www.pobox.com/~japhy/&amp;quot;&amp;gt;Jeff&amp;#039;s&amp;lt;/a&amp;gt; web site&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;As you can see, it handles nested elements fine (even if a good element is in a bad element, or vice-versa).&lt;/p&gt;
&lt;p&gt;I apologize for the UTTER lack of documentation in the module, but I assure you it will look much better once it is officially released.  In the meantime, I offer any and all user support needed.  I hope the sample code above is pretty self-explanatory, though.&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 15:02:35 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1075617 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Security, Part II:  System Interaction</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075616</link>
    <description> &lt;p&gt;Another place to take caution is when you use user input in a system command.  Take this VERY SIMPLE (and very insecure) CGI program:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;#!/usr/bin/perl&lt;br /&gt;&lt;br /&gt;use CGI &amp;#039;param&amp;#039;;&lt;br /&gt;my $function = param(&amp;#039;perlfunc&amp;#039;);&lt;br /&gt;print &amp;quot;Content-type: text/plain\n\n&amp;quot;;&lt;br /&gt;print `perldoc -f $function`;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;This code is supposed to get the name of a Perl function from a form (the text element is called &#039;perlfunc&#039;), and then display the information about that function in the &#039;perlfunc&#039; document.  &lt;strong&gt;Who can find the security hole?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What if I enter &quot;; ls -lag&quot; as my &quot;perl function&quot;?  Now, my program blindly runs &lt;strong&gt;perldoc -f; ls -lag&lt;/strong&gt;, and the user sees the contents of the current directory.  Hmm, and since all Perl CGI programs have to be readable by the &#039;nobody&#039; user, that means that I can see the NAMES of the other CGI programs.&lt;/p&gt;
&lt;p&gt;Then I can just send the program &quot;; cat secret_prog.cgi&quot; and now I&#039;ve seen the contents of THAT program -- I sure hope you don&#039;t use plaintext passwords, or you&#039;re ruined.&lt;/p&gt;
&lt;p&gt;The solution is to use Perl&#039;s taint checking.  This is available with the -T switch to perl.  Taint checking requires you validate input from outside of your program -- this is usually done with a rigorous regex to ensure the right stuff:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;#!/usr/bin/perl -T&lt;br /&gt;&lt;br /&gt;use CGI &amp;#039;param&amp;#039;;&lt;br /&gt;my ($func) = param(&amp;#039;perlfunc&amp;#039;) =~ /(-[a-zA-Z]|[a-zA-Z]+)/;&lt;br /&gt;# notice the ()&amp;#039;s around $func -- this is important&lt;br /&gt;# a regex in LIST CONTEXT returns parenthesized sub-patterns&lt;br /&gt;# so $func gets set to the valid portion of the string, if any&lt;br /&gt;&lt;br /&gt;print &amp;quot;Content-type: text/plain\n\n&amp;quot;;&lt;br /&gt;print `perldoc -f $func`;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;That program should run... right?  Sorry.  Perl thinks the environment is unsafe, and requests that you make it safe, too -- specifically, &lt;strong&gt;$ENV{PATH}&lt;/strong&gt;.  This is so that YOU run the &#039;perldoc&#039; program you THINK you&#039;re running.&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;#!/usr/bin/perl -wT&lt;br /&gt;&lt;br /&gt;use CGI &amp;#039;param&amp;#039;;&lt;br /&gt;use strict;&lt;br /&gt;&lt;br /&gt;# we should always use -w and &amp;#039;strict&amp;#039; and -T for CGI programs&lt;br /&gt;&lt;br /&gt;$ENV{PATH} = &amp;quot;/bin:/usr/bin:/usr/local/bin&amp;quot;;&lt;br /&gt;&lt;br /&gt;my ($func) = param(&amp;#039;perlfunc&amp;#039;) =~ /(-[a-zA-Z]|[a-zA-Z]+)/;&lt;br /&gt;&lt;br /&gt;print &amp;quot;Content-type: text/plain\n\n&amp;quot;;&lt;br /&gt;print `perldoc -f $func`;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;That runs fine.  And, for even more safety, you might want to change that last line to have the full path to &#039;perldoc&#039;, just in case you&#039;re paranoid (which you should be).&lt;/p&gt;
&lt;p&gt;This is a simplistic example -- the big error I often see is people calling a mail program with the user&#039;s email address ON THE COMMAND-LINE.  This is just a hole waiting to be exploited:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;open MAIL, &amp;quot;| /usr/bin/sendmail $email&amp;quot;;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;Ouch.  I don&#039;t think anyone REALLY has an email address of &quot;foo@bar.com; mail &lt;a href=&quot;mailto:foo@bar.com&quot;&gt;foo@bar.com&lt;/a&gt; &amp;lt; /etc/passwd&quot;, but someone SURE could enter that.  You&#039;re probably best off not trying to validate an email address yourself, but rather, tell sendmail (or whatever client you use) to look in the headers of the message for the To: field:&lt;/p&gt;
&lt;p&gt;&lt;div class=&quot;codeblock&quot;&gt;&lt;code&gt;open MAIL, &amp;quot;| /usr/bin/sendmail -t&amp;quot; or die &amp;quot;can&amp;#039;t run sendmail: $!&amp;quot;;&lt;/code&gt;&lt;/div&gt;&#039;&lt;/p&gt;
&lt;p&gt;That&#039;s all for now (again).&lt;/p&gt;
&lt;p&gt;Be sure to read the &lt;strong&gt;perlsec&lt;/strong&gt; documentation, which covers tainting in more detail.&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 14:43:43 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1075616 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Re: Perl Security</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075615</link>
    <description> &lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;p&gt;Quote: &lt;em&gt;Originally posted by japhy &lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;You have several options when accepting text from a form for displaying on an HTML page.&lt;/strong&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;great! &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/big.png&quot; title=&quot;Laughing out loud&quot; alt=&quot;Laughing out loud&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
&lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;p&gt;Quote:&lt;br /&gt;
&lt;strong&gt;[=1]&lt;br /&gt;
[*] (attempt to) remove all HTML tags -- this requires a competent parser (like HTML::Parser, or my YAPE::HTML module), because without a parser, you might get rid of some non-HTML content&lt;br /&gt;
[/=1]&lt;/strong&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;how do i include such a parser and how do i use it? i think i&#039;ll consult my perlbooks... &lt;img src=&quot;https://www.webmaster-forums.net/misc/smileys/wink.png&quot; title=&quot;Wink&quot; alt=&quot;Wink&quot; class=&quot;smiley-content&quot; /&gt;&lt;/p&gt;
&lt;blockquote class=&quot;bb-quote-body&quot;&gt;&lt;p&gt;Quote: &lt;strong&gt;
&lt;li&gt; escape potentially unsafe characters -- change &amp;lt; to &amp;lt; and &amp;gt; to &amp;gt; and &amp;amp; to &amp;amp;, and you&#039;ll be safe&lt;/li&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;that sounds great! i&#039;d say this is an &#039;easy&#039; regexp s/&lt;br /&gt;
[/]&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 14:31:34 +0000</pubDate>
 <dc:creator>merlin</dc:creator>
 <guid isPermaLink="false">comment 1075615 at https://www.webmaster-forums.net</guid>
  </item>
  <item>
    <title>Perl Security</title>
    <link>https://www.webmaster-forums.net/serverside-scripting/security-holes#comment-1075614</link>
    <description> &lt;p&gt;You have several options when accepting text from a form for displaying on an HTML page.&lt;/p&gt;
&lt;p&gt;[=1]&lt;/p&gt;
&lt;li&gt; (attempt to) remove all HTML tags -- this requires a competent parser (like HTML::Parser, or my YAPE::HTML module), because without a parser, you might get rid of some non-HTML content&lt;/li&gt;
&lt;li&gt; (attempt to) remove some HTML tags -- this too requires a parser so that certain tags can be left in, while disallowing others, and since HTML elements can be nested, you can&#039;t do this with just a regex&lt;/li&gt;
&lt;li&gt; use an alternate tagging syntax -- like most bulletin boards, that use brackets instead of greater/less than signs (this is closely related to the next one, which is...)&lt;/li&gt;
&lt;p&gt;[*] escape potentially unsafe characters -- change &amp;lt; to &amp;lt; and &amp;gt; to &amp;gt; and &amp;amp; to &amp;amp;, and you&#039;ll be safe&lt;br /&gt;
[/=1]&lt;/p&gt;
&lt;p&gt;While it would be very cool of you to incorporate a working HTML parser in your guestbook or message board, etc., so that people can use (a select subset of) tags normally, it&#039;s probably far easier to use a combination of 3 and 4.&lt;/p&gt;
&lt;p&gt;That&#039;s what I see practically all forums doing nowadays.  My only qualm is that I don&#039;t see the forums telling me the precise usage of brackets -- where can I have whitespace?  Do I need to escape brackets that aren&#039;t to be interpreted as tags?  Etc.  With HTML parsing, you can be very explicit with instructions:  &quot;you are allowed to enter tags normally, but only , , and &lt;A&gt; tags will be recognized&quot;.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Ok, that&#039;s my spiel.[/]&lt;/p&gt;
 </description>
     <pubDate>Wed, 13 Dec 2000 14:01:23 +0000</pubDate>
 <dc:creator>japhy</dc:creator>
 <guid isPermaLink="false">comment 1075614 at https://www.webmaster-forums.net</guid>
  </item>
  </channel>
</rss>
