From 72dbecd208fe47cd2b6f47ff8666211efb9d678b Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 7 Nov 2026 14:54:03 +0100 Subject: [PATCH] local_handler: Fix files where the sever has no access/read rights --- src/lib/utils.c | 3 ++- src/worker/local_handler.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/lib/utils.c b/src/lib/utils.c index 245b545..0751fd4 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -293,7 +293,8 @@ long clock_cpu(void) { long stat_mtime(const char *filename) { struct stat stat_buf; - stat(filename, &stat_buf); + if (stat(filename, &stat_buf) != 0) + return 0; // FIXME use actual error value return stat_buf.st_mtime; } diff --git a/src/worker/local_handler.c b/src/worker/local_handler.c index 2717a8b..f0f46c3 100644 --- a/src/worker/local_handler.c +++ b/src/worker/local_handler.c @@ -7,6 +7,7 @@ */ #include "func.h" +#include "../lib/error.h" #include "../logger.h" #include "../lib/utils.h" #include "../lib/compress.h" @@ -112,7 +113,7 @@ static int local_handler(client_ctx_t *ctx) { http_uri *uri = &ctx->uri; char *err_msg = ctx->err_msg; - char buf1[1024], buf2[1024]; + char buf1[1024], buf2[1024], err_buf[256]; int accept_if_modified_since = 0; if (streq(req->method, "TRACE")) { @@ -153,6 +154,9 @@ static int local_handler(client_ctx_t *ctx) { } else if (strlen(uri->pathinfo) != 0 && ctx->conf->local.dir_mode != URI_DIR_MODE_INFO) { res->status = http_get_status(404); return 0; + } else if (access(uri->filename, R_OK) != 0) { + res->status = http_get_status(403); + return 0; } if (!uri->is_static) { @@ -259,7 +263,13 @@ static int local_handler(client_ctx_t *ctx) { return 0; } - if (ctx->file == NULL) ctx->file = fopen(uri->filename, "rb"); + if (ctx->file == NULL) { + if ((ctx->file = fopen(uri->filename, "rb")) == NULL) { + res->status = http_get_status(500); + sprintf(err_msg, "Unable to open file: %s.", error_str(errno, err_buf, sizeof(err_buf))); + return 0; + } + } ctx->content_length = fsize(ctx->file); return 0;