commit 51bee24

default  ·  2023-04-22 05:58:31 +0000 UTC
parent 1de8095
Fail on static data ids with / in them.
1 files changed,  +25, -15
M data.c
M data.c
+25, -15
 1@@ -1326,10 +1326,15 @@ int actor_get(snac *snac, const char *actor, d_char **data)
 2 }
 3 
 4 
 5+/** static data **/
 6+
 7 d_char *_static_fn(snac *snac, const char *id)
 8 /* gets the filename for a static file */
 9 {
10-    return xs_fmt("%s/static/%s", snac->basedir, id);
11+    if (strchr(id, '/'))
12+        return NULL;
13+    else
14+        return xs_fmt("%s/static/%s", snac->basedir, id);
15 }
16 
17 
18@@ -1340,9 +1345,8 @@ int static_get(snac *snac, const char *id, d_char **data, int *size)
19     FILE *f;
20     int status = 404;
21 
22-    *size = XS_ALL;
23-
24-    if ((f = fopen(fn, "rb")) != NULL) {
25+    if (fn && (f = fopen(fn, "rb")) != NULL) {
26+        *size = XS_ALL;
27         *data = xs_read(f, size);
28         fclose(f);
29 
30@@ -1359,7 +1363,7 @@ void static_put(snac *snac, const char *id, const char *data, int size)
31     xs *fn = _static_fn(snac, id);
32     FILE *f;
33 
34-    if ((f = fopen(fn, "wb")) != NULL) {
35+    if (fn && (f = fopen(fn, "wb")) != NULL) {
36         fwrite(data, size, 1, f);
37         fclose(f);
38     }
39@@ -1370,12 +1374,15 @@ void static_put_meta(snac *snac, const char *id, const char *str)
40 /* puts metadata (i.e. a media description string) to id */
41 {
42     xs *fn = _static_fn(snac, id);
43-    fn     = xs_str_cat(fn, ".txt");
44-    FILE *f;
45 
46-    if ((f = fopen(fn, "w")) != NULL) {
47-        fprintf(f, "%s\n", str);
48-        fclose(f);
49+    if (fn) {
50+        fn = xs_str_cat(fn, ".txt");
51+        FILE *f;
52+
53+        if ((f = fopen(fn, "w")) != NULL) {
54+            fprintf(f, "%s\n", str);
55+            fclose(f);
56+        }
57     }
58 }
59 
60@@ -1384,13 +1391,16 @@ xs_str *static_get_meta(snac *snac, const char *id)
61 /* gets metadata from a media */
62 {
63     xs *fn    = _static_fn(snac, id);
64-    fn        = xs_str_cat(fn, ".txt");
65     xs_str *r = NULL;
66-    FILE *f;
67 
68-    if ((f = fopen(fn, "r")) != NULL) {
69-        r = xs_strip_i(xs_readline(f));
70-        fclose(f);
71+    if (fn) {
72+        fn = xs_str_cat(fn, ".txt");
73+        FILE *f;
74+
75+        if ((f = fopen(fn, "r")) != NULL) {
76+            r = xs_strip_i(xs_readline(f));
77+            fclose(f);
78+        }
79     }
80     else
81         r = xs_str_new("");