Compare commits

...

4 Commits

Author SHA1 Message Date
Xavier Del Campo Romero b4930f72bb
Bump version to 0.3.0 2024-02-19 23:33:27 +01:00
Xavier Del Campo Romero d9836a5b42
CMakeLists.txt: Fix dynstr version
It was accidentally bumped to 0.2.0 during libweb's 0.2.0 release.
2024-02-19 23:28:12 +01:00
Xavier Del Campo Romero afe0681c0b
Limit maximum multipart/form-data pairs and files
A malicious user could inject an infinite number of empty files or
key/value pairs into a request in order to exhaust the device's
resources.
2024-02-19 23:00:56 +01:00
Xavier Del Campo Romero 9d9e0c2979
html.c: Avoid half-init objects on html_node_add_attr
The previous implementation would leave half-initialised objects if one
of the calls to strdup(3) failed. Now, n->attrs is only modified when
all previous memory allocations were successful.
2024-02-19 22:49:09 +01:00
30 changed files with 108 additions and 42 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.13.5)
option(BUILD_EXAMPLES "Build examples" ON)
project(web LANGUAGES C VERSION 0.2.0)
project(web LANGUAGES C VERSION 0.3.0)
add_library(${PROJECT_NAME}
handler.c
html.c
@ -8,7 +8,7 @@ add_library(${PROJECT_NAME}
server.c
wildcard_cmp.c)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_LIST_DIR}/cmake)
find_package(dynstr 0.2.0)
find_package(dynstr 0.1.0)
if(NOT DYNSTR_FOUND)
message(STATUS "Using in-tree dynstr")

View File

@ -1,4 +1,4 @@
.TH HANDLER_ADD 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HANDLER_ADD 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
handler_add \- add an endpoint to a web server handler object

View File

@ -1,4 +1,4 @@
.TH HANDLER_ALLOC 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HANDLER_ALLOC 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
handler_alloc \- allocate a web server handler object

View File

@ -1,4 +1,4 @@
.TH HANDLER_FREE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HANDLER_FREE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
handler_free \- free a web server handler object

View File

@ -1,4 +1,4 @@
.TH HANDLER_LISTEN 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HANDLER_LISTEN 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
handler_listen \- initialize server to listen to a given port

View File

@ -1,4 +1,4 @@
.TH HANDLER_LOOP 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HANDLER_LOOP 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
handler_loop \- listen to and handle incoming connections on a web

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_ADD_ATTR 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_ADD_ATTR 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_add_attr \- add attribute to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_ADD_CHILD 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_ADD_CHILD 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_add_child \- add child to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_ADD_SIBLING 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_ADD_SIBLING 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_add_sibling \- add sibling to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_ALLOC 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_ALLOC 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_alloc \- allocate a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_FREE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_FREE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_free \- free a HTML node and its children

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_SET_VALUE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_SET_VALUE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_set_value \- set value to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_NODE_SET_VALUE_UNESCAPED 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_NODE_SET_VALUE_UNESCAPED 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_node_set_value_unescaped \- set value to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTML_SERIALIZE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTML_SERIALIZE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
html_serialize \- add attribute to a HTML node

View File

@ -1,4 +1,4 @@
.TH HTTP_ALLOC 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_ALLOC 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_alloc \- allocate a HTTP context object

View File

@ -1,4 +1,4 @@
.TH HTTP_COOKIE_CREATE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_COOKIE_CREATE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_cookie_create \- creates a HTTP/1.1 cookie

View File

@ -1,4 +1,4 @@
.TH HTTP_DECODE_URL 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_DECODE_URL 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_decode_url \- decodes a percent-encoded null-terminated string

View File

@ -1,4 +1,4 @@
.TH HTTP_ENCODE_URL 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_ENCODE_URL 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_encode_url \- allocates a percent-encoded null-terminated string

View File

@ -1,4 +1,4 @@
.TH HTTP_FREE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_FREE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_free \- free a HTTP context object

View File

@ -1,4 +1,4 @@
.TH HTTP_RESPONSE_ADD_HEADER 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_RESPONSE_ADD_HEADER 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_response_add_header \- adds a HTTP/1.1 header to a response

View File

@ -1,4 +1,4 @@
.TH HTTP_UPDATE 3 2024-02-12 0.2.0 "libweb Library Reference"
.TH HTTP_UPDATE 3 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
http_update \- updates a HTTP context object

View File

@ -1,4 +1,4 @@
.TH LIBWEB_HANDLER 7 2024-02-12 0.2.0 "libweb Library Reference"
.TH LIBWEB_HANDLER 7 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
libweb_handler \- libweb high-level website configuration
@ -76,6 +76,7 @@ struct handler_cfg
int (*\fIlength\fP)(unsigned long long len, const struct http_cookie *c, struct http_response *r, void *user);
void *\fIuser\fP;
size_t \fImax_headers\fP;
struct http_cfg_post \fIpost\fP;
};
.EE
.in
@ -83,9 +84,10 @@ struct handler_cfg
.IR tmpdir ,
.IR length ,
.I user
and
.IR user ,
.I max_headers
and
.I post
are passed directly to the
.I struct http_cfg
object used to initialize a

View File

@ -1,4 +1,4 @@
.TH LIBWEB_HTML 7 2024-02-12 0.2.0 "libweb Library Reference"
.TH LIBWEB_HTML 7 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
libweb_html \- libweb HTML serializer

