1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
-- Prosody IM
-- Copyright (C) 2021 Kim Alvefur
--
-- This project is MIT/X11 licensed. Please see the
-- COPYING file in the source package for more information.
--
-- XEP-0363: HTTP File Upload
-- Again, from the top!
local t_insert = table.insert;
local jid = require "util.jid";
local st = require "util.stanza";
local url = require "socket.url";
local dm = require "core.storagemanager".olddm;
local jwt = require "util.jwt";
local errors = require "util.error";
local dataform = require "util.dataforms".new;
local dt = require "util.datetime";
local hi = require "util.human.units";
local cache = require "util.cache";
local namespace = "urn:xmpp:http:upload:0";
module:depends("disco");
module:add_identity("store", "file", module:get_option_string("name", "HTTP File Upload"));
module:add_feature(namespace);
local uploads = module:open_store("uploads", "archive");
-- id, <request>, time, owner
local secret = module:get_option_string(module.name.."_secret", require"util.id".long());
local external_base_url = module:get_option_string(module.name .. "_base_url");
local file_size_limit = module:get_option_number(module.name .. "_size_limit", 10 * 1024 * 1024); -- 10 MB
local file_types = module:get_option_set(module.name .. "_allowed_file_types", {});
local safe_types = module:get_option_set(module.name .. "_safe_file_types", {"image/*","video/*","audio/*","text/plain"});
local expiry = module:get_option_number(module.name .. "_expires_after", 7 * 86400);
local daily_quota = module:get_option_number(module.name .. "_daily_quota", file_size_limit*10); -- 100 MB / day
local access = module:get_option_set(module.name .. "_access", {});
if not external_base_url then
module:depends("http");
end
module:add_extension(dataform {
{ name = "FORM_TYPE", type = "hidden", value = namespace },
{ name = "max-file-size", type = "text-single" },
}:form({ ["max-file-size"] = tostring(file_size_limit) }, "result"));
local upload_errors = errors.init(module.name, namespace, {
access = { type = "auth"; condition = "forbidden" };
filename = { type = "modify"; condition = "bad-request"; text = "Invalid filename" };
filetype = { type = "modify"; condition = "not-acceptable"; text = "File type not allowed" };
filesize = { type = "modify"; condition = "not-acceptable"; text = "File too large";
extra = {tag = st.stanza("file-too-large", {xmlns = namespace}):tag("max-file-size"):text(tostring(file_size_limit)) };
};
filesizefmt = { type = "modify"; condition = "bad-request"; text = "File size must be positive integer"; };
quota = { type = "wait"; condition = "resource-constraint"; text = "Daily quota reached"; };
});
local upload_cache = cache.new(1024);
-- Convenience wrapper for logging file sizes
local function B(bytes) return hi.format(bytes, "B", "b"); end
local function get_filename(slot, create)
return dm.getpath(slot, module.host, module.name, "bin", create)
end
-- TODO cache
function get_daily_quota(uploader)
local iter, err = uploads:find(nil, {with = uploader; start = os.time() - 86400});
if not iter then return iter, err; end
local total_bytes = 0;
for _, slot in iter do
local size = tonumber(slot.attr.size);
if size then total_bytes = total_bytes + size; end
end
return total_bytes;
end
function may_upload(uploader, filename, filesize, filetype) -- > boolean, error
local uploader_host = jid.host(uploader);
if not ((access:empty() and prosody.hosts[uploader_host]) or access:contains(uploader) or access:contains(uploader_host)) then
return false, upload_errors.new("access");
end
if not filename or filename:find"/" then
-- On Linux, only '/' and '\0' are invalid in filenames and NUL can't be in XML
return false, upload_errors.new("filename");
end
if not filesize or filesize < 0 or filesize % 1 ~= 0 then
return false, upload_errors.new("filesizefmt");
end
if filesize > file_size_limit then
return false, upload_errors.new("filesize");
end
local uploader_quota = get_daily_quota(uploader);
if uploader_quota + filesize > daily_quota then
return false, upload_errors.new("quota");
end
if not ( file_types:empty() or file_types:contains(filetype) or file_types:contains(filetype:gsub("/.*", "/*")) ) then
return false, upload_errors.new("filetype");
end
return true;
end
function get_authz(uploader, filename, filesize, filetype, slot)
return jwt.sign(secret, {
sub = uploader;
filename = filename;
filesize = filesize;
filetype = filetype;
slot = slot;
exp = os.time()+300;
});
end
function get_url(slot, filename)
local base_url = external_base_url or module:http_url();
local slot_url = url.parse(base_url);
slot_url.path = url.parse_path(slot_url.path or "/");
t_insert(slot_url.path, slot);
if filename then
t_insert(slot_url.path, filename);
slot_url.path.is_directory = false;
else
slot_url.path.is_directory = true;
end
slot_url.path = url.build_path(slot_url.path);
return url.build(slot_url);
end
function handle_slot_request(event)
local stanza, origin = event.stanza, event.origin;
local request = st.clone(stanza.tags[1], true);
local filename = request.attr.filename;
local filesize = tonumber(request.attr.size);
local filetype = request.attr["content-type"] or "application/octet-stream";
local uploader = jid.bare(stanza.attr.from);
local may, why_not = may_upload(uploader, filename, filesize, filetype);
if not may then
origin.send(st.error_reply(stanza, why_not));
return true;
end
module:log("info", "Issuing upload slot to %s for %s", uploader, B(filesize));
local slot, storage_err = errors.coerce(uploads:append(nil, nil, request, os.time(), uploader))
if not slot then
origin.send(st.error_reply(stanza, storage_err));
return true;
end
local authz = get_authz(uploader, filename, filesize, filetype, slot);
local slot_url = get_url(slot, filename);
local upload_url = slot_url;
local reply = st.reply(stanza)
:tag("slot", { xmlns = namespace })
:tag("get", { url = slot_url }):up()
:tag("put", { url = upload_url })
:text_tag("header", "Bearer "..authz, {name="Authorization"})
:reset();
origin.send(reply);
return true;
end
function handle_upload(event, path) -- PUT /upload/:slot
local request = event.request;
local authz = request.headers.authorization;
if authz then
authz = authz:match("^Bearer (.*)")
end
if not authz then
module:log("debug", "Missing or malformed Authorization header");
event.response.headers.www_authenticate = "Bearer";
return 403;
end
local authed, upload_info = jwt.verify(secret, authz);
if not (authed and type(upload_info) == "table" and type(upload_info.exp) == "number") then
module:log("debug", "Unauthorized or invalid token: %s, %q", authed, upload_info);
return 401;
end
if not request.body_sink and upload_info.exp < os.time() then
module:log("debug", "Authorization token expired on %s", dt.datetime(upload_info.exp));
return 410;
end
if not path or upload_info.slot ~= path:match("^[^/]+") then
module:log("debug", "Invalid upload slot: %q, path: %q", upload_info.slot, path);
return 400;
end
if request.headers.content_length and tonumber(request.headers.content_length) ~= upload_info.filesize then
return 413;
-- Note: We don't know the size if the upload is streamed in chunked encoding,
-- so we also check the final file size on completion.
end
local filename = get_filename(upload_info.slot, true);
if not request.body_sink then
module:log("debug", "Preparing to receive upload into %q, expecting %s", filename, B(upload_info.filesize));
local fh, err = errors.coerce(io.open(filename.."~", "w"));
if not fh then
return err;
end
request.body_sink = fh;
if request.body == false then
return true;
end
end
if request.body then
module:log("debug", "Complete upload available, %s", B(#request.body));
-- Small enough to have been uploaded already
local written, err = errors.coerce(request.body_sink:write(request.body));
if not written then
return err;
end
request.body = nil;
end
if request.body_sink then
local final_size = request.body_sink:seek();
local uploaded, err = errors.coerce(request.body_sink:close());
if final_size ~= upload_info.filesize then
-- Could be too short as well, but we say the same thing
uploaded, err = false, 413;
end
if uploaded then
module:log("debug", "Upload of %q completed, %s", filename, B(final_size));
assert(os.rename(filename.."~", filename));
upload_cache:set(upload_info.slot, {
name = upload_info.filename;
size = tostring(upload_info.filesize);
type = upload_info.filetype;
time = os.time();
});
return 201;
else
assert(os.remove(filename.."~"));
return err;
end
end
end
function handle_download(event, path) -- GET /uploads/:slot+filename
local request, response = event.request, event.response;
local slot_id = path:match("^[^/]+");
local basename, filetime, filetype, filesize;
local cached = upload_cache:get(slot_id);
if cached then
module:log("debug", "Cache hit");
-- TODO stats (instead of logging?)
basename = cached.name;
filesize = cached.size;
filetype = cached.type;
filetime = cached.time;
upload_cache:set(slot_id, cached);
-- TODO cache negative hits?
else
module:log("debug", "Cache miss");
local slot, when = errors.coerce(uploads:get(nil, slot_id));
if not slot then
module:log("debug", "uploads:get(%q) --> not-found, %s", slot_id, when);
else
module:log("debug", "uploads:get(%q) --> %s, %d", slot_id, slot, when);
basename = slot.attr.filename;
filesize = slot.attr.size;
filetype = slot.attr["content-type"];
filetime = when;
upload_cache:set(slot_id, {
name = basename;
size = slot.attr.size;
type = filetype;
time = when;
});
end
end
if not basename then
return 404;
end
local last_modified = os.date('!%a, %d %b %Y %H:%M:%S GMT', filetime);
if request.headers.if_modified_since == last_modified then
return 304;
end
local filename = get_filename(slot_id);
local handle, ferr = errors.coerce(io.open(filename));
if not handle then
return ferr or 410;
end
local disposition = "attachment";
if safe_types:contains(filetype) or safe_types:contains(filetype:gsub("/.*", "/*")) then
disposition = "inline";
end
response.headers.last_modified = last_modified;
response.headers.content_length = filesize;
response.headers.content_type = filetype or "application/octet-stream";
response.headers.content_disposition = string.format("%s; filename=%q", disposition, basename);
response.headers.cache_control = "max-age=31556952, immutable";
response.headers.content_security_policy = "default-src 'none'; frame-ancestors 'none';"
response.headers.strict_transport_security = "max-age=31556952";
response.headers.x_content_type_options = "nosniff";
response.headers.x_frame_options = "DENY"; -- replaced by frame-ancestors in CSP?
response.headers.x_xss_protection = "1; mode=block";
return response:send_file(handle);
end
if expiry >= 0 and not external_base_url then
-- TODO HTTP DELETE to the external endpoint?
local array = require "util.array";
local async = require "util.async";
local ENOENT = require "util.pposix".ENOENT;
local function sleep(t)
local wait, done = async.waiter();
module:add_timer(t, done)
wait();
end
local reaper_task = async.runner(function(boundary_time)
local iter, total = assert(uploads:find(nil, {["end"] = boundary_time; total = true}));
if total == 0 then
module:log("info", "No expired uploaded files to prune");
return;
end
module:log("info", "Pruning expired files uploaded earlier than %s", dt.datetime(boundary_time));
local obsolete_files = array();
local i = 0;
for slot_id in iter do
i = i + 1;
obsolete_files:push(get_filename(slot_id));
upload_cache:set(slot_id, nil);
end
sleep(0.1);
local n = 0;
obsolete_files:filter(function(filename)
n = n + 1;
if i % 100 == 0 then sleep(0.1); end
local deleted, err, errno = os.remove(filename);
if deleted or errno == ENOENT then
return false;
else
module:log("error", "Could not delete file %q: %s", filename, err);
return true;
end
end);
local deletion_query = {["end"] = boundary_time};
if #obsolete_files == 0 then
module:log("info", "All %d expired files deleted", n);
else
module:log("warn", "%d out of %d expired files could not be deleted", #obsolete_files, n);
deletion_query = {ids = obsolete_files};
end
local removed, err = uploads:delete(nil, deletion_query);
if removed == true or removed == n or removed == #obsolete_files then
module:log("debug", "Removed all metadata for expired uploaded files");
else
module:log("error", "Problem removing metadata for deleted files: %s", err);
end
end);
module:add_timer(1, function ()
reaper_task:run(os.time()-expiry);
return 60*60;
end);
end
module:hook("iq-get/host/urn:xmpp:http:upload:0:request", handle_slot_request);
if not external_base_url then
module:provides("http", {
streaming_uploads = true;
route = {
["PUT /*"] = handle_upload;
["GET /*"] = handle_download;
}
});
end
|