changeset 1045:e719679a8439

static: check URI prefix separately from the file name itself This avoids duplication, and gets us closer to allowing blahgd instances with other than "/" document URIs. (E.g., http://example.com/blahg/) Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Thu, 13 Aug 2020 10:51:57 -0400
parents 4053acf70698
children 36c1bd8ea8db
files static.c
diffstat 1 files changed, 15 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/static.c	Thu Aug 13 10:41:08 2020 -0400
+++ b/static.c	Thu Aug 13 10:51:57 2020 -0400
@@ -25,6 +25,9 @@
 #include "static.h"
 #include "utils.h"
 
+/* must include trailing '/' */
+#define URI_PREFIX	"/"
+
 struct uri_info {
 	const char *uri;
 	const char *content_type;	/* required for URI_STATIC */
@@ -32,11 +35,11 @@
 };
 
 static const struct uri_info safe_uris[] = {
-	{ "/",			NULL,		URI_DYNAMIC, },
-	{ "/bug.png",		"image/png",	URI_STATIC,  },
-	{ "/favicon.ico",	"image/png",	URI_STATIC,  },
-	{ "/style.css",		"text/css",	URI_STATIC,  },
-	{ "/wiki.png",		"image/png",	URI_STATIC,  },
+	{ "",			NULL,		URI_DYNAMIC, },
+	{ "bug.png",		"image/png",	URI_STATIC,  },
+	{ "favicon.ico",	"image/png",	URI_STATIC,  },
+	{ "style.css",		"text/css",	URI_STATIC,  },
+	{ "wiki.png",		"image/png",	URI_STATIC,  },
 };
 
 static const struct uri_info *get_uri_info(const char *path)
@@ -49,6 +52,13 @@
 	if (hasdotdot(path))
 		return NULL;
 
+	/* all URIs must start with the proper prefix */
+	if (strncmp(URI_PREFIX, path, strlen(URI_PREFIX)))
+		return NULL;
+
+	/* strip the prefix from the uri */
+	path += strlen(URI_PREFIX);
+
 	for (i = 0; i < ARRAY_LEN(safe_uris); i++) {
 		if (!strcmp(safe_uris[i].uri, path))
 			return &safe_uris[i];