View File

@ -1,4 +1,4 @@
.TH LIBWEB_HTTP 7 2024-02-12 0.2.0 "libweb Library Reference"
.TH LIBWEB_HTTP 7 2024-02-19 0.3.0 "libweb Library Reference"
.SH NAME
libweb_http \- libweb HTTP connection handling and utilities
@ -94,6 +94,11 @@ struct http_cfg
const char *\fItmpdir\fP;
void *\fIuser\fP;
size_t \fImax_headers\fP;
struct http_cfg_post
{
size_t \fImax_pairs\fP, \fImax_files\fP;
} \fIpost\fP;
};
.EE
.in
@ -221,6 +226,31 @@ Any extra headers sent by the client outside this maximum value shall be
silently ignored by
.IR libweb .
.I post
contains configuration parameters specific to
.B POST
requests:
.I max_pairs
refers to the maximum number of key/value pairs that shall be accepted by
.I libweb
on
.B POST
.IR multipart/form-data -encoded
requests. If the maximum number of pairs is exceeded by the request,
.I libweb
shall terminate the connection.
.I max_files
refers to the maximum number of files that shall be accepted by
.I libweb
on
.B POST
.IR multipart/form-data -encoded
requests. If the maximum number of files is exceeded by the request,
.I libweb
shall terminate the connection.
.SS HTTP payload
When a client submits a request to the server,

View File

@ -110,7 +110,8 @@ static struct client *find_or_alloc_client(struct handler *const h,
.length = on_length,
.user = ret,
.tmpdir = h->cfg.tmpdir,
.max_headers = h->cfg.max_headers
.max_headers = h->cfg.max_headers,
.post = h->cfg.post
};
*ret = (const struct client)

35
html.c
View File

@ -110,30 +110,41 @@ int html_node_add_attr(struct html_node *const n, const char *const attr,
const char *const val)
{
const size_t el = n->n + 1;
struct html_attribute *const attrs = realloc(n->attrs,
el * sizeof *n->attrs), *a = NULL;
char *const attrdup = strdup(attr), *valdup = NULL;
struct html_attribute *attrs = NULL;
if (!attrs)
if (!attrdup)
{
fprintf(stderr, "%s: strdup(3) attr: %s\n", __func__, strerror(errno));
goto failure;
}
else if (val && !(valdup = strdup(val)))
{
fprintf(stderr, "%s: strdup(3) val: %s\n", __func__, strerror(errno));
goto failure;
}
else if (!(attrs = realloc(n->attrs, el * sizeof *attrs)))
{
fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno));
return -1;
}
a = &attrs[n->n];
*a = (const struct html_attribute){0};
struct html_attribute *const a = &attrs[n->n];
if (!(a->attr = strdup(attr))
|| (val && !(a->value = strdup(val))))
*a = (const struct html_attribute)
{
fprintf(stderr, "%s: strdup(3): %s\n", __func__, strerror(errno));
free(a->attr);
free(a->value);
return -1;
}
.attr = attrdup,
.value = valdup
};
n->attrs = attrs;
n->n = el;
return 0;
failure:
free(attrdup);
free(valdup);
return -1;
}
void html_node_add_sibling(struct html_node *const n,

18
http.c
View File

@ -1728,7 +1728,16 @@ static int apply_from_file(struct http_ctx *const h, struct form *const f)
m->f = NULL;
const struct http_cfg_post *const cfg = &h->cfg.post;
const size_t n = m->nfiles + 1;
if (n > cfg->max_files)
{
fprintf(stderr, "%s: exceeded maximum number of files (%zu)\n",
__func__, cfg->max_files);
return 1;
}
struct http_post_file *const files = realloc(m->files,
n * sizeof *m->files);
@ -1777,10 +1786,17 @@ static int apply_from_mem(struct http_ctx *const h, struct form *const f)
if (name_exists(m, f))
return 1;
const struct http_cfg_post *const cfg = &h->cfg.post;
struct http_post_pair *pairs, *p;
const size_t n = m->npairs + 1;
if (!(f->value = strndup(h->line, m->written)))
if (n > cfg->max_pairs)
{
fprintf(stderr, "%s: exceeded maximum number of pairs (%zu)\n",
__func__, cfg->max_pairs);
return 1;
}
else if (!(f->value = strndup(h->line, m->written)))
{
fprintf(stderr, "%s: strndup(3): %s\n", __func__, strerror(errno));
return -1;

View File

@ -14,6 +14,7 @@ struct handler_cfg
struct http_response *r, void *user);
void *user;
size_t max_headers;
struct http_cfg_post post;
};
struct handler *handler_alloc(const struct handler_cfg *cfg);

View File

@ -106,6 +106,11 @@ struct http_cfg
const char *tmpdir;
void *user;
size_t max_headers;
struct http_cfg_post
{
size_t max_pairs, max_files;
} post;
};
struct http_ctx *http_alloc(const struct http_cfg *cfg);

View File

@ -6,6 +6,6 @@ libdir=${exec_prefix}/lib
Name: libweb
Url: https://gitea.privatedns.org/xavi/libweb
Description: A simple and lightweight web framework
Version: 0.2.0
Version: 0.3.0
Cflags: -I${includedir}
Libs: -L${libdir} -lweb