Skip to content

Commit

Permalink
Use Host header in pastebin sample rather than hardcoded constants
Browse files Browse the repository at this point in the history
(This allows one to insert any string through the Host header in the
response, as there's no validation other than "there's no newline",
which is part of the HTTP parsing code.)
  • Loading branch information
lpereira committed Nov 29, 2023
1 parent a365916 commit 34b1abe
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/samples/pastebin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
#include "lwan-cache.h"
#include "lwan-private.h"

#define SERVER_NAME "paste.lwan.ws"
#define SERVER_PORT 443
#define CACHE_FOR_HOURS 2

static struct cache *pastes;
Expand Down Expand Up @@ -123,9 +121,15 @@ static enum lwan_http_status post_paste(struct lwan_request *request,
cache_coro_get_and_ref_entry(pastes, request->conn->coro, key);

if (paste) {
const char *host_hdr = lwan_request_get_header(request, "Host");

if (!host_hdr)
return HTTP_BAD_REQUEST;

response->mime_type = "text/plain";
lwan_strbuf_printf(response->buffer, "https://%s/p/%zu\n\n",
SERVER_NAME, (uint64_t)(uintptr_t)key);
host_hdr, (uint64_t)(uintptr_t)key);

return HTTP_OK;
}
}
Expand All @@ -136,6 +140,11 @@ static enum lwan_http_status post_paste(struct lwan_request *request,
static enum lwan_http_status doc(struct lwan_request *request,
struct lwan_response *response)
{
const char *host_hdr = lwan_request_get_header(request, "Host");

if (!host_hdr)
return HTTP_BAD_REQUEST;

response->mime_type = "text/plain";

lwan_strbuf_printf(
Expand All @@ -152,7 +161,7 @@ static enum lwan_http_status doc(struct lwan_request *request,
"response with different MIME-type.\n"
"\n"
"Items are cached for %d hours and are not stored on disk",
SERVER_NAME, SERVER_NAME, CACHE_FOR_HOURS);
host_hdr, host_hdr, CACHE_FOR_HOURS);

return HTTP_OK;
}
Expand Down

0 comments on commit 34b1abe

Please sign in to comment.