# HG changeset patch # User Josef 'Jeff' Sipek # Date 1597330317 14400 # Node ID e719679a8439f51189a163b160f2b7de86a76cfe # Parent 4053acf70698e46699044904cc19f527e32b7ab4 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 diff -r 4053acf70698 -r e719679a8439 static.c --- 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];