Coplan has asked for the wisdom of the Perl Monks concerning the following question:
Before I show you my little sub-routine, a little background. For obvious reasons, I am not going to display the whole program here. The premise of this sub-routine is that I have a template file, an HTML document, that has commands within it (for example: <!-- fillin(getContent) --> ). Basically, a template is loaded, and based on the input into the CGI, the content will replace the fillin() tags. That in itself is not a security hole as one might think, because I have it set to check whether said commands actually exist earlier in the script (an earlier subroutine). Anyhow, my concern is with the open() command. Here's my code:
# getHTML: Get template and fill in with generated data sub getHTML { $UserInfo = getUserInfo($tempUID); $theme = getTheme($UserInfo->{Theme}); my $template = $theme->{ThemeDir}."index.html"; my $HTML .= <<END; Pragma: no-cache Cache-Control: no-cache,must-revalidate,max-age=0,no-store,private Expires: -1 Content-type: text/html END open(FILE, "<$template") or die "Template : Couldn't open $template +: $!\n"; while (<FILE>) { $HTML .= $_ } close(FILE); while ($HTML =~ /<!-- fillin\((.+?)\) -->/) { my $command_output = &$1($call); $HTML =~ s/<!-- fillin\(.+?\) -->/$command_output/; } return $HTML; } # end getHTML()
Is there any sort of protection I should be adding over the opening of such a file? Maybe some sort of regexp to disallow anything that shouldn't belong? I know in my friend's case, his program allowed user input through the URL. In my case, there is no way for the person to input that template file. The template file is based on a theme that they can select from the database. The file itself is stored within the database.
While I am at it, is there a safer way, in general, to open files in perl? Most of what I do with perl is web development, so this might be useful to know anyhow.
Once again, thank you for your help. I've learned a lot from this wonderful community, and I hope to continue doing so.
--Coplan