commit 40076e9

default  ·  2024-01-26 18:01:59 +0000 UTC
parent 6056567
mastoapi: avoid crashing on empty payloads.
1 files changed,  +15, -6
+15, -6
 1@@ -240,8 +240,10 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
 2     char *i_ctype = xs_dict_get(req, "content-type");
 3     xs *args      = NULL;
 4 
 5-    if (i_ctype && xs_startswith(i_ctype, "application/json"))
 6-        args = xs_json_loads(payload);
 7+    if (i_ctype && xs_startswith(i_ctype, "application/json")) {
 8+        if (!xs_is_null(payload))
 9+            args = xs_json_loads(payload);
10+    }
11     else
12     if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
13         xs *upl = xs_url_dec(payload);
14@@ -250,6 +252,9 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
15     else
16         args = xs_dup(xs_dict_get(req, "p_vars"));
17 
18+    if (args == NULL)
19+        return 400;
20+
21     xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
22 
23     srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
24@@ -1981,8 +1986,10 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
25     xs *args      = NULL;
26     char *i_ctype = xs_dict_get(req, "content-type");
27 
28-    if (i_ctype && xs_startswith(i_ctype, "application/json"))
29-        args = xs_json_loads(payload);
30+    if (i_ctype && xs_startswith(i_ctype, "application/json")) {
31+        if (!xs_is_null(payload))
32+            args = xs_json_loads(payload);
33+    }
34     else
35         args = xs_dup(xs_dict_get(req, "p_vars"));
36 
37@@ -2504,8 +2511,10 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path,
38     xs *args      = NULL;
39     char *i_ctype = xs_dict_get(req, "content-type");
40 
41-    if (i_ctype && xs_startswith(i_ctype, "application/json"))
42-        args = xs_json_loads(payload);
43+    if (i_ctype && xs_startswith(i_ctype, "application/json")) {
44+        if (!xs_is_null(payload))
45+            args = xs_json_loads(payload);
46+    }
47     else
48         args = xs_dup(xs_dict_get(req, "p_vars"));
49