Commit 5cb0be1
authored
Fix the webserver class to return 500 on invalid MHD Response
This particularly applies in case of a file_response (in case the file doesn't exist or is a directory).
* webserver: Return 500 on invalid MHD response
If `http_response::get_raw_response()` returns nullptr instead of a
valid `MHD_Response*` for whatever reason, that pointer would be passed
on to `http_response::decorate_response()` and
`http_response::enqueue_response()` eventually, leading to different API
calls to libmicrohttpd with NULL as argument `struct MHD_Response
*response`. MHD does not guarantee any form of behaviour for invalid
input, so we have to consider it undefined behaviour and avoid passing
invalid input to MHD.
HTTP status 500 (Internal Server Error) is returned for consistency with
surrounding error handling, we don't know what caused
`http_response::get_raw_response()` to return nullptr, so we can not
give a better answer here.
Fixes: #255
* file_response: Add return value checks
Both `open()` and `lseek()` might fail depending on how `filename` is
set in object of class `httpserver::file_response()`. In the case of a
missing file *fd* got -1 and lseek set *size* (which had the wrong type
btw.) to 0xffffffff aka (off_t) -1. Passing an invalid file descriptor
and a massively huge size value on to `MHD_create_response_from_fd()`
might lead to unpredictable results depending how well libmicrohttpd
treats such invalid values.
Note: Before f9b7691 ("Use MHD_create_response_from_fd for files")
`httpserver::http::load_file()` was used, which throws an exception in
case a file can not be opened successfully. That exception would have
lead to returning HTTP status 500 (Internal Server Error).
References: #255
* test: Add unit test for missing file response
The constructor of class `httpserver::file_response` can be called with
a `filename` pointing into the void, to a file which does not actually
exist. The webserver should fail predictably in that case.
References: #255
* file_response: Test on regular file
It was possible before to pass a path to a directory, or a to a device
file, or to basically any path. `open()` would happily open it.
In case of a directory, `lseek()` returns LONG_MAX (0x7FFFFFFFFFFFFFFF)
and `MHD_create_response_from_fd()` is invoked. To avoid such nonsense,
we test the path now and allow regular files only.
References: #255
* file_response: Add API doc to constructor
This documents a possible pitfall for users when passing filename of not
existing files.
References: #255
* test: Add unit test for file_response pointing to directory
The constructor of class `httpserver::file_response` can be called with
a `filename` pointing to a directory instead of a regular file.
The webserver should fail predictably in that case.
References: #255
* readme: Document requirements and behaviour of file_response
Suggested-by: Sebastiano Merlino <sebastiano@hey.com>
References: #2551 parent ae1ab5d commit 5cb0be1
File tree
5 files changed
+94
-2
lines changed- src
- httpserver
- test/integ
5 files changed
+94
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
603 | 603 | | |
604 | 604 | | |
605 | 605 | | |
606 | | - | |
| 606 | + | |
607 | 607 | | |
608 | 608 | | |
609 | 609 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
25 | 27 | | |
26 | 28 | | |
27 | 29 | | |
| |||
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
33 | 44 | | |
34 | | - | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
35 | 50 | | |
36 | 51 | | |
37 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
40 | 53 | | |
41 | 54 | | |
42 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
623 | 623 | | |
624 | 624 | | |
625 | 625 | | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
626 | 630 | | |
627 | 631 | | |
628 | 632 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
224 | 238 | | |
225 | 239 | | |
226 | 240 | | |
| |||
896 | 910 | | |
897 | 911 | | |
898 | 912 | | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
| 927 | + | |
| 928 | + | |
| 929 | + | |
| 930 | + | |
| 931 | + | |
| 932 | + | |
| 933 | + | |
| 934 | + | |
| 935 | + | |
| 936 | + | |
| 937 | + | |
| 938 | + | |
| 939 | + | |
| 940 | + | |
| 941 | + | |
| 942 | + | |
| 943 | + | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
899 | 959 | | |
900 | 960 | | |
901 | 961 | | |
| |||
0 commit comments