CGI Interface -- Advice for CGI Programming

### Advice for CGI Programming

- Which language should I use? You can use any language you feel comfortable programming in. Of course, programs usually run faster in compiled language, so if your program is computationally intensive, you might want to use C or another compiled language. Most of the examples and shareware programs available on the Web are written in C or Perl.

- How can I prevent CGI programs from causing security problems? A CGI program is basically a program that you let anyone else in the world run on your system. Someone with bad intentions could cause you some problems if you don't follow these rules:

  - Keep your CGI programs in a separate CGI directory or give them the file extension you specify in the configuration file. Don't give outsiders write access to these files and directories This should prevent casual users from reading, modifying, or adding CGI programs.

  - Don't allow server-parsed HTML to run on your CGI directory or on files with extensions mapped as CGI programs.

  - Don't trust the data the browser sends to your program. Parse the QUERY_STRING or standard input. If your program is a non-compiled script, characters with special meanings in that language can cause problems if the browser fails to encode them as hexadecimal values.

  - Check for odd file names and directory paths in the input. For example, you should be careful about allow paths containing: `.`,`../`,`//`, or the name of the directory that contains your CGI programs.

  - Be careful with statements that construct and execute a command line or system call using input from the reader. For example, be careful using the `eval` statement in Perl and the Bourne shell. If the reader sends input that begins with a semicolon (;), they may be able to get your system to perform any command they like. Likewise, if you use calls to `popen()` and `system()`, make sure you put a backslash (\) before any characters with special meaning in the shell that will run.

- How can I debug my CGI programs? Errors that go to the stderr location will be available in the AOLserver's `server.log` file.

  One simple way to debug CGI programs is to temporarily include print statements that send additional diagnostic information to the client or to a file. If your program is written in C and you have a debugging tool on your system, you can call sleep (or use a long loop) at the beginning of the program. Then, you can attach to the program with the debugger while the program is sleeping.

  If your programs are not executed, make sure the program file allows read and execute access.

Top of Page
Copyright © 1998-99 America Online, Inc.