You can decrease the likelihood that the CGI programs you're using have security vulnerabilities by following these guidelines:
If you use CGI programs written elsewhere, make sure they come from a source you can trust.
If you write your own CGI programs, read the CERT Advisory on Sanitizing User-Supplied Data in CGI Scripts. Also review The World Wide Web Security FAQ and this CGI Security Tutorial. The collective wisdom of many CGI programmers is bundled within these pages.
To learn more:
Please see CGI Programs and Security (700 words) for a more thorough discussion of security vulnerabilities in CGI programs.