Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

page_name_is_good function #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions src/wiki.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,26 @@ wiki_show_footer(HttpResponse *res)
);
}

int page_name_is_good(char* page_name)
{
/* We should give access only to subdirs of didiwiki root.
I guess that check for absense of '/' is enough.

TODO: Use realpath()
*/
if (!page_name)
return FALSE;

if (!isalnum(page_name[0]))
return FALSE;

if (strstr(page_name, ".."))
return FALSE;

return TRUE;
}


void
wiki_handle_rest_call(HttpRequest *req,
HttpResponse *res,
Expand All @@ -866,7 +886,7 @@ wiki_handle_rest_call(HttpRequest *req,
if (page == NULL)
page = http_request_get_query_string(req);

if (page && (access(page, R_OK) == 0))
if (page && page_name_is_good(page) && (access(page, R_OK) == 0))
{
http_response_printf(res, "%s", file_read(page));
http_response_send(res);
Expand All @@ -879,11 +899,14 @@ wiki_handle_rest_call(HttpRequest *req,
if( ( (wikitext = http_request_param_get(req, "text")) != NULL)
&& ( (page = http_request_param_get(req, "page")) != NULL))
{
file_write(page, wikitext);
if (page_name_is_good(page))
{
file_write(page, wikitext);
http_response_printf(res, "success");
http_response_send(res);
return;
}
}
}
else if (!strcmp(func, "page/delete"))
{
Expand All @@ -892,7 +915,7 @@ wiki_handle_rest_call(HttpRequest *req,
if (page == NULL)
page = http_request_get_query_string(req);

if (page && (unlink(page) > 0))
if (page && page_name_is_good(page) && (unlink(page) > 0))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When would unlink() return a result >0 ? My manpage has:

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and
       errno is set appropriately.

Thanks

{
http_response_printf(res, "success");
http_response_send(res);
Expand All @@ -906,7 +929,7 @@ wiki_handle_rest_call(HttpRequest *req,
if (page == NULL)
page = http_request_get_query_string(req);

if (page && (access(page, R_OK) == 0))
if (page && page_name_is_good(page) && (access(page, R_OK) == 0))
{
http_response_printf(res, "success");
http_response_send(res);
Expand Down Expand Up @@ -1005,7 +1028,7 @@ wiki_handle_http_request(HttpRequest *req)
/* A little safety. issue a malformed request for any paths,
* There shouldn't need to be any..
*/
if (strchr(page, '/'))
if (!page_name_is_good(page))
{
http_response_set_status(res, 404, "Not Found");
http_response_printf(res, "<html><body>404 Not Found</body></html>\n");
Expand Down