-
Notifications
You must be signed in to change notification settings - Fork 3
/
Filer.py
executable file
·386 lines (310 loc) · 10.9 KB
/
Filer.py
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
#!venv/bin/python
import hashlib
import base64
import time
import gpgencryption
from os import unlink, path, getenv, listdir, mkdir, chmod, umask, urandom
from shutil import rmtree
from threading import Thread
from random import randint
from sys import stderr, exit
from flask import (
Flask,
render_template,
jsonify,
request,
redirect,
send_from_directory,
g,
)
from flask_wtf.csrf import CSRFProtect, CSRFError
from flask_dropzone import Dropzone
from flask_babel import Babel, _, refresh
from argparse import ArgumentParser
from werkzeug.utils import secure_filename
app = Flask(__name__)
### start of config
app.config["SECRET_KEY"] = getenv("SECRET_KEY", None)
# app.jinja_env.trim_blocks = True
# app.jinja_env.lstrip_blocks = True
app.config["DROPZONE_ALLOWED_FILE_CUSTOM"] = True
app.config["DROPZONE_ALLOWED_FILE_TYPE"] = ""
app.config["DROPZONE_SERVE_LOCAL"] = True
app.config["DROPZONE_ENABLE_CSRF"] = True
app.config["DROPZONE_TIMEOUT"] = 600000
app.config["WTF_CSRF_SSL_STRICT"] = False # Disable looking at referrer
app.config["SESSION_COOKIE_SECURE"] = True
app.config["SESSION_COOKIE_SAMESITE"] = 'Strict'
app.config["ORGANIZATION"] = getenv("ORGANIZATION", "Kanzlei Hubrig")
app.config["TITLE"] = "Filer"
app.config["LANGUAGES"] = ["en", "de"]
filettl = int(getenv("FILER_FILETTL", 10)) # file lifetime in days
support_public_docs = True
gpg_enable_upload_encryption = True # encrypt customer-uploaded data via GPG
gpg_recipient_fprint = None
gpg_key_server = "keys.openpgp.org"
basedir = getenv("FILER_BASEDIR", "./Daten")
publicdir = getenv("FILER_PUBLICDIR", "Public")
documentsdir = getenv("FILER_DOCUMENTSDIR", "Dokumente")
clientsdir = getenv("FILER_CLIENTSSDIR", "Mandanten")
gpg_home_dir = path.join(basedir, "gpghome")
### end of config
csrf = CSRFProtect(app)
dropzone = Dropzone(app)
babel = Babel(app)
nonce = base64.b64encode(urandom(64)).decode("utf8")
default_http_header = {
"Content-Security-Policy": f"default-src 'self'; img-src 'self' data:; script-src 'self' 'nonce-{nonce}'",
"X-Frame-Options": "SAMEORIGIN",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy" : "no-referrer"
}
def update_dropzone_message():
app.config["DROPZONE_DEFAULT_MESSAGE"] = _(
"Ziehe die Dateien hier hin, um sie hochzuladen oder klicken Sie zur Auswahl."
)
#### ADMIN FACING DIRECTORY LISTS ####
####
####
@app.route("/admin", methods=["GET"])
def admin():
update_dropzone_message()
url_root = request.url_root.replace("http://", "https://", 1)
users = listdir(path.join(basedir, clientsdir))
return (
render_template(
"admin.html",
users=users,
tree=make_tree(basedir, publicdir),
url_root=url_root,
documentsdir=documentsdir,
support_public_docs=support_public_docs,
nonce=nonce,
organization=app.config["ORGANIZATION"],
title=app.config["TITLE"],
),
200,
default_http_header,
)
@app.route("/admin/" + documentsdir + "/<user>", methods=["GET"])
def admin_dokumente(user):
update_dropzone_message()
return (
render_template(
"mandant.html",
admin="admin/",
user=secure_filename(user),
tree=make_tree(basedir, path.join(documentsdir, secure_filename(user))),
documentsdir=documentsdir,
support_public_docs=support_public_docs,
nonce=nonce,
organization=app.config["ORGANIZATION"],
title=app.config["TITLE"],
),
200,
default_http_header,
)
#
# API
#
@app.route("/admin/del-user/<user>", methods=["POST"])
def admin_deluser(user):
method = request.form.get("_method", "POST")
if method == "DELETE":
rmtree(path.join(basedir, documentsdir, secure_filename(user)))
unlink(path.join(basedir, clientsdir, secure_filename(user)))
return redirect("/admin")
@app.route("/admin/new-user", methods=["POST"])
def admin_newuser():
password = request.form.get("password", "")
user = request.form.get("user", "")
if not password or not user:
return "Username or password missing", 400
directory = secure_filename(user)
salt = urandom(4)
sha = hashlib.sha1(password.encode("utf-8"))
sha.update(salt)
digest_salt_b64 = base64.b64encode(sha.digest() + salt)
tagged_digest_salt = "{{SSHA}}{}".format(digest_salt_b64.decode("ascii"))
try:
make_dir(path.join(basedir, documentsdir, directory))
with open(
path.join(basedir, clientsdir, directory), "w+", encoding="utf-8"
) as htpasswd:
htpasswd.write("{}:{}\n".format(secure_filename(user), tagged_digest_salt))
except OSError as error:
return "Couldn't create user scope", 500
return redirect("/admin")
#### USER FACING DIRECTORY LIST ####
####
####
@app.route("/" + documentsdir + "/<user>", methods=["GET"])
def mandant(user):
update_dropzone_message()
return (
render_template(
"mandant.html",
admin="",
user=secure_filename(user),
tree=make_tree(basedir, path.join(documentsdir, secure_filename(user))),
documentsdir=documentsdir,
support_public_docs=support_public_docs,
nonce=nonce,
organization=app.config["ORGANIZATION"],
title=app.config["TITLE"],
),
200,
default_http_header,
)
#### UPLOAD FILE ROUTES ####
####
####
@app.route("/" + documentsdir + "/<user>", methods=["POST"])
def upload_mandant_as_mandant(user):
return _upload_mandant(
user,
encrypt=(gpg_enable_upload_encryption and gpg_recipient_fprint is not None),
)
@app.route("/admin/" + documentsdir + "/<user>", methods=["POST"])
def upload_mandant_as_admin(user):
return _upload_mandant(user)
@app.route("/admin", methods=["POST"])
def upload_admin():
return _upload_mandant()
def _upload_mandant(user=None, encrypt=False):
for key, f in request.files.items():
if key.startswith("file"):
filename = secure_filename(f.filename)
if user:
username = secure_filename(user)
pathname = path.join(basedir, documentsdir, username, filename)
if encrypt:
pathname += ".gpg"
enc = gpgencryption.GPGEncryption(gpg_home_dir, gpg_key_server)
enc.encrypt_fh(gpg_recipient_fprint, f, pathname) # no signing
else:
f.save(pathname)
else:
f.save(path.join(basedir, publicdir, filename))
return "upload template"
# handle CSRF error
@app.errorhandler(CSRFError)
def csrf_error(e):
return e.description, 400
#### DELETE FILE ROUTES ####
####
####
@app.route("/" + documentsdir + "/<user>/<path:filename>", methods=["POST"])
def delete_file_mandant(user, filename):
method = request.form.get("_method", "POST")
if method == "DELETE":
unlink(
path.join(
basedir, documentsdir, secure_filename(user), secure_filename(filename)
)
)
return redirect("/" + documentsdir + "/" + secure_filename(user))
@app.route("/admin/" + documentsdir + "/<user>/<path:filename>", methods=["POST"])
def delete_file_mandant_admin(user, filename):
method = request.form.get("_method", "POST")
if method == "DELETE":
unlink(
path.join(
basedir, documentsdir, secure_filename(user), secure_filename(filename)
)
)
return redirect("/admin/" + documentsdir + "/" + secure_filename(user))
@app.route("/admin/" + publicdir + "/<path:filename>", methods=["POST"])
def delete_file_admin(filename):
method = request.form.get("_method", "POST")
if method == "DELETE":
unlink(path.join(basedir, publicdir, secure_filename(filename)))
return redirect("/admin")
#### SERVE FILES RULES ####
####
####
@app.route("/admin/" + documentsdir + "/<user>/<path:filename>", methods=["GET"])
@app.route("/" + documentsdir + "/<user>/<path:filename>", methods=["GET"])
def custom_static(user, filename):
return send_from_directory(
path.join(basedir, documentsdir), path.join(user, filename)
)
@app.route("/" + publicdir + "/<path:filename>")
def custom_static_public(filename):
return send_from_directory(path.join(basedir, publicdir), filename)
def make_tree(rel, pathname, clean_expired=True):
tree = dict(name=pathname, download=path.basename(pathname), children=[])
try:
lst = listdir(path.join(rel, pathname))
except OSError:
pass # ignore errors
else:
for name in lst:
fn = path.join(pathname, name)
if path.isdir(path.join(rel, fn)):
tree["children"].append(make_tree(rel, fn, clean_expired))
else:
ttl = filettl - int(
(time.time() - path.getmtime(path.join(rel, fn))) / (24 * 3600)
)
if clean_expired and ttl < 0:
unlink(path.join(rel, fn))
else:
tree["children"].append(dict(name=fn, download=name, ttl=ttl))
return tree
# Start a cleaner thread that will trigger make_tree's side effect of
# wiping old files
def cleaner_thread():
while True:
make_tree(basedir, documentsdir)
# sleep for 6h plus jitter
time.sleep(21600 + randint(1, 1800))
def make_dir(dir_name):
if not path.exists(dir_name):
mkdir(dir_name)
chmod(dir_name, 0o700)
@babel.localeselector
def get_locale():
if not g.get("lang_code", None):
g.lang_code = request.accept_languages.best_match(app.config["LANGUAGES"])
return g.lang_code
# Main program
umask(0o177)
thread = Thread(target=cleaner_thread, args=())
thread.daemon = True
thread.start()
# Ensure all working directories are there
try:
for datadir in (
basedir,
path.join(basedir, documentsdir),
path.join(basedir, clientsdir),
path.join(basedir, publicdir),
):
make_dir(datadir)
except:
stderr.write("Error: Basedir not accessible\n")
exit(1)
if app.config["SECRET_KEY"] is None:
stderr.write("Error: Flask secret key is not set.\n")
exit(1)
# download GPG key if enabled
if gpg_enable_upload_encryption and gpg_recipient_fprint is not None:
enc = gpgencryption.GPGEncryption(gpg_home_dir, gpg_key_server)
enc.download_key(gpg_recipient_fprint)
if __name__ == "__main__":
parser = ArgumentParser(description="Filer")
parser.add_argument(
"-H",
"--host",
help="Hostname of the Flask app " + "[default %s]" % "127.0.0.1",
default="127.0.0.1",
)
parser.add_argument(
"-P",
"--port",
help="Port for the Flask app " + "[default %s]" % "5000",
default="5000",
)
args = parser.parse_args()
app.run(host=args.host, port=int(args.port))