commit 03d270a
Stefano Marinelli
·
2026-01-06 11:18:00 +0000 UTC
parent 688c54c
Implement metadata stripping for uploaded videos - Extend `strip_media` to support video files using `ffmpeg`. - Use `ffmpeg -map_metadata -1 -c copy` to strip global metadata. - Support formats: mp4, m4v, mov, webm, mkv, avi. - Add `ffmpeg_path` configuration option. - Implement robust relative path handling (`user/` heuristic) to support jailed environments. - Enforce strict checks on startup: fail if tools (mogrify/ffmpeg) are missing when enabled.
2 files changed,
+106,
-16
M
snac.c
+7,
-3
1@@ -297,13 +297,17 @@ failed instance is detected, this counter is reset for it.
2 .It Ic vkey
3 Public vapid key. Used for notification on some client.
4 .It Ic strip_exif
5-If set to true, EXIF metadata will be stripped from uploaded images (jpg, png, webp, heic, avif, tiff, gif, bmp). This requires the
6+If set to true, EXIF and other metadata will be stripped from uploaded images (jpg, png, webp, heic, avif, tiff, gif, bmp) and videos (mp4, m4v, mov, webm, mkv, avi). This requires the
7 .Nm mogrify
8-tool to be installed. If
9+(from ImageMagick) and
10+.Nm ffmpeg
11+tools to be installed. If
12 .Nm snac
13-cannot find or execute the tool at startup, it will refuse to run.
14+cannot find or execute these tools at startup, it will refuse to run.
15 .It Ic mogrify_path
16 Overrides the default "mogrify" command name or path. Use this if the tool is not in the system PATH or has a different name.
17+.It Ic ffmpeg_path
18+Overrides the default "ffmpeg" command name or path. Use this if the tool is not in the system PATH or has a different name.
19 .El
20 .Pp
21 You must restart the server to make effective these changes.
M
snac.c
+99,
-13
1@@ -33,6 +33,8 @@
2 #include <sys/time.h>
3 #include <sys/stat.h>
4 #include <sys/wait.h>
5+#include <limits.h>
6+#include <stdlib.h>
7
8 xs_str *srv_basedir = NULL;
9 xs_dict *srv_config = NULL;
10@@ -177,12 +179,25 @@ int strip_media(const char *fn)
11 /* strips EXIF data from a file */
12 {
13 int ret = 0;
14+
15 const xs_val *v = xs_dict_get(srv_config, "strip_exif");
16
17 if (xs_type(v) == XSTYPE_TRUE) {
18- xs *l_fn = xs_tolower_i(xs_dup(fn));
19+ /* Heuristic: find 'user/' in the path to make it relative */
20+ /* This works for ~/user/..., /var/snac/user/..., etc. */
21+ const char *r_fn = strstr(fn, "user/");
22+
23+ if (r_fn == NULL) {
24+ /* Fallback: try to strip ~/ if present */
25+ if (strncmp(fn, "~/", 2) == 0)
26+ r_fn = fn + 2;
27+ else
28+ r_fn = fn;
29+ }
30
31- /* check extensions */
32+ xs *l_fn = xs_tolower_i(xs_dup(r_fn));
33+
34+ /* check image extensions */
35 if (xs_endswith(l_fn, ".jpg") || xs_endswith(l_fn, ".jpeg") ||
36 xs_endswith(l_fn, ".png") || xs_endswith(l_fn, ".webp") ||
37 xs_endswith(l_fn, ".heic") || xs_endswith(l_fn, ".heif") ||
38@@ -193,7 +208,7 @@ int strip_media(const char *fn)
39 if (mp == NULL)
40 mp = "mogrify";
41
42- xs *cmd = xs_fmt("%s -strip \"%s\" 2>/dev/null", mp, fn);
43+ xs *cmd = xs_fmt("cd \"%s\" && %s -auto-orient -strip \"%s\" 2>/dev/null", srv_basedir, mp, r_fn);
44
45 ret = system(cmd);
46
47@@ -203,12 +218,64 @@ int strip_media(const char *fn)
48 code = WEXITSTATUS(ret);
49
50 if (code == 127)
51- srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'mogrify_path' in server.json.", fn, mp));
52+ srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'mogrify_path' in server.json.", r_fn, mp));
53 else
54- srv_log(xs_fmt("strip_media: error stripping %s %d", fn, ret));
55+ srv_log(xs_fmt("strip_media: error stripping %s %d", r_fn, ret));
56 }
57 else
58- srv_debug(1, xs_fmt("strip_media: stripped %s", fn));
59+ srv_debug(1, xs_fmt("strip_media: stripped %s", r_fn));
60+ }
61+ else
62+ /* check video extensions */
63+ if (xs_endswith(l_fn, ".mp4") || xs_endswith(l_fn, ".m4v") ||
64+ xs_endswith(l_fn, ".mov") || xs_endswith(l_fn, ".webm") ||
65+ xs_endswith(l_fn, ".mkv") || xs_endswith(l_fn, ".avi")) {
66+
67+ const char *fp = xs_dict_get(srv_config, "ffmpeg_path");
68+ if (fp == NULL)
69+ fp = "ffmpeg";
70+
71+ /* ffmpeg cannot modify in-place, so we need a temp file */
72+ /* we must preserve valid extension for ffmpeg to guess the format */
73+ const char *ext = strrchr(r_fn, '.');
74+ if (ext == NULL) ext = "";
75+ xs *tmp_fn = xs_fmt("%s.tmp%s", r_fn, ext);
76+
77+ /* -map_metadata -1 strips all global metadata */
78+ /* -c copy copies input streams without re-encoding */
79+ /* we don't silence stderr so we can debug issues */
80+ /* we explicitly cd to srv_basedir to ensure relative paths work */
81+ xs *cmd = xs_fmt("cd \"%s\" && %s -y -i \"%s\" -map_metadata -1 -c copy \"%s\"", srv_basedir, fp, r_fn, tmp_fn);
82+
83+ ret = system(cmd);
84+
85+ if (ret != 0) {
86+ int code = 0;
87+ if (WIFEXITED(ret))
88+ code = WEXITSTATUS(ret);
89+
90+ if (code == 127)
91+ srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'ffmpeg_path' in server.json.", r_fn, fp));
92+ else {
93+ srv_log(xs_fmt("strip_media: error stripping %s %d", r_fn, ret));
94+ srv_log(xs_fmt("strip_media: command was: %s", cmd));
95+ }
96+
97+ /* try to cleanup, just in case */
98+ /* unlink needs full path too if we are not in basedir */
99+ xs *full_tmp_fn = xs_fmt("%s/%s", srv_basedir, tmp_fn);
100+ unlink(full_tmp_fn);
101+ }
102+ else {
103+ /* rename tmp file to original */
104+ /* use full path for source because it was created relative to basedir */
105+ xs *full_tmp_fn = xs_fmt("%s/%s", srv_basedir, tmp_fn);
106+
107+ if (rename(full_tmp_fn, fn) == 0)
108+ srv_debug(1, xs_fmt("strip_media: stripped %s", fn));
109+ else
110+ srv_log(xs_fmt("strip_media: error renaming %s to %s", full_tmp_fn, fn));
111+ }
112 }
113 }
114
115@@ -222,14 +289,33 @@ int check_strip_tool(void)
116 int ret = 1;
117
118 if (xs_type(v) == XSTYPE_TRUE) {
119- const char *mp = xs_dict_get(srv_config, "mogrify_path");
120- if (mp == NULL)
121- mp = "mogrify";
122+ /* check mogrify */
123+ {
124+ const char *mp = xs_dict_get(srv_config, "mogrify_path");
125+ if (mp == NULL)
126+ mp = "mogrify";
127
128- xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", mp);
129-
130- if (system(cmd) != 0)
131- ret = 0;
132+ xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", mp);
133+
134+ if (system(cmd) != 0) {
135+ srv_log(xs_fmt("check_strip_tool: '%s' not working", mp));
136+ ret = 0;
137+ }
138+ }
139+
140+ /* check ffmpeg */
141+ if (ret) {
142+ const char *fp = xs_dict_get(srv_config, "ffmpeg_path");
143+ if (fp == NULL)
144+ fp = "ffmpeg";
145+
146+ xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", fp);
147+
148+ if (system(cmd) != 0) {
149+ srv_log(xs_fmt("check_strip_tool: '%s' not working", fp));
150+ ret = 0;
151+ }
152+ }
153 }
154
155 return ret;