-/* $Id: cgi.c,v 1.15 2014/07/18 19:02:07 schwarze Exp $ */
+/* $Id: cgi.c,v 1.16 2014/07/19 11:35:09 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2014 Ingo Schwarze <schwarze@usta.de>
puts("<!-- End search form. //-->");
}
+static int
+validate_filename(const char *file)
+{
+
+ if ('.' == file[0] && '/' == file[1])
+ file += 2;
+
+ return ( ! (strstr(file, "../") || strstr(file, "/..") ||
+ (strncmp(file, "man", 3) && strncmp(file, "cat", 3))));
+}
+
static void
pg_index(const struct req *req)
{
int prio, priouse;
char sec;
+ for (i = 0; i < sz; i++) {
+ if (validate_filename(r[i].file))
+ continue;
+ fprintf(stderr, "invalid filename %s in %s database\n",
+ r[i].file, req->q.manpath);
+ pg_error_internal();
+ return;
+ }
+
if (1 == sz) {
/*
* If we have just one result, then jump there now
static void
resp_show(const struct req *req, const char *file)
{
- if ('.' == file[0] || '/' == file[1])
+
+ if ('.' == file[0] && '/' == file[1])
file += 2;
if ('c' == *file)
return;
}
+ if ( ! validate_filename(sub)) {
+ pg_error_badrequest(
+ "You specified an invalid manual file.");
+ return;
+ }
+
resp_begin_html(200, NULL);
resp_searchform(req);
resp_show(req, sub);