my ($query) = new CGI;
-my ($doci) = ($query->param('doc') =~ /^\s*(.*?\.txt)\s*$/);
-my ($doc) = "../documentation/$doci";
-print "content-type: text/html\n\n";
+# Note filenames may only have one dot in them, in the ".txt".
+# This prevents malicious users using "../" to view files.
+my ($doc) = ($query->param('doc') =~ /^\s*([^\.]*?\.txt)\s*$/);
+
+print "Content-type: text/html\n\n";
+
+unless (defined $doc) {
+ print "The link to this page was broken - it must specify a .txt file.";
+ exit;
+}
+
+# Prevent hackers from supplying a malformed document string.
+# I.e. only allow normal characters, slashes and dots.
+unless ($doc =~ /^[a-zA-Z_\-0-9\.\/]+$/) {
+ print "Go Away, you nasty hax0r!";
+ exit;
+}
+$doc = "../documentation/".$doc;
print <<"END";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
docs.cgi
Web-based text file viewer and formatter.
Created by pjm2 19/10/2000
- Last modified 19/10/2000
+ Last modified 02/11/2000
-->
<html>
<td valign="top">
END
-&print_file($left);
+&print_html($left);
print <<"END";
<td valign="top">
END
-&print_file($title);
-
-print "<PRE>\n";
+&print_html($title);
&print_file($doc);
-print "</PRE>\n";
-
-&print_file($bottom);
+&print_html($bottom);
print <<"END";
exit 0;
+# Print a file, whilst escaping HTML: -
sub print_file ($) {
- my ($filename) = @_;
- print `cat $filename`;
-}
-
-sub print_file_old ($) {
+ my ($urls) = '(' . join ('|', qw{
+ http
+ telnet
+ gopher
+ file
+ wais
+ ftp
+ } )
+ . ')';
+
+ my ($ltrs) = '\w';
+ my ($gunk) = '/#~:.?+=&%@!\-';
+ my ($punc) = '.:?\-';
+ my ($any) = "${ltrs}${gunk}${punc}";
my ($filename) = @_;
open(FILE, $filename) or die "Cannot open $filename: $!\n";
- while (my ($line) = <FILE>) {
- print $line;
+ print "<pre>\n";
+ # Use $_ implicitly throughout.
+ while (<FILE>) {
+ # Must do the next line first!
+ s/&/&/g;
+ s/</</g;
+ s/>/>/g;
+ s/"/"/g;
+ s/\b($urls:[$any]+?)(?=[$punc]*[^$any]|$)/<a href="$1">$1<\/a>/igox;
+ print;
}
+ print "</pre>";
}
+# Print a file without escaping HTML: -
+sub print_html ($) {
+ my ($filename) = @_;
+ print `cat $filename 2>&1`;
+}