⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch01_04.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<em class="filename">htdocs</em> directory. All files beneath thisdirectory are browsable. By default, the <em class="filename">cgi-bin</em>directory is not beneath <em class="filename">htdocs</em>, so if we wereto disable our <tt class="literal">ScriptAlias</tt> directive, for example,there would be no way to access the CGI scripts. There is a very goodreason for this, and it is not simply to protect yourself fromsomeone accidentally deleting the <tt class="literal">ScriptAlias</tt>directive.</p><p>Here is an example why you should not place your CGI script directorywithin the document root. Say you do decide that you want to havemultiple directories for CGI scripts throughout your web site withinthe document root. You might decide that it would be nice to have adirectory for each of your major applications. Say that you have anonline widget store that you put in<em class="filename">/usr/local/apache/htdocs/widgets</em> and the CGIscript directory at<em class="filename">/usr/local/apache/htdocs/widgets/cgi</em>. You thenadd the following directive:</p><blockquote><pre class="code">ScriptAlias     /widgets-cgi   /usr/local/apache/htdocs/widgets/cgi</pre></blockquote><p>If you were to do this and test it, it would work fine. However,suppose that your company later expands to sell woozles in additionto widgets, so the store needs a more general name. You rename the<em class="filename">widgets</em> directory to <em class="filename">store</em>,update the <tt class="literal">ScriptAlias</tt> directive, update allrelated HTML links, and create a symbolic link from<em class="filename">widgets</em> to <em class="filename">store</em> in orderto support those users who bookmarked the old name. Sounds like agood plan, right?</p><p>Unfortunately, that last step, the symbolic link, just<a name="INDEX-148" /><a name="INDEX-149" /><a name="INDEX-150" />created a large security hole. The problemis that it is now possible to access your CGI scripts via twodifferent URLs. For example, you may have a CGI script called<em class="filename">purchase.cgi</em> that can be accessed either ofthese two ways:</p><blockquote class="simplelist"><p><em class="emphasis">http://localhost/store-cgi/purchase.cgi</em></p><p><em class="emphasis">http://localhost/widgets-cgi/purchase.cgi</em></p></blockquote><p>The first URL will be handled by the <tt class="literal">ScriptAlias</tt>directive; the second will not. If users attempt to access the secondURL, instead of being greeted by a web page, they will be greetedwith the source code of your CGI script. If you're lucky,someone will send you an email notifying you of the problem. Ifyou're not, a mischievous user may start poking around yourscripts to find security holes to break into your system to get atmore valuable information (like database passwords or credit cardnumbers).</p><p>Any symbolic link above a directory containing CGI scripts allowsthis security hole.<a href="#FOOTNOTE-1">[1]</a> The scenario about renaming adirectory and providing a link to its old name is simply one exampleof a situation when this may occur innocently. If you place your CGIscripts outside of your server's document root, you never haveto worry about someone accidentally exposing your scripts this way.</p><blockquote><a name="FOOTNOTE-1" /><p>[1]It is possible to configureApache to not follow symbolic links, which provides an alternativesolution. However, symbolic links in general can be quite useful, andthey are enabled by default. The problem in this situation is notwith the symbolic link; it is with having the CGI scripts in abrowsable location.</p></blockquote><p>You may wonder why revealing your <a name="INDEX-151" />source code is such a problem. CGIscripts have certain characteristics that make them quite differentthan other forms of executables from a security standpoint. Theyallow remote, anonymous users to run programs on your system. Thus,security should always be an important consideration, and your codemust be flawless if you are willing to allow potential attackers toreview your source code. Although security through obscurity is notgood protection in and of itself, it certainly doesn't hurtwhen combined with other forms of security. We will discuss securityin much greater detail in <a href="ch08_01.htm">Chapter 8, "Security"</a>.</p></div><a name="ch01-7-fm2xml" /><div class="sect3"><h3 class="sect3">1.4.1.2. Configuring by extension</h3><p>The alternative to configuring <a name="INDEX-152" />CGIscripts via a common <a name="INDEX-153" />directory is to distribute themthroughout your document tree and have your web server recognize themby their <a name="INDEX-154" />filename extension, such as<em class="filename">.cgi</em>. This is a very bad idea, from thestandpoint of both <a name="INDEX-155" /><a name="INDEX-156" />architecture and security.</p><p>From an architectural standpoint, you should not do this becausehaving a common directory for all of your CGI scripts helps youmanage them. As web sites grow, it may be difficult to keep track ofall of the CGI scripts that your site uses. Placing them under acommon directory makes them easier to find and promotes creating CGIscripts that are general solutions to multiple problems instead ofhandfuls of single-use scripts. You can then create subdirectoriesbeneath the main <em class="filename">/cgi</em> directory to organize yourscripts.</p><p>There are two reasons why configuring CGI scripts by extension isinsecure. First, it allows anyone who has permissions to update HTMLfiles to create CGI scripts. As we said, CGI scripts requireparticular security considerations, and you should not allow noviceprogrammers to create scripts on production web servers. Second, itincreases the likelihood that someone can view the source code toyour CGI scripts. Many <a name="INDEX-157" />texteditors create <a name="INDEX-158" />backup files while you areediting a file; some of them create these files in the same directorywhere you are working. For example, if you were editing a file called<em class="filename">top_secret.cgi</em> with <tt class="command">emacs</tt>, ittypically creates a backup file called<em class="filename">top_secret.cgi~</em>. If this second file makes itonto the production web server and someone with a lucky hunchattempts to request that file, the web server will not recognize theextension and will simply return the raw source code.</p><p>Of course, your text editor ideally should delete these files whenyou finish working on them, and you really should not be editingfiles directly on a production web server. But files like this do getleft around sometimes, and they might make it to the production webserver. Files also get renamed manually sometimes. A developer maywish to make changes to a file but save a backup of this file bymaking a copy and renaming it with a <em class="filename">.bak</em>extension. If a backup file were in a directory configured with<tt class="literal">ScriptAlias</tt>, then it is not displayed; it istreated like any other CGI script and executed, which is a much saferalternative.</p><p>So, if your web server happens to be configured to allow CGI scriptsanywhere, here is how to fix it. The following line tells the webserver to execute any file ending with a <em class="filename">.cgi</em>suffix:</p><blockquote><pre class="code">AddHandler    cgi-script    .cgi</pre></blockquote><p>You can <a name="INDEX-159" />comment it<a name="INDEX-160" />out by preceding it with<tt class="literal">#</tt>, just like in Perl. Without this directive,Apache will treat <em class="filename">.cgi</em> files as unknown filesand return them according to the default media type -- typicallyplain text. So be sure that you move all of your CGI scripts outsidethe document root before you remove this directive.</p><p>You may also turn off the CGI<a name="INDEX-161" /><a name="INDEX-162" /><a name="INDEX-163" />execute permissions for particulardirectories by disabling the<tt class="literal">ExecCGI</tt><a name="INDEX-164" /> option. The line to enable it lookslike this:</p><blockquote><pre class="code">&lt;Directory "/usr/local/apache/htdocs"&gt;  .  .  Options Indexes FollowSymLinks ExecCGI  .  .&lt;/Directory&gt;</pre></blockquote><p>There are probably many other lines above and below the<tt class="literal">Options</tt> directive, and the<tt class="literal">Options</tt> directive on your system may differ. Ifyou remove <tt class="literal">ExecCGI</tt>, then even with the CGI handlerdirective enabled above, Apache will not execute CGI scripts in thelocation that this <tt class="literal">Options</tt> directiveapplies -- in this case, the document root,<em class="filename">/usr/local/apache/htdocs</em>. Users will instead getan error page telling them "Permission Denied."</p><p>Now that we have our web server set up, and we have gotten a chanceto see what CGI can do, we can investigate CGI in more detail. Westart the next chapter by reviewing HTTP, the language of the Web<a name="INDEX-165" /><a name="INDEX-166" /><a name="INDEX-167" />and thefoundation <a name="INDEX-168" />of CGI.</p></div></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch01_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch02_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">1.3. Alternative Technologies</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">2. The Hypertext Transport Protocol </td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